I have to do this tic tac toe program which can be scaled from 3x3 to 10x10. I have the basics of the game set with help of a tutorial but have not succeeded to find out how to make it scalable.
The program is player vs computer and the computer tries to stop the player from winning the game. Rules of the game are basic tictactoe/fiveinarow/sixinarow... etc.
public class TicTacToe {
static int A1, A2, A3, B1, B2, B3, C1, C2, C3; // Board spots.
static Scanner sc = new Scanner(System.in);
/**
*
*
*
*/
public static void main(String[] args) {
String prompt = "Make you move with the letters A-C and numbers 1-3"
+ "\nYou are \"X\" and the computer is \"O\"\n";
String yourTurn; // player's playturn
String computerTurn; // computer's playturn
boolean gameIsWon = false; // Boolean to game win/lose
// There are a maximum of nine plays, so a for loop keeps track of
// the number of plays. The game is over after the ninth play.
// Each time through the loop, both the human and the computer play.
// So i is incremented in the body of the loop after the computer plays.
for (int i = 1; i <= 9; i++) {
// Player
yourTurn = getMove(prompt);
updateBoard(yourTurn, 1);
displayBoard(); // shows the board
if (isGameWon()) {
System.out.println("YOU WIN!");
gameIsWon = true;
break;
}
// Computer
if (i < 9) {
computerTurn = computerTurn();
System.out.println("Computer's turn: " + computerTurn);
updateBoard(computerTurn, 2);
displayBoard();
if (isGameWon()) {
System.out.println("YOU LOSE!");
gameIsWon = true;
break;
}
prompt = "Your turn: ";
i++;
}
}
if (!gameIsWon) {
System.out.println("NO WINNER! DRAW!");
}
}
/**
*
*
*
* @param prompt
* @return
*/
public static String getMove(String prompt) {
String play;
System.out.print(prompt);
do {
play = sc.nextLine();
if (!isValidPlay(play)) {
System.out.println("Error! Use only combination of A-C and 1-3!"
+ " For Example: A1"
+ "\nIf you used the right combination "
+ "check if you or the computer do not already have "
+ "\"X\" or \"O\" there.");
}
} while (!isValidPlay(play));
return play;
}
/**
* Checks if the play is possible.
*
*
* @param play
* @return
*/
public static boolean isValidPlay(String play) {
if (play.equalsIgnoreCase("A1") & A1 == 0) {
return true;
}
if (play.equalsIgnoreCase("A2") & A2 == 0) {
return true;
}
if (play.equalsIgnoreCase("A3") & A3 == 0) {
return true;
}
if (play.equalsIgnoreCase("B1") & B1 == 0) {
return true;
}
if (play.equalsIgnoreCase("B2") & B2 == 0) {
return true;
}
if (play.equalsIgnoreCase("B3") & B3 == 0) {
return true;
}
if (play.equalsIgnoreCase("C1") & C1 == 0) {
return true;
}
if (play.equalsIgnoreCase("C2") & C2 == 0) {
return true;
}
return play.equalsIgnoreCase("C3") & C3 == 0;
}
/**
*
*
*
* @param play
* @param player
*/
public static void updateBoard(String play, int player) {
if (play.equalsIgnoreCase("A1")) {
A1 = player;
}
if (play.equalsIgnoreCase("A2")) {
A2 = player;
}
if (play.equalsIgnoreCase("A3")) {
A3 = player;
}
if (play.equalsIgnoreCase("B1")) {
B1 = player;
}
if (play.equalsIgnoreCase("B2")) {
B2 = player;
}
if (play.equalsIgnoreCase("B3")) {
B3 = player;
}
if (play.equalsIgnoreCase("C1")) {
C1 = player;
}
if (play.equalsIgnoreCase("C2")) {
C2 = player;
}
if (play.equalsIgnoreCase("C3")) {
C3 = player;
}
}
/**
*
*
*
*/
public static void displayBoard() {
String line;
System.out.println();
line = " " + getXO(A1) + " | " + getXO(A2) + " | " + getXO(A3);
System.out.println(line);
System.out.println("-----------");
line = " " + getXO(B1) + " | " + getXO(B2) + " | " + getXO(B3);
System.out.println(line);
System.out.println("-----------");
line = " " + getXO(C1) + " | " + getXO(C2) + " | " + getXO(C3);
System.out.println(line);
System.out.println();
}
public static String getXO(int square) {
if (square == 1) {
return "X";
}
if (square == 2) {
return "O";
}
return " ";
}
/**
*
*
*
* @return
*/
public static String computerTurn() {
if (A1 == 0) {
return "A1";
}
if (A2 == 0) {
return "A2";
}
if (A3 == 0) {
return "A3";
}
if (B1 == 0) {
return "B1";
}
if (B2 == 0) {
return "B2";
}
if (B3 == 0) {
return "B3";
}
if (C1 == 0) {
return "C1";
}
if (C2 == 0) {
return "C2";
}
if (C3 == 0) {
return "C3";
}
return "";
}
/**
*
*
*
* @return
*/
public static boolean isGameWon() {
if (isRowWon(A1, A2, A3)) {
return true;
}
if (isRowWon(B1, B2, B3)) {
return true;
}
if (isRowWon(C1, C2, C3)) {
return true;
}
if (isRowWon(A1, B1, C1)) {
return true;
}
if (isRowWon(A2, B2, C2)) {
return true;
}
if (isRowWon(A3, B3, C3)) {
return true;
}
if (isRowWon(A1, B2, C3)) {
return true;
}
return isRowWon(A3, B2, C1);
}
/**
*
*
*
* @param a
* @param b
* @param c
* @return
*/
public static boolean isRowWon(int a, int b, int c) {
return ((a == b) & (a == c) & (a != 0));
}
}