I am trying to create a really basic AI response for a game of connect four in Java. Currently, I have a functional game where two human players can play against eachother. However, I would like to include extremely basic AI functionality, so that a human can play against the computer.

For example, if player 1 (human) selects a column and plays a counter, player 2 (computer) will automatically place a counter in a random column straight after the human player has had their turn (it doesn't matter whether it is good or bad move, just as long as there is a response from the computer). Is this possible? I have been reading online about the minimax solution, however I dont need to implement something that complex.

Here is my current code for the play class when two human players play the game:

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 {

            //player 2
            userInput = getUserInput();
            move = Integer.parseInt(userInput);


            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;
}

What would be the best way to change the player 2 section of my code so that the computer decides where to put a counter, rather than a human?

2 Answers

2
Conffusion On Best Solutions

You should work with a Player interface:

public interface Player {
    public String getUserInput(Board board);
}

and 2 implementations, one for a human and one for your robot:

public class HumanPlayer implements Player {
    String name;
    public HumanPlayer(String name) {
        this.name=name;
    }
    public String getUserInput(Board board) {
        // your code to ask a move from the user input (System.in.)
    }
}

public class RobotPlayer implements Player {
    String name;
    public RobotPlayer(String name) {
        this.name=name;
    }
    public String getUserInput(Board board) {
        // random answer calculated by some AI logic
    }
}

I see you still haven't renamed your board class to Board :)

In your main application you can keep a list of players:

List<Player> playersList;
playersList.add(new HumanPlayer("Ben"));
playersList.add(new RobotPlayer("R2D2"));

and in your main game loop you start each iteration by selecting the next player:

int i=playerIndex=0;
while (!win) {
    Player currentPlayer=players.get(i);
    System.out.println("Current player:"+currentPlayer.name;
    // put here all your code for one player
    // ....

    // calculate index of next player   
    i= i<playersList.size()?i++:0;
}
0
Community On

Just use Math.random() to get integers between 1 and 4