0% found this document useful (0 votes)
430 views

Game of Nim Code

This document contains code for a game where two players take turns removing marbles from a pile until there are none left. It defines classes for the Player and Pile as well as a Game class that runs the game. The Player class can be either human or computer controlled. The computer player has either a smart or stupid strategy for choosing how many marbles to remove. The Game class initializes the pile, player types, and runs the turn-taking loop until a winner is determined.

Uploaded by

AdiSri1997
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
430 views

Game of Nim Code

This document contains code for a game where two players take turns removing marbles from a pile until there are none left. It defines classes for the Player and Pile as well as a Game class that runs the game. The Player class can be either human or computer controlled. The computer player has either a smart or stupid strategy for choosing how many marbles to remove. The Game class initializes the pile, player types, and runs the turn-taking loop until a winner is determined.

Uploaded by

AdiSri1997
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

/*

* To change this template, choose Tools | Templates


* and open the template in the editor.
*/
package nimgame;

/**
*
* @author Adithya
*/
import java.util.Scanner;

public class Player {

int playerType;
Scanner in;

private void humanMove(Pile pile)
{
System.out.println("How many marbles do you want to take out?");
int marblesToRemove = new Integer(in.nextLine()).intValue();

boolean isValid = pile.remove(marblesToRemove);

while (!isValid) {

System.out.println("The number is not valid. Enter another: ");
marblesToRemove = new Integer(in.nextLine()).intValue();
isValid = pile.remove(marblesToRemove);

}
}

private void smartMove(Pile p)
{
int m = p.marblesInPile();

if (m == 1 || m == 3 || m == 7 || m == 15 || m == 31 || m == 63) {
stupidMove(p);
return;
}

int x = 1;

if (m > 63) {
x = m - 63;
} else if (m > 31) {
x = m - 31;
} else if (m > 15) {
x = m - 15;
} else if (m > 7) {
x = m - 7;
} else if (m > 3) {
x = m - 3;
} else if (m == 2) {
x = 1;
}
System.out.println("The computer took " + x + " marbles.");
p.remove(x);
}

private void stupidMove(Pile p)
{
int x = (int)(Math.random()*(p.marblesInPile() / 2 - 1)) + 1;
System.out.println("The computer took " + x + " marbles.");
p.remove(x);
}

public void move(Pile p)
{
if (playerType == 1) {
humanMove(p);
} else if (playerType == 2) {
smartMove(p);
} else {
stupidMove(p);
}
}

public void Wins()
{
if (playerType == 1) {
System.out.println("You win.");
} else {
System.out.println("Computer wins.");
}
}

public Player(int p)
{
playerType = p;
in = new Scanner(System.in);
}
}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package nimgame;

/**
*
* @author Adithya
*/
public class Pile {

int numberOfMarbles;

public int marblesInPile() {
return numberOfMarbles;
}

public boolean remove(int marblesToRemove) {
if ((marblesToRemove != 1) && ((marblesToRemove == 0) ||
(marblesToRemove > (numberOfMarbles / 2)))) {
return false;
} else {
numberOfMarbles -= marblesToRemove;

if (numberOfMarbles > 0) {
System.out.println("There are " + numberOfMarbles + " marbles left");
}

return true;
}
}

public Pile(int marbles) {
numberOfMarbles = marbles;
}
}




package nimgame;

import java.util.Scanner;

/**
*
* @author asrikanthan16
*/
public class Game {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int numberOfMarbles = (int)(Math.random()*91)+10;
Pile pile = new Pile(numberOfMarbles);
System.out.println("Marbles in pile: " + numberOfMarbles);

boolean humanTurnFirst = Math.random() <= 0.5;
if (humanTurnFirst)
{
System.out.println("You will get the first turn");
}
else
{
System.out.println("The computer gets the first turn");
}

boolean computerisSmart = Math.random() <= 0.5;

if (computerisSmart)
{
System.out.println("The computer is smart");
}
else
{
System.out.println("The computer is stupid");
}


// human: playerType = 1
//computer smart: playerType = 2
// computer stupid: playerType = 3

int computerType;

if (computerisSmart)
{
computerType = 2;
}
else
{
computerType = 3;
}

Player player1;
Player player2;
if (humanTurnFirst)
{
player1 = new Player(1);
player2 = new Player(computerType);
}
else
{
player1 = new Player(computerType);
player2 = new Player(1);
}

while (true)
{
player1.move(pile);
if (pile.marblesInPile() == 0)
{
player2.Wins();
break;
}
player2.move(pile);
if (pile.marblesInPile() == 0)
{
player1.Wins();
break;
}
}

}
}

You might also like