This is what I have so far. This is a two player game. I need to make it a one player game (player vs. computer). I am also trying to make the computer smart.. for example, if there is an open corner, then it should take that spot. Basically, the computer should try to win. The computer's moves should not be random. Please help me! Here is my code:
import java.util.Scanner;
public class Game1 {
public static void main(String[] arg) {
Scanner keyboard = new Scanner(System.in);
TicTacToe game = new TicTacToe();
game.print();
while ( !game.won() && !game.stalemate() ) {
game.move(keyboard);
game.print();
}
if ( game.won() ) System.out.println("You won!");
if ( game.stalemate() ) System.out.println("Stalemate.");
}
}
class TicTacToe {
private char[][] board; // 3 x 3 board
private char player; // next player: either 'X' or 'O'
//make a new game
public TicTacToe() {
player = 'X'; //first player is 'X'
board = new char[3][3];
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
board[i][j] = ' '; //board initially empty
}
//get the opposite player to the one given
private static char opposite(char thePlayer) {
if ( thePlayer == 'X' ) return 'O'; else return 'X';
}
//attempt play in position pos of the form "B2" etcetera,
//return false if the move is not valid
public boolean play(String pos) {
if ( pos.length() != 2 ) return false;
int row, col;
switch ( pos.charAt(0) ) {
case 'A': row = 0; break;
case 'B': row = 1; break;
case 'C': row = 2; break;
default: return false;
}
switch ( pos.charAt(1) ) {
case '1': col = 0; break;
case '2': col = 1; break;
case '3': col = 2; break;
default: return false;
}
if ( board[row][col] != ' ' ) return false;
board[row][col] = player;
player = opposite(player);
return true;
}
//prompt for a move and attempt to play it until this succeeds
public void move(Scanner keyboard) {
System.out.print(player+":");
while ( !play( keyboard.nextLine() ) )
System.out.print("Illegal move: try again.\n"+player+":");
return;
}
//find out if the player who just played won the game
public boolean won() {
return playerWon( opposite(player) );
}
//find out if thePlayer has won the game
private boolean playerWon(char thePlayer) {
if ( check3(board[0][0], board[1][1], board[2][2], thePlayer) ) return true;
if ( check3(board[0][2], board[1][1], board[2][0], thePlayer) ) return true;
for (int i = 0; i < 3; ++i) {
if ( check3(board[i][0], board[i][1], board[i][2], thePlayer) ) return true;
if ( check3(board[0][i], board[1][i], board[2][i], thePlayer) ) return true;
}
return false;
}
//are p1, p2 and p3 which will come from three positions in a row
//on the board, all equal to last (i.e. has last got three in a row)
private static boolean check3(char p1, char p2, char p3, char last) {
return p1==last && p2==last && p3==last;
}
//is the board full and no-one has won?
public boolean stalemate() {
if ( playerWon('X') || playerWon('O') ) return false;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
if ( board[i][j] == ' ' ) return false;
return true;
}
//print the board to the console
public void print() {
String gap = " ";
System.out.println(gap+" 1 2 3");
System.out.println(gap+" | |");
System.out.println(gap+"A "+board[0][0]+" | "+board[0][1]+" | "+board[0][2]);
System.out.println(gap+" - - - - - - - - -");
System.out.println(gap+"B "+board[1][0]+" | "+board[1][1]+" | "+board[1][2]);
System.out.println(gap+" - - - - - - - - -");
System.out.println(gap+"C "+board[2][0]+" | "+board[2][1]+" | "+board[2][2]);
System.out.println(gap+" | |");
}
}