首页 > 解决方案 > 如何使用 Java 中的 math.random() 函数让计算机在我的 Connect4 游戏中选择随机列?

问题描述

我一直在用 Java 创建一个四人连接游戏。最初,它是为两个人类玩家对战而设计的。但是,我现在尝试math.random()为第二个玩家(现在是计算机)使用该功能,以便计算机选择一个随机列来放置一个计数器(不一定是好棋或坏棋,只是随机的)。

目前,该math.random()功能只是在人类玩家每次走后放置计数器的顶部放置一个计数器。如何获得math.random()选择 0 到 6 之间的随机列的函数?

我知道我的代码有重复/重复/不正确的格式,在我设法解决这个 AI 功能后,我将重构我的代码。

播放.java

public class play {

private Connect4Game connect;
public play(Connect4Game connect) {
    this.connect=connect;
}

public void playGame() {
    System.out.println("Welcome to Connect 4");
    System.out.println("To play the game type in the number of the column you want to drop you counter in");
    System.out.println("Player One = r Player 2 = y");
    System.out.println("");


    board boardObj = new board(connect);
    boardObj.printBoard();


    boolean win = false;
    while(!win){

        // player 1
        String userInput = getUserInput();
        int move = Integer.parseInt(userInput);

        counter counterObj = new counter(connect);
        counterObj.placeCounter('r', move);


        boolean hasWon = false;
        int count = 0;

        // check horizontal
        for(int i=0; i<connect.board.length; i++){
            for(int j=0; j<connect.board[i].length; j++){
                if(connect.board[i][j] == 'r'){
                    count = count + 1;
                    if(count == 4){
                        hasWon = true;

                    }
                }
                else{
                    count = 0;
                }
            }

        }

        // check vertical 
        count = 0;
        for(int i=0; i<connect.board[0].length; i++){
            for(int j=0; j<connect.board.length; j++){
                if(connect.board[j][i] == 'r'){
                    count = count + 1;
                    if(count >= 4){
                        hasWon = true;

                    }
                }
                else{
                    count = 0;
                }
            }

        }
        boardObj.printBoard();
        if(hasWon){
            win = true;
            System.out.println("You Have Won!!!");
        }

        else {

            //Computer player
            math.random();


            counterObj.placeCounter('y',move);


            hasWon = false;
            count = 0;

            // check horizontal
            for(int i=0; i<connect.board.length; i++){
                for(int j=0; j<connect.board[i].length; j++){
                    if(connect.board[i][j] == 'y'){
                        count = count + 1;
                        if(count >= 4){
                            hasWon = true;

                        }
                    }
                    else{
                        count = 0;
                    }
                }

            }

            // check vertical 
            count = 0;
            for(int i=0; i<connect.board[0].length; i++){
                for(int j=0; j<connect.board.length; j++){
                    if(connect.board[j][i] == 'y'){
                        count = count + 1;
                        if(count >= 4){
                            hasWon = true;

                        }
                    }
                    else{
                        count = 0; 
                    }
                }

            }
            boardObj.printBoard();
            if(hasWon){
                win = true;
                System.out.println("You Have Won!!!");
            }
        }

    }

}



public String getUserInput(){
    String toReturn = null;
    try{            
        toReturn = connect.input.readLine();
    }
    catch(Exception e){

    }
    return toReturn;
}

标签: java

解决方案


您可以通过以下方式获取随机数:

Random r = new Random();
int num = r.nextInt(7);

我设置了 7 而不是 6,因为最大值是独占的。所以如果我输入 6,它会给你一个从 0 到 5 的数字。


推荐阅读