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

Need help to fix my code so that the game can play human vs. human, computer vs.

ID: 3584329 • Letter: N

Question


Need help to fix my code so that the game can play human vs. human, computer vs. computer or human vs. computer. sample outputs are below code, thanks for all your help.

import java.util.*;

import java.util.Scanner;

public class cursedgold

{

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("there is 16 gold.");

int left = 16;


while(left>1)

{

if(turn==0)

int computer=getComputerMove(left);

System.out.println("Computer takes "+computer);

left-=computer;

System.out.println("Now there are "+left+" left.");

if(left==1)

{

System.out.println("Computer wins!");

return;

}

System.out.println("What's your move? (1 or 2 or 3)");

int person=sc.nextInt();

while(person!=1 && person!=2 && person!=3)

{

System.out.println(person+" not allowed, choose 1 or 2 0r 3.");

person=sc.nextInt();

}

left-=person;

System.out.println("Now there are "+left+" left.");

if(left == 1)

{

System.out.println("You win!");

return;

}

}

}

public static void main(String[] args)

{

cursedgold CGold=new cursedgold();

CGold.play();

}

}


Explanation / Answer

create one function in which provide three choices for the user as per ur question. design separate functions for each of the cases and switch to them as soon as user enters his option. for computer his turn should be implemented by math.random which picks random values between 1-3. hope that helps.