This is a simple implementation of Connect four in Java. It is working correctly so far. What I haven't done yet are the diagonals check and input validation.
package game;
import java.util.Scanner;
//TODO check for diagonals, input validation
public class Game {
private char[][] gameBoard;
private State gameState;
private Turn turn;
private int[] possiblePositions;
private static final int BOARD_HEIGHT = 6;
private static final int BOARD_WIDTH = 7;
enum Turn {
PLAYER_ONE, PLAYER_TWO
}
enum State {
PLAYER_1_WIN, PLAYER_2_WIN, RUNNING
}
public Game() {
gameBoard = new char[BOARD_HEIGHT][BOARD_WIDTH];
for (int row = 0; row < BOARD_HEIGHT; row++) {
for (int col = 0; col < BOARD_WIDTH; col++) {
gameBoard[row][col] = '□';
}
}
turn = Turn.PLAYER_ONE;
gameState = State.RUNNING;
possiblePositions = new int[] { 5, 5, 5, 5, 5, 5, 5 };
}
public void playerMove(int column) {
switch (turn) {
case PLAYER_ONE:
gameBoard[possiblePositions[column]][column] = '■';
turn = Turn.PLAYER_TWO;
break;
case PLAYER_TWO:
gameBoard[possiblePositions[column]][column] = '⊠';
turn = Turn.PLAYER_ONE;
break;
}
possiblePositions[column]--;
}
public void printBoard() {
System.out.println("0 1 2 3 4 5 6");
for (int row = 0; row < BOARD_HEIGHT; row++) {
for (int col = 0; col < BOARD_WIDTH; col++) {
System.out.print(gameBoard[row][col] + " ");
}
System.out.println();
}
}
public void checkRowAndCol() {
int chainLength = 1;
for (int row = 0; row < BOARD_HEIGHT - 1; row++) {
for (int col = 0; col < BOARD_WIDTH - 1; col++) {
if (gameBoard[row][col] == gameBoard[row][col + 1] && gameBoard[row][col] != '□') {
++chainLength;
if (chainLength == 4) {
switch (gameBoard[row][col]) {
case '■':
gameState = State.PLAYER_1_WIN;
break;
case '⊠':
gameState = State.PLAYER_2_WIN;
break;
}
}
} else {
chainLength = 1;
}
}
chainLength = 1;
}
for (int col = 0; col < BOARD_WIDTH - 1; col++) {
for (int row = 0; row < BOARD_HEIGHT - 1; row++) {
if (gameBoard[row][col] == gameBoard[row + 1][col] && gameBoard[row][col] != '□') {
++chainLength;
if (chainLength == 4) {
switch (gameBoard[row][col]) {
case '■':
gameState = State.PLAYER_1_WIN;
break;
case '⊠':
gameState = State.PLAYER_2_WIN;
break;
}
}
} else {
chainLength = 1;
}
}
chainLength = 1;
}
}
public void start() {
Scanner reader = new Scanner(System.in);
printBoard();
while (gameState == State.RUNNING) {
playerMove(reader.nextInt());
checkRowAndCol();
printBoard();
}
System.out.println(gameState);
reader.close();
}
}