-2

I am making a tic tac toe game and one of the requirements is to have the game randomly decide who goes first. I assume I should be using Math.random() but I don't know how to implement it. If anyone can, please help adjust my code thanks :)

import java.util.Scanner;
public class TicTacToe
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner (System.in);

        Game ticTacToe = new Game();

        String no = "no";
        System.out.println("~~~Tic Tac Toe~~~");
        System.out.println("Would you like to play?");
        String playerAnswer = console.nextLine();
        while(!playerAnswer.equals(no))
        {
            ticTacToe.play();
            System.out.println("Thanks for playing");
            System.out.println("Would you like to play again? Press any key for yes, type no if you don't ");
            playerAnswer=console.nextLine();
        }
    }
}
class Game
{
    private final int empty = 0;
    private final int player = 1;
    private final int com = 2;
    private final int size = 3;
    private int[][] board;

    public void printScreen()
    {
        int col;
        int row;
        System.out.println();
        System.out.print(" ");
        for (col = 0; col < size; col ++)
        {
            System.out.print(" " + (col+1));
        }
        System.out.println();
        System.out.print(" ");
        for (col = 0; col < size; col ++)
        {
            System.out.print("--");
        }
        System.out.println("-");
        for (row = 0; row < size; row ++)
        {
            System.out.print((row+1) + "|");
            for (col = 0; col < size; col ++)
            {
                if (board[row][col] == empty)
                {
                    System.out.print(" ");
                }
                else if (board[row][col] == player)
                {
                    System.out.print("X");
                }
                else if (board[row][col] == com)
                {
                    System.out.print("O");
                }
                System.out.print("|");
            }
            System.out.println();
            System.out.print(" ");
            for (col = 0; col < size; col ++)
            {
                System.out.print("--");
            }
            System.out.println("-");
        }
    }

    public void clear()
    {
        int col;
        int row;
        board = new int[size][size];
        for (row = 0; row < size; row ++)
        {
            for (col = 0; col < size; col ++)
            {
                board[row][col] = empty;
            }
        }
    }

    public void computerMove()
    {
        int col;
        int row;
        int count;
        int select;
        count = 0;
        for (row = 0; row < size; row ++)
            for (col = 0; col < size; col ++)
                if (board[row][col] == empty)
                    count ++;
        select = (int) (Math.random() * count);
        count = 0;
        for (row = 0; row < size; row ++)
        {
            for (col = 0; col < size; col ++)
            {
                if (board[row][col] == empty)
                {
                    if (count == select)
                    {
                        board[row][col] = com;
                        System.out.println("The computer selects row" + (row+1) + " column " + (col+1) + ".");
                    }
                    count ++;
                }
            }
        }
    }

    public void playerMove()
    {
        Scanner console = new Scanner (System.in);
        boolean a;
        int col;
        int row;
        a = true;
        while (a)
        {
            System.out.println("What is your move?  Select a row number from 1 to " + size + " and a column number from 1 to " + size + ".");
            row = console.nextInt();
            col = console.nextInt();
            if ((row < 1) || (row > size) || (col < 1) || (col > size))
            {
                System.out.println("Invalid choice, row " + row + " or column " + col + " must be from 1 to " + size + ".");
            }
            else
            {
                row --;
                col --;
                if (board[row][col] != empty)
                {
                    System.out.println("That spot is already filled");
                    printScreen();
                }
                else
                {
                    board[row][col] =player;
                    a = false;
                }
            }
        }
    }

    public boolean checkWinner()
    {
        int col;
        int row;
        int count;
        int win;
        win = empty;
        for (row = 0; row < size; row ++)
        {
            count = 0;
            if (board[row][0] != empty)
                for (col = 0; col < size; col ++)
                    if (board[row][0] == board[row][col])
                        count ++;
            if (count == size)
                win = board[row][0];
        }
        for (col = 0; col < size; col ++)
        {
            count = 0;
            if (board[0][col] != empty)
                for (row = 0; row < size; row ++)
                    if (board[0][col] == board[row][col])
                        count ++;
            if (count == size)
                win = board[0][col];
        }
        count = 0;
        if (board[0][0] != empty)
            for (row = 0; row < size; row ++)
                if (board[0][0] == board[row][row])
                    count ++;
        if (count == size)
            win = board[0][0];
        count = 0;
        if (board[0][size-1] != empty)
            for (row = 0; row < size; row ++)
                if (board[0][size-1] == board[row][size-row-1])
                    count ++;
        if (count == size)
            win = board[0][size-1];
        if (win != empty)
        {
            if (win == player)
                System.out.println("Congratz you won");
            else if(win == 3)
                System.out.println("you lost");
            return true;
        }
        count = 0;
        for (row = 0; row < size; row ++)
            for (col = 0; col < size; col ++)
                if (board[row][col] == empty)
                    count ++;
        if (count == 0)
        {
            System.out.println("Its a tie!");
            return true;
        }
        return false;
    }

    public void play()
    {
        boolean e;
        clear();
       e=false;
        while (!e)
        {
            printScreen();
            playerMove();
            printScreen();
            e = checkWinner();
            if (!e)
            {
                computerMove();
                e = checkWinner();
                if (e)
                    printScreen();
            }
        }
    }
}

2 Answers2

1

There may be many ways to do this including using a Random generator class, but as tic-tac only has two players, this can simply be done as getting a boolean

boolean player1Starts = (System.currentTimeMillis() % 2) == 0;
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

To implement this you would have to change your play method.

public void play()
{
    boolean playerTurn = Math.random() <= .5 
//this function returns a value between 0 an 1 exclusive
    clear();
    printScreen();
    while (!checkWinner())
    {
        if (playerTurn) {
            playerMove;
        } else {
            computerMove();
        }
        playerTurn = !playerTurn;
        printScreen();
    }
}
  1. What this does is first determine if the player starts.
  2. Then it clears and prints the empty board.
  3. Then It loops through checking if there is a winner
  4. If there isn't a winner we go into the loop and then if its the players turn they move else the computer moves
  5. Then we update the playerTurn to be the opposite of what it was.
  6. Then print the screen to see the move.
  7. If there is a winner we exit the loop and finish the play method