Anda di halaman 1dari 6

1

CS 140 Introduction to Computer Science


Project #4
(Due: 8/20)

The game of Pig is a simple two player dice game in which the first player to reach 100 or more
points wins. Players take turns. On each turn a player rolls a six-sided die. After each roll:

If the player rolls a 1, then the player gets no new points and it becomes the other
players turn.
If the player rolls 2 through 6, then he can either:
o ROLL AGAIN or
o HOLD. At this point the sum of all rolls is added to the players score and it
becomes the other players turn.

Write a program that plays the game of Pig, where one player is a human and the other is the
computer. When it is the humans turn, the program should show both the current turn score and
the current total score of the player. Allow the human to input r to roll again or h to hold.

The computer program should play according to the following rule:

Keep rolling when it is the computers turn until it has accumulated 20 or more points,
then hold. If the computer wins or rolls a 1, then the turn ends immediately.

Allow the human to roll first. Sample input and output runs are given on the next page.

In your cs140/ pr oj ect directory, create a J ava Source Code file named Pi g. j ava. Your
J ava program must begin with the comments below and follow the naming and coding
conventions posted on Blackboard 9.

/ / your name
/ / CS140, sect i on 01
/ / Pr oj ect 4 The Game of Pi g
/ / date

Generate a scr i pt file pj 4. t xt with appropriate time stamps and the following steps visible:

1. a pwd to show the current working directory
2. an l s to show the files in your cs140/ pr oj ect directory
3. a cat to display Pi g. j ava
4. compile Pi g. j ava
5. execution of Pi g (run your program at least twice with one case showing that you
win and another case that computer wins)

Turn in:
- A printout of pj 4. t xt , the run using the script command
- Soft copy of the program file, (using Blackboard)
Pi g. j ava
2
The following are sample interactions where the users input is shown in bold:

gsyoung@f l uf f y ~ $ j ava Pi g
Wel come t o t he game of Pi g!

You r ol l ed: 6
Your t ur n scor e i s 6 and your t ot al scor e i s 0
I f you hol d, you wi l l have 6 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 5
Your t ur n scor e i s 11 and your t ot al scor e i s 0
I f you hol d, you wi l l have 11 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 6
Your t ur n scor e i s 17 and your t ot al scor e i s 0
I f you hol d, you wi l l have 17 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 5
Your t ur n scor e i s 22 and your t ot al scor e i s 0
I f you hol d, you wi l l have 22 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
h
Your scor e i s 22

I t i s t he comput er ' s t ur n.
The comput er r ol l ed: 6
The comput er r ol l ed: 5
The comput er r ol l ed: 5
The comput er r ol l ed: 1
The comput er l ost i t s t ur n! Comput er t ot al i s 0
The comput er ' s scor e i s 0

You r ol l ed: 1
You l ose your t ur n! Your t ot al i s 22
Your scor e i s 22

I t i s t he comput er ' s t ur n.
The comput er r ol l ed: 2
The comput er r ol l ed: 2
The comput er r ol l ed: 5
The comput er r ol l ed: 6
The comput er r ol l ed: 5
The comput er hol ds.
The comput er ' s scor e i s 20

You r ol l ed: 1
You l ose your t ur n! Your t ot al i s 22
Your scor e i s 22

I t i s t he comput er ' s t ur n.
The comput er r ol l ed: 5
The comput er r ol l ed: 2
3
The comput er r ol l ed: 1
The comput er l ost i t s t ur n! Comput er t ot al i s 20
The comput er ' s scor e i s 20

You r ol l ed: 5
Your t ur n scor e i s 5 and your t ot al scor e i s 22
I f you hol d, you wi l l have 27 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 3
Your t ur n scor e i s 8 and your t ot al scor e i s 22
I f you hol d, you wi l l have 30 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 4
Your t ur n scor e i s 12 and your t ot al scor e i s 22
I f you hol d, you wi l l have 34 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 4
Your t ur n scor e i s 16 and your t ot al scor e i s 22
I f you hol d, you wi l l have 38 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
h
Your scor e i s 38

I t i s t he comput er ' s t ur n.
The comput er r ol l ed: 6
The comput er r ol l ed: 1
The comput er l ost i t s t ur n! Comput er t ot al i s 20
The comput er ' s scor e i s 20

You r ol l ed: 5
Your t ur n scor e i s 5 and your t ot al scor e i s 38
I f you hol d, you wi l l have 43 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 1
You l ose your t ur n! Your t ot al i s 38
Your scor e i s 38

I t i s t he comput er ' s t ur n.
The comput er r ol l ed: 2
The comput er r ol l ed: 4
The comput er r ol l ed: 5
The comput er r ol l ed: 4
The comput er r ol l ed: 4
The comput er r ol l ed: 6
The comput er hol ds.
The comput er ' s scor e i s 45

You r ol l ed: 1
You l ose your t ur n! Your t ot al i s 38
Your scor e i s 38

4
I t i s t he comput er ' s t ur n.
The comput er r ol l ed: 5
The comput er r ol l ed: 3
The comput er r ol l ed: 2
The comput er r ol l ed: 3
The comput er r ol l ed: 3
The comput er r ol l ed: 5
The comput er hol ds.
The comput er ' s scor e i s 66

You r ol l ed: 2
Your t ur n scor e i s 2 and your t ot al scor e i s 38
I f you hol d, you wi l l have 40 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 3
Your t ur n scor e i s 5 and your t ot al scor e i s 38
I f you hol d, you wi l l have 43 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 4
Your t ur n scor e i s 9 and your t ot al scor e i s 38
I f you hol d, you wi l l have 47 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 1
You l ose your t ur n! Your t ot al i s 38
Your scor e i s 38

I t i s t he comput er ' s t ur n.
The comput er r ol l ed: 5
The comput er r ol l ed: 4
The comput er r ol l ed: 4
The comput er r ol l ed: 5
The comput er r ol l ed: 4
The comput er hol ds.
The comput er ' s scor e i s 88

You r ol l ed: 4
Your t ur n scor e i s 4 and your t ot al scor e i s 38
I f you hol d, you wi l l have 42 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 2
Your t ur n scor e i s 6 and your t ot al scor e i s 38
I f you hol d, you wi l l have 44 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 5
Your t ur n scor e i s 11 and your t ot al scor e i s 38
I f you hol d, you wi l l have 49 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
r
You r ol l ed: 3
Your t ur n scor e i s 14 and your t ot al scor e i s 38
5
I f you hol d, you wi l l have 52 poi nt s.
Ent er ' r ' t o r ol l agai n, ' h' t o hol d.
h
Your scor e i s 52

I t i s t he comput er ' s t ur n.
The comput er r ol l ed: 4
The comput er r ol l ed: 3
The comput er r ol l ed: 4
The comput er r ol l ed: 2
The comput er hol ds.
The comput er ' s scor e i s 101

THE COMPUTER WI NS!
6
Here is the algorithm for implementing the game of Pig:

whi l e ( game i s not over )
{
/ / human s t ur n
do
{
r ol l di ce
i f ( di ce == 1)
{
di spl ay You l ose your t ur n! and out put cur r ent t ot al
set t ur n over t o t r ue and exi t do- whi l e l oop
}
el se
{
accumul at e t ur n scor e
di spl ay cur r ent t ur n scor e and t ot al scor e
ask user s i nput whet her t o r ol l agai n or hol d?
i f ( user s choi ce == h )
set t ur n over t o t r ue and exi t do- whi l e l oop
}
} whi l e ( t ur n i s not over )
add i n any poi nt s t he human got and out put cur r ent t ot al
check f or human wi nni ng
i f ( human s scor e >= 100)
{
di spl ay YOU WI N!
set game over t o t r ue and exi t t he whi l e l oop
}

/ / comput er s t ur n
do
{
r ol l di ce
i f ( di ce == 1)
{
di spl ay The comput er l ost i t s t ur n! and out put t ot al
set t ur n over t o t r ue and exi t do- whi l e l oop
}
el se
{
accumul at e t ur n scor e
i f ( t ur n scor e >= 20 or t ot al pl us t ur n scor e >= 100)
di spl ay The comput er hol ds and set t ur n over t r ue
}
} whi l e ( t ur n i s not over )
add i n any poi nt s t he comput er got and out put cur r ent t ot al
check f or comput er wi nni ng
i f ( comput er s scor e >= 100)
{
di spl ay THE COMPUTER WI NS!
set game over t o t r ue and exi t t he whi l e l oop
}
}

Anda mungkin juga menyukai