HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Connect Four in Java for two human players

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
humanconnectfourjavaplayerstwofor

Problem

Connect Four kinda caught my eye, and I decided to start from the very basics. Here I have bare-bones implementation for playing the game between two human players on the command line:

Board.java

```
package net.coderodde.game.connect4;

import java.util.Objects;

/**
* This class implements the actual Connect Four board.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Jun 28, 2016)
*/
public class Board {

/**
* The number of columns in the board.
*/
private static final int BOARD_WIDTH = 7;

/**
* The number of rows in the board.
*/
private static final int BOARD_HEIGHT = 6;

/**
* The minimum length of a winning pattern.
*/
private static final int WINNING_PATTERN_LENGTH = 4;

private final PlayerColor[][] board = new PlayerColor[BOARD_HEIGHT]
[BOARD_WIDTH];

public int getWidth() {
return board[0].length;
}

public int getHeight() {
return board.length;
}

/**
* Performs a ply of the player with the color {@code color} putting a new
* piece at column {@code x}.
*
* @param x the column index.
* @param color the player color.
*/
public void put(final int x, final PlayerColor color) {
checkX(x);
checkColumnNotFull(x);
Objects.requireNonNull(color, "The input color is null.");

int y = board.length - 1;

while (board[y][x] != null) {
--y;
}

board[y][x] = color;
}

public PlayerColor checkVictory() {
if (checkVictory(PlayerColor.RED)) {
return PlayerColor.RED;
}

if (checkVictory(PlayerColor.YELLOW)) {
return PlayerColor.YELLOW;
}

return null;
}

public boolean isFull() {
for (int y = 0; y = 0; --y, ++height) {
if (board[y][x] == null) {
return height;
}
}

r

Solution

/**
 * Performs a ply of the player with the color {@code color} putting a new
 * piece at column {@code x}.
 * 
 * @param x     the column index.
 * @param color the player color.
 */
public void put(final int x, final PlayerColor color) {


First, typo, should be "Performs a play".

Second, why does it take final int x when all your documentation contains is "the column index"? Why not... final int columnIndex?

int y = board.length - 1;


This is hard to read. Make use of your own helper methods:

int y = getHeight() - 1;


public boolean isFull() {
    for (int y = 0; y < board.length; ++y) {
        for (int x = 0; x < board[0].length; ++x) {
            if (board[y][x] == null) {
                return false;
            }
        }
    }

    return true;
}


Weirdly enough, all that matters for connect-four is that the top row has to be filled in order to block all remaining plays. Perhaps that's not what this method checks, but if you're going to use isFull to check if a new move is possible, then what this really should consist of is checking if the top row has any free spaces. There's no need to start from the bottom up.

Code Snippets

/**
 * Performs a ply of the player with the color {@code color} putting a new
 * piece at column {@code x}.
 * 
 * @param x     the column index.
 * @param color the player color.
 */
public void put(final int x, final PlayerColor color) {
int y = board.length - 1;
int y = getHeight() - 1;
public boolean isFull() {
    for (int y = 0; y < board.length; ++y) {
        for (int x = 0; x < board[0].length; ++x) {
            if (board[y][x] == null) {
                return false;
            }
        }
    }

    return true;
}

Context

StackExchange Code Review Q#133297, answer score: 2

Revisions (0)

No revisions yet.