patternjavaMinor
Connect Four in Java
Viewed 0 times
fourconnectjava
Problem
I wrote this program as an assignment for an introductory programming course in Java, which I then decided to improve past the minimum assignment requirements. It allows two human players to play Connect Four. (If you don't know this game, its rules are explained in the code below.)
I would appreciate any feedback such as feature suggestions, bug fixes, optimizations, or other improvements!
```
/*ConnectFour
by Jughead
November 27th, 2016
This program lets two human players play Connect Four.
*/
import java.util.Scanner;
public class ConnectFour {
public static void main(String[] args) {
int turnAlternator, turnNumber, match, player, player1Wins, player2Wins;
final int NUMBER_OF_ROWS, NUMBER_OF_COLUMNS, MINIMUM_CHAIN_TO_WIN;
String player1GamePiece, player2GamePiece;
boolean gameOver;
String[][] gameBoard;
Object [] gameOverAndPlayer1WinsAndPlayer2Wins; //Object array of following variables: gameOver, player1Wins, and player2Wins.
System.out.println(" ____ ___ _ _ _ _ _____ ____ _____ _____ ___ _ _ ____ \n / ___/ _ \\| \\ | | \\ | | ____/ ___|_ _| | ___/ _ \\| | | | _ \\ \n" +
"| | | | | | \\| | \\| | _|| | | | | |_ | | | | | | | |_| |\n| |__| |_| | |\\ | |\\ | |__| |___ | | | _|| |_| | |_| | _ | \n" +
" \\____\\___/|_| \\_|_| \\_|_____\\____| |_| |_| \\___/ \\___/|_| \\_\\\n\n" +
"Connect Four is a two-player connection game in which the players\nfirst choose a colour and then take turns dropping colored discs\nfrom the top into a seven-column, six-row grid. " +
"The pieces fall\nstraight down, occupying the next available space within the column.\nThe objective of the game is to connect four of one's own discs of\nthe same color next to each other vertically, horizontally, or\ndiagonally before your opponent.\n"); //Intro text.
NUMBER_OF_ROWS = 6;//Number of game board rows.
NUMBER_OF_COLUMNS =
I would appreciate any feedback such as feature suggestions, bug fixes, optimizations, or other improvements!
```
/*ConnectFour
by Jughead
November 27th, 2016
This program lets two human players play Connect Four.
*/
import java.util.Scanner;
public class ConnectFour {
public static void main(String[] args) {
int turnAlternator, turnNumber, match, player, player1Wins, player2Wins;
final int NUMBER_OF_ROWS, NUMBER_OF_COLUMNS, MINIMUM_CHAIN_TO_WIN;
String player1GamePiece, player2GamePiece;
boolean gameOver;
String[][] gameBoard;
Object [] gameOverAndPlayer1WinsAndPlayer2Wins; //Object array of following variables: gameOver, player1Wins, and player2Wins.
System.out.println(" ____ ___ _ _ _ _ _____ ____ _____ _____ ___ _ _ ____ \n / ___/ _ \\| \\ | | \\ | | ____/ ___|_ _| | ___/ _ \\| | | | _ \\ \n" +
"| | | | | | \\| | \\| | _|| | | | | |_ | | | | | | | |_| |\n| |__| |_| | |\\ | |\\ | |__| |___ | | | _|| |_| | |_| | _ | \n" +
" \\____\\___/|_| \\_|_| \\_|_____\\____| |_| |_| \\___/ \\___/|_| \\_\\\n\n" +
"Connect Four is a two-player connection game in which the players\nfirst choose a colour and then take turns dropping colored discs\nfrom the top into a seven-column, six-row grid. " +
"The pieces fall\nstraight down, occupying the next available space within the column.\nThe objective of the game is to connect four of one's own discs of\nthe same color next to each other vertically, horizontally, or\ndiagonally before your opponent.\n"); //Intro text.
NUMBER_OF_ROWS = 6;//Number of game board rows.
NUMBER_OF_COLUMNS =
Solution
only
Java is an object oriented programming language so you should get used to use objects. This begins by using an object of you class in the first place. Some may argue that this in not needed in you approach but I could think of some objects which could collaborate to make up the game.
naming
You correctly use camelCase names for your identifiers.
But the names of the identifiers should clearly express their purpose. Also method names should start with a verb while variable names should start with a noun or an adjective.
booleans are somewhat special and should start with "is" or "has" prefix.
Comments to structure the code
You use comments to guide the reader through your code.
You should better separate the code to which the comment belongs to a method of its own with a name derived from the comment.
[edit]
@Jughead: "What do you mean by "You should better separate the code to which the comment belongs to a method of its own with a name derived from the comment."?"
example
old code
improvement
Of cause this does not compile because of missing parameters and return values, but I hope you get the idea...
Comments in general should tell why the code is like it is.
odd ball solutions
Your use of the
This would require a comment to explain it.
code duplication
large parts of your code are repeated. you should place that code in parameterized methods.
static methodsJava is an object oriented programming language so you should get used to use objects. This begins by using an object of you class in the first place. Some may argue that this in not needed in you approach but I could think of some objects which could collaborate to make up the game.
naming
You correctly use camelCase names for your identifiers.
But the names of the identifiers should clearly express their purpose. Also method names should start with a verb while variable names should start with a noun or an adjective.
booleans are somewhat special and should start with "is" or "has" prefix.
Comments to structure the code
You use comments to guide the reader through your code.
You should better separate the code to which the comment belongs to a method of its own with a name derived from the comment.
[edit]
@Jughead: "What do you mean by "You should better separate the code to which the comment belongs to a method of its own with a name derived from the comment."?"
example
old code
public static String[][] dropPiece (String[][] gameBoard, int column, int player, int NUMBER_OF_COLUMNS, String player1GamePiece, String player2GamePiece) {//Drops game piece into selected column.
int row;
outerLoop:
for (row = gameBoard.length - 1; row >= -1; row--) {
if (row < 0) {//If chosen column is full, asks for a different column.
System.out.println("Column " + (column + 1) + " is already full.");
dropPiece(gameBoard, getColumn(player, NUMBER_OF_COLUMNS, player1GamePiece, player2GamePiece) - 1, player, NUMBER_OF_COLUMNS, player1GamePiece, player2GamePiece);
break;
}improvement
public static String[][] dropPieceIntoSelectedColumn (String[][] gameBoard, int column, int player, int NUMBER_OF_COLUMNS, String player1GamePiece, String player2GamePiece) {
for (int row = gameBoard.length - 1; row >= -1; row--) {
if(isChosenColumnFull(row)) {
askForDifferentColumn();
} else { // do not use 'breake' to leave a loop
// other 2 if here
}
}
}Of cause this does not compile because of missing parameters and return values, but I hope you get the idea...
Comments in general should tell why the code is like it is.
odd ball solutions
Your use of the
for loop is quite unusual. for (turnAlternator = player; ; turnAlternator++, turnNumber++)This would require a comment to explain it.
code duplication
large parts of your code are repeated. you should place that code in parameterized methods.
Code Snippets
public static String[][] dropPiece (String[][] gameBoard, int column, int player, int NUMBER_OF_COLUMNS, String player1GamePiece, String player2GamePiece) {//Drops game piece into selected column.
int row;
outerLoop:
for (row = gameBoard.length - 1; row >= -1; row--) {
if (row < 0) {//If chosen column is full, asks for a different column.
System.out.println("Column " + (column + 1) + " is already full.");
dropPiece(gameBoard, getColumn(player, NUMBER_OF_COLUMNS, player1GamePiece, player2GamePiece) - 1, player, NUMBER_OF_COLUMNS, player1GamePiece, player2GamePiece);
break;
}public static String[][] dropPieceIntoSelectedColumn (String[][] gameBoard, int column, int player, int NUMBER_OF_COLUMNS, String player1GamePiece, String player2GamePiece) {
for (int row = gameBoard.length - 1; row >= -1; row--) {
if(isChosenColumnFull(row)) {
askForDifferentColumn();
} else { // do not use 'breake' to leave a loop
// other 2 if here
}
}
}for (turnAlternator = player; ; turnAlternator++, turnNumber++)Context
StackExchange Code Review Q#148171, answer score: 2
Revisions (0)
No revisions yet.