|
Fall Semester 2002 |
Consider the problem defined below:
import java.io.*;
class One {
public static void main(String[] args) throws IOException {
String line;
boolean done = false;
BufferedReader b =
new BufferedReader(new InputStreamReader(System.in));
int sum = 0, number, computer;
System.out.println("Welcome to the game.");
System.out.println("The sum is zero.");
System.out.println("You choose first.");
do {
System.out.print("Choose at [" + sum + "]: ");
line = b.readLine();
try {
number = Integer.parseInt(line);
} catch (Exception e) {
number = 0;
}
if (number <= 0 || number > 10) {
System.out.println("Not a valid choice, please choose again.");
continue;
}
//----------------------------------------------( the user )----
sum += number;
System.out.println("The sum becomes... " + sum);
if (sum >= 100) {
System.out.println("You win.");
System.out.println("Thanks for playing.");
System.exit(0);
}
//----------------------------------------------( the machine )-
computer = (int)(Math.random() * 10) + 1;
System.out.println("The computer chooses... " + computer);
sum += computer;
System.out.println("The sum becomes... " + sum);
if (sum >= 100) {
System.out.println("You lose.");
System.out.println("Thanks for playing.");
System.exit(0);
}
} while (!done);
}
}
Use PHP or CGI/Perl to implement a web-based version of it. Here's the program in action:
Essentially the game goes by these rules:frilled.cs.indiana.edu%java One Welcome to the game. The sum is zero. You choose first. Choose at [0]: 1 The sum becomes... 1 The computer chooses... 10 The sum becomes... 11 Choose at [11]: 12 Not a valid choice, please choose again. Choose at [11]: 1 The sum becomes... 12 The computer chooses... 7 The sum becomes... 19 Choose at [19]: 4 The sum becomes... 23 The computer chooses... 4 The sum becomes... 27 Choose at [27]: 7 The sum becomes... 34 The computer chooses... 3 The sum becomes... 37 Choose at [37]: 8 The sum becomes... 45 The computer chooses... 5 The sum becomes... 50 Choose at [50]: 6 The sum becomes... 56 The computer chooses... 7 The sum becomes... 63 Choose at [63]: 4 The sum becomes... 67 The computer chooses... 5 The sum becomes... 72 Choose at [72]: 6 The sum becomes... 78 The computer chooses... 1 The sum becomes... 79 Choose at [79]: 10 The sum becomes... 89 The computer chooses... 10 The sum becomes... 99 Choose at [99]: 2 The sum becomes... 101 You win. Thanks for playing. frilled.cs.indiana.edu%
What technologies can you use?
Any of the following:
Answer: just one.
Choose one that you like best and implement a solution.
Post a working version and the source code under your protected by Thu 5:30pm.
For Thu (when we have the written exam) please be comfortable with all four technologies:
Good luck and do well!