Next time we see each other: midterm exam. 

Thu 8am. Study Guide:

http://www.cs.indiana.edu/classes/c212-dgerman/fall2015/studyGuide.html

Exam is made of two problems:

(a) loops, reading from the user, maybe arrays 

(b) modeling (simple) 

Today: 

http://www.cs.indiana.edu/classes/c212-dgerman/fall2015/10122015.pdf

Problem 2:

public class Drunkard {
  private int x, y; 
  public Drunkard(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public String toString() {
    return "D(" + this.x + ", " + this.y + ")";  
  }
  public void go() {
    int dice = (int)(Math.random() * 4); 
    if (dice == 0) { // North
      this.y -= 1; // short form of this.y = this.y - 1; 
    } else if (dice == 1) { // East
      this.x = this.x + 1;       
    } else if (dice == 2) { // West 
      this.x = this.x - 1;             
    } else { // dice is 3 (direction is South) 
      this.y = this.y + 1;       
    }
  }
}

public class Two {
  public static void main(String[] args) {
    Drunkard d = new Drunkard(5, 6); 
    for (int i = 0; i < 100; i++) {
      d.go(); 
      System.out.println( d ); 
    }
  }
}

http://www.cs.indiana.edu/classes/a201-dger/spr2004/notes/Thirteen.html

Problem 1:

http://www.cs.indiana.edu/classes/a201-dger/spr2003/notes/Fourteen.html

Lane, Troy, Trevor, Shea, Jing, Chia-Hsuan, Evan, Noor, Jack, Rob, Ryan, Tommy

Alex, Yi, Jun, Kongchen, Chenyang, Qingyue

Where is Shengyu? 

I play Nim with Yi. We start with 50 marbles on the table. 

  Yi: Take 2 marbles.
  Pile now has 48 marbles. 
  Adrian: Take 24 marbles. 
  Pile now has 24 marbles. 
  Yi: Take 1 marble. 
  Pile now has 23 marbles. 
  Adrian: Take 11 marbles.
  Pile now has 12 marbles. 
  Yi: Take 6 marbles. 
  Pile has 6 marbles left. 
  Adrian: Take 2 marbles. 
  Pile has 4 marbles. 
  Yi: Take 1 marble. 
  Pile has 3 marbles. 
  Adrian: Take 1 marble. 
  Pile has 2 marbles now. 
  Yi: I take 1 marble.
  Pile has 1 marble.
  Yi has just won the game.

--

import java.util.Scanner;

public class Nim {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);      
    int pile = (int) (Math.random() * 50 + 30); 
    System.out.println("Pile: " + pile); 
    while (true) {
      System.out.print("Adrian: "); 
      int adrian = Integer.parseInt( s.nextLine() ); 
      if (adrian <= 0 || adrian > pile/2) {
        System.out.println("Adrian just lost."); 
        break;         
      }
      pile = pile - adrian; 
      System.out.println("Pile: " + pile); 
      if (pile == 1) {
        System.out.println("Adrian just won."); 
        break; 
      }
      int computer = (int) (Math.random() * pile / 2 + 1); 
      System.out.println("Computer takes " + computer + " marble(s)"); 
      pile = pile - computer; 
      System.out.println("Pile: " + pile); 
      if (pile == 1) {
        System.out.println("Computer wins."); 
        break; 
      }
    }
  }
}

This is it. 

--