import java.util.Scanner;
public class EzConnect4 {
public static void main(String[] args) {
// generate game board
char[][] board = new char[6][7];
// involve scanner so that user can input choice
Scanner scanner = new Scanner(System.in);
// make board with initial completely empty spaces
for (char[] row : board) {
// each row has empty spaces
java.util.Arrays.fill(row, ' ');
}
char currentPlayer = 'X'; // player1 (first user)
boolean gameRunning = true; // checks if the game is still running, user is still active
// main loop
while (gameRunning) {
printBoard(board); // prints game board
int chosenColumn = getColumnChoice(scanner, currentPlayer); // figures out what column the user has inputted
// put X or O in the respective column
for (int row = board.length - 1; row >= 0; row--) {
// finds the first possible spot it can go
if (board[row][chosenColumn] == ' ') {
board[row][chosenColumn] = currentPlayer;
break;
}
}
// checks for a match result
if (hasPlayerWon(board, currentPlayer)) {
printBoard(board);
System.out.println("Player " + currentPlayer + " wins!");
gameRunning = false; // End the game
} else if (isBoardFull(board)) {
printBoard(board);
System.out.println("It's a draw!");
gameRunning = false; // End the game
}
// other players turn!
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
}
// prints game board AFTER a turn is made
public static void printBoard(char[][] board) {
// Print each row of the game board
for (char[] row : board) {
// Print each cell with formatting
for (char cell : row) {
System.out.print("| " + cell + " ");
}
System.out.println("|");
}
// reprints column numbers (each time)
System.out.println(" 1 2 3 4 5 6 7");
}
// what column has the player chosen
public static int getColumnChoice(Scanner scanner, char currentPlayer) {
int column;
while (true) {
// tells the player to choose a column
System.out.print("Player " + currentPlayer + ", choose a column (1-7): ");
column = scanner.nextInt() - 1; // array index by subtracting 1
if (column >= 0 && column < 7) {
return column;
}
System.out.println("Invalid column choice. Please try again.");
}
}
// check the result - has the player won or not?
public static boolean hasPlayerWon(char[][] board, char player) {
// truly checks if there are four in a row (diagonal, horizontal, vertical)
// horizontal
for (int row = 0; row < board.length; row++) {
for (int col = 0; col <= board[row].length - 4; col++) {
// Check for four consecutive tokens of the same player
if (board[row][col] == player && board[row][col + 1] == player &&
board[row][col + 2] == player && board[row][col + 3] == player) {
return true; // Player has won
}
}
}
// vertical
for (int col = 0; col < board[0].length; col++) {
for (int row = 0; row <= board.length - 4; row++) {
// Check for four consecutive tokens of the same player
if (board[row][col] == player && board[row + 1][col] == player &&
board[row + 2][col] == player && board[row + 3][col] == player) {
return true; // Player has won
}
}
}
// diagonal
for (int row = 0; row <= board.length - 4; row++) {
for (int col = 0; col <= board[row].length - 4; col++) {
// Check for four consecutive tokens of the same player
if (board[row][col] == player && board[row + 1][col + 1] == player &&
board[row + 2][col + 2] == player && board[row + 3][col + 3] == player) {
return true; // Player has won
}
}
}
return false; // nobody has won yet
}
// if the board is full and nobody has won, it is a DRAW
public static boolean isBoardFull(char[][] board) {
// checks whether or not board is full
for (char[] row : board) {
for (char cell : row) {
if (cell == ' ') {
return false;
}
}
}
return true; // Board is full
}
}
// calls main public class
EzConnect4.main(null);
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
1 2 3 4 5 6 7
Player X, choose a column (1-7): | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | X | | | |
1 2 3 4 5 6 7
Player O, choose a column (1-7): | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | X | O | | |
1 2 3 4 5 6 7
Player X, choose a column (1-7): | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | X | | | |
| | | | X | O | | |
1 2 3 4 5 6 7
Player O, choose a column (1-7): | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | O | | | |
| | | | X | | | |
| | | | X | O | | |
1 2 3 4 5 6 7
Player X, choose a column (1-7): | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | O | | | |
| | | | X | X | | |
| | | | X | O | | |
1 2 3 4 5 6 7
Player O, choose a column (1-7): | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | O | | | |
| | | | X | X | | |
| | | | X | O | O | |
1 2 3 4 5 6 7
Player X, choose a column (1-7): | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | O | | | |
| | | | X | X | X | |
| | | | X | O | O | |
1 2 3 4 5 6 7
Player O, choose a column (1-7): | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | O | | | |
| | | | X | X | X | |
| | | | X | O | O | O |
1 2 3 4 5 6 7
Player X, choose a column (1-7): | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | O | | | |
| | | | X | X | X | X |
| | | | X | O | O | O |
1 2 3 4 5 6 7
Player X wins!