CS 312 Assignment 10, Connect Four
Programming Assignment 10: Individual Assignment. You must complete this assignment by yourself. You cannot work with anyone else in the class or with someone outside of the class. You are encouraged to get help from the instructional staff. Recall we will check submissions with plagiarism detection software. If you copy code from someone else you will be subject to the penalties for academic dishonesty.
Please note, I typically have multiple cheating cases on this assignment every semester. DO NOT COPY CODE FROM THE WEB OR OTHER STUDENTS WHEN COMPLETING CS312 PROGRAMMING ASSIGNMENTS.
Placed online: Tuesday, April 12
20 points, ~2% of total grade
Due: no later than
11 pm, Thursday, April 21
General Assignment Requirements
The purpose of this assignment is to implement a program to allow two users to play the game of connect four.
You are restricted to the Java features from chapters 1 - 7 of the textbook.
Provided Files:
Given the same input, your program must produce the exact same output. You may assume the user never just presses enter without entering a value. Use a diff tool such as the one at this website (www.diffchecker.com/) to ensure your program produces the correct output. Even minor differences in output will cause you to fail grading tests and lose points
Background Information: Connect four is a 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 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:
Of course there are alternative approaches you are free to explore. HINT: You should strive for a generalized method that can move in any of eight directions (up, down, left, right, and the four diagonals) from a given cell using parameters to specify the initial row and column and parameters that indicated how to change the row and column. For example a columnChange of -1 and a row change of -1 to move down and to the left (southwest).
As always I expect you to use good programming style including removing redundancy, 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. Do not us class variables as a form of global variables.
When you finish turn in your program named ConnectFour.java via Canvas for assignment 10.
By way of comparison my solution is 250 lines long (including many blank lines and lines with a single }) and 19 total methods.
Checklist: Did you remember to: