Nim Game Implementation in Java

Verified

Added on  2020/05/16

|15
|2137
|33
AI Summary
This Java assignment involves programming a text-based implementation of the strategy game Nim. Students need to write code for both player and computer turns, taking input from the user, calculating remaining stones, and determining the winner based on game rules. The program should handle valid user input, provide clear output, and continue playing until a player wins.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Running head: PROGRAMMING IN JAVA
Programming in Java
Name of the student:
Name of the University:
Author note:

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
1
PROGRAMMING IN JAVA
Table of Contents
TASK-1 (Elevator Simulator)....................................................................................................2
Algorithm...............................................................................................................................2
Source code............................................................................................................................4
Output.....................................................................................................................................7
TASK-2 (NIM Game)................................................................................................................9
Algorithm...............................................................................................................................9
Source code..........................................................................................................................10
Output...................................................................................................................................14
Document Page
2
PROGRAMMING IN JAVA
TASK-1 (Elevator Simulator)
Algorithm
START
Step 1. Function main()
Step 2. Print menu
Step 3. Take user choice in ‘choice’ and convert to uppercase
Step 4. If choice is ‘S’ then goto next step else goto 24
Step 5. Call SelectFloor()
Step 6. If called function returns 1 goto next step else 2
Step 7. If floor value entered by user is 13 print message of ‘12a’
Step 8. If prevFloor value Greater than floor value goto next step else goto 15
Step 9. Print going down
Step 10. While prevFloor value >= floor value, else goto step 15
Step 11. Time delay of 500 milliseconds
Step 12. If prevFloor is not 13 print prevFloor else print ‘12a’
Step 13. Decrement prevFloor by 1
Step 14. Goto step 10
Step 15. Increment prevFloor by 1
Step 16. Print going up
Step 17. While prevFloor value <= floor value, else goto step 22
Step 18. Time delay of 500 milliseconds
Step 19. If prevFloor is not 13 print prevFloor else print ‘12a’
Step 20. Increment prevFloor by 1
Step 21. Goto step 17
Document Page
3
PROGRAMMING IN JAVA
Step 22. Decrement prevFloor by 1
Step 23. Goto step 2
Step 24. If user choice is ‘F’ Call function FireAlarm()
Step 25. Goto step 2
Step 26. If user choice is ‘Q’ print QUIT message
Step 27. Goto step 30
Step 28. Else print ERROR message
Step 29. Goto step 2
Step 30. End main() function
Function SelectFloor()
Step 1. Take user input of floor in floortemp
Step 2. If 100 < floortemp > 1, print error message
Step 3. goto step 1.
Step 4. If floortemp = prevFloor value goto next step else step 7
Step 5. print ‘same floor’ message
Step 6. return 0 to calling function
Step 7. Set floor to floortemp value
Step 8. return 1 to calling function.
Step 9. End function SelectFloor().
Function FireAlarm()
Step 1. Function FireAlarm()
Step 2. print danger message
Step 3. If prevFloor value is not 1 goto next step, else goto step 10
Step 4. Print “going down message”

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
4
PROGRAMMING IN JAVA
Step 5. While prevFloor > or = 1, goto next step else step 9
Step 6. Delay 500 mSecs
Step 7. Print prevFloor value and decrement prevFloor by 1
Step 8. goto step 5
Step 9. Increment prevFloor value by 1
Step 10. End function FireAlarm()
STOP
Source code
import java.io.*;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
/*@author: Gurpreet Singh
*@authorID: MIT 173258
*Class purpose: The purpose of this class is to simulate an Elevator system. The system
takes in user input for Floor numbers they wish to travel,
*checks for validity of inputs and travels accordingly. It also allows users to raise Fire
Alarms and quit the system. The system also omits floor #13.
*/
class Elevator
{
static int floor=1;
static int prevFloor=1;
public static void main(String []args)throws IOException
{
Scanner sc = new Scanner(System.in);
int i;
char choice='S';
System.out.println("Welcome to MIT173258: Gurpreet Singh's elevator simulator!\n");
while(choice!='Q')
{
System.out.print("\nPress S: Select a floor\nPress F: Raise fire alarm\nPress Q: Quit
elevator system\nPress now... ");
choice=sc.next(".").charAt(0);
Document Page
5
PROGRAMMING IN JAVA
choice=Character.toUpperCase(choice);
switch(choice)
{
case 'S': if(SelectFloor()==1)
{
if(floor==13)
{
System.out.println("We do not have a 13th floor. You are being taken to
floor 12A.");
}
if(prevFloor>floor)
{
System.out.println("Going down...");
while(prevFloor>=floor)
{
try
{
Thread.sleep(500);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
if(prevFloor!=13)
System.out.println(prevFloor+"...");
else
System.out.println("12A...");
prevFloor--;
}
prevFloor++;
System.out.println("Ding!!");
}
else
{
System.out.println("Going up...");
while(prevFloor<=floor)
{
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
Thread.currentThread().interrupt();
}
if(prevFloor!=13)
System.out.println(prevFloor+"...");
else
System.out.println("12A...");
prevFloor++;
Document Page
6
PROGRAMMING IN JAVA
}
prevFloor--;
System.out.println("Ding!!");
}
}
break;
case 'F': FireAlarm();
break;
case 'Q': System.out.println("Thank you!!");
break;
default: System.out.println("Invalid Entry. Try again!");
}
}
}
public static int SelectFloor()throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the floor that you would like to go to: ");
int floortemp=sc.nextInt();
if(floortemp<1 || floortemp>100)
{
System.out.println("Invalid Selection. Floor value must be in the range 1-100. Try
again!");
return 0;
}
else if(floortemp==prevFloor)
{
System.out.println("You are already on this floor. Try again!");
return 0;
}
else
{
floor=floortemp;
return 1;
}
}
public static void FireAlarm()
{
System.out.println("Danger!! You must exit the building now.");
if(prevFloor!=1)
{
System.out.println("Going down..");
while(prevFloor>=1)
{
try
{
Thread.sleep(500);

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
7
PROGRAMMING IN JAVA
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
if(prevFloor!=13)
System.out.println(prevFloor+"...");
else
System.out.println("12A...");
prevFloor--;
}
prevFloor++;
System.out.println("Ding!!");
}
}
}
Output
Document Page
8
PROGRAMMING IN JAVA
Document Page
9
PROGRAMMING IN JAVA
TASK-2 (NIM Game)
Algorithm
START
Step 1. Function main().
Step 2. User enters number of stones.
Step 3. If number of stones LESS or EQUALS TO 0, goto step 2, else step 4.
Step 4. Set value of remaining to number of stones
Step 5. User enters first going choice in ch.
Step 6. If choice is ‘Y’ or ‘YES’, set turn to 1 and Call user() function and goto step 7
else step 8.
Step 7. Print remaining stones.
Step 8. If user enters neither Y, YES, N or NO, goto step 5.
Step 9. Until remains value reaches 0, goto step 10.
Step 10. Delay 500millisecs, Set turn to ‘c’ and call function computer().
Step 11. Print remaining stones.
Step 12. If remaining stones is 0, goto step 17.
Step 13. Set turn to ‘u’.
Step 14. Call user() function.
Step 15. Print remaining stones.
Step 16. If turn is ‘c’, print ‘Congratulation’ message, else print ‘Computer Won’.
Step 17. User input choice to continue or not.
Step 18. If user enters ‘Y’ or ‘YES’ goto step 3. Else goto step 19.
Step 19. If user enters N or NO, print goodbye message and goto step 21.
Step 20. Otherwise, goto step 17.

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
10
PROGRAMMING IN JAVA
Step 21. End of main() function.
Function user()
Step 1. Function user().
Step 2. User input number of stones to remove.
Step 3. If remaining stones is 1 and also user enters 2, set remove to 1 and print
‘removing 1’ message.
Step 4. If user enters 1 or 2 set remaining <= remaining-remove, goto step 6
Step 5. If user enters other than values 1 or 2, print error message and goto step 2.
Step 6. End function user().
Function computer()
Step 1. Function computer().
Step 2. If remaining’s value is divisible by 3, set remove to 2 else set remove to 1.
Step 3. Set remaining<=remaining-remove
Step 4. End function computer().
STOP
Source code
import java.io.*;import java.io.*;
import java.util.Scanner;
/**
/*@author: Gurpreet singh
*@authorID: MIT 173258
*The purpose of this class is to simulate the NIM game.
*The player can enter the number of stones to start off the game and who should start the
game.
*The one to remove the stone at last loses.
*Computer uses the optimal playing strategy to play turns.
*/
public class NIM
Document Page
11
PROGRAMMING IN JAVA
{
static int remaining=0;
public static void user()
{
Scanner sc = new Scanner(System.in);
while(true)
{
int remove;
System.out.println("Enter the number of stones to remove (1 or 2): ");
remove=sc.nextInt();
if(remove==2 && remaining==1)
{
System.out.println("You do not have 2 stones left.\nRemoving 1 stone.");
remove=1;
remaining-=remove;
break;
}
if(remove==1 || remove==2)
{
remaining-=remove;
System.out.println("Player removes "+remove+" stones.");
break;
}
else
System.out.println("You can remove either 1 or 2 stones only. Enter again!");
}
}
public static void computer()
{
int remove;
if(remaining%3==0)
{
remove=2;
}
else
{
remove=1;
}
remaining-=remove;
System.out.println("Computer removes "+remove+" stones.");
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int stones=0, flag=1;
String choice="YES";
char turn='u';
System.out.println("!! THE GAME OF NIM !!\n");
while(flag==1)
Document Page
12
PROGRAMMING IN JAVA
{
stones=0;
while(stones<=0)
{
System.out.println("Enter the number of stones: ");
stones=sc.nextInt();
if(stones<=0)
System.out.println("Number of stones cannot be 0 or negative!! Try again.");
else
{
remaining=stones;
break;
}
}
System.out.print("Would you like to go first (YES,Y/NO,N)? Enter... ");
choice=sc.next().toUpperCase();
if(choice.equals("YES") || choice.equals("Y"))
{
turn='u';
user();
System.out.println("Number of stones left is "+remaining);
}
else if(!choice.equals("YES") && !choice.equals("Y") && !choice.equals("NO") &&
!choice.equals("N"))
{
System.out.println("Invalid entry. Try again!!");
continue;
}
while(remaining!=0)
{
turn='c';
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
Thread.currentThread().interrupt();
}
computer();
System.out.println("Number of stones left is "+remaining);
if(remaining==0)
break;
turn='u';
user();
System.out.println("Number of stones left is "+remaining);
}

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
13
PROGRAMMING IN JAVA
if(turn=='c')
System.out.println("\nCONGRATULATIONS!! YOU WON.\n");
else
System.out.println("\nCOMPUTER WON!!\n");
while(true)
{
System.out.print("Would you like to go continue (YES,Y/NO,N)? Enter... ");
choice=sc.next().toUpperCase();
if(choice.equals("YES") || choice.equals("Y"))
{
flag=1;
break;
}
else if(choice.equals("NO") || choice.equals("N"))
{
System.out.println("Thank You for playing NIM!! Have a nice day.");
flag=0;
break;
}
else
{
System.out.println("Invalid Entry!! Enter again.");
}
}
}
}
}
Document Page
14
PROGRAMMING IN JAVA
Output
1 out of 15
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]