Fall Semester 2002

Practical Exam: The first one to reach 100 (one hundred) wins...

Consider the problem defined below:

```import java.io.*;

class One {
public static void main(String[] args) throws IOException {
String line;
boolean done = false;
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 + "]: ");
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:

```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%```
Essentially the game goes by these rules:

• The computer and the user take turns.
• The user makes the first move, then the computer moves.
• A valid move consists of choosing a number between 1 and 10 (inclusive).
• All numbers are being added into a sum.
• The player that first turns the sum into a number >= 100 wins.

What technologies can you use?

Any of the following:

• CGI/Perl with hidden fields
• CGI/Perl with sessions (MySQL, DBI.pm, CGI.pm)
• PHP with hidden fields
• PHP with sessions
How many implementations are needed?

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:

• you will have to solve a similar problem, but
• you may not have a choice of implementation technology.

Good luck and do well!

Last updated: Oct 22, 2002 by Adrian German for A348/A548