Question: Background Information: Connect four is two player game. Players take turns dropping red and black checkers into the columns of a board. The board is
Background Information: Connect four is two player game. Players take turns dropping red and black checkers into the columns of a board. The board is upright so checkers fall down to the lowest open spot in a column.
Write a program to run a game of connect fours between two human players. The players use red and black checkers. Red goes first. Players alternate turns until one player has four checkers in a row horizontally, vertically, or diagonally. It is possible for the game to end in a draw if no player can achieve four in a row.
The board has 6 rows and 7 columns per row. To make the game easier for human users number the columns 1 to 7. The actual columns in your 2d array are numbered 0 to 6 so you will have to adjust the user input. Error check the input to ensure it is an int (that method is already provided), that the selected column is a valid column, and that there is an open spot in that column. A checker cannot be placed in a column that is full.
Approach: The description of the game is simple and you can try implementations in Adobe Flash on the web. My suggested solution is roughly 250 lines long including comments, blank lines, and lines with a single closing brace. My solution has 19 methods. I am providing you with some of the more mundane methods. You can use them if you want or ignore them. Most of the really interesting methods and algorithms are left to you.
Since this is such a difficult problem I cannot stress how important it is to plan out your work on paper first. Break the problem up into steps. Write a method for the step and if it is too large break it up into smaller steps and methods. Use parameters and methods to provide a structure to your program.
When you code a method, test it right away. This is especially important when dealing with the user input for the column which uses 1 based indexing and translating it to the proper column for your board that uses 0 based indexing. It is as simple as subtracting 1, but it is easy to forget or make a mistake. It is also important to to test as you go, so if you don't finish, at least you have a program that performs some of the required tasks and can get partial credit.
The most difficult thing is checking to see if a player has won. It is best to save this for last. There are many ways to determine if someone has one. One possibility is searching the board for the current player's piece. When it is found you must then check that piece in four directions to see if it is part of four checkers in a row. Even though there are 8 directions a four in a row could occur, you only need to check 4 of the due to symmetry. If you search for the current player's piece from left to right, top to bottom, you really need to only check these four directions:
down and to the left (southwest)
down (south)
down and to the right (southeast)
to the right (east)
Of course there are alternative approaches you are free to explore.
Tackle the problem of determining if a win has occurred last. Get all of the other features (getting input, placing check in correct spot, displaying board. game ending because of no more open spaces) working before working on ending the game with a win.
As always I expect you to use good programming style including good spacing and brace placement, meaningful variable names, constants, methods to provide structure and readability, commenting your code, proper use of parameters, and so forth as specified in the program hygiene guide on the assignment guidelines page.
Recall, do not create multiple Scanners connected to System.in. Create one Scanner and pass it as a parameter as necessary.
Sample Output:
This program allows two people to play the game of Connect four. Each player takes turns dropping a checker in one of the open columns on the board. The columns are numbered 1 to 7. The first player to get four checkers in a row horizontally, vertically, or diagonally wins the game. If no player gets fours in a row and and all spots are taken the game is a draw. Player one's checkers will appear as r's and player two's checkers will appear as b's. Open spaces on the board will appear as .'s. Player 1 enter your name: Bob Player 2 enter your name: Jen Current Board 1 2 3 4 5 6 7 column numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bob it is your turn. Your pieces are the r's. Bob, enter the column to drop your checker: -1 -1 is not a valid column. Bob, enter the column to drop your checker: 0 0 is not a valid column. Bob, enter the column to drop your checker: 8 8 is not a valid column. Bob, enter the column to drop your checker: eight eight is not an integer. Bob, enter the column to drop your checker: 5 Current Board 1 2 3 4 5 6 7 column numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . r . . Jen it is your turn. Your pieces are the b's. Jen, enter the column to drop your checker: 1 Current Board 1 2 3 4 5 6 7 column numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . b . . . r . . Bob it is your turn. Your pieces are the r's. Bob, enter the column to drop your checker: 5 Current Board 1 2 3 4 5 6 7 column numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . r . . b . . . r . . Jen it is your turn. Your pieces are the b's. Jen, enter the column to drop your checker: 2 Current Board 1 2 3 4 5 6 7 column numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . r . . b b . . r . . Bob it is your turn. Your pieces are the r's. Bob, enter the column to drop your checker: 5 Current Board 1 2 3 4 5 6 7 column numbers . . . . . . . . . . . . . . . . . . . . . . . . . r . . . . . . r . . b b . . r . . Jen it is your turn. Your pieces are the b's. Jen, enter the column to drop your checker: 3 Current Board 1 2 3 4 5 6 7 column numbers . . . . . . . . . . . . . . . . . . . . . . . . . r . . . . . . r . . b b b . r . . Bob it is your turn. Your pieces are the r's. Bob, enter the column to drop your checker: 5 Bob wins!! Final Board 1 2 3 4 5 6 7 column numbers . . . . . . . . . . . . . . . . . . r . . . . . . r . . . . . . r . . b b b . r . .
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
