Skip to content

Instantly share code, notes, and snippets.

@davidalencar
Created June 20, 2017 17:28
Show Gist options
  • Save davidalencar/fb46c22b6586c99bc477ba537edda12a to your computer and use it in GitHub Desktop.
Save davidalencar/fb46c22b6586c99bc477ba537edda12a to your computer and use it in GitHub Desktop.
nimgame
import java.util.Scanner;
/**Implements a game of nim, person vs. computer.
* The game starts with a number of elements chosen
* by the person. The computer takes 1 or 2 elements.
* Then the person takes 1 or 2 elements. The game
* continues until there are no elements left. The
* play who takes the last turn wins.
* @author Jam Jenkins
*/
public class Nim
{
/**gets the computer's move, 1 or 2. Currently
* this method gets a random number. Your job
* is to make the computer choose such that it
* wins every time it is possible. First, solve
* with recursion. After successfully completing
* this, run the program a few times to see if you
* can recognize the pattern the computer is taking.
* Then see if you can get the computer to choose
* its move without looping or recursion.
* @param left
* @return
*/
public int getComputerMove(int left)
{
return (int)(Math.random()*2)+1;
}
/**
* plays the game of nim, computer versus person
*/
public void play()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements to start.");
int left = sc.nextInt();
while(left>0)
{
int computer=getComputerMove(left);
System.out.println("Computer takes "+computer);
left-=computer;
System.out.println("Now there are "+left+" left.");
if(left<=0)
{
System.out.println("Computer wins!");
return;
}
System.out.println("What's your move? (1 or 2)");
int person=sc.nextInt();
while(person!=1 && person!=2)
{
System.out.println(person+" not allowed, choose 1 or 2.");
person=sc.nextInt();
}
left-=person;
System.out.println("Now there are "+left+" left.");
if(left<=0)
{
System.out.println("You win!");
return;
}
}
}
public static void main(String[] args)
{
Nim nim=new Nim();
nim.play();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment