0

I have been creating a connect four game in Java. Originally, it was designed for two human players to play against eachother. However, I am now trying to use the math.random() function for the second player (now the computer) so that the computer picks a random column to drop a counter in (does not have to be a good or bad move, just random).

Currently, the math.random() function just drops a counter on top of where the human player places their counter after every go. How do I get the math.random() function to select a random column between 0 and 6?

I am aware that my code has duplication / repetition / incorrect formatting and I will be refactoring my code after I have managed to solve this AI functionality.

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;
}
Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37
Ben Craig
  • 77
  • 7
  • 1
    The compiler should tell you what's wrong with `math.random();`. – jsheeran Apr 30 '19 at 07:49
  • 1
    Does it have to be `Math.random()` or could you use the `Random` class as well? It wouldn't be that hard with `Math.random()` though, just multiply with the upper bound, truncate the result to an int - and _use_ that number. – Thomas Apr 30 '19 at 07:49
  • 1
    And read about java naming conventions: class names go UpperCase, always. – GhostCat Apr 30 '19 at 11:35

1 Answers1

1

You can get a random number by doing:

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

I set 7 instead of 6 because the max value is exclusive. So if I would have put 6, it would have give you a number from 0 to 5.

JensW
  • 442
  • 2
  • 12
  • So i replace the math.random function with this, it should work? Away from my laptop at the moment so unable to test straight away – Ben Craig Apr 30 '19 at 11:43
  • 1
    Well, this gives you a random number from 0 to 6, so if your code is correct, it should work. – JensW Apr 30 '19 at 12:20