Java - Tic Tac Toe - Why won't my "switch player" method work? -
why switchplayer() method work once? method should work independently fine, , when put inside playgame() method after placemark(), works continuously, messes currentplayer variable, messes other methods. not want make player object, assume missing simple, place switchplayer() method.
my result goes this:
- currentplayer 1, place mark.
- [places mark, good]
- currentplayer 2, place mark.
- [places mark, good]
- currentplayer 2, place mark. // problem line.. doesn't switch
- [places mark, good]
relevant code sub class:
public void playgame(){ assignnames(); assignmarks(); explaingame(); while(checkwin() == false && fullboard() == false){ getnum(); placemark(); printboard(); } gameresult(); } public void placemark(){ if(validnumber() == true){ if(num == 1){ board[0][0] = currentmark; switchplayer(); } if(num == 2){ board[0][1] = currentmark; switchplayer(); } if(num == 3){ board[0][2] = currentmark; switchplayer(); } if(num == 4){ board[1][0] = currentmark; switchplayer(); } if(num == 5){ board[1][1] = currentmark; switchplayer(); } if(num == 6){ board[1][2] = currentmark; switchplayer(); } if(num == 7){ board[2][0] = currentmark; switchplayer(); } if(num == 8){ board[2][1] = currentmark; switchplayer(); } if(num == 9){ board[2][2] = currentmark; switchplayer(); } } } public void switchplayer(){ if(currentmark == 'x') currentmark = 'o'; else{ currentmark = 'x'; } if(currentmark == 'o' && playermark1 == 'o'){ currentplayer = playername1; } else{ currentplayer = playername2; } }
relevant code main class:
tictactoe game = new tictactoe(); game.playgame();
it seems though should post whole code... here is.
public class tictactoe { private static char[][] board; scanner input = new scanner(system.in); int num; string entry; string playername1; char playermark1; string playername2; char playermark2; char currentmark; string currentplayer; public void playgame(){ assignnames(); assignmarks(); explaingame(); while(checkwin() == false && fullboard() == false){ getnum(); placemark(); printboard(); } gameresult(); } public tictactoe(){ board = new char[3][3]; clearboard(); } // end tictactoe() public void clearboard(){ for(int = 0; < 3; a++){ for(int b = 0; b < 3; b++){ board[a][b] = ' '; } } }// end clearboard() public void printboard(){ system.out.println("-------------"); (int = 0; < 3; a++) { system.out.print("| "); (int b = 0; b < 3; b++) { system.out.print(board[a][b] + " | "); } system.out.println(); system.out.println("-------------"); } } public boolean fullboard() { boolean full = true; (int = 0; < 3; i++) { (int j = 0; j < 3; j++) { if (board[i][j] == ' ') { full = false; } } } return full; } public void explaingame(){ system.out.println("-------------"); system.out.println("| 1 | 2 | 3 |"); system.out.println("-------------"); system.out.println("| 4 | 5 | 6 |"); system.out.println("-------------"); system.out.println("| 7 | 8 | 9 |"); system.out.println("-------------"); system.out.println("the above board shows numbers must enter place mark in respective locations.\n"); system.out.println("randomly assigning player marks. . ."); system.out.println(playername1+" player "+playermark1+", , "+playername2+" player "+playermark2 + "."); system.out.println("player x go first. press key begin playing."); entry = input.nextline(); } public void assignnames(){ system.out.println("enter name player 1."); entry = input.nextline(); playername1 = entry; system.out.println("enter name player 2."); entry = input.nextline(); playername2 = entry; } public void assignmarks(){ random rand = new random(); int = rand.nextint(2); if( == 0 ){ playermark1 = 'x'; playermark2 = 'o'; } else{ playermark1 = 'o'; playermark2 = 'x'; } currentmark = 'x'; if(playermark1 == 'x') currentplayer = playername1; else currentplayer = playername2; } public void switchplayer(){ if(currentmark == 'x') currentmark = 'o'; else{ currentmark = 'x'; } if(currentmark == 'o' && playermark1 == 'o'){ currentplayer = playername1; } else{ currentplayer = playername2; } } public int getnum(){ system.out.println(currentplayer + ", place mark (1-9)."); num = input.nextint(); return num; } public boolean validspot(int num){ boolean goodspot = false; if(num == 1 && board[0][0] == ' ') goodspot = true; if(num == 2 && board[0][1] == ' ') goodspot = true; if(num == 3 && board[0][2] == ' ') goodspot = true; if(num == 4 && board[1][0] == ' ') goodspot = true; if(num == 5 && board[1][1] == ' ') goodspot = true; if(num == 6 && board[1][2] == ' ') goodspot = true; if(num == 7 && board[2][0] == ' ') goodspot = true; if(num == 8 && board[2][1] == ' ') goodspot = true; if(num == 9 && board[2][2] == ' ') goodspot = true; if(goodspot == false){ system.out.println("input error. make sure spot not taken."); } return goodspot; } public boolean validrange(int num){ boolean goodrange = false; if(num >= 1 && num <= 9 ){ goodrange = true; } if(goodrange == false){ system.out.println("input error. make sure number between 1-9."); } return goodrange; } public boolean validnumber(){ return ((validrange(num) && validspot(num))); } public void placemark(){ if(validnumber() == true){ if(num == 1){ board[0][0] = currentmark; switchplayer(); } if(num == 2){ board[0][1] = currentmark; switchplayer(); } if(num == 3){ board[0][2] = currentmark; switchplayer(); } if(num == 4){ board[1][0] = currentmark; switchplayer(); } if(num == 5){ board[1][1] = currentmark; switchplayer(); } if(num == 6){ board[1][2] = currentmark; switchplayer(); } if(num == 7){ board[2][0] = currentmark; switchplayer(); } if(num == 8){ board[2][1] = currentmark; switchplayer(); } if(num == 9){ board[2][2] = currentmark; switchplayer(); } } } public boolean checkspot(char c1, char c2, char c3) { return ((c1 != ' ') && (c1 == c2) && (c2 == c3)); } public boolean checkwin() { return (checkwinrows() || checkwincolumns() || checkwindiagonals()); } public boolean checkwinrows() { (int = 0; < 3; i++) { if (checkspot(board[i][0], board[i][1], board[i][2]) == true) { return true; } } return false; } public boolean checkwincolumns() { (int = 0; < 3; i++) { if (checkspot(board[0][i], board[1][i], board[2][i]) == true) { return true; } } return false; } public boolean checkwindiagonals() { return ((checkspot(board[0][0], board[1][1], board[2][2]) == true) || (checkspot(board[0][2], board[1][1], board[2][0]) == true)); } public void gameresult(){ switchplayer(); if(checkwin()){ system.out.println("\ngame on -- " +currentplayer+ " wins! woo hoo!"); } else if(fullboard()){ system.out.println("\ngame on -- draw!"); } } } // end tictactoe
change
if(currentmark == 'o' && playermark1 == 'o')
to
if(currentplayer==playername2)
your code doesn't work because changes player 1 when both current mark = 0 , player1 mark = 0. is, works when player 1 assigned "o" in beginning of game.
hope helps.
Comments
Post a Comment