Finding the winner in Tic-Tac-Toe (Java using 2-D arrays) -
I'm having trouble with how to display the winner of the game in this Tic Tac-toe program . The checkfirster method was provided to us and it can not be changed, because of what I have seen, without taking into account all the potential winning states without taking the player This is how I am lost, the way the winner will be displayed. As any input I could do with that method would be great. Thank you for your attention. Edit: The displayWinner method has been added that I have tried, which does not seem to work. The reason for this is that you have examined the board before the last move, and there was no winner on time (otherwise, you would have been out of the game first). Now that we have a winner, then whatever the last step we should take is the player who won. Note that your current call import java.util. *; Public class tick {public static zero main (string [] args) {scanners = new scanner (System.in); Boolean flag = false; Four [] [] board = {{'', '', ''}, '', '', ''}, {'', '', ''}}; Boolean did = wrong; Int player = 1; Int line = 0; Int col = 0; While (flag! = True) {checkfourwear (board); System.out.println ("Enter the row and column for your next move"); Line = in.nextInt (); Col = in.nextInt (); If (player == 1) {board [row] [col] = 'x'; Player = 2; CheckForWinner (board); } And {board [row] [col] = 'o'; Player = 1; CheckForWinner (board); } Print board (board); checkForWinner (board); } Display Winners (player, flag); } Public static zero printboard (four [] [] board) {for (int row = 0; line and lieutenance; 3; line ++) {for (int col = 0; col & lt; 3; col ++) { System.out.print "|" + Board [row] [colonel] + "|"); } System.out.println (); Println ("-------"); }} Public Stable Boolean Check Fourier (Four [] [] Board) The {/ checkForWinner () method determines that if a data is stored in 2D four array then a player has won the game. Boolean flag = false; Boolean flag 1 false; Boolean flag2 = wrong; Boolean flag3 = wrong; Boolean flag4 = false; // Checks for match data for the contents of each row (int i = 0; i & lt; = 2; i ++) {if ((board [i] [0] == board [i] [1 ] & Amp; Board [i] [1] == Board [i] [2]) and Board [i] [2]! = '') Flag = true; } // Checks the match data for the contents of each column (int j = 0; j & lt; = 2; j ++) {if (([[board] [0] [j] == Board [1 ] [J] & amp; Board [1] [J] == Board [2] [J]) & Board [2] [J]! = '') Flag = 2; True; } // If the data is checked for matching the contents of a diagonal ((board [0] [0] == board [1] [1] & board [1] [1] == board [2] ] [2]) & Amp; Board [2] [2]! = '') Flag3 = true; // checks the contents of other diagonals to match the data if ((board [0] [2] == board [1] [1] & board [1] [1] == board [2] [0]) & Amp; amp; Board [2] [0]! = '') Flag4 = true; // Checks if any of the previous situations evaluates the truth (flag 1 == true || flag2 == true) flag3 == true || flag4 == true) flag = true; // If a winner was found correct returns; The false return is not a winner's return flag; } // the end of the check / after method public static zero displaywinner (integer player, boolean flag) {if (flag == true) {int currentplayer; Currentplayer = player; System.out.println ("the winner of the game" + is the current player); }}}
true , you know that whichever transit takes place is the winner.
checkForWinner is useless, because you ignore the return value. Currently, the
flag remains unassigned.
displaywinner needs to get the winner number, no
flag
Comments
Post a Comment