随机选择Tic Tac Toe gama java中的First Player

时间:2016-10-20 00:30:46

标签: java

我正在制作一个tic tac toe游戏,其中一个要求是让游戏随机决定谁先行。我假设我应该使用Math.random()但我不知道如何实现它。如果有人可以,请帮助调整我的代码,谢谢:)

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 个答案:

答案 0 :(得分:1)

可能有很多方法可以做到这一点,包括使用Random生成器类,但由于tic-tac只有两个玩家,这可以简单地做为获取布尔值

boolean player1Starts = (System.currentTimeMillis() % 2) == 0;

答案 1 :(得分:1)

要实现这一点,您必须更改播放方法。

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. 这样做首先确定玩家是否开始。
  2. 然后它清除并打印空板。
  3. 然后循环检查是否有胜利者
  4. 如果没有胜利者我们进入循环,然后如果玩家转动他们移动其他计算机移动
  5. 然后我们将playerTurn更新为与之相反的内容。
  6. 然后打印屏幕以查看移动。
  7. 如果有胜利者,我们退出循环并完成播放方法