Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java中的math.random()函数让计算机在Connect4游戏中选择一个随机列?_Java - Fatal编程技术网

如何使用Java中的math.random()函数让计算机在Connect4游戏中选择一个随机列?

如何使用Java中的math.random()函数让计算机在Connect4游戏中选择一个随机列?,java,Java,我一直在用Java创建一个connect four游戏。最初,它是为两个人类玩家设计的,他们可以相互对抗。但是,我现在尝试为第二个播放器(现在是计算机)使用math.random()函数,以便计算机选择一个随机列来放入计数器(不必是好的或坏的移动,只需随机) 目前,math.random()函数只是在人类玩家每次移动后放置计数器的位置上方放置一个计数器。如何使用math.random()函数选择0到6之间的随机列 我知道我的代码有重复/重复/不正确的格式,在我设法解决这个AI功能后,我将重构我的

我一直在用Java创建一个connect four游戏。最初,它是为两个人类玩家设计的,他们可以相互对抗。但是,我现在尝试为第二个播放器(现在是计算机)使用
math.random()
函数,以便计算机选择一个随机列来放入计数器(不必是好的或坏的移动,只需随机)

目前,
math.random()
函数只是在人类玩家每次移动后放置计数器的位置上方放置一个计数器。如何使用
math.random()
函数选择0到6之间的随机列

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

play.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;
}
公共类游戏{
私有连接4game-connect;
公共播放(连接4GAME连接){
this.connect=connect;
}
公共游戏{
System.out.println(“欢迎连接4”);
System.out.println(“要玩游戏,请键入要输入计数器的列的编号”);
System.out.println(“播放器1=r播放器2=y”);
System.out.println(“”);
board boardObj=新板(连接);
boardObj.printBoard();
布尔赢=假;
而(!赢){
//玩家1
字符串userInput=getUserInput();
int move=Integer.parseInt(userInput);
计数器计数器OBJ=新计数器(连接);
计数器对象放置计数器('r',移动);
布尔hasWon=false;
整数计数=0;
//水平检查

对于(int i=0;i您可以通过执行以下操作获得一个随机数:

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

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

编译器应该告诉你
math.random();
。它必须是
math.random()吗
或者您也可以使用
Random
类吗?使用
Math.Random()并不难
不过,只需乘以上限,将结果截断为整数,然后使用该数字。阅读java命名约定:类名总是大写。因此,我用这个函数替换math.random函数,它应该可以工作吗?目前我的笔记本电脑无法直接测试,这会给你一个随机数0到6,因此如果代码正确,它应该可以工作。