How do you alternate between 2 human players?
switching between 2 people in TicTacToe.java
Collapse
X
-
Effectively, you are passing a boolean value that is true if it's player 1's turn to input data. Also, win is a boolean value that is returned if a player has one the game.Code:int i; while((i <= 9) && (!win)) // 9 squares, so 9 inputs. { System.out.println("Player " + (i%2) + "'s turn"); win = GetInputAndUpdate(((i%2)==1)); i++; } if(win) { System.out.println("Player " + ((i-1)%2) + " wins!"); }
If you have an N player game, then do (i % N). -
I have a boolean method named makeMove that tries to make a move and returns true if the move is valid and false if it isn't.
I also have a public String promptMove() method that prompts a player to move a piece on the board.
How do I set up a for loop in the promptMove method that will loop between playerOne which is "X" and playerTwo which is "O"?Code:public boolean makeMove( String move ){ for(int row=0; row<3; row++){ for(int col=0; col<3; col++){ if(board[row][col]=="") move.equals(board[row][col]); canvas.repaint(board, row, col); return true; } } return false; } public String promptMove(){ while(!gameOver()){ currentPlayer=playerOne; if(currentPlayer.makeMove(String move)) currentPlayer=playerTwo; if(currentPlayer.makeMove(String move)) currentPlayer=playerOne; if(currentPlayer.makeMove(String move)) currentPlayer=playerTwo; if(currentPlayer.makeMove(String move)) currentPlayer=playerOne; if(currentPlayer.makeMove(String move)) currentPlayer=playerTwo; if(currentPlayer.makeMove(String move)) currentPlayer=playerOne; if(currentPlayer.makeMove(String move)) currentPlayer=playerTwo; if(currentPlayer.makeMove(String move)) currentPlayer=playerOne; } }Comment
-
I've finished all the code but I can't get the "X" and "O"s to show up on the board or to switch between human players.
Code:package csc216.project2; import javax.swing.JOptionPane; public class TicTacToe implements GameBoard{ private GameCanvas canvas; private String [] [] board; private String currentPlayer; private String playerOne="X"; private String playerTwo="O"; private int row; private int col; public TicTacToe(){ canvas=new GameCanvas(); currentPlayer=null; board=new String[3][3]; row=3; col=3; } /** *Draws current state of the board */ public void displayBoard(){ canvas.repaint(board, row, col); } /** *Query whether the game is completed *@return TRUE if game is over, FALSE otherwise */ public boolean gameOver(){ for(int row=0; row<3; row++ ) for(int col=0; col<3; col++) if(board[0][col]=="X" || board[0][col]=="O") return true; else if(board[1][col]=="X" || board[1][col]=="O") return true; else if(board[2][col]=="X" || board[2][col]=="O") return true; else if(board[row][0]=="X" || board[row][0]=="O") return true; else if(board[row][1]=="X" || board[row][1]=="O") return true; else if(board[row][2]=="X" || board[row][2]=="O") return true; else if(board[0][0]=="X" && board[1][1]=="X" && board[2][2]=="X") return true; else if(board[0][0]=="O" && board[1][1]=="O" && board[2][2]=="O") return true; else if(board[2][0]=="X" && board[1][1]=="X" && board[0][2]=="X") return true; else if(board[2][0]=="O" && board[1][1]=="O" && board[0][2]=="O") return true; else if(board[row][col]!="") return true; return false; } /** *Initialize the board to a starting state */ public void initBoard(){ for(int row=0; row<3; row++) for(int col=0; col<3; col++) board[row][col]=""; canvas.repaint(board, row, col); } /** * Attempt to move a playing piece on the board *@param move the proposed move *@return TRUE if the move is valid, FALSE otherwise */ public boolean makeMove(String move){ for(int row=0; row<3; row++) for(int col=0; col<3; col++) if(board[row][col]==""){ board[row][col]=move; return true; } else{ String msg = "Invalid move " + move + " entered."; JOptionPane.showMessageDialog( null, msg, "Invalid Move", JOptionPane.ERROR_MESSAGE ); return false; } return false; } /** *Prompt a player to move a playing piece on the board */ public String promptMove(){ currentPlayer=playerOne; String inp = JOptionPane.showInputDialog( null, "Enter a valid move:" ); if(currentPlayer==playerOne){ return "X"; } String inp1 = JOptionPane.showInputDialog( null, "Enter a valid move:" ); if(currentPlayer==playerTwo) board[row][col]="O"; return "O"; } public void quit(){ canvas.dispose(); System.exit(1); } public static void main(String args[]){ GameBoard gBoard=new TicTacToe(); gBoard.initBoard(); gBoard.promptMove(); gBoard.makeMove("X"); gBoard.displayBoard(); } }Comment
-
You get the user input as inpl, and then do absolutely nothing with it. You assume it magically gets transformed into a row and a col value. You need to decode inpl into row and col. Otherwise, you keep changing game[3][3], and the last entry should be game[2][2].Comment
-
Originally posted by D_CYou get the user input as inpl, and then do absolutely nothing with it. You assume it magically gets transformed into a row and a col value. You need to decode inpl into row and col. Otherwise, you keep changing game[3][3], and the last entry should be game[2][2].
I am unclear as to what you have just said.Comment
Comment