// Use Class Tic-Tac-Toe Board to play a game
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
TicTacToeBoard board = new TicTacToeBoard();
Scanner sc = new Scanner(System.in);
boolean gameOver = false;
int row = 0;
int col = 0;
int totalTurns = 0;
String[] player = new String[3];
char[] moves = {' ','X','O'}; // player[1] = X, player[2] = O
System.out.println("Welcome! Tic-Tac-Toe is a two player game.");
System.out.print("\nEnter player one's name: ");
player[1] = sc.nextLine();
System.out.print("\nEnter player two's name: ");
player[2] = sc.nextLine();
System.out.println("\n\nPlayers take turns marking a square. Only squares\n" +
"not already marked can be picked. Once a player has\n" +
"marked three squares in a row, he or she wins! If all squares\n" +
"are marked and no three squares are the same, a tied game is\n" +
"declared. Have Fun!\n\n");
do {
for (int p = 1; ((p <=2) && (!gameOver) && (totalTurns< 9)); p++) {
System.out.println("\n" + board.toString() + "\n");
System.out.println("It is " + player[p] + "'s turn.");
System.out.print("Pick a row between 1 and 3: ");
row = sc.nextInt();
System.out.print("Pick a column between 1 and 3: ");
col = sc.nextInt();
if (!board.makeMove(row, col, moves[p])) {
System.out.println("\nThat was an ILLEGAL move ");
p = p - 1; // so it remain player[p]'s turn
}
else if (board.isWin()) {
System.out.println("You Win!! Way to go " + player[p] + "!!\n");
gameOver=true;
}
else {
totalTurns++; // the move was valid
}
} // for loop taking turns
} while(!gameOver && (totalTurns < 9));
if (!gameOver)
System.out.print("\nGood Game, but it is a tie\n");
} // main
} // TicTacToe class
|