Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help writing the game of nim in python. Write a program in which the comput

ID: 3681918 • Letter: N

Question

Need help writing the game of nim in python.

Write a program in which the computer plays against a human opponent. Generate a random integer between 10 and 100 to denote the initial size of the pile. Generate a random integer between 0 and 1 to decide wheter the computer or the human takes the first turn. Generate a random integer between 0 and 1 to decide whether the computer plays smart or stupid. In stupid mide the computer simply takes a random legal value (between 1 and n/2) from the pile whenever it has a turn. In smart mode the computer takes off enough marbles to make the size of the pile a power of twer minus 1 - that is, 3,7,15,31, or 63. That is always a legal move, except when the zise of the pile is currently one less than a power of two. In that casem the computer makes a random legal move. Obviously the computer cannot be beaten in smart mode when it has the first move unless the pile size happeneds to be 15,31, or 63. And if the human has the first turn and knows the winning strategy they can win agains tthe computer.

Explanation / Answer

import java.util.Scanner;

public class NimGame
{
public static int player = 1;//THIS IS A FIX
public static boolean flag = true;


public static void main(String[] args)
{

//Declare variables
//TOOK player AWAY HERE AND MADE IT GLOBAL STATIC
int pile = 0;//which pile to pick from
int stones = 0;//how many stones to pick
int counter = 0;//if the game is over or not -> 0 not over
int move;

//Scanner
Scanner input = new Scanner(System.in);

NimClass myAssistant = new NimClass();
myAssistant.displayBoard();//board gets set up

//Begin loop
while(counter == 0)
{
//Choose pile you want to remove stones from.
System.out.print("Player " + player + " Please enter the pile you wish" +
" to remove from. (1, 2, 3 or 4) ");
pile = input.nextInt();//get a pile number

//Enter # of stones you want to remove.
System.out.print("Player " + player + " Please enter the amount of" +
" stones you wish to remove ");
stones = input.nextInt();


myAssistant.playerMove(pile, stones);





//Determine what player is entering the values.
if (player == 1 && flag == true)
{
player = 2;
}
else if(player == 1 && flag == false)
{
player = 1;
flag = true;//set the flag back to true
}
else if(player == 2 && flag == true)
{
player = 1;
}
else
{
player = 2;
flag = true;
}


//Display board / determine the winner of the game.
myAssistant.displayBoard();
myAssistant.determineWinner();


if (myAssistant.determineWinner() != -1)
counter = 1;

}



}





}
nim class:
public class NimClass
{
//Declare varaiables
private int [] _board;//the board for the game -> just an array


public NimClass()
{


//Determing # of stones in each pile at the beginning.
_board = new int [4];
_board[0] = 3;//pile 1 - 3 stones
_board[1] = 5;//pile 2 - 5 stones
_board[2] = 7;//pile 3 - 7 stones
_board[3] = 9;//pile 4 - 9 stones

System.out.println("Welcome to the Game of Nim " +
"You'll need to first pick a pile " +
"Then select how many stones you wish to remove ");
}


//Display the game board
public void displayBoard()//just setting up the board with default values
{
for(int row = 0; row < 4; row++)
{
if(_board[row] == 0)
System.out.print(" 0 ");
else
System.out.print(" " + _board[row] + " ");
}
//Print out the piles
System.out.println(" Pile 1 Pile 2 Pile 3 Pile 4 ");

}

//Determine the player's moves
public boolean playerMove (int pile, int stones)
{
if ((pile < 1) || (pile > _board.length))
{
return false;//user specified wrong pile
}

if (_board[pile - 1] /2 >= stones || _board[pile-1] == 1)
{
_board[pile - 1] -= stones;
return true;
}
else
{
System.out.println("Invalid number of stones");
NimGame.flag = false;
return false;
}
}


//Determine the winner of the game
public int determineWinner()
{
boolean complete = true;
int winner = -1;

//Checking the piles to see if they're empty.
if((_board[0] == 0) && ( _board[1]== 0) && ( _board[2]==0) && ( _board[3]==0))
{
winner = NimGame.player;//FIXED THIS HERE
complete = false;//MADE CHANGE HERE!
}

if(complete == false && winner == 1)
{
//Display the results of player 1 winning.
System.out.println("Player 1 Wins!");
winner = 0;
}

if(complete == false && winner == 2)
{
//Display the results of player 2 winning.
System.out.println("Player 2 Wins!");
winner = 0;
}
return winner;

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote