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 dishoesty.

Placed online: Tuesday, November 10
20 points, ~2% of total grade
Due: no later than 11 pm, Thursday, November 19
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. Use a diff tool such as the one at this website ( www.quickdiff.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 two player game. Players take turn 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 a 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 method 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 that if you don't finish at least you have 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.

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.

When you finish turn in your program named ConnectFour.java via Canvas for assignment 10.

Checklist: Did you remember to:

Back to the CS 312 homepage.