CSC2310 Assignment 4

advertisement
CSC2310 Assignment 4
Due on: Thursday Oct 23
Note: (1) Always use comments to explain your program.
(2) Write your name in the comment at the beginning of the program.
(3) Email both java source code and class file in a .zip file to
ysun10@student.gsu.edu and submit a hard copy in class on due date.
(4) No copying allowed.
Problems:
1.
Write a Java program for the game of BULLS and COWS, called Game.java. The
objective of this game is to make the user (player) guess a 4-digit number which
is randomly generated in the program. The 4-digit number should not have
repeating digits and will be in the range 1000 to 9999. The player will be given a
maximum of 10 tries to guess. The player WINS if he/she guesses the number
within 10 tries; otherwise the player LOSES the game.
The main program should first display a Welcome Message explaining the rules
of the games. Then, it should generate a 4-digit random number between 1000
and 9999 with no repeating digits. After that, it should repeatedly ask the user for
his/her guess and report the BULLS and COWS for the guess compared with the
random number. The game stops when either the player has made a correct guess
or when the player had made 10 guesses.
The game interaction should look something like this:
Welcome to the game of BULLS and COWS.
The Objective in this game is for you to guess a 4-digit number
The computer responds with how close your guess is to the target
BULLS = # common digits with exact matches and
COWS = # common digits in wrong position.
1. Enter your guess: 1234
Bulls = 1 Cows = 0
2. Enter your guess: 8567
Bulls = 0 Cows = 1
3. Enter your guess: 1233
Your guess should not contain repeating digits
4. Enter your guess: 7654
Bulls = 0 Cows = 1
5. Enter your guess: 7642
Bulls = 0 Cows = 0
6. Enter your guess: 5913
Bulls = 1 Cows = 2
7. Enter your guess: 5091
Bulls = 2 Cows = 1
8. Enter your guess: 5039
Bulls = 4 Cows = 0
Congratulations; You Won!!
or a message saying that you lost and PRINT THE ACTUAL NUMBER.
*Note: For two numbers 3462 and 4563, Bulls is 1 (number 6 is common number
with exactly matching) and Cows is 2 (3 and 4 are common numbers but wrong
positions).
Download