I have been working on this applet and have a problem with index out of bounds. The idea is to display a set of clues using white or black markers to specify whether the correct colour or colour/position of the guesses are correct. when checking the guess against the answer in the checkAnswer() method, where the program should display clues to the guesses made by the user, as to the correct colour or correct colour and correct position, indicated with a black or white circle respectively, is where the error seems to occur. I am sure this is quite a simple mistake that I've overlooked, even so , your help would be appreciated
the program so far is as follows:
/*
* mastermind game applet, the user selects coloured pegs to match
* randomly selected answer, revealed only if player loses or guesses correctly
* before ten attempts. player is informed after each attempt, by the use of a black
* or white flaged peg, whether choice is correct colour/position
*
*/
import java.awt.*;
import java.applet.*;
import java.util.*;
publicclass Mastermind extends Applet {
// declare variables
Font f, r;
Color colorOptions[] = new Color[6];
Color guesses[][] = new Color[10][4];
Color rightColorPosition[] = new Color[4];
Color rightColor[] = new Color[4];
Color cluesW[][] = new Color[10][4];
Color cluesb[][] = new Color[10][4];
int answer[] = newint[4];
int guess[] = newint[4];
int blackFlag, whiteFlag, guessNum, rowNum;
boolean win, lose;
//================================================
publicvoid init() {
// initialise variables
Graphics g=getGraphics();
setBackground(Color.lightGray);
f=new Font("TimesRoman", Font.BOLD,28); // bold font
r=new Font("Courier", Font.PLAIN,12); // regular font
colorOptions[0]=Color.green;
colorOptions[1]=Color.blue;
colorOptions[2]=Color.red;
colorOptions[3]=Color.white;
colorOptions[4]=Color.black;
colorOptions[5]=Color.yellow;
win=false;
lose=false;
blackFlag=0;
whiteFlag=0;
guessNum=0;
rowNum=0;
for(int i=0; i<10; i++) {
for(int j=0; j<4; j++ ) { // initialise guesses to background color
g.setColor(guesses[i][j]=Color.lightGray);
g.setColor(cluesW[i][j]=Color.lightGray);
g.setColor(cluesb[i][j]=Color.lightGray);
}
}
generateAnswer();
} // end init()
//================================================
publicvoid paint(Graphics g) {
displayText(g);
displayGuesses(g);
displayAnswer(g);
displayFlags(g);
numOfGuess(g);
// checkAnswer(g);
g.drawString(""+whiteFlag, 20,20);
g.drawString(""+blackFlag, 20,40);
} // end paint()
//================================================
publicvoid checkAnswer(Graphics g) {
// compare the arrays contents, call displayFlags()
blackFlag=0;
whiteFlag=0;
if(lose==true){
gameOver(g);
}
if(Arrays.equals(guess, answer)){
gameWon(g);
}
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
if(guess[j]==answer[i]) {
if (i==j) {
blackFlag++;
}
whiteFlag++;
break;
}
}
}
// displayFlags(g);
} // end checkAnswer
//================================================
publicvoid displayFlags(Graphics g) {
cluesW[rowNum][whiteFlag] = Color.white;
cluesb[rowNum][blackFlag] = Color.black;
for(int i=0; i<rowNum; i++) {
for(int j=0; j<=whiteFlag; j++ ) {
g.setColor(cluesW[i][j]);
g.fillRect(360+(j*10),45+(i*12), 8, 10);
}
}
// g.setColor(Color.black);
for(int i=0; i<rowNum; i++) {
for(int j=0; j<=blackFlag; j++ ) {
g.setColor(cluesb[i][j]);
g.fillRect(360+(j*10),45+(i*12), 8, 10);
}
}
} // end displayFlags
//================================================
publicvoid displayGuesses (Graphics g) {
// displays current and previous guesses
for(int i=0; i<=rowNum; i++) {
for(int j=0; j<4; j++ ) {
g.setColor(guesses[i][j]);
g.fillOval(270+(j*20), 45+(i*20), 16, 16);
}
}
} // end displayGuesses
//================================================
publicboolean mouseDown(Event evt, int x, int y) {
Graphics g = getGraphics();
if(rowNum>9){
gameOver(g);
g.setColor(Color.black);
g.setFont(r);
g.drawString("select the green peg to play again",20,280);
}
if((win || lose)&&(x>50&&x<68)&&(y>70&&y<88)) {
newGame(g);
}
if(!win && !lose) {
if(guessNum<4) {
// get mouse down positions and assign each color to user guess
if((x>50&&x<68)&&(y>70&&y<88)) {
guess[guessNum]=0;
}
elseif((x>72&&x<90)&&(y>70&&y<88)) {
guess[guessNum]=1;
}
elseif((x>94&&x<112)&&(y>70&&y<88)) {
guess[guessNum]=2;
}
elseif((x>116&&x<134)&&(y>70&&y<88)) {
guess[guessNum]=3;
}
elseif((x>138&&x<156)&&(y>70&&y<88)) {
guess[guessNum]=4;
}
elseif((x>160&&x<178)&&(y>70&&y<88)) {
guess[guessNum]=5;
}
elsereturnfalse;
guesses[rowNum][guessNum]=colorOptions[guess[guessNum]];
guessNum++;
if(guessNum==4) {
checkAnswer(g);
rowNum++;
guessNum=0;
}
repaint();
returntrue;
}
}
returnfalse;
} // end mousedown
//================================================
publicvoid displayText(Graphics g) {
// method sets the initial stage for the game
displayColorOptions(g);
g.drawRect(10, 10, this.size().width-20, this.size().height-20); // Applet border
g.drawRect(180, 225, 82, 20); //
for(int i=0; i<4; i++){
// draws the blank ovals that mask the answer
g.fillOval(183+(i*20), 227, 16, 16);
}
g.drawString("Click a coloured peg", 50, 60 );
g.drawString("Guess the hidden code", 50, 240);
g.setColor(Color.blue);
g.setFont(f);
g.drawString("MasterMind", 50, 130);
g.drawString("and", 50, 155);
g.drawString("Java!!", 50, 185);
} // end displayText()
//================================================
publicvoid displayColorOptions(Graphics g) {
// draws the option color pegs
for(int i=0; i<6; i++) {
g.setColor(colorOptions[i]);
g.fillRect(50+(i*22), 70, 18, 18);
}
g.setColor(Color.black);
} // end displayColorOptions
//================================================
publicvoid generateAnswer() {
// generates random answer
for(int i=0; i<4; i++) {
answer[i]=((int) (Math.random()*6));
}
} // end generateAnswer
//================================================
publicvoid displayAnswer(Graphics g) {
// displays randomly generated answer
for(int i=0; i<4; i++) {
g.setColor(colorOptions[answer[i]]);
g.fillOval(183+(i*20), 227, 16, 16);
}
} // end displayAnswer()
//================================================
publicvoid gameWon(Graphics g) {
// inform user if Won
g.setFont(f);
g.setColor(Color.blue);
g.drawString("YOU WIN!", 250, 280);
displayAnswer(g);
win=true;
} // end gameWon
//================================================
publicvoid gameOver(Graphics g) {
// inform user if Lost
g.setFont(f);
g.setColor(Color.red);
g.drawString("YOU LOSE!", 250, 280);
displayAnswer(g);
lose=true;
} // end gameLost
//================================================
publicvoid numOfGuess(Graphics g) {
// displays the guess number as a string
// adjacent to the colored guesses
g.setFont(r);
g.setColor(Color.black);
if(rowNum<1)
g.drawString("<-- 1st Guess",360, 55);
if(rowNum==1)
g.drawString("<-- 2nd Guess",360, 75);
if(rowNum==2)
g.drawString("<-- 3rd Guess",360, 95);
if(rowNum>2) {
for(int i=rowNum; i<10;i++) {
g.drawString("<-- "+(rowNum+1)+"th Guess",360, 55+(rowNum*20));
}
}
}
//================================================
publicvoid newGame(Graphics g) {
// initialise variables required for new start
win=false;
lose=false;
blackFlag=0;
whiteFlag=0;
guessNum=0;
rowNum=0;
for(int i=0; i<10; i++) {
for(int j=0; j<4; j++ ) {
guess[j]=0;
g.setColor(guesses[i][j]=Color.lightGray);
g.setColor(cluesW[i][j]=Color.lightGray);
g.setColor(cluesb[i][j]=Color.lightGray);
}
}
generateAnswer();
}
}