Download Due: Sep. 9 (Wednesday) Finding Ace What to Do

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
IT 179, Fall 2015
Assignment 2, 20 points
Due: Sep. 9 (Wednesday)
In this assignment, you will practices basic operations of the Java’s generic class named ArrayList
and its iterator. You will also practices how to set and use a user defined package named poker
for your program. The problem to solve is described as follows.
Finding Ace
Consider the standard 52-card deck for the poker game. Let the faces of the cards be denoted as
follows:
C1, C2, . . . , C10, C11, C12, C13, D1, . . . , D13, H1, . . . , H13, S1, . . . , S13,
where the letters denote four different suits and the numbers denote the kinds of the cards from
Ace to King. A shuffled deck is a pack of the 52 cards arranged in a random order. When we say
“pick up a suit, say Diamond, from a shuffled deck,” we mean to pick up all Diamond cards from
a shuffled deck and preserve their relative order.
The game we are going play is to find the Ace from a pack of 13 cards of the same suit in the
following way. If the card on the top is 1 (i.e., Ace), then the game is over. Otherwise, check the
number on the top card, say n, then remove the first n cards from the pack and put them back in
the reverse order. Repeat the process until Ace surfaces to the top. (Not of our concerns but an
interesting question is, will this process always stop? Why?)
Here is an example. Given a shuffled deck as follows:
C5 H2 H1 D7 S10 H12 D13 H3 S12 D10 H6 C11 D4 S4 C8 S3 C10 S2 H4 S7 S1 D5 C7 H13 C1 C6
C12 D1 H7 S8 D6 D2 S6 S5 S11 D12 D11 C2 C13 D9 H11 H9 H8 S13 C3 H5 S9 C4 D3 D8 C9 H10
If we pick up Diamonds from the shuffled deck above, we will have
D7 D13 D10 D4 D5 D1 D6 D2 D12 D11 D9 D3 D8.
According to the Ace finding game, we have to revers first n cards 6 times for Ace to surface.
D7 D13 D10 D4 D5 D1 D6 D2
D6 D1 D5 D4 D10 D13 D7 D2
D13 D10 D4 D5 D1 D6 D7 D2
D8 D3 D9 D11 D12 D2 D7 D6
D6 D7 D2 D12 D11 D9 D3 D8
D9 D11 D12 D2 D7 D6 D3 D8
D1 D8 D3 D6 D7 D2 D12 D11
Reverse 6 times.
D12 D11 D9 D3 D8
D12 D11 D9 D3 D8
D12 D11 D9 D3 D8
D1 D5 D4 D10 D13
D1 D5 D4 D10 D13
D1 D5 D4 D10 D13
D9 D5 D4 D10 D13
What to Do
You will use the given classes Card and PokerDeck from poker package to generate a shuffled deck.
Check the Java document for details about how to use Card and PokerDeck (there is a link on the
2nd line of the assignment side frame.)
Your program will play the Ace finding game on four different suits picked up from the same
Due: Sep. 9 (Wednesday)
c Chung-Chih Li
P-1/3
IT 179, Fall 2015
Assignment 2, 20 points
shuffled deck. Your program should print out the original shuffled deck and the process of playing
4 games (one for each suit). For each game, using the format as above to show the process. At the
end of each game, you have to indicate how many times the reversed has performed.
1. From now on, I will use peekapoo to refer to your secret directory in your public/myIT179/
directory. You have to copy your work to it for me to grade. (Check assignment 1 for details.)
2. Make two directories ∼/IT179/Asg2/ and ∼/IT179/poker/ on your Unix account for this
assignment, where ∼ denotes your home directory (use Unix command echo $HOME to find
out what is your home directory). poker is the name of the package where you will put
PokerDeck.calss and Card.class.
3. From my /home/cli2/public/IT179/poker/ directory, copy PokerDeck.java and Card.java
to your ∼/IT179/poker/ directory.
4. Browse the Java documents from the class web page for Card and PokerDeck, and make sure
you know how to use them.
5. See next page for setting up your java class path.
6. In directory ∼/IT179/Asg2/, create a java program named Asg2.java that will do as we just
described above. Important: you have to use ArrayList<Card> to handle your cards.
7. After your have finished and tested your program. Copy your Asg2.java to your peekapoo.
Also, copy your poker directory and its contents to peekapoo. You can try to compile and
run Asg2.java from peekapoo/ to make sure everything is fine. I will compile and run your
program a few times. Make sure that your Asg2.java does not belong to any package, i.e.,
there is no “package” directive in the beginning of he program.
8. This is important, hence I repeat again. Before you leave your work in peekapoo/
for me to grade, make sure you open up everything in the directory, including files in
∼/public/myIT179/peekapoo/poker and all class files generated by the compiler. To do
so, you can change to the directory and use chmod 777 * command.
9. You have to submit the report on the due day according to the guidelines in the syllabus
(or check the web page). In particular, do include your source code and output. You should
print the source code with reasonable indentation that reflects the nested logical structures of
your program. You may have extra return for each lines if you transfer them between differet
editor. So, make sure it is single spaced.
10. The cover page must contain the following information:
(a) Your full name.
(b) Your student ID.
(c) Assignment number.
(d) Unix account’s UID and name of your secret directory.
If I can’t find your program in the designated directory or I do not have a permission to run,
I can’t grade your works and you will receive 0 points.
11. You should stop updating your programs after the end of the due day.
Due: Sep. 9 (Wednesday)
c Chung-Chih Li
P-2/3
IT 179, Fall 2015
Assignment 2, 20 points
How to set up class path
When you compile your Asg2.java, your present working directory is different from the one that
contains package poker. We have to let the java compiler know where to find it. Also, in your
future assignment, you will create another package myUtil and all your future projects will use the
same package myUtil. There is no point to duplicate the package in every project. We can prepare
just one copy and let the system know where to find it.
1. Let’s start with a little experiment. Before you set up anything, try to compile your Asg2.java
from your ∼/IT179/Asg2/ directory. You will find that the compiler will not be able to find
the needed classes. To fix this problem, you should use -classpath option as follows to tell
where to find the package: Let xxxxx/ be your home directory (e.g., mine is /home/cli2/,
and you can use echo $HOME to find out yours. You should modify /home/cli2/ in the
following commands to your own.)
javac -classpath .:/home/cli2/IT179/ Asg2.java
java -classpath .:/home/cli2/IT179/ Asg2
Don’t forget .: in the front. The commands above specifies two directories to find classes,
the present working directory and /home/cli2/IT179/, where : is the delimiter symbol.
2. Since we’ve decided to make xxxxx/IT179/ and xxxxx/public/myIT179/peekapoo/ for all
your packages poker and myUtil, we can use an environment variable to direct Java compiler
and its runtime machine to this directory. Move back to your home directory, and use ls -a
to see the hidden files. In Unix, files with a name started by . are hidden files. Locate file
.bashrc and add the following line to it.
export CLASSPATH=.:xxxxx/IT179:xxxxx/public/myIT179/peekapoo
Note, xxxxx is your home directory. After you save .bashrc, every newly open terminal
will know the path. (Note: terminals open before the change will not know the change.)
Afterwards, you should be able to compile and run your programs without using -classpath
option as long as your packages are put in your ∼/IT179/.
Due: Sep. 9 (Wednesday)
c Chung-Chih Li
P-3/3