Newer
Older
This is a simple implementation of the Connect Four game in Java. The game is played on a 6x7 grid where two players take turns dropping colored disks into the columns. The first player to connect four of their colored disks in a row, column, or diagonal wins the game. If the board is full and no player has won, the game ends in a draw.
1. The program will display the board on the console, with the column numbers on top and the disk colors as R (red) and Y (yellow). The board has 6 rows and 7 columns, and initially all the cells are empty.
2. The program will prompt the first player (red) to choose a column (0-6) to drop their disk. The player can enter an integer from 0 to 6, and the program will drop the disk in the lowest available row of that column. If the column is full or the input is invalid, the program will display an error message and ask the player to try again.
3. The program will then switch to the second player (yellow) and repeat the same process. The players will alternate turns until one of them wins the game or the board is full.
4. The program will check if the current player has formed a line of four disks of their own color after each move. If so, the program will display the board and a congratulatory message for the winner. If the board is full and no one has won, the program will display the board and a message indicating a draw.
- Each player has a color: RED and YELLOW.
- Players take turns dropping their colored disks into the columns.
- Disks are dropped into the lowest available row in the chosen column.
- The first player to connect four disks of their color in a row, column, or diagonal wins.
- If the board is full and no player has won, the game ends in a draw.
- ConnectFour.java: Contains the main game logic including board initialization, player input, disk dropping, win detection, and board display.
- ROWS: Number of rows on the game board (6).
- COLUMNS: Number of columns on the game board (7).
- RED: Constant representing the RED player's disk.
- YELLOW: Constant representing the YELLOW player's disk.
- EMPTY: Constant representing an empty space on the board.
- main: Main method where the game is initialized and played.
- displayBoard: Method to display the current state of the game board.
- dropDisk: Method to drop a disk into a specified column.
- isWinningMove: Method to check if a disk drop results in a winning move.
- isBoardFull: Method to check if the game board is full.
- This program uses the java.util.Scanner class for user input.
|0 1 2 3 4 5 6 |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
Please choose a column (0-6) to drop the RED disk: 3
|0 1 2 3 4 5 6 |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |R| | | |
Please choose a column (0-6) to drop the YELLOW disk: 4
|0 1 2 3 4 5 6 |
| | | | | | | |
| | | | | | | |
| | | |R| | | |
| | | |R| | | |
| | | |R| |Y| |
| | | |R|Y|Y| |
...
Congratulations! RED has won the game!