[CS230 Home Page] [Syllabus] [Assignments] [Documentation] [CS Dept.]
Note on Programming Style: Programs that you compose for your CS230 assignments and labs will be evaluated not only on whether your programs work correctly, but also on programming style. This includes the use of informative names, good comments, easily readable formatting, and a good balance between compactness and clarity (e.g. do not define lots of unnecessary variables, or write multiple statements to perform a task that can be expressed clearly in a single statement.)
In large software development projects involving a team of programmers, it is important to include comments at the beginning of each code file that include information such as the file name, the name of the primary developer, purpose for the code, and history of major modifications. These comments may also include the names of collaborators or other sources for the ideas or code in the file. Starting with this first assignment, we would like you to include this information at the beginning of each code file that you create or modify.
Dice Poker is a variation of the game of Poker that uses dice instead of cards. In this problem, you will complete the implementation of a program that simulates a Dice Poker game played between the computer and user. The game consists of multiple rounds. In each round, the computer and user each roll five dice. The combination of five dice values is ranked on a scale from 0 to 6 as follows:
rank 6: Five of a Kind:
all five dice have the same value
rank 5: Four of a Kind: four of the five dice have the
same value
rank 4: Full House: the dice include three of one value
and two of another
rank 3: Three of a Kind: three dice have the same value
and the remaining two have different values
rank 2: Two Pairs: the dice include two pairs of the
same values and a fifth die of a different value
rank 1: One Pair: two dice have the same value and the
remaining three have different values
rank 0: Nothing: all five dice have different
values
The player with the highest ranking roll of the dice wins the round. The player who wins the most rounds is the winner of the game.
We are providing you with the skeleton of a program that simulates the playing of Dice Poker, with some code filled in. For simplicity, the code is all placed in a single file named DiceGame.java that contains the definition of a single class named DiceGame. This skeleton code file is stored in the /home/cs230/download/DiceGame subdirectory on puma. To begin, copy this directory to your own directory using the cp command:
cp -R /home/cs230/download/DiceGame .
The -R flag specifies that the directory and all of its contents should be copied, and the period at the end of the command indicates that the DiceGame subdirectory should be copied to the directory that you are currently connected to. You can immediately compile and execute the existing program, but it doesn't do much yet. The main() method contained in the DiceGame.java file assumes that the user has entered a name in the command line when executing the program, for example:
java PlayDice Dave
(If you do not immediately recognize the joke, you may want to take a look at http://www.palantir.net/2001/sounds.html) If you do not supply a name in the command line, the program will just print out a message asking you to please enter a name, and then terminate.
Below is a sample run of the complete program.
w-142-227:~/tm$ java DiceGame Dave
Hello Dave. I'm completely operational and all my circuits are functioning
perfectly.
Would you like to play a game of Dice Poker? I play very well.
*** ROUND 1
HAL: 6 5 6 1 3 : One pair
You: 5 1 4 6 2 : Nothing
*** ROUND 2
HAL: 6 2 3 2 2 : Three of a kind
You: 1 1 3 2 1 : Three of a kind
*** ROUND 3
HAL: 3 3 5 1 6 : One pair
You: 6 1 5 3 5 : One pair
*** ROUND 4
HAL: 4 2 4 4 4 : Four of a kind
You: 2 2 4 6 4 : Two pairs
*** ROUND 5
HAL: 6 1 3 4 3 : One pair
You: 5 1 4 2 4 : One pair
The game was won by the HAL with a score of 2 to 0 in 5 rounds.
w-142-227:~/tm$
java DiceGame Dave
Hello Dave. I'm completely operational and all my circuits are functioning
perfectly.
Would you like to play a game of Dice Poker? I play very well.
*** ROUND 1
HAL: 3 1 1 3 4 : Two pairs
You: 3 1 4 3 4 : Two pairs
*** ROUND 2
HAL: 4 4 6 5 1 : One pair
You: 4 6 4 6 6 : Full House
*** ROUND 3
HAL: 3 1 6 3 1 : Two pairs
You: 1 2 2 1 1 : Full House
*** ROUND 4
HAL: 2 1 3 2 4 : One pair
You: 4 3 2 5 1 : Nothing
*** ROUND 5
HAL: 4 4 4 6 6 : Full House
You: 3 1 6 4 2 : Nothing
The game was tied with a score of 2 to 2 in 5 rounds.
In the implementation, the five dice values for the computer and user should be stored in an array of five integers. The method throw5Dice(), whose definition is already complete, fills an input array with five random integers between 1 and 6. It uses the built-in Java method Math.random(), which returns a random number of type double in the range from 0.0 up to, but not including, 1.0. For future reference, the Java Math class has a number of methods for performing useful mathematical functions. You should browse the documentation on the available Math methods at the Sun Java website:
http://java.sun.com/j2se/1.5/docs/api/
In the list of Java 2 Platform Packages, select java.lang, and under the Class Summary list, choose Math.
The program is complete except of 4 methods. Your task is to complete the definitions of the following methods:
(1) private void accumulateValues(int[] input, int[] diceResults) {
Given an input array input containing five dice values, this method should determine how many of each of the possible six dice values appear in input, and store this information in the input array diceResults. For simplicity, assume that diceResults is an array of 7 integers and use only the top 6 locations of this array to store the information about how many of each dice value appear in input (i.e., do not use the array location diceResults[0]). When this method is complete, diceResults[1] should contain the number of 1's that appear in input, diceResults[2] should contain the number of 2's, and so on. Think carefully about how to implement this method compactly -- its definition can be quite short. The purpose of defining this method is to facilitate the implementation of the next method, getRank().
(2) public int getRank (int[] input) {
Given an input array input containing five dice values, this method should determine the rank of the combination of values (an integer between 0 and 6, as described earlier), print a brief message describing the contents of input (e.g. Five of a Kind), and return the rank (e.g. 6 in the case of Five of a Kind). In the program skeleton, the body of the getRank() method already contains two statements that set up an array diceResults to store how many of each dice value are contained in the input input and call accumulateValues(). There are many strategies for implementing the rest of this method -- think carefully about different options before writing any code.
(3) public int playOneRound() {
This method simulates one round of the game by first simulating the computer's turn (throws the 5 dice and getting its rank), then simulating the player's turn, and then determining the winner. It returns an interger that signifies the winner.
(4) public void playDiceGame (String name, int numRounds) {
Finally, this method should simulate the playing of numRounds of the dice game between the computer and the user whose name is provided as input. The method should print the outcome of each round and print the winner at the end of the game. Currently, this method just prints a message to the user.
An important piece of advice that we will reinforce throughout the semester is to construct your implementation incrementally -- build and test one method at a time! Begin by completing the definition of accumulateValues() and adding test code to the playOneRound() method, to test your code. Next complete the definition and testing of the getRank() method. And so on. Larger methods can also be created and tested in small pieces. As you build your implementation, also keep in mind the comments about programming style at the beginning of this handout. In the comments at the beginning of the file, be sure to add your name and the date of completion of your code.
Submission details: Hand in a hardcopy of your final DiceGame.java code file and drop off an electronic copy of your code file by connecting to your DiceGame directory and executing the following command:
submit cs230 DiceGame *.*
The aims of this problem are to practice working with Java's built-in String class and methods, and to implement three new methods that operate on strings. After learning about the String class, you will be ready to implement the three new methods described below.
To begin your programming, first create a new directory named StringOps, and inside this directory, place a file named StringOps.java that you create from scratch. This file should define a new class named StringOps and should include definitions of the three methods removeChar(), testAnagramsIter() and testAnagramsRec() described below, and a main() method that contains code to test your new definitions. Your final submission should include the examples that you place in the main() method to test the other three methods.
(1) public static String removeChar (String S, char ch) {
This method should return a new string that is constructed from the input string S, with the first occurrence of the character ch removed. If the character ch is not contained in the input string S, then this method should return the original string S. For example, the expression removeChar("java", 'a') should return the string "jva" and removeChar("java", 'q') should return "java".
(2) public static void testAnagramsIter (String word1, String word2) {
(3) public static void testAnagramsRec (String word1, String word2) {
Anagrams are two words that have exactly the same letters, but in a different order. For example, the following pairs of words are anagrams: (melon, lemon), (mothers, thermos) and (ocean, canoe). Given two input strings, the testAnagramsIter() and testAnagramsRec() methods should both determine whether the two strings are anagrams and print the outcome to the user. For example, the method call
testAnagramsIter("melon", "lemon");
might result in the following printout:
melon and lemon are anagrams
while the method call
testAnagramsIter("hello", "world");
might result in:
hello and world are not anagrams
Assume that a word is not an anagram of itself, so the pair of words (hello, hello) are not anagrams. The two methods testAnagramsIter() and testAnagramsRec() should use different implementation strategies to perform the same task. In particular, testAnagramsIter() should use iteration (e.g. a while loop or for loop) in its implementation, while testAnagramsRec() should use recursion. In the case of testAnagramsRec() it is ok to write a pair of methods, one of which is non-recursive and calls a recursive auxiliary method. Think carefully about possible strategies for implementing the two methods before writing any code. You can assume that both input strings consist entirely of lowercase letters. Hint: Your removeChar() method could be handy in the implementation of these last two methods! Again consider the comments about programming style mentioned at the beginning of this handout. Place comments at the beginning of the file that include the file name, your name, date of completion and purpose for the code (you do not need to include a modification history in this case).
Submission details: Hand in a hardcopy of your final StringOps.java code file and drop off an electronic copy of your code file by connecting to your StringOps directory and executing the following command:
submit cs230 StringOps *.*