Programming Project 5
Due Sunday, December 8 at 11:59pm
As Monday, December 9 is Reading Day and that day should to be spent preparing for final exams, no late assignments will be accepted.
I. Overview
The purpose of this homework is to give you practice designing and creating a complete program.
II. Code Readability (20% of your project grade)
To receive the full readability marks, your code must follow the following guideline:
All variables (fields, parameters, local variables) must be given appropriate and descriptive names.
All variable and method names must start with a lowercase letter. All class names must start with an uppercase letter.
The class body should be organized so that all the fields are at the top of the file, the constructors are next, the non-static methods next, and the static methods at the bottom with the main method last.
There should not be two statements on the same line.
All code must be properly indented (see Appendix F of the Lewis book for an example of good style). The amount of indentation is up to you, but it should be at least 2 spaces, and it must be used consistently throughout the code.
You must be consistent in your use of {, }. The closing } must be on its own line and indented the same amount as the line containing the opening {.
There must be an empty line between each method.
There must be a space separating each operator from its operands as well as a space after each comma.
There must be a comment at the top of the file that is in proper JavaDoc format and includes both your name and a description of what the class represents. The comment should include tags for the author. (See Appendix J of the Lewis book of pages 226-234 if the Evans and Flanagan book.)
There must be a comment directly above each method (including constructors) that is in proper JavaDoc format and states what task the method is doing, not how it is doing it. The comment should include tags for any parameters, return values and exceptions, and the tags should include appropriate comments that indicate the purpose of the inputs, the value returned, and the meaning of the exceptions.
There must be a comment directly above each field that, in one line, states what the field is storing.
There must be a comment either above or to the right of each non-field variable indicating what the variable is storing. Any comments placed to the right should be aligned so they start on the same column.
There must be a comment above each loop that indicates the purpose of the loop. Ideally, the comment would consist of any preconditions (if they exist) and the subgoal for the loop iteration.
Any code that is complicated should have a short comment either above it or aligned to the right that explains the logic of the code.
III. Program Testing (20% of your project grade)
You are to write a test report that indicates the types of tests needed to thoroughly test your project. The tests should demonstrate that all parts of your code behave correctly. Any unit of your program involving conditional statements will need tests that go through each branch of the execution. Any unit of your program involving loops will need tests that cover the test 0, test 1, test many and test first, test middle, test last guidelines. Your testing report should not list the actual tests and results.
You are to have a JUnit test class or classes that implement as many of the tests as you can. You should have comments, names, or other indicators in your JUnit tests that easily link the JUnit tests back to the testing report.
The testing report must be separate from the JUnit class. In most companies, the testing document will be written in a style that allows both programmers and non-programmers to read it and recognize whether all the needed test cases were included.
Note that you will not be able to (easily) test methods involving user input or screen output with JUnit. For these parts of your program, your testing report should indicate the specific tests you did to test these routines.
Hint 1: Make lots of helper methods for each of the different parts of the game. That will make it much easier to design tests for your game, and it will make the testing shorter! You can make the methods public or private. In Mondays lecture, you will learn how to write tests specifically for private methods.
Hint 2: The JavaFX GUI components are not always simple to create outside of a JavaFX application. As a result, it will be easier to write JUnit tests if you have the game mechanics done in helper methods that do not directly use GUI components.
At the minimum, you should have the following two helper methods:
1.numberInLine should take as parameters a two-dimensional array representing the game board, two int values representing the row and column of the currently played piece, and a way of specifying the direction to search. There are 8 possible directions: left, up, right, down, and the four diagonals. How you specify the direction is up to you. The method should return the number of pieces of the same color in a straight line, starting from and including the currently played piece, and in the direction specified.
2.isOpen should take as parameters a two-dimensional array representing the game board, two int values representing the row and column of the currently played piece, and a way of specifying the direction to search. There are 8 possible directions: left, up, right, down, and the four diagonals. How you specify the direction is up to you. The method should return true if, when starting at the currently played piece and following pieces of the same color in the direction specified, you reach an empty square.
Hint: if you take some time to think about how your loops will work, this can be done without a lot of complicated code.
IV. Java Programming (60% of your grade)
For this project you will implement the game of Gomoku (called Wuziqi in China).
Rules of the game:
The game is played on a grid of squares for two players: Black and White. Black moves first. Each player alternates playing pieces on the board. Once played, a piece cannot be moved. A player wins if they get five of their pieces in a row, horizontally, vertically, or diagonally. In addition, we will add the following additional rules.
Overline: There must be exactly five in a row to win. Six or more in a row does not win, and the game continues.
Four-Four: A player may not make a move if that move simultaneously creates two or more groups of four in a row.
Three-Three A player may not make a move if that move simultaneously creates two or more groups of three in a row such that both ends of the three have empty squares.
Traditionally, the game is played on the intersections instead of the squares of the grid, but for simplicity, you can have the game played on the squares.
What you must do:
Important: Read the instructions for the testing part of this project. You need to design your code so that it is easy to test. If you put off writing the testing code to the end of the project, you may need to rewrite your code to get testing to work.
You are to create a class called Gomoku. The Gomoku class extend the JavaFX Appliction class.
Designing your program
Below the instructions provide the basic steps you need to accomplish to get your game to work. However, it is strongly recommended that you spend some time designing what you will be coding before you code. It is very strongly recommended that you employ helper methods and/or additional classes in the design of this game. Doing so will shorten the amount of work you need to do. As a hint, if you find yourself writing the same piece of code over and over, it might be easier to either write a helper method to do that operation or to write a class that extends the class you are working with so that the class a method that does what you need.
Create the board
You are to create a board by making a two-dimensional grid of Buttons. This simplest way to to that is to create a GridPane instance that will hold the buttons, and add the GridPane to the JavaFX Scene.
You can then create a 2-dimensional array of Buttons and add them to the GridPane using the add method of GridPane with the appropriate values so that the location of a button in the array corresponds to its location in the display grid. (You are welcome to change this, but you should have a two-dimensional array in your program somewhere that stores the current board situation.)
For the basic display, you should give each button a color. There will be three basic visuals.
A blank square will be a button with a green background. You use setBackground to set the BackgroundFills for the button, and doing that, you can set the color. Also set the Insets for the BackgroundFill so that the edges of the button are visible. (Be sure to look at the API so you understand what this refers to.)
A square with a players piece on it will have a background that is a white or black piece on top of a green background. You again use the setBackground method, but now the piece should be either white or black, and you should set both the Insets and the CornerRadii so that the piece looks like a round stone in the middle of the square.
You are welcome to change these visuals if you wish.
Here is a picture of what your board should look like in the middle of a game:
Respond to button clicks
You should create an EventHandler for the buttons. Recall that an EventHandler has a method handle that is called every time the button is pressed. The handle method has a single parameter, ActionEvent e, and you can get the button that was pressed by using the code
Button b = (Button)e.getSource();
b is the button that was pressed. You can then look up the button in your array.
When a button is pressed, you should check that there is not already a piece at that button. If not, add a piece of the appropriate color. Then you should check the following using the methods you created above:
1.If the piece creates exactly 5 pieces in a straight line, print a message indicating who won (or have some other means of declaring a victor) and prevent any more moves of the game.
2.If the piece creates either a four-four or a three-three, print a message indicating that (or have some other means of informing the user), and remove the piece from the board.
3.Otherwise, the piece stays and the turn changes to the next player.
Step 5: Add a main method
The Gomoku class should have a main method that launches the game. With a main method, you should be able to play your game by typing:
java Gomoku
in the Interactions pane. You should also allow the user to enter two arguments for the board size. For example:
java Gomoku should start a game with a 1919 grid,
java Gomoku 15 12 should start a game with 15 rows and 12 columns,
java Gomoku 7 should start a game with a 1919 grid and to win you need 7 in a row, and
java Gomoku 6 15 12 should start a game with 15 rows and 12 columns and to win you need 6 in a row. If the user enters something other than realistic numbers, your code should do something appropriate, but not crash.
Important:If the we are playing a game where the players are trying to get more than 5 in a row, then the four-four and three-three rules need to change as well. The four-four should prevent a move that creates two or more groups with one less than the amount needed to win. The three-three should prevent a move if that move creates two or more groups of two less than is needed to win in a row such that both ends of the groups have empty squares. (So, if the game is now to get 7 in a row, four-four becomes six-six, and three-three becomes five-five.)
There are two ways you can deal with the command line arguments. You can either process them directly in the main method. Of you can pass the arguments to the launch method, and then in the start method, you can access them using the getParameters().getRaw() method of Application.
Hint: Do not try to code everything at once! First just get the game board displaying. Then add the ability to click on a button and have a piece appear. Then get it so the game alternately places pieces of different colors. Then start implementing the rules of the game. Finally, get the main method working.
Extra Credit:
If you decide to do the extra credit, you must state in the Canvas comments what you did. Dont make us hunt through your code to figure out what extra you did.
Make further improvements to the aesthetics and play of the game. Extra credit will be awarded to improvements that require coding challenge, a lot of work with the API, or creativity. Note: Do not look up online Gomoku games and copy their code or style. This extra credit is for you to have some fun and be creative. It is an academic integrity offense if you copy code or ideas from online versions of the game.
Reviews
There are no reviews yet.