Game: Connect Four is a two-player board game in which the players alternately drop colored disks into a seven-column, six-row vertically suspended grid, as shown below.
The objective of the game is to connect four same-colored disks in a row, a column, or a diagonal before an opponent can do likewise. The program prompts a player to choose a color disk. Then the computer and the player will drop a red or yellow disk alternately. In the figure, the player with the red disk is the winner of the game.
This implementation is text based. Take a look a the simplicity of the format. This will enable you to concentrate on the algorithm and the strategy you need to implement to have the computer to win. Let’s develop the “computer to win” a bit further. You do not want to play against an infallible player (the computer) because you will not even want to play the game. So a better approach would be a good blend of random moves with smart moves. You decide how “smart” the computer is.
Whenever a disk is dropped, the program redisplays the board on the console and determines the status of the game (win, draw, or continue). Here is a sample run:
Let's assume the user is controlling the red disk, | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ——————————————— Drop a red disk at column (0–6): 0 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |R| | | | | | | ——————————————— Computer drops a yellow disk at column 4 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |R| | | |Y| | | . . . . Computer drops a yellow disk at column 6 | | | | | | | | | | | | | | | | | | | | |Y| | | | | | |Y|Y|Y|R| | | |Y|R|R|Y|Y| |R|Y|R|Y|R|R|R| ——————————————— The computer won Do you want to play again?
- Write a one-player program, Connect4TwoD_YI.java using 2D array. Display at a every move the board as shown in the example above. Include a prompt to continue playing.
- Write a one-player program, Connect4OneD_YI.java using only 1D array. Display at a every move the board as shown in the example above. Include a prompt to continue playing. This is a bit more complex but you will find a pattern to help you implement it.
Little help #1: Use paper and pencil to design, trace and test your algorithm before you start.
Little help #2: You can use static methods (functions) to make the code more readable and easier to manage. This is only optional.