• Initialize the board
• While there is not a winner and it is not a draw
o Display the board
o Get the player’s move and set the corresponding board position to “X”
o Determine if the player wins
o Compute the computer’s move and set the corresponding board position to “O”
o Determine if the computer wins
The player will win if the current move will result in 3 “X”s in a row, column or diagonal (if the position) is on a diagonal.
The computer will win if the current move will result in 3 “O”s in a row, column or diagonal (if the position) is on a diagonal.
This is what I've come up with so far:
package tictactoe;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @
*/
public class TicTacToe {
private String board[][] = new String[3][3]; // to hold board contents
private int moveBoard[][] = new int[3][3]; // used to evaluate the computer move
private Scanner input = new Scanner(System.in);
private int playerX; // holds the row choice of the user
private int playerY; // holds the column choice of the user
public TicTacToe() {
initializeBoard();
}
// This method sets all positions in the board to " "
private void initializeBoard() {
for (String[] row : board) {
Arrays.fill(row, " ");
}
}
// This method will initialize the moveBoard prior to determining
// the next move. If the board contains a blank the position is set to 0
// otherwise the position is set to -10 because the position is filled and we
// don't want to be able to select the position
private void initializeMoveBoard() {
for (int row = 0; row < moveBoard.length; row++) {
for (int col = 0; col < moveBoard[row].length; col++) {
if (board[row][col].equals(" ")) {
moveBoard[row][col] = 0;
} else {
moveBoard[row][col] = -10;
}
}
}
}
// this method displays the board
public void displayBoard() {
System.out.println("\t 1 2 3");
System.out.println("\t-------------");
for (int row = 0; row < board.length; row++) {
System.out.print(" " + (row + 1) + "\t");
for (int col = 0; col < board[row].length; col++) {
System.out.print("| " + board[row][col] + " ");
}
System.out.println("|");
System.out.println("\t-------------");
}
}
// this method displays the moveBoard values. This is added for debugging
// purposes if required.
private void displayMoveBoard() {
for (int row = 0; row < moveBoard.length; row++) {
for (int col = 0; col < moveBoard[row].length; col++) {
System.out.printf("%2d ", moveBoard[row][col]);
}
System.out.println();
}
}
// This method gets a valid player move
private void getPlayerMove() {
do {
do {
System.out.print("Move (row col separated by blanks): ");
playerX = input.nextInt();
if (playerX < 1 || playerX > 3) {
System.out.println("Invalid row");
}
playerY = input.nextInt();
if (playerY < 1 || playerY > 3) {
System.out.println("Invalid column");
}
} while (playerX < 1 || playerX > 3 || playerY < 1 || playerY > 3);
if (!board[playerX - 1][playerY - 1].equals(" ")) {
System.out.println("Board at position " + playerX + " " + playerY
+ " is occupied");
}
} while (!board[playerX - 1][playerY - 1].equals(" "));
}
public void playGame() {
initializeBoard(); // reset the board for this game
initializeMoveBoard();
displayMoveBoard();
displayBoard();
getPlayerMove();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
TicTacToe game = new TicTacToe();
game.playGame();
}
}

New Topic/Question
Reply


MultiQuote



|