java - What is wrong with my TicTacToe code and how do you check already taken spots? -
i have edited question. feel close reason output of code prints 'x' or 'o' above board. can't pinpoint why doing , how check if spot taken , not allow spot overridden. have tried putting if(board[i][j]!= ' ') {s.o.p("not valid") }but not function correctly. maybe i'm not writing in wrong spot?
the last thing when want restart game winner declared. how game reset board?
i have been @ hours , appreciate help.
import java.util.scanner; public class tictactoe { public static char[][] board = new char[3][3]; public static char player; public static void newgame() { system.out.println("new game: x goes first."); player = 'x'; } public static void writeboard() { //drawing game board string line = "______"; system.out.println(""); //making seperate columns , rows for(int i= 0;i<3; i++) { for(int j=0;j<3;j++) system.out.print(board[i][j]+"|"); system.out.println(""); system.out.println(line); } } public static void getmove() { scanner keyboard = new scanner(system.in); int i, j; if(player == 'x') { system.out.println("x's turn"); system.out.println("where want x placed?"); system.out.println("please enter row number , column number seperated space."); = keyboard.nextint(); j = keyboard.nextint(); if(board[i][j] != ' ') system.out.println("that position taken"); system.out.println("you have entered row" + " " + + " "+ "and column" + " " + j); system.out.println("thank selection."); system.out.print(board[i][j]='x'); player = 'o'; } else if(player == 'o') { system.out.println("o's turn"); system.out.println("where want o placed?"); system.out.println("please enter row number , column number seperated space."); = keyboard.nextint(); j = keyboard.nextint(); if(board[i][j] != ' ') { system.out.println("that position taken"); } system.out.println("you have entered row"+" " + +" "+ "and column" +" " + j); system.out.println("thank selection."); system.out.println(board[i][j] = 'o'); player = 'x'; } } public static boolean winner() { //check row (int = 0; < 3; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && (board[i][0] == 'x')) { system.out.println("x winner"); return true; } else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] &&(board[i][0]=='o')) { system.out.println("o winner"); return true; } //check column else if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && (board[0][i] =='x')) { system.out.println("x winner"); return true; } else if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && (board[0][i]=='o')) { system.out.println("o winner"); return true; } } //check diagnols left if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && (board[0][0]=='o')) { system.out.println("o winner!"); return true; } else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && (board[0][0] == 'x')) { system.out.println("x winner!"); return true; } //checks diagnols right else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && (board[0][2]=='o')) { system.out.println("o winner!"); return true; } else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && (board[0][2]=='x')) { system.out.println("x winner!"); return true; } else { return false; } } public static boolean tie() { //if there no winner(a tie) for(int i=0;i<3;i++) { for(int j=0; j<3;j++) { if(board[i][j]==' ') { system.out.println("it's tie!"); return false; } } } return true; } } import java.util.scanner; public class tictactoedemo { public static char[][] board = new char[3][3]; public static void main(string[] args) { scanner keyboard = new scanner(system.in); int i, j; string answer; do{ tictactoe.newgame(); while(!tictactoe.winner()) { tictactoe.writeboard(); tictactoe.getmove(); tictactoe.tie(); } system.out.println("would rematch?"); system.out.println("enter yes or no"); answer = keyboard.next(); } while(answer.equalsignorecase("yes")); //do-while loop keep game going if wanted } }
tictactoe class
newgame() missing return statement, add void
player not initialized, make global variable with:
private static char player; public static void newgame() { system.out.println("new game: x goes first."); player = 'x'; }
do refactoring of winner method, complex. maybe separate conditions separate methods easier readability. can find lots of examples on how should done.
fix error handling (i entered "22 33" , got arrayindexoutofboundsexception, "a b" , got inputmismatchexception), need prevent this.
tictactoedemo class
board never used, , j also.
why do/while? can done while loop...
this few fixes made running need address other issues.
what ide using development? should report errors , give explanation of it.
Comments
Post a Comment