java - How would I incorporate a tie counter in my tic tac toe code? I would like to put it at the end of my program -


this tic tac toe.i have method checks winner , go in there. think need search numbers on game board , if remain not tie , if there remain tie.

    public class tictactoe { //beginning of tictactoe     static string[][] board = new string[3][3];  public static void main (string [] args) { // main routine string move; //user input string marker = "o"; //the character/marker starts @ o , switches x // asigning variables boolean computer = false;  boolean done = false; boolean playagain = true; int wins[] = {0,0,0};//1st. p1 wins, 2nd. p2/comp wins, 3rd. ties  system.out.println("do want play against computer (y/n)? typing no give 2 player board."); computer = textio.getlnboolean(); //option , input player 1 gets choose if want play against computer or player2  { //beginning of play again loop   fillarrays();// fills arry values   printboard(); // prints board   { //beginning of loop runs board     marker = switchmarker(marker); // turns o x       { // error check loop       move = getusermove(marker, computer);      } while (!ismovevalid(move));     insertmove(marker, move);     printboard();     done = checkforwinner();   } while(!done);   printwinner(marker,wins);   playagain = getplayagain(); //sets variable play again method getplayagain, asks user if he/she wants play again } while(playagain); //loops while play again true     } //end of main routine   public static void fillarrays(){ //fills game board  int number =1; for(int a=0; a<3; a++) {   (int b=0; b<3; b++) {     board[a][b]= string.valueof(number++);   } }    }    public static boolean getplayagain(){ //meathod play again     system.out.println("would play again? (y/n)");     string playagain = textio.getln();     if(playagain.equals("y")){       return true;     }     else{       return false;     }   }    public static boolean ismovevalid(string mov) { //error checks numbers greater 9 , less 0      int nummove = integer.parseint(mov);      if (nummove < 1 || nummove > 9) {        system.out.println("sorry move not valid! please re enter:");       return false;     }     else {       return true;     }   }    public static string switchmarker(string marker){ // replaces o x     if(marker.equals("x")){        return "o";     }     else{       return "x";     }   }    public static string getcomputermove(){ //determins computer decides play     boolean found = false;     string randstring;     do{       int rand =(int)(math.random()*9)+1;       randstring = integer.tostring(rand);       for(int a=0;a<3; a++){         for(int b=0;b<3; b++){           if(randstring.equals(board[a][b])){             found = true;             // try , code            }         }       }     }while(!found);     return randstring;   }   public static string getusermove(string marker, boolean computer){ //chooses computer or player2     if(marker.equals("x")){       system.out.println("player 1 please enter move");     }     else if(computer){       return getcomputermove();     }     else{       system.out.println("player 2 please enter move");     }     return textio.getln();   }    public static void insertmove(string move, string spot){ // places character on board     for(int a=0;a<3; a++){       for(int b=0;b<3; b++){         if(spot.equals(board[a][b])){           board[a][b] = move;         }       }     }   }        public static void printboard(){ //prints board      for(int a=0;a<3; a++){       (int b = 0; b<3; b++){         system.out.print(board[a][b] + " ");       }       system.out.println();     }      system.out.println();     }    public static boolean checkforwinner(){ // checks possible win combos       //combo 1 - horizontal line     for(int a=0;a<3; a++){ //horizontal checks       if((board[a][0].equals(board[a][1]))&&(board[a][1].equals(board[a][2]))){         return true;       }     }     //combo 2 - vertical lines     for(int a=0;a<3; a++){ //vertical checks       if((board[0][a].equals(board[1][a]))&&(board[1][a].equals(board[2][a]))){         return true;       }     }     //combo 3 - diagonal lines     if((board[0][0].equals(board[1][1]))&&(board[1][1].equals(board[2][2]))||(board[0][2].equals(board[1][1]))&&(board[1][1].equals(board[2][0]))){       return true;     }      //if above haven't returned true, either haven't won yet.. or it's tie.       //cycle through looking number in board. if there isn't one, board full, , because know     //there isn't winning move on board.. must full! so, return true.        return false; //return false. don't have winner yet.   }    public static void printwinner(string marker,int wins[]){ // counts wins, ties , losses     if(marker.equals("x")){       system.out.println("player 1 win!");       wins[0]++;     }     else{       system.out.println("player 2 win!");       wins[1]++;     }    // if (     system.out.println("player 1 has " + wins[0] + " wins , player 2 has " + wins[1] + " wins , there " + wins[2] + " ties.");     return;   } } 

absolute simplest maintain count of number_of_moves_performed. when reaches 9, , there no winner, you've got tie. no need search board positions unmarked square.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -