function:RenderSidePart pageleftbodycaption pageleftbody sidenote Main.OneTwoTenGame-SideNote Main.SideNote Site.SideNote

# One Two Ten Game

This is a good game once you learn simple "if" statements and "for" & while loops.

This Game is played on a peg board. The first player is picked or chosen, then decides to put a peg in the first or second hole. The next player must choose to advance the peg either one or two pegs. The turns then alternate between players, each choosing to advance the peg either one or two holes, until someone loses by placing the peg in the last hole. You can make variations of the game by changing the number of holes, or the number of choices. The misere version has the winner be the one who places the peg in the last hole. I learned about this game from Dr Dan Garcia's GamesCrafters in Berkeley.

Here is an example game:

Would you like to go first? (Type Y or N) y
_ _ _ _ _ _ _ _ _ _
Human's turn (move 1 or 2):2
_ X _ _ _ _ _ _ _ _
Computer's turn (move 1 or 2):2
_ _ _ X _ _ _ _ _ _
Human's turn (move 1 or 2):1
_ _ _ _ X _ _ _ _ _
Computer's turn (move 1 or 2):1
_ _ _ _ _ X _ _ _ _
Human's turn (move 1 or 2):1
_ _ _ _ _ _ X _ _ _
Computer's turn (move 1 or 2):1
_ _ _ _ _ _ _ X _ _
Human's turn (move 1 or 2):1
_ _ _ _ _ _ _ _ X _
Computer's turn (move 1 or 2):2
And the winner is: Human


# The Board Class

First make a class to represent and print the board. Here is a tester class to see if it works:

public class BoardTester
{
public static void main(String[] args)
{
Board board=new Board(10);
for(int i=0;i<=10;i++)
{
board.setCurrentPosition(i);
System.out.println(board);
System.out.println();
}
}
}


The board should keep track of the current position and be able to display any possible game state from before the start to the finish. It should print something like this:

_ _ _ _ _ _ _ _ _ _

X _ _ _ _ _ _ _ _ _

_ X _ _ _ _ _ _ _ _

_ _ X _ _ _ _ _ _ _

_ _ _ X _ _ _ _ _ _

_ _ _ _ X _ _ _ _ _

_ _ _ _ _ X _ _ _ _

_ _ _ _ _ _ X _ _ _

_ _ _ _ _ _ _ X _ _

_ _ _ _ _ _ _ _ X _

_ _ _ _ _ _ _ _ _ X



# The Player class

/**
* Player can be initialized with either a Name alone
* which will be a robot that makes random moves (1 or 2)
* or with a Name and a Scanner, which will ask a human
* player at the keyboard for a valid move (either 1 or 2)
*
* @author Chris Thiel, ofmcap
*
*/

import java.util.Scanner;
public class Player
{
private String name;
private Scanner kb;
public Player(String theName)
{
setName(theName);
kb=null;
}
public Player(String theName, Scanner input){
this(theName);
kb=input;
}
public boolean isRobot()
{
if (kb==null)
return true;
return false;
}
public boolean isHuman()
{
}
public boolean valid(String s)
{
}
public int makeMove()
{
if (isHuman())
{
{
System.out.print("Not valid-move 1 or 2? ");
}
}
else
{
if(Math.random()< .5)
return 1;
return 2;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString()
{
return name;
}
}



# The OneTwoTenGame class

import java.util.Scanner;
public class OneTwoTenGame
{
public static void main (String[] args)
{
Scanner kb=new Scanner(System.in);
Player p1;
Player p2;
System.out.print("Would you like to go first? (Type Y or N) ");
{
p1=new Player("Human", kb);
p2=new Player("Computer");
} else {
p2=new Player("Human", kb);
p1=new Player("Computer");
}
Player currentPlayer=p1;
int move=0;
Board b=new Board(10);
while(b.getCurrentPosition()<10)
{
System.out.println(b);
System.out.print(currentPlayer+ "'s turn (move 1 or 2):");
move=currentPlayer.makeMove();
if (currentPlayer.isRobot())
System.out.println(move);
if (b.setCurrentPosition(move+b.getCurrentPosition()))
{
if(currentPlayer==p1)
currentPlayer=p2;
else
currentPlayer=p1;
}
}
System.out.println("And the winner is: "+currentPlayer);
}
}



# Things to try

1. Make the misere version of the game where the winner takes the last spot
2. Make the board a different size than 10.
3. Make "3" a valid move, and let the computer pick 1, 2, or 3
4. Once you know about subclasses, make a subclass of Player that replaces the makeMove method so the robot play plays perfectly if it goes first (Hint: choose 1 or 2 not randomly, but by the even or oddness of the current position)