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

You need to modify the code so that two computer players will play a large numbe

ID: 3771788 • Letter: Y

Question

You need to modify the code so that two computer players will play a large number (say 1000) games of Nim against each other. Then use the results to play against a human player. The example code uses two arrays, one to count the moves for a specific game, and one to hold the totals of a large number of games.

CODE 1:

import java.util.Scanner;
/**
Implements a game of nim, user vs. computer.
The game starts with the user entering the number of elements in the game.
The computer takes 1 or 2 elements.
Then the user takes 1 or 2 elements.
The game continues until there are no elements left.
Whoever takes the last turn wins.
*/

public class NimGame
{

static int CanBeRemoved = 0;
static int PlayerOne[][];
static int PlayerTwo[][];   

public static void main(String[] args)
{
int computerMoveOne = 0;
int computerMoveTwo = 0;   
int userMove = 0;
int elementsRemaining = 0;
int elements = 0;
  
  
Scanner sc = new Scanner(System.in);

  
System.out.println();
System.out.println();
System.out.println("Enter number of elements to start.");
  
elements = sc.nextInt();
elementsRemaining = elements;
  
PlayerOne = new int[elements][10];
PlayerTwo = new int[elements][10];
  
PrintPlayerOne(elements, 10);
PrintPlayerTwo(elements, 10);
  
System.out.println();
System.out.println();
System.out.println("Enter number of elements that can be removed each turn.");
  
CanBeRemoved = sc.nextInt();   
  

  
for(int training = 0; training < 10; training++)
{
elementsRemaining = elements - 1;
while(elementsRemaining > -1)
{
computerMoveOne = getComputerMove(elementsRemaining);
PlayerOne[elementsRemaining][training] = computerMoveOne;
  
//System.out.println("Player One Takes "+computerMoveOne);

elementsRemaining -= computerMoveOne;
  
//System.out.println("Now there are "+(elementsRemaining + 1)+" elements remaning.");
//System.out.println();
  
if(elementsRemaining <= -1) // Test to determine if computer has won
{
System.out.println("Player One wins!");
for(int wipeout = 0; wipeout < elements; wipeout++)
{
PlayerTwo[wipeout][training] = 0;
}
}
else{
computerMoveTwo = getComputerMove(elementsRemaining);
PlayerTwo[elementsRemaining][training] = computerMoveTwo;
  
//System.out.println("Player Two Takes "+computerMoveTwo);

elementsRemaining -= computerMoveTwo;
  
//System.out.println("Now there are "+(elementsRemaining + 1)+" elements remaning.");
//System.out.println();
  
if(elementsRemaining <= -1) // Test to determine if computer has won
{
System.out.println("Player Two wins!");
for(int wipeout = 0; wipeout < elements; wipeout++)
{
PlayerOne[wipeout][training] = 0;
}
}
}
}
}
PrintPlayerOne(elements, 10);
PrintPlayerTwo(elements, 10);
  
}
  

  
/**
Currently this method gets a random number, either 1 or 2.
Your job is to make the computer choose such that it wins every time possible.
*/

public static int getComputerMove(int left)
{
   if(left == 0)
   {
   return 1;
   }
   else
   {
return (int)(Math.random()*CanBeRemoved)+1;
}
}
  
  
  
public static void PrintPlayerOne(int x, int y)
{
System.out.println("Player 1");
   for(int trainingDepth = 0; trainingDepth < y; trainingDepth++)
   {
       for(int elementNumber = 0; elementNumber < x; elementNumber++)
       {
           System.out.print(PlayerOne[elementNumber][trainingDepth] + " ");
      
       }
       System.out.println();
       }
       System.out.println();
}
  
  
public static void PrintPlayerTwo(int x, int y)
{
  
System.out.println("Player 2");
   for(int trainingDepth = 0; trainingDepth < y; trainingDepth++)
   {
       for(int elementNumber = 0; elementNumber < x; elementNumber++)
       {
           System.out.print(PlayerTwo[elementNumber][trainingDepth] + " ");
      
       }
       System.out.println();
       }
       System.out.println();
}
  
}

CODE 2:

import java.util.Scanner;

public class NimGameFunctions
{

public static void main(String[] args)
{   
printStory();
Scanner sc = new Scanner(System.in);
int buttonsRemaining = getInitalbuttonsNumber(sc);
  
  
while(buttonsRemaining > 0)
{
buttonsRemaining -= getComputerMove(buttonsRemaining);
buttonsRemaining -= getHumanMove(sc, buttonsRemaining);   
}
}   

public static void printStory()
{   
System.out.println(" THE RULES OF NIM: ");
System.out.println("The game starts with the user entering the number of buttons in the game.");
System.out.println("The computer takes 1 or 2 buttons.");
System.out.println("Then the user takes 1 or 2 buttons.");
System.out.println("The game continues until there are no buttons left.");
System.out.println("Whoever takes the final button wins.");

}

public static int getInitalbuttonsNumber(Scanner paramScanner)
{
System.out.println(" Enter number of buttons to start.");
return paramScanner.nextInt();
}

public static int getComputerMove(int left)
{
int computerMove = 0;
   if(left == 1)
   {
   computerMove = 1;
   System.out.println("Computer takes 1");
System.out.println("Now there are 0 buttons remaning.");
   }
   else
   {
computerMove = (int)(Math.random()*2)+1;
System.out.println("Computer takes "+computerMove);   
System.out.println("Now there are "+ left +" buttons remaning.");
System.out.println();
}
left -= computerMove;
if(left <= 0) // Test to determine if computer has won
{
System.out.println();
System.out.println("Computer wins!");
}
return computerMove;
}
  
public static int getHumanMove(Scanner paramScanner, int left)
{
System.out.println("How many buttons do you want to remove? (1 or 2)");
int userMove = paramScanner.nextInt();
  
while((userMove !=1) && (userMove !=2))
{
System.out.println("Taking " + userMove + " buttons is not allowed, please choose either 1 or 2.");
System.out.println();
userMove = paramScanner.nextInt();
}
left -= userMove;
if(left <= 0) // Test to determine if user has won
{
System.out.println("You took the final button.");
System.out.println();
System.out.println("You win!");
System.out.println();
}
else
{
System.out.println("Now there are " + left + " buttons remaining.");
System.out.println();
}
return userMove;
}
}

Explanation / Answer

import java.util.*;
public class NimGame
{
private NimPlayer player1, player2;
public static void main (String [] args)
{
    System.out.println ("Welcome to NimGame");
    System.out.println(" >");
    NimPlayer.addPlayer();
}
public NimPlayer (String usersName, String playernamegiven, String playerfamilyname,
                      String Playernewfamily, String Playernewgiven)
{
    this.usersName = usersName;
    this.playernamegiven = playernamegiven;
    this.playerfamilyname = playerfamilyname;
    this.Playernewfamily = Playernewfamily;
    this. Playernewgiven= Playernewgiven;
}
public class Nimsys
{
public static void addPlayer()
{
    Scanner keyboard = new Scanner (System.in);
    String [ ] player11 = new String [100];

    for (int i11 = 0; i11 < 100; i11++)
    {
    boolean isInArray = false;
    String NimPlayer = keyboard.next();
    currPlayer = new NimPlayer();
    StringTokenizer nameFactory = new StringTokenizer (NimPlayer, delimiters);
    String delimiters = " add player, ";
currPlayer.userName = nameFactory.nextToken();
currPlayer.playerfamilyname = nameFactory.nextToken();
currPlayer.playernamegiven = nameFactory.nextToken();

    if (i11>0)
    {
        for (int j11 = 0; j11 < player11.size(); j11++)
        {
            NimPlayer storePlayer = player11.get(j);
            if (storePlayer.userName.equalsIgnoreCase(currPlayer.userName))
            {
                isInArray = true;
                System.out.println("The player is already exists.");
                System.out.println(" >");
            }
            else
            {
                player.add(currPlayer);
                System.out.println(" >");
            }
        }
        }


    }
}
}
}

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