Reference no: EM132766792
Assignment
Instructions:
• Once you are done with the assignment, upload your source code to Omnivox as you would any exercise/assignment.
Overview
Connect 4 is a board game for 2 players in which the goal is to make a line, or column, or diagonal, of 4 colored discs in a grid of 6 rows and 7 columns. Each colored disc is "dropped" in the grid by a player, and falls straight down to the lowest available position within the column in which it was dropped. In other words, a dropped disc either falls to the bottom of the column, or onto another disc that had already been dropped in that column.
Each player has 21 discs; one player has blue discs and the other player (in this case, the computer) has green discs. One after the other, the players drop one disc at a time in the grid. When a player manages to make a line, column, or diagonal of 4 discs, the game ends.
What you must do
You must code a game of Connect 4 which is played in a 2D array of 6 rows and 7 columns. A player wins as soon as a line, column or diagonal of 4 discs is made by that player.
Each spot of the 2D array, like all 2D arrays, has a row-column coordinate. The program first asks the player which column the disc has to be dropped into, then checks that it is a valid column, then calculates at which line the disc stops. Of course, as long as the selected column is not valid, the program asks the player for the number of another column. Then, for each disc dropped by player 1 or player 2, your program has to calculate at which line the disc stops.
You must document that game; meaning, you must write a technical design document for it.
You must also write unit tests for at least 4 functions (one unit test per function).
The appearance of the game's grid
To simplify things, the game could be printed to the screen with textual characters. And so, player 1 could have blue discs represented by a ‘B' on the screen and player 2 (the computer) could have green discs represented by a ‘G' on the screen.
Below, you have an example of a game in which player 2 won by putting 4 G(reen) discs in diagonal:
6 | | | | | | | |
5 | | | | | | | |
4 | | | |G|B| | |
3 |B|B|B|G|G| | |
2 |G|G|B|B|B|G| |
1 |B|G|G|G|B|B|G|
- - - - - - - 1 2 3 4 5 6 7
Note: the grid above is only an example of what you might print to the screen. There are many ways to represent a Connect 4 grid on a screen.
Your program, step by step
First, the initial grid is shown on the screen.
Second, you use a prompt to ask the player 1 to enter the number of a column, using the following message:
Which column do you put a disc into?
If the player's input is invalid because it is not a number, you tell the player to enter a number.
If the player's input is invalid because it is a number that falls outside of the grid's boudaries, you tell the player to choose the number of a column.
If the player's input is invalid because the chosen column is already full of discs, you tell the player that it is full and ask him/her to choose another column.
If the player's input is valid, you move on the to the next step.
Third, the grid is shown again on the screen after the disc has been placed by the player. Underneath the grid, a message is shown, saying:
Player has placed a blue disc in row x, column y. For example, such a message could say:
Player has placed a blue disc in row 6, column 7.
This would of course correspond to the row at index 0 and the column at index 6 in the 2D array.
Fourth, the grid is evaluated to see if there is a winner. If there is a winner, the game stops and the winner is signaled through an onscreen message saying:
Player 1 wins!
Then, the program goes to the eighth step.
Fifth, player 2 (the computer) chooses a column to put a disc into.
Sixth, the grid is shown again on the screen after the disc has been placed by player
2. Underneath the grid, a message is shown, saying, for example: Player 2 has placed a blue disc in row 6, column 7.
Seventh, the grid is evaluated to see if there is a winner. If there is a winner, the game stops and the winner is signaled through an onscreen message saying:
Player 2 wins!
Then, the program goes to the eighth step.
Eighth, if there is no winner, the program goes back to the second step. If there is a winner or if this is a draw (if no one wins), the program asks the player if he/she wants to play another game. If the player answers with "yes" or "YES", another game is played from the first step. If not, the program ends.
Hints
Write a function that creates a 2-dimensional array and fills it with empty spots. This function can be initially called to create an array of 6 rows and 7 columns.
Write a function that will be used to print the grid onscreen. Write other functions as needed.
Do not neglect the documentation. Do not neglect the unit tests.
If you want to use the scanner for the player's inputs, but find that Android Studio is giving you trouble in that regard, you can use another IDE.
Attachment:- Instructions.rar