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

Connect Four game in Java

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

Problem

I was wondering if you guys could point out any mistakes I've made in this program, and how to improve the code. It's a simple console-based Connect Four game. You use 0-5 to place your piece in the column you want. The goal of the game is to get four in a row, either vertically, horizontally or diagonally.

```
//imports
import java.util.Scanner;

public class Main{
//global variables
final static int WIDTH = 6;
final static int HEIGHT = 6;
final static int BOTTOM_ROW = WIDTH - 1;

//game board
static char[][] board = new char[WIDTH][HEIGHT];

//creates scanner
static Scanner scanner = new Scanner(System.in);

public static void main(String[] args){
//creates board
CreateBoard();

//tells player how to play
System.out.println("Use 0-5 to choose what column you want");

//displays board
PrintBoard();

//creates boolean to determine status of game
boolean flag = true;

//main game loop
while(flag){
//activates player 1s turn, then prints board
DropX();
PrintBoard();

//determines if player 1 has won
if(!CheckX()){
flag = false; //sets flag to false so loop is not repeated if player 1 won
break; //break to skip player 2s turn if won
}

//activates player 2s turn, then prints board
DropO();
PrintBoard();

//determines if player 1 has won
if(!CheckO()){
flag = false; //sets flag to false so loop is not repeated if player 2 won
break; // break for consistency
}
}
}
public static void CreateBoard() {
//fills board with '.' for the width and height
for (int w = 0; WIDTH > w; w += 1) {
for (int h = 0; HEIGHT > h; h += 1) {
board[w][h] = '.';
}
}
}

public static void Pr

Solution

Without commenting much on the logic/intricacies of the code...

  • Java method names follow the camelCase convention.



  • There's too much in-code comments. Comments should document the why, not the how, and if they have to document the how, that is usually a hint that variable names are too cryptic, or the approach can be improved.



-
There's lot of code duplication, since you have embedded the token to check (O vs X) into your method pairs. So, instead of:

private static boolean checkODiagonalBack() {
    ...
    if (board[w][h] == 'O') { ... }
    ...
}

private static boolean checkXDiagonalBack() {
    ...
    if (board[w][h] == 'X') { ... }
    ...
}


You should have a method that takes the token as an argument:

private static boolean checkDiagonalBack(char token) {
    ...
    if (board[w][h] == token) { ... }
    ...
}

Code Snippets

private static boolean checkODiagonalBack() {
    ...
    if (board[w][h] == 'O') { ... }
    ...
}

private static boolean checkXDiagonalBack() {
    ...
    if (board[w][h] == 'X') { ... }
    ...
}
private static boolean checkDiagonalBack(char token) {
    ...
    if (board[w][h] == token) { ... }
    ...
}

Context

StackExchange Code Review Q#100917, answer score: 3

Revisions (0)

No revisions yet.