OverviewThe goal of this project is to design a simple version of the "Connect Four" game. For the ones who have never heard of this game at all, wikipedia provides a good description. The idea for the overall design is the following:
This is how it should look when classes are complete (click to enlarge) As you can see, Connect4Game will use Connect4Board and Connect4Logic. Connect4Logic doesn't use any other class. It is basically self-contained. Connect4Board on the other hand uses the two classes Circle and Square, which both require Canvas. Circle, Square, and Canvas are the same classes that we already used in the Shapes Assignment. The interesting point to notice here is that Connect4Board and Connect4Logic are independent of each other. They don't need each other and they don't interact. This makes the programming process a lot easier. We can concentrate on one class without having to do the other one at the same time. Starting the ProjectCreate a new project "Connect4" and bring in Circle.java, Square.java, and Canvas.java from Assignment 1. Having these, you should be ready to go. On to part 1, the graphical display...1. Part: The Graphical DisplayThe goal of the first part is to develop the class Connect4Board, which manages the graphical output. Looking at Figure 1, we can identify following components and functionalities:
So, go ahead and create a new class Connect4Board. Then start filling in the code. Blue Background with White CirclesAs you might have noticed, we don't have a way to draw a rectangle. But that's no problem. A rectangle can easily be composed by using a bunch of squares. To do so, carry out the following steps. Make sure that once you have implemented a step, you test it carefully before going on to the next one!1. Task Think about how you can draw one blue square with a white circle inside. From now on, let's refer to such a square/circle combination as "tile".
2. Task Now that we have the method that draws one tile, let's think about how we can draw the whole board.
Now create two constructors. The default constructor should draw a board with 6 rows and 7 columns. Another constructor should take two integer parameters, which specify the number of rows and the number of columns of the board. This way, it will be possible to create boards of different size. Hint 1: It is very helpful to have fields that store the number of rows and columns. Hint 2: Inside your constructor you can call other methods of your class, such as drawBoard(). Placement of DiscsWe will need to write a method that inserts a disc into a certain column and places it in the appropriate row.However, before we do this, we will implement a helper method, which is easier to implement. Remember, we program in baby steps, so we don't get overwhelmed. 4. Task Write a method that places a disc in a specified location, using a specified color. The location should be specified by row and column. Rows are specified by an integer number, where the rows are enumerated from bottom to top, starting with row number 1. Columns are specified the same way, starting with column number 1 on the left side. Hint 1: You can call your method whatever you like. Hint 2: The method needs to have parameters. Hint 3: On how to use colors in Java, see Shapes Lab. Hint 4: In order to use colors in Java, you have to import the package java.awt. You do this by adding the following code as the first line in your Connect4Board class: import java.awt.*; 5. Task Now we write the method that places the disc in the correct row. This method will make use of the method that you just wrote (the helper method). The header of the new method should be exactly as follows: public void insertDisc(int column, int player) where column is the column (enumerated from left to right, starting with 1) in which the disc should be placed. Furthermore, player specifies one of two players (either 1 or 2), which in turn affects the color of the inserted disc (red or black). Note: The newly inserted disc will be placed in the row closest to the bottom, which does not yet have a disc in it. Therefore, before we can call the method that actually places the disc in the right spot, we need to determine in which row the disc will end up. How can we do this? How can we remember, which row in a certain column is the first empty one? My suggestion is to use an array, which stores for each column the number of the highest row that is already filled. At the beginning, each entry is obviously 0, since no discs have been entered, yet. Every time a disc is entered into a certain column, the respective value in this array has to be incremented. Hint: Be aware, rows and columns on the board are numbered from 1 upwards, while array index values start at 0. Milestone for 1. PartYou should now have a working Connect4Board class. You should be able to create boards of various size and you should be able to insert a disc into a certain column calling the appropriate method. The interface of your classConnect4Board should look exactly like the image at the top of this page. You can check this in BlueJ by selecting the "Interface" view from the drop down menu on the top right corner (by default it shows "Implementation").In order to hide your helper methods, which are only used inside your class and should never be called by another object from outside the class, you can declare them as "private" instead of "public". Deliverables:
DocumentationYou must document any code that you created or modified (e.g., your Square and Circle classes). Indicate which methods/constructors you wrote (or the whole class if you did) in a comment. Include both your name and your partner's in a comment. You must also include a comment indicating how much time each partner spent typing. Remember that each partner should type half the time and watch half the time and that you should use one computer for the two of you. You should use javadoc (/** this method does this */) to describe each method. You will lose up to 15 points if you do not have comments (especially how long you and your partner spent typing, not a percentage, but number of minutes or hours). |