Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello. Completely stumped by this assignment (the art of programming really eludes me). If a kind soul would help out that would be awesome! This

Hello. Completely stumped by this assignment (the art of programming really eludes me). If a kind soul would help out that would be awesome! This assignment is in Java! Here are the details: This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws exceptions when problems occur.

Basic Rules of Nim Most versions of the game Nim involves 3 rows of sticks. For example: | | | | | | | | | | | | | | |

Players alternate turns, each removing between 1 and 3 sticks from some row of their choice. For example, if player 1 takes 3 sticks from row 2, the board will then look like: | | | | | | | | | | | |

Of course, a player cannot take more sticks from a row than there are left. For example, a player cannot remove 3 sticks from row 2 of the above board.

The game continues until some player is forced to take the last stick, leaving none on the board. That player loses.

In your application, a human player will alternate moves with a computer player. To simplify things, the computer player will choose a random number of sticks (between 1 and 3) from a random row. Of course, the number of sticks removed must be legal.

The NimGame Support Class

Required Constructors and Methods Your NimGame support class is required to have the following constructors and methods:

public NimGame(int[] initialSticks)

This constructor should create a new game with the initial number of sticks in each row set to the corresponding elements of initialSticks.

For example, to create a game with initially 3, 5, and 7 sticks, this would be called as new NimGame(new int[]{3, 5, 7});

public int getRow(int r)

This inspector method returns the current number of sticks in row r. Note that the user will enter a number between 1 and 3, not between 0 and 2 (this is more user friendly).

public void play(int r, int s)

This modifier method should remove s sticks from row r. Again, note that the user will enter a number between 1 and 3 for the row.

It should also do validation on the number of sticks, throwing one of the following exceptions if the number of sticks, returning the following exceptions: NoSuchRowException if the row is not between 1 and 3. IllegalSticksException if the number of sticks taken is not between 1 and 3. NotEnoughSticksException if the number of sticks taken is between 1 and 3, but more than the number of sticks remaining in that row.

public boolean isOver()

This inspector returns true if there are no sticks left in any row, false otherwise.

public void AIMove()

This modifier takes a random number of sticks (between 1 and 3) from a random row, in order to make the computers move.

Note that this must also do validation, making sure that it does not take more sticks than there are left in a row. A simple way to do this is generate and test:

while (not enough sticks in that row) { generate a random row and number of sticks }

public String toString()

This should return the state of the object (that is, the number of sticks in each row) as a string.

Note that it is not used in the visual application, but will be used for the JUnit testing described below.

Important design note: Your NimGame methods will only throw exceptions. They will not be catching any exceptions. All exceptions will be caught within the actionPerformed method of the NimApp class.

Designing Additional Exceptions The above situations are not the only in which an exception can occur. You are to look over your NimGame class methods and constructors and identify at least two other instances where it would be appropriate for an exception to be thrown. This is the main design exercise of this assignment.

In each case, create an appropriate exception class, and modify the code to throw the exception if it occurs.

The NimApp Application Class I have provided a simple command line application called NimApp for running your support class. When run (with a working NimGame class), it looks like the following:

When the player enters the row and sticks, the application calls the play method and redraws the sticks by calling the getRow method for each row). It then calls the AIMove method to make the computer move, and then redraws the sticks again.

This continues until there are no more sticks (checked by isOver calling the method), at which point whoever made the last move loses:

Carefully examine the main method in the application to see how this works, particularly with respect to how the play method is called.

Handling NimGame Exceptions Note that there is currently no exception handling in this method. You are to modify this code to handle the three exceptions that may be thrown by the play method. In each case, it should pop up an approriate error message. For example:

Also note that if the player makes an illegal move, it must remain their turn. That is, the AIMove method must not be called in that case. Hint: Think carefully about what code should be inside the try block. Handling Other Exceptions Aside from the exceptions thrown by the play method, there is one other type of exception that might be thrown in the main loop in this application due to player error. Add code to catch this exception and to display an appropriate messge.

Hint: When you play the game, think about what unusual input a player could try to enter.

Creating JUnit Testing Cases

Finally, you are to create a set of testing tools for your NimGame class using the JUnit testing tool.

To start, right click the NimGame in NetBeans, and go to Tools Create JUnit Tests. This will set up a file called NimGameTest under the Test Packages folder.

As shown in class, it will generate a test method for each of your methods. To simplify things, you are only required to create tests for the methods: getRow play You can comment out the rest of the tests if you like.

Testing the getRow method

Since this will be easier, I suggest that you start with this one. Your tester will need to do the following (some of which is already built in): Construct an instance object of a NimGame. Call your getRow method for some row to determine whether it returns the correct number of sticks in that row.

Testing the play method

You are to provide two tests for this method: Taking a legal number of sticks from a legal row. Taking sticks from an illegal row.

Test case for legal play

Since this is a modifier, you will need to make sure the state has changed correctly. More specifically: Construct an instance object of a NimGame. Call the play method, with a legal row and number of sticks. Use your toString method to compare the expected state of the object to the actual state.

Test case for illegal row

Since your method will be expected to throw an exception if the player chooses an illegal row, the structure of these testing methods will be different: Construct an instance object of a NimGame. Inside a try block: Call the play method for some illegal row. Call fail (since we would have exited the block if an exception were thrown). Inside the catch block: Use your toString method to make sure the state of the game has not changed.

Documentation

Be sure to document the exception handling and testing components of your code. Specifically: Document each method of your NimGame class, particularly the sections related to detecting and throwing exceptions. You are to use Javadoc format for this.

Document the changes you make to the main method related to handling the exceptions thrown by NimGame methods.

Document the two testing methods you create in JUnit, describing what they are testing and how.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data And Hadoop Fundamentals Tools And Techniques For Data Driven Success

Authors: Mayank Bhushan

2nd Edition

9355516665, 978-9355516664

More Books

Students also viewed these Databases questions

Question

Classify delivery styles by type.

Answered: 1 week ago