TicTacToe in Java - why does it go on too long? -


i writing tictactoe game, , repeatedly prompts user move. asking selection more 9 times , not sure why.

my design has 2-dimensional array store information current state of board, use joptionpane ask users selection of board (1 top left, 2 top middle, 3 top right, 4 middle left, etc). after every move, output console current board. if spot used, prompt user again. (there no need winner check, told time.

here entire code:

import javax.swing.joptionpane;  // basic tictactoe game. public class tictactoe1 {     public static void main(string[] args){         // hello there!         object[] options = { "i'm ready play!",};         object[] options2 = { "select number.",};         joptionpane.showoptiondialog(null,"to play tictactoe, use numbers 1 9 choose place mark. ready?","tictactoe1",                 joptionpane.default_option, joptionpane.information_message, null, options, options[0]);         // define board.         char board[][] = new char[3][3];         // 0 out board.         for(int a=0; a<3; a++) {             for(int b=0; b<3; b++) {                 board[a][b] = ' ';             }         }         // print initial, clean board.         print(board);         // use loop ask selection , nest selector it.         for(int i=1; i<10 ; i++) {             if ((i%2) == 1) {                 boolean goahead = true;                 while (goahead) {                     int selection = integer.parseint(joptionpane.showinputdialog(null,"where want place x?"));                     if ((board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='x') || (board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='o')) {                         joptionpane.showoptiondialog(null,"i'm sorry, number "+selection+" spot being used.","tictactoe1",                                 joptionpane.default_option, joptionpane.information_message, null, options2, options2[0]);                     } else {                         goahead = false;                         board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3] = 'x';                         print(board);                     }                 }             }             if ((i%2) == 1) {                 boolean goahead = true;                 while (goahead) {                     int selection = integer.parseint(joptionpane.showinputdialog(null,"where want place o?"));                     if ((board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='x') || (board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='o')) {                         joptionpane.showoptiondialog(null,"i'm sorry, number "+selection+" spot being used.","tictactoe1",                                 joptionpane.default_option, joptionpane.information_message, null, options2, options2[0]);                     } else {                         goahead = false;                         board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3] = 'o';                         print(board);                     }                 }             } //          didsomeonewinyet(board);         }     }      // make helper function named print print board     public static void print(char board[][]){         for(int a=0; a<3; a++) {             for(int b=0; b<3; b++) {                 system.out.print("|"+board[a][b]+"|");             }             system.out.println();         }         system.out.println();     }     // winner checker. //  public static void didsomeonewinyet(char board[][]){         // manually checking yield 16 cases (2 diagonal, 3 horizontal, 3 vertical, either x or o), not bad. //  } } 

why ask selection many times?

your if conditions in loop same both occur during same iteration of loop:

if ((i%2) == 1) { 

one of if statements should be:

if ((i%2) == 0) { 

depending on want go first.

alternatively use else statement:

if ((i%2) == 1) {  //code x go  }else { //code y go } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -