![]() |
![]() Spring Semester 2004 |
The homework due date has been extended because you need to turn in two distinct implementations:
CGI.pm
(documentation
from lecture notes nine)
CGI.pm
We'll talk about this in class on Thu, Feb 5.
Also, please make sure you
the Computer Science Department's Statement on Academic Integrity before turning in your assignment.
You and the computer play a game.
Your program should be a CGI script that behaves just like this one, above.import java.io.*; import java.util.*; class Two { public static void main(String[] args) throws IOException { BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); String line; int user, computer, pointsUser = 0, pointsComputer = 0; String[] choices = { "rock", "scissors", "paper" }; Hashtable h = new Hashtable(); h.put("rock" , new Integer(0)); h.put("scissors", new Integer(1)); h.put("paper" , new Integer(2)); Object choice; System.out.println("Hello and welcome to the game."); do { computer = (int)(Math.random() * choices.length); System.out.println("*** The computer has chosen: (" + choices[computer] + ")"); System.out.println("Now it's your turn."); System.out.println("Paper, scissors, or rock..."); System.out.print("Which will it be? Type here: "); line = b.readLine(); choice = h.get(line); String message = "*** The computer's choice was: " + choices[computer]; if (choice == null) { System.out.println("Sorry, this is not a valid choice."); System.out.println("The computer has just scored one on you."); System.out.println("Its choice was: " + choices[computer]); pointsComputer += 1; } else { user = ((Integer)choice).intValue(); if ((user + 1) % 3 == computer) { System.out.println("Very good.\n" + message); System.out.println("***(So, you win.)***"); pointsUser += 1; } else if ((computer + 1) % 3 == user) { System.out.println("Too bad.\n" + message); System.out.println("***(So, you lose.)***"); pointsComputer += 1; } else { System.out.println(message + "\n***(This game's a draw.)***"); } System.out.println("The score is now: "); System.out.println(" Computer: " + pointsComputer); System.out.println(" You : " + pointsUser); } } while (true); } }
Here's a session with the program above:
frilled.cs.indiana.edu%javac Two.java frilled.cs.indiana.edu%java Two Hello and welcome to the game. *** The computer has chosen: (rock) Now it's your turn. Paper, scissors, or rock... Which will it be? Type here: scissors Too bad. *** The computer's choice was: rock ***(So, you lose.)*** The score is now: Computer: 1 You : 0 *** The computer has chosen: (scissors) Now it's your turn. Paper, scissors, or rock... Which will it be? Type here: paper Too bad. *** The computer's choice was: scissors ***(So, you lose.)*** The score is now: Computer: 2 You : 0 *** The computer has chosen: (paper) Now it's your turn. Paper, scissors, or rock... Which will it be? Type here: rock Too bad. *** The computer's choice was: paper ***(So, you lose.)*** The score is now: Computer: 3 You : 0 *** The computer has chosen: (paper) Now it's your turn. Paper, scissors, or rock... Which will it be? Type here: scissors Very good. *** The computer's choice was: paper ***(So, you win.)*** The score is now: Computer: 3 You : 1 *** The computer has chosen: (rock) Now it's your turn. Paper, scissors, or rock... Which will it be? Type here: rock *** The computer's choice was: rock ***(This game's a draw.)*** The score is now: Computer: 3 You : 1 *** The computer has chosen: (rock) Now it's your turn. Paper, scissors, or rock... Which will it be? Type here: ^Cfrilled.cs.indiana.edu%