Java Game of Nim Implementation
VerifiedAdded on 2020/05/16
|15
|1971
|338
AI Summary
This Java programming assignment focuses on implementing the classic game of Nim. Students are tasked with creating a program that simulates the gameplay, allowing both human players and the computer to take turns removing stones from a pile. The assignment involves understanding basic game logic, iterating through turns, and incorporating user input. Additionally, it may require implementing a strategy for the computer player to attempt to win the game.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
Running head: JAVA PROGRAMMING
JAVA Programming
Name of the student:
Name of the University:
Author note:
JAVA Programming
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.
1
JAVA PROGRAMMING
Table of Contents
TASK-1 (Elevator System)........................................................................................................2
Algorithm...............................................................................................................................2
Source Code...........................................................................................................................3
Output.....................................................................................................................................6
TASK-2 (Game of NIM)............................................................................................................8
Algorithm...............................................................................................................................8
Source code............................................................................................................................9
Output...................................................................................................................................12
JAVA PROGRAMMING
Table of Contents
TASK-1 (Elevator System)........................................................................................................2
Algorithm...............................................................................................................................2
Source Code...........................................................................................................................3
Output.....................................................................................................................................6
TASK-2 (Game of NIM)............................................................................................................8
Algorithm...............................................................................................................................8
Source code............................................................................................................................9
Output...................................................................................................................................12
2
JAVA PROGRAMMING
TASK-1 (Elevator System)
Algorithm
START
Step 1. Declare function main().
Step 2. Display menu and take user input for choice.
Step 3. Convert character input into uppercase.
Step 4. In Case user choses ‘S’ goto next step else goto step 7.
Step 5. Call function SelectFloor().
Step 6. If function returns 1 call function Movement(), else goto step 2.
Step 7. In Case user enters ‘F’, call function FireAlarm() and then goto step 2.
Step 8. In Case user enters ‘Q’, display Exit message and goto step 10.
Step 9. If user enters wrong choice, display error message and goto step 2.
Step 10. End of function main().
Function SelectFloor()
Step 1. Take user input of floors he wishes to travel in variable temp.
Step 2. If tempInput <1 or tempInput>100, display error message. goto step 1.
Step 3. If tempInput = currentFloor value, display “same floor error” message and
return 0 to calling function, else next step.
Step 4. Set floors to tempInput and return 1 to calling function.
Step 5. End of function SelectFloor().
Function Movement()
Step 1. If currentFloor value > floors value, goto step 2 else goto step 7.
Step 2. Display message “going down”
JAVA PROGRAMMING
TASK-1 (Elevator System)
Algorithm
START
Step 1. Declare function main().
Step 2. Display menu and take user input for choice.
Step 3. Convert character input into uppercase.
Step 4. In Case user choses ‘S’ goto next step else goto step 7.
Step 5. Call function SelectFloor().
Step 6. If function returns 1 call function Movement(), else goto step 2.
Step 7. In Case user enters ‘F’, call function FireAlarm() and then goto step 2.
Step 8. In Case user enters ‘Q’, display Exit message and goto step 10.
Step 9. If user enters wrong choice, display error message and goto step 2.
Step 10. End of function main().
Function SelectFloor()
Step 1. Take user input of floors he wishes to travel in variable temp.
Step 2. If tempInput <1 or tempInput>100, display error message. goto step 1.
Step 3. If tempInput = currentFloor value, display “same floor error” message and
return 0 to calling function, else next step.
Step 4. Set floors to tempInput and return 1 to calling function.
Step 5. End of function SelectFloor().
Function Movement()
Step 1. If currentFloor value > floors value, goto step 2 else goto step 7.
Step 2. Display message “going down”
3
JAVA PROGRAMMING
Step 3. While currentFloor value is > or currentFloor= floors value then goto next
step, else goto step 6.
Step 4. Display currentFloor value.
Step 5. Decrease currentFloor by 1, goto step 3.
Step 6. Increase currentFloor value by 1, once. Goto step 12
Step 7. Display message “going up”
Step 8. While currentFloor value < or = floors value goto next step, else goto step 11.
Step 9. Display currentFloor value.
Step 10. Increase currentFloor by 1, goto step 8.
Step 11. Decrease currentFloor value by 1, once. Goto step 12
Step 12. End of function Movement().
Function FireAlarm()
Step 1. Declare function FireAlarm().
Step 2. Display danger message.
Step 3. If currentFloor value is not 1 goto next step, else goto step 8.
Step 4. Print “going down message”
Step 5. While currentFloor > or = 1, goto next step else step 7.
Step 6. Display currentFloor value and decrease currentFloor value by 1, goto step 5.
Step 7. Increase currentFloor value by 1, once.
Step 8. End of function FireAlarm().
STOP
Source Code
package elevatorsim;
import java.io.*;
JAVA PROGRAMMING
Step 3. While currentFloor value is > or currentFloor= floors value then goto next
step, else goto step 6.
Step 4. Display currentFloor value.
Step 5. Decrease currentFloor by 1, goto step 3.
Step 6. Increase currentFloor value by 1, once. Goto step 12
Step 7. Display message “going up”
Step 8. While currentFloor value < or = floors value goto next step, else goto step 11.
Step 9. Display currentFloor value.
Step 10. Increase currentFloor by 1, goto step 8.
Step 11. Decrease currentFloor value by 1, once. Goto step 12
Step 12. End of function Movement().
Function FireAlarm()
Step 1. Declare function FireAlarm().
Step 2. Display danger message.
Step 3. If currentFloor value is not 1 goto next step, else goto step 8.
Step 4. Print “going down message”
Step 5. While currentFloor > or = 1, goto next step else step 7.
Step 6. Display currentFloor value and decrease currentFloor value by 1, goto step 5.
Step 7. Increase currentFloor value by 1, once.
Step 8. End of function FireAlarm().
STOP
Source Code
package elevatorsim;
import java.io.*;
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
4
JAVA PROGRAMMING
import java.util.Scanner;
/*@author: Asdur Rehman Gara
*@authorID: MIT172984
*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.
*/
public class ElevatorSim
{
static int floors=1;
static int currentFloor=1;
public static int SelectFloor()
{
Scanner sc = new Scanner(System.in);
System.out.println("Which floor you would like to go to: ");
int tempInput=sc.nextInt();
if(tempInput<1 || tempInput>100)
{
System.out.println("Floor value cannot be out of range 1-100. ERROR!");
return 0;
}
else if(tempInput==currentFloor)
{
System.out.println("Same floor. Please try again!");
return 0;
}
else
{
floors=tempInput;
return 1;
}
}
public static void Movement()
{
if(currentFloor<floors)
{
System.out.println("Going up...");
while(currentFloor<=floors)
{
System.out.print(currentFloor+"...");
currentFloor++;
}
currentFloor--;
}
else
JAVA PROGRAMMING
import java.util.Scanner;
/*@author: Asdur Rehman Gara
*@authorID: MIT172984
*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.
*/
public class ElevatorSim
{
static int floors=1;
static int currentFloor=1;
public static int SelectFloor()
{
Scanner sc = new Scanner(System.in);
System.out.println("Which floor you would like to go to: ");
int tempInput=sc.nextInt();
if(tempInput<1 || tempInput>100)
{
System.out.println("Floor value cannot be out of range 1-100. ERROR!");
return 0;
}
else if(tempInput==currentFloor)
{
System.out.println("Same floor. Please try again!");
return 0;
}
else
{
floors=tempInput;
return 1;
}
}
public static void Movement()
{
if(currentFloor<floors)
{
System.out.println("Going up...");
while(currentFloor<=floors)
{
System.out.print(currentFloor+"...");
currentFloor++;
}
currentFloor--;
}
else
5
JAVA PROGRAMMING
{
System.out.println("Going down...");
while(currentFloor>=floors)
{
System.out.print(currentFloor+"...");
currentFloor--;
}
currentFloor++;
}
System.out.print("Ding!\n");
}
public static void FireAlarm()
{
System.out.println("Danger!! You must exit the building now.");
if(currentFloor!=1)
{
System.out.println("Going down..");
while(currentFloor>=1)
{
System.out.print(currentFloor+"...");
currentFloor--;
}
currentFloor++;
System.out.print("Ding!\n");
}
}
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
char ch='S';
int i;
System.out.println("Welcome to MIT172984: Asdur Rehman Gara's elevator simulator!\
n");
while(ch!='Q')
{
System.out.print("\nEnter S: Select floor to travel\nEnter F: Raise the fire-alarm\
nEnter Q: Exit the elevator\nEnter option now: ");
ch=sc.next(".").charAt(0);
ch=Character.toUpperCase(ch);
switch(ch)
{
case 'S': if(SelectFloor()==1)
{
Movement();
}
break;
JAVA PROGRAMMING
{
System.out.println("Going down...");
while(currentFloor>=floors)
{
System.out.print(currentFloor+"...");
currentFloor--;
}
currentFloor++;
}
System.out.print("Ding!\n");
}
public static void FireAlarm()
{
System.out.println("Danger!! You must exit the building now.");
if(currentFloor!=1)
{
System.out.println("Going down..");
while(currentFloor>=1)
{
System.out.print(currentFloor+"...");
currentFloor--;
}
currentFloor++;
System.out.print("Ding!\n");
}
}
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
char ch='S';
int i;
System.out.println("Welcome to MIT172984: Asdur Rehman Gara's elevator simulator!\
n");
while(ch!='Q')
{
System.out.print("\nEnter S: Select floor to travel\nEnter F: Raise the fire-alarm\
nEnter Q: Exit the elevator\nEnter option now: ");
ch=sc.next(".").charAt(0);
ch=Character.toUpperCase(ch);
switch(ch)
{
case 'S': if(SelectFloor()==1)
{
Movement();
}
break;
6
JAVA PROGRAMMING
case 'F': FireAlarm();
break;
case 'Q': System.out.println("Exiting...");
break;
default: System.out.println("Invalid Entry.");
}
}
}
}
Output
JAVA PROGRAMMING
case 'F': FireAlarm();
break;
case 'Q': System.out.println("Exiting...");
break;
default: System.out.println("Invalid Entry.");
}
}
}
}
Output
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
7
JAVA PROGRAMMING
JAVA PROGRAMMING
8
JAVA PROGRAMMING
TASK-2 (Game of NIM)
Algorithm
START
Step 1. Define function main().
Step 2. Display welcome message.
Step 3. User enters number of stones.
Step 4. If number of stones LESS or EQUALS TO 0, goto step 3, else step 5.
Step 5. Set value of remains to number of stones, i.e., n.
Step 6. User enters first going choice in ch.
Step 7. If choice is ‘Y’ or ‘YES’, set turn to 1 and Call player() function and goto step
8 else step 9.
Step 8. Display remaining stones.
Step 9. If user enters neither Y, YES, N or NO, goto step 6.
Step 10. Until remains value reaches 0, goto step 11.
Step 11. Set turn to 2 and call function comp().
Step 12. Display remaining stones.
Step 13. If remaining stones is 0, goto step 17.
Step 14. Set turn to 1.
Step 15. Call player() function.
Step 16. Display remaining stones.
Step 17. If turn is 2, display “Player Won” message, else display “Computer Won”.
Step 18. Take user choice for continuation.
Step 19. If user enters Y or YES goto step 3. Else step 20.
Step 20. If user enters N or NO, display goodbye message and goto step 22.
JAVA PROGRAMMING
TASK-2 (Game of NIM)
Algorithm
START
Step 1. Define function main().
Step 2. Display welcome message.
Step 3. User enters number of stones.
Step 4. If number of stones LESS or EQUALS TO 0, goto step 3, else step 5.
Step 5. Set value of remains to number of stones, i.e., n.
Step 6. User enters first going choice in ch.
Step 7. If choice is ‘Y’ or ‘YES’, set turn to 1 and Call player() function and goto step
8 else step 9.
Step 8. Display remaining stones.
Step 9. If user enters neither Y, YES, N or NO, goto step 6.
Step 10. Until remains value reaches 0, goto step 11.
Step 11. Set turn to 2 and call function comp().
Step 12. Display remaining stones.
Step 13. If remaining stones is 0, goto step 17.
Step 14. Set turn to 1.
Step 15. Call player() function.
Step 16. Display remaining stones.
Step 17. If turn is 2, display “Player Won” message, else display “Computer Won”.
Step 18. Take user choice for continuation.
Step 19. If user enters Y or YES goto step 3. Else step 20.
Step 20. If user enters N or NO, display goodbye message and goto step 22.
9
JAVA PROGRAMMING
Step 21. Otherwise, goto step 18.
Step 22. End of main() function.
Function player()
Step 1. Declare function player().
Step 2. User enters number of stones to remove.
Step 3. If remaining stones is 1 and also user enters 2, goto step 2, else step 4.
Step 4. If user enters 1 or 2 set value of remaining stones by decreasing removed
stones from it and goto step
Step 5. Else it user enters other values, display error message and goto step 2.
Step 6. End of function player().
Function comp()
Step 1. Declare function comp().
Step 2. If remaining stone is divisible by 3, set remove to 2 else set remove to 1.
Step 3. Decrease remaining stones by remove.
Step 4. End of function comp().
STOP
Source code
package nim.game;
import java.io.*;
import java.util.Scanner;
/**
*@author: Asdur Rehman Gara #MIT172984
*This class simulates the game of NIM.
*It allows the players to select how many stones to start with and remove 1 or 2 stones from
that pile. This continues until no stones are left.
*The computer uses the (OPS)Optimal Playing Strategy for the gaming.
*/
JAVA PROGRAMMING
Step 21. Otherwise, goto step 18.
Step 22. End of main() function.
Function player()
Step 1. Declare function player().
Step 2. User enters number of stones to remove.
Step 3. If remaining stones is 1 and also user enters 2, goto step 2, else step 4.
Step 4. If user enters 1 or 2 set value of remaining stones by decreasing removed
stones from it and goto step
Step 5. Else it user enters other values, display error message and goto step 2.
Step 6. End of function player().
Function comp()
Step 1. Declare function comp().
Step 2. If remaining stone is divisible by 3, set remove to 2 else set remove to 1.
Step 3. Decrease remaining stones by remove.
Step 4. End of function comp().
STOP
Source code
package nim.game;
import java.io.*;
import java.util.Scanner;
/**
*@author: Asdur Rehman Gara #MIT172984
*This class simulates the game of NIM.
*It allows the players to select how many stones to start with and remove 1 or 2 stones from
that pile. This continues until no stones are left.
*The computer uses the (OPS)Optimal Playing Strategy for the gaming.
*/
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
10
JAVA PROGRAMMING
public class NIMGame
{
static int remains=0;
public static void player()
{
Scanner sc = new Scanner(System.in);
while(true)
{
int remove;
while(true)
{
System.out.println("Enter the number of stones to remove (1 or 2): ");
remove=sc.nextInt();
if(remains==1)
{
if(remove==2)
{
System.out.println("Last stone remaining. Cannot remove 2 stones.");
continue;
}
}
break;
}
if(remove==1 || remove==2)
{
remains=remains-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 comp()
{
int remove;
if(remains%3==0)
{
remove=2;
}
else
{
remove=1;
}
remains=remains-remove;
System.out.println("Computer removes "+remove+" stones.");
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
JAVA PROGRAMMING
public class NIMGame
{
static int remains=0;
public static void player()
{
Scanner sc = new Scanner(System.in);
while(true)
{
int remove;
while(true)
{
System.out.println("Enter the number of stones to remove (1 or 2): ");
remove=sc.nextInt();
if(remains==1)
{
if(remove==2)
{
System.out.println("Last stone remaining. Cannot remove 2 stones.");
continue;
}
}
break;
}
if(remove==1 || remove==2)
{
remains=remains-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 comp()
{
int remove;
if(remains%3==0)
{
remove=2;
}
else
{
remove=1;
}
remains=remains-remove;
System.out.println("Computer removes "+remove+" stones.");
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
11
JAVA PROGRAMMING
int n=2;
String ch="YES";
int turn=1;
int flag=1;
System.out.println("THE GAME OF NIM");
while(flag==1)
{
while(n>0)
{
System.out.print("\nEnter the number of starting stones: ");
n=sc.nextInt();
if(n<=0)
System.out.println("Invalid entry. Number of stones must be greater than 0.");
else
{
remains=n;
break;
}
}
System.out.print("Would you like to go first? (YES/NO): ");
ch=sc.next();
if(ch.equalsIgnoreCase("Y") || ch.equalsIgnoreCase("YES"))
{
turn=1;
player();
System.out.println("Number of stones left is "+remains);
}
else if(!ch.equalsIgnoreCase("y") && !ch.equalsIgnoreCase("yes") && !
ch.equalsIgnoreCase("n") && !ch.equalsIgnoreCase("no") )
{
System.out.println("Invalid entry.");
continue;
}
while(remains>0)
{
turn=2;
comp();
System.out.println("Number of stones left is "+remains);
if(remains==0)
break;
turn=1;
player();
System.out.println("Number of stones left is "+remains);
}
if(turn==2)
System.out.println("\nYOU WON NIM !! CONGRATULATIONS !!");
else
JAVA PROGRAMMING
int n=2;
String ch="YES";
int turn=1;
int flag=1;
System.out.println("THE GAME OF NIM");
while(flag==1)
{
while(n>0)
{
System.out.print("\nEnter the number of starting stones: ");
n=sc.nextInt();
if(n<=0)
System.out.println("Invalid entry. Number of stones must be greater than 0.");
else
{
remains=n;
break;
}
}
System.out.print("Would you like to go first? (YES/NO): ");
ch=sc.next();
if(ch.equalsIgnoreCase("Y") || ch.equalsIgnoreCase("YES"))
{
turn=1;
player();
System.out.println("Number of stones left is "+remains);
}
else if(!ch.equalsIgnoreCase("y") && !ch.equalsIgnoreCase("yes") && !
ch.equalsIgnoreCase("n") && !ch.equalsIgnoreCase("no") )
{
System.out.println("Invalid entry.");
continue;
}
while(remains>0)
{
turn=2;
comp();
System.out.println("Number of stones left is "+remains);
if(remains==0)
break;
turn=1;
player();
System.out.println("Number of stones left is "+remains);
}
if(turn==2)
System.out.println("\nYOU WON NIM !! CONGRATULATIONS !!");
else
12
JAVA PROGRAMMING
System.out.println("\nAlas !! YOU LOST !! COMPUTER WON NIM!!");
while(true)
{
System.out.print("Would you like to go continue? (Y/N):");
ch=sc.next();
ch=ch.toUpperCase();
if(ch.equalsIgnoreCase("Y") || ch.equalsIgnoreCase("YES"))
{
flag=1;
break;
}
else if(ch.equalsIgnoreCase("NO") || ch.equals("N"))
{
System.out.println("Goodbye!!");
flag=0;
break;
}
else
{
System.out.println("Invalid Entry.");
}
}
}
}
}
Output
JAVA PROGRAMMING
System.out.println("\nAlas !! YOU LOST !! COMPUTER WON NIM!!");
while(true)
{
System.out.print("Would you like to go continue? (Y/N):");
ch=sc.next();
ch=ch.toUpperCase();
if(ch.equalsIgnoreCase("Y") || ch.equalsIgnoreCase("YES"))
{
flag=1;
break;
}
else if(ch.equalsIgnoreCase("NO") || ch.equals("N"))
{
System.out.println("Goodbye!!");
flag=0;
break;
}
else
{
System.out.println("Invalid Entry.");
}
}
}
}
}
Output
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
13
JAVA PROGRAMMING
JAVA PROGRAMMING
14
JAVA PROGRAMMING
JAVA PROGRAMMING
1 out of 15
Related Documents
Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
© 2024 | Zucol Services PVT LTD | All rights reserved.