Search code examples
javaarraystic-tac-toe

can not get the right output in my java tictactoe game


Hi Guys I really need help with my assignment. I tried doing it but I can't figure it out.

starting board:

1  2  3     

4  5  6     

7  8  9

result desire:

x  2  3

o  x  6

7  o  x

however every time I execute I always get the following whenever there is a winner:

x 2 3

o x 6 

7 o 9

Lastly, how do I calculate the winning ratio of the player that won the most?

Restrictions:

  • You must use a 2D array for the tic-tac-toe board. (I am aware I did not used a 2D array because I could not figure out how to make the compiler accept 1-9 input from a 2D array. )

  • You cannot use classes for this question.

  • You can use static methods if you like.

    import java.util.Scanner;
    public class TicTacToe {    public static String answer = "yes";
    public static Scanner input = new Scanner(System.in);
    public static String getName(int noPlayer) {
        System.out.print("Enter name of Player " + noPlayer + ": ");
        return input.next();
    }
    
    public static int getMove(String board[], String player) {
        printBoard(board);
        System.out.print("Enter move for " + player + ": ");
        int move = input.nextInt() - 1;
        while (moveTaken(board, move)) {
            System.out.println("Move taken.");
            System.out.print("Enter move for " + player + ": ");
            move = input.nextInt() - 1;
        }
        return move;
    }
    public static String gameResult(String board[]) {
        final int checkWin[][] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
                                        {0, 3, 6}, {1, 4, 7}, {2, 5, 8},
                                        {0, 4, 8}, {2, 4, 6}};
        for (int[] i: checkWin) {
            if (board[i[0]].equals(board[i[1]]) && board[i[0]].equals(board[i[2]]) && board[i[1]].equals(board[i[2]])) {
                if (board[i[0]].equals("O")) {
                    return "X wins";
                }
                else {
                    return "O wins";
                }
            }
        }
        if (!board[0].equals("1") && !board[1].equals("2") && !board[2].equals("3") &&
            !board[3].equals("4") && !board[4].equals("5") && !board[5].equals("6") &&
            !board[6].equals("7") && !board[7].equals("8") && !board[8].equals("9")) {
            return "draw";
        }
        return "not completed";
    }
    public static boolean moveTaken(String board[], int move) {
        if (board[move].equals("O") || board[move].equals("X")) {
            return true;
        }
        return false;
    }
    public static void printBoard(String board[]) {
        System.out.println(" " + board[0] + " " + board[1] + " " + board[2] +
                           "\n" +
                           " " + board[3] + " " + board[4] + " " + board[5] +
                           "\n" +
                           " " + board[6] + " " + board[7] + " " + board[8]);
    }
    public static void conclusion(String result, String pO, String pX) {
        if (result.equals("O wins")) {
            System.out.println(pO + " wins!");
    
        }
        else if (result.equals("X wins")) {
            System.out.println(pX + " wins!");
        }
        else {
            System.out.println("Draw.");
        }
    }
    public static void main(String args[]) {
    
          String X = getName(1);
          String O = getName(2);
    
      int count = 1;
      do{   
    
        String nextPlayer = X;
        String board[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
        int move;
            while (gameResult(board).equals("not completed")) {
                move = getMove(board, nextPlayer);
                if (nextPlayer == X) {
                    board[move] = "X";
                    nextPlayer = O;
                }
                else {
                    board[move] = "O";
                    nextPlayer = X;
                }
            }
        System.out.println("game up to date: " + count);
    
       conclusion(gameResult(board), X, O);
    
        System.out.println("Do you want to play again? yes or no");
         answer = input.next();
         count++; 
    
    
    }
        while(answer.equalsIgnoreCase("yes"));
    
    
    
    
      }
    }
    

Solution

  • You only print the board in getMove, prior to performing the move. Therefore the last move is never displayed.

    You should add a call to printBoard when the game ends.

    Here's one possible place to add it :

    ...
    System.out.println("game up to date: " + count);
    printBoard(board);
    conclusion(gameResult(board), X, O);
    ...