Download public class Game - Sonoma Valley High School

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
import java.util.Scanner;
import java.util.Random;
/**
* Write a description of class Game here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Game
{
private Pile nimPile;
private Player humanplayer;
private Player computer;
private int first;
// instance variables - replace the example below with your own
public static void main(String[] args)
{
System.out.println("Welcome to the Game of Nim!");
Scanner input = new Scanner(System.in);
Random gameGen = new Random();
int cMode = gameGen.nextInt(2);
if (cMode==0) System.out.println("The computer will play in basic mode.");
else System.out.println("The computer will play in expert mode.");
int first = gameGen.nextInt(2);
if (first == 0)System.out.println("You will make the first move.");
else System.out.println("The computer will make the first move.");
Player computer = new Player(cMode);
Player human = new Player(2);
Pile nimPile = new Pile(10,100);
System.out.println("The size of the pile is: " + nimPile.getMarbles());
boolean done = false;
if (first == 0)
{
while (!done)
{
human.takeTurn(nimPile);
if (human.playerWon(nimPile))
{
System.out.println("You won - the computer must take the last marble!");
done = true;
}
computer.takeTurn(nimPile);
if (computer.playerWon(nimPile))
{
System.out.println("The computer won - you must take the last marble!");
done = true;
}
}
}
else if (first ==1)
{
while(!done)
{
computer.takeTurn(nimPile);
if (computer.playerWon(nimPile))
{
System.out.println("The computer won - you must take the last marble!");
done = true;
}
human.takeTurn(nimPile);
if (human.playerWon(nimPile))
{
System.out.println("You won - the computer must take the last marble!");
done = true;
}
}
}
}}
import java.util.Random;
/**
* Write a description of class Pile here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Pile
{
// instance variables - replace the example below with your own
private int numMarblesInPile;
/**
* Constructor for objects of class Pile
*/
public Pile(int a, int b)
{
// initialise instance variables
Random pileNum = new Random();
numMarblesInPile = pileNum.nextInt(b-9);
numMarblesInPile = numMarblesInPile + a;
}
/**
* An example of a method - replace this comment with your own
*
* @param y
a sample parameter for a method
* @return
the sum of x and y
*/
public int getMarbles()
{
return numMarblesInPile;
}
public void takeMarbles(int n)
{
numMarblesInPile = numMarblesInPile - n;
}
}
import java.util.Random;
import java.util.Scanner;
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player
{
// instance variables - replace the example below with your own
private int mode;
/**
* Constructor for objects of class Player
*/
public Player(int aMode)
{
// initialise instance variables
mode = aMode;
}
/**
* An example of a method - replace this comment with your own
*
* @param y
a sample parameter for a method
* @return
the sum of x and y
*/
public void takeTurn(Pile nimPile)
{
if (mode == 0)
{
Random cTurn = new Random();
if (nimPile.getMarbles() <= 2)
{
nimPile.takeMarbles(1);
System.out.println("The computer removed 1 marble.");
}
else
{
int turn = cTurn.nextInt(nimPile.getMarbles()/2)+1;
nimPile.takeMarbles(turn);
System.out.println("The computer removed " + turn + " marbles and there are now " + nimPile.getMarbles() +
" left on the pile.");
}
}
else if (mode == 1 && isPowerOfTwoMinusOne(nimPile.getMarbles()))
{
Random cTurn = new Random();
int turn = cTurn.nextInt(nimPile.getMarbles()/2)+1;
nimPile.takeMarbles(turn);
System.out.println("The computer removed " + turn + " marbles and there are now " +
nimPile.getMarbles() + " left on the pile.");
}
else if (mode ==1 && !isPowerOfTwoMinusOne(nimPile.getMarbles()))
{
boolean done = false;
for (int i = nimPile.getMarbles()/2; i>1; i--)
{
if (isPowerOfTwoMinusOne(nimPile.getMarbles()- i) && !done)
{
nimPile.takeMarbles(i);
System.out.println("The computer removed " + i + " marbles and there are now " +
nimPile.getMarbles() + " left on the pile.");
done =true;
}
}
}
else if (mode == 2)
{
Scanner input = new Scanner(System.in);
boolean done = false;
while(!done)
{
System.out.print("Enter number of marbles you would like to remove: ");
int turn = input.nextInt();
if (turn > nimPile.getMarbles()/2)
{
System.out.println ("You may only remove " + nimPile.getMarbles()/2 + " or less.");
}
else
{
nimPile.takeMarbles(turn);
System.out.println("There are now " + nimPile.getMarbles() + " left in the pile.");
done = true;
}
}
}
}
public boolean isPowerOfTwoMinusOne(int n)
{
boolean power = false;
for (int i = 1; i< 7; i++)
if (n == (Math.pow(2,i)-1)) power = true;
return power;
}
public boolean playerWon(Pile nimPile)
{
return (nimPile.getMarbles()==1);
}
}
Related documents