doremi
YaBB Newbies
Offline
Posts: 7
Re: AI tic tac toe game (with code but doesn't work..)
Reply #4 - Apr 19th , 2010, 4:13pm
//playAgain method, boolean playAgain () { gameState = 3; //declares variables boolean cont = true; //clear screen fill(255); rect (0, 130, 640, 340); fill(0); //set font textFont (regular); //prompt input text ("Would you like to play again?", 175, 200); drawBox (240, 250); text ("Yes", 245, 285); text ("No", 350, 285); //infinite loop for input if(keyPressed) { if (key == 'f') gameState = 2; else if (key == 'l' && cont) { fill(0); rect (0, 250, 640, 100); drawBox (340, 250); text ("Yes", 245, 285); text ("No", 350, 285); cont = false; } else if (key == 'j' && !cont) { fill(255); rect (0, 250, 640, 100); drawBox (240, 250); text ("Yes", 245, 285); text ("No", 350, 285); cont = true; } else if (key == 'h') { intro (); stroke(0); text ("Would you like to play again?", 175, 200); drawBox (240, 250); text ("Yes", 245, 285); text ("No", 350, 285); cont = true; } } return cont; } //end of playAgain method //playerTurn method int[] [] playerTurn (int[] [] play, char choice) { //variable declaration int row = 0, col = 0; //draws the board drawBoard (play, choice); drawBox (215, 215); //infinite loop for player input if (keyPressed) { if (key == 'f' && (play [row] [col]) == 0) { play [row] [col] = -1; return play; } else if (key == 'k' && row != 2) { row++; } else if (key == 'j' && col != 0) { col--; } else if (key == 'l' && col != 2) { col++; } else if (key == 'i' && row != 0) { row--; } else if (key == 'h') intro (); } drawBoard (play, choice); drawBox ((215 + 75 * col), (215 + 75 * row)); return play; } //drawShape method void drawShape (char choice, int x, int y, boolean player) { //if it is not the player, draw the opposite symbol if (!player) if (choice == 'X') //x choice = 'X'; else if (choice == 'O') //o choice = 'O'; //set colour, draw symbol fill(0); if (choice == 'X') { //x for (int i = -2 ; i < 3 ; i++) { line (x + i, y, x + i + 30, y + 30); line (x + i, y + 30, x + i + 30, y); } } else if (choice == 'O') { //o stroke(0); noFill(); ellipse (x + 15, y + 15, 40, 40); } } //drawBoard method, draws the game board void drawBoard (int[] [] play, char choice) { //clear screen fill (255); rect (0, 130, 640, 300); fill (0); //draws the lines for (int i = -2 ; i < 3 ; i++) { line (200, 275 + i, 425, 275 + i); line (200, 350 + i, 425, 350 + i); line (275 + i, 200, 275 + i, 425); line (350 + i, 200, 350 + i, 425); } //nested for loop to access each value in 2D array play for (int row = 0 ; row < 3 ; row++) { for (int col = 0 ; col < 3 ; col++) { if (play [row] [col] == 1) drawShape (choice, (225 + col * 75), (225 + row * 75), false); else if (play [row] [col] == -1) drawShape (choice, (225 + col * 75), (225 + row * 75), true); } } } //end of drawBoard method