CSCI A201/A597 and I210

Lab Notes Eight

Second semester 2000-2001


Chapters 6 games.
The minute paper is in QuizSite. As expected.

When is it due? It's due Tuesday (3/6) at the end of the day.


What does it ask for? A summary of lecture notes sixteen.

A brief one. Just the main points.

I see. And that, by Tuesday night.

OK. Have you ever played Nim? No. But do you play croquet?

I'd love to but I don't have the time. How do you play Nim?

Here's how you play a game with a computer.
import java.io.*; 
import java.util.*; 

class Nim {
    public static void main(String[] args) {
	ConsoleReader console = new ConsoleReader(System.in); 
	Random generator = new Random(); 
	int height = Math.abs(generator.nextInt()) % 90 + 10; 
	PileOfMarbles pile = new PileOfMarbles(height); 
	System.out.println("You are working with a pile of height: " 
			   + pile.report());
	int number, currentHeight; 
	while (true) {
	    System.out.println("*** Computer moves."); 
	    System.out.println("Pile of marbles of height: " + pile.report()); 
	    currentHeight = pile.report(); 
	    if (currentHeight == 1) { 
              number = 1; 
            } else { 
              number = Math.abs(generator.nextInt()) % (currentHeight / 2) + 1; 
            } 
	    System.out.println("Computer chooses to remove: " 
			       + number + " marbles.");
	    pile.move(number); 
	    System.out.println("--------------------------"); 
	    System.out.println("*** Now the user has to move."); 
	    System.out.println("Pile of marbles of height: " + pile.report()); 
	    System.out.print("Please enter number of marbles you want to take: "); 
	    number = console.readInt(); 
	    pile.move(number); 
	    System.out.println("--------------------------"); 
	} 
    } 
} 

class PileOfMarbles {
    int height; 
    PileOfMarbles (int height) {
	this.height = height; 
    }
    int report() {
	return this.height;
    } 
    void move(int number) {
	System.out.println("***Removing " + number + " marbles from the pile."); 
	if (number <= 0 || 
            ((number > height / 2) && (number != 1))) {
           // A player must take at least one but at most half of the marbles. 
           // (The players take turns, the one who takes the last marble loses). 

           // How do you write that? 

	    System.out.println("***Bad move: you lose."); 
	    System.exit(0); 
	} else {
	    this.height -= number; 
	    if (this.height == 0) {
		System.out.println("***End of game: you lost.");
		System.exit(0); 
	    } 
	} 
	System.out.println("Pile of marbles is now: " + this.report());
    } 

} 

class ConsoleReader { 
    public ConsoleReader(InputStream inStream) { 
        reader = new BufferedReader(
                   new InputStreamReader(
                     inStream));     
    }
    public String readLine() { 
        String inputLine = "";
        try {
            inputLine = reader.readLine(); 
        } catch (IOException e) {
            System.out.println(e); 
            System.exit(1); 
        } 
        return inputLine; 
    }
    public int readInt() { 
        String inputString = readLine(); 
        int n = Integer.parseInt(inputString); 
        return n; 
    }
    public double readDouble() { 
        String inputString = readLine(); 
        double x = Double.parseDouble(inputString); 
        return x; 
    } 
    private BufferedReader reader; 
}

Nim is so easy. I know. I like it already.

Last updated: Mar 1, 2001 by Adrian German for A201