2

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));
    }
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
jjimih
  • 89
  • 11
  • 1
    @Turing85 I believe he doesn't mean scale in the sense of scalability, but rather he wants the implementation to cope with physically scaling the board from 3x3 to 9x9. – Michael Nov 11 '15 at 16:54
  • @Michael He mentioned 5-in-a-row so I think he actually wants to implement Gomoku. – Jaroslaw Pawlak Nov 11 '15 at 17:00
  • Gomoku and Tic Tac Toe or two very different games from technical point of few. Tic Tac Toe has very few possible outcomes, and assuming perfect strategy it will always result in a draw. You mentioned computer player - AI for Gomoku is far more complicated. In case of Tic Tac Toe I wouldn't even call it AI, but just an if-statement. You cannot make this board scalable in as easy way as you would like. – Jaroslaw Pawlak Nov 11 '15 at 17:02

1 Answers1

3

It's really long to write how to scale this program but some tips:

static int A1, A2, A3, B1, B2, B3, C1, C2, C3; // Board spots.

Actually this simulates

static int board[][] = new int[3][3];

To scale your program replace declaration of variables by

static int size;
static int board[][];

Ask the user for size and

System.out.print("Insert size of the board : ");
size = c.nextInt();
board = new int[size][size];

After this, update your displayBoard, isValidPlay and updateBoard accordingly.

NOTE I would prefer to play making moves like 1,1 instead of A1, if you don't want to use a comma, start your board in 0 you can play with 01

for example (you will see methods now are much shorter).

public static boolean isValidPlay(String play) {  // play = 1,1 or 3,1 
    int x = Integer.valueOf(play.split(",")[0]); 
    int y = Integer.valueOf(play.split(",")[0]);

    // check if move is inside the board
    if (x > size || y > size) return false;

    // check if given position is empty and return accordingly
    if (board[x][y] == 0) return true;
    else return false;
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 1
    Thank you. This helped a lot. I will try to implement this today. – jjimih Nov 12 '15 at 06:01
  • Couldn't get it to work. Edited my question with the code I have now. – jjimih Nov 13 '15 at 13:57
  • Dont do this, ask a new question – Jordi Castilla Nov 13 '15 at 14:02
  • Thanks for the tip and help. – jjimih Nov 13 '15 at 14:28
  • @jjimih don't hesitate to ask a new question with the new parts where you're stucked and link it here if you want, I will take a look and try to solve your issues, but, SackOverflow is against chameleon questions, this case can be useful for other users, so, if now you're stucked in another part, just mark this as accepted and create a new one explaining the problem, if you edit the questions with evolution of the code, most answers become obsolete and it's hard to other users to follow the edits. – Jordi Castilla Nov 15 '15 at 11:25
  • http://stackoverflow.com/questions/33738336/how-do-i-determine-a-winner-in-my-tic-tac-toe-program – jjimih Nov 16 '15 at 15:00
  • @jjimih already answered new question, don't hesitate to comment in my answer there if you have problems, also, as this is your first question, you must know you can mark this question as accepted clicking the check mark, this will give both (you and me) some points of reputation ;) – Jordi Castilla Nov 16 '15 at 15:40