Write a program that plays the game Nim the user of the program. The program and
ID: 3655436 • Letter: W
Question
Write a program that plays the game Nim the user of the program. The program and the user alternate turns, picking up one, two, or three straws on each turn. The program goes first. The player to pick up the last straw loses. Your program should start by generating and displaying a random integer (which represents the number of straws) between 10 and 20 inclusive. If this number is 1 more than multiple of 4, add 1. For example, if the random integer is 20, then start with 20 straws. But if the random integer is 17(which is one more than a multiple of 4), start with 18. On a userExplanation / Answer
/*you may also download the code from http://www.2shared.com/file/zCz41N38/NimGame.html*/ import java.util.Random; import java.util.Scanner; public class NimGame { public static void main(String[] args) { int randomValueForStraw; int userSelect; int computerSelect; int flag=0; System.out.println("Welcome to Nim Game"); Random randomStraw=new Random(); randomValueForStraw=randomStraw.nextInt(20-10)+10; if(randomValueForStraw%4==1){ randomValueForStraw++; } System.out.println("Total number of straw:"+randomValueForStraw); while(randomValueForStraw>0){ do{ System.out.println("User's Turn"); System.out.println("pick up the straw:"); Scanner input=new Scanner(System.in); userSelect=input.nextInt(); if((userSelect!=1 && userSelect!=2 && userSelect!=3) || userSelect>randomValueForStraw){ System.out.println("You entered wrong data"); System.out.println("Remaining number of straw:"+randomValueForStraw); } }while((userSelect!=1 && userSelect!=2 && userSelect!=3) || userSelect>randomValueForStraw); randomValueForStraw=randomValueForStraw-userSelect; System.out.println("Remaining number of straw:"+randomValueForStraw); if(randomValueForStrawRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.