logo

NIM Game Playing Interface in Java

This assignment requires a team of 2 students to write an algorithm/flowchart and then convert it to a Java program for a given game of NIM. The purpose of the assessment is to assess the students' ability to apply principles of abstraction and problem solving in an object-oriented programming language, apply knowledge of programming constructs, and create programs based on incremental development processes.

8 Pages2250 Words180 Views
   

Added on  2023-06-11

About This Document

This Java code implements the NIM game where players take turns to remove stones from a pile. The player who removes the last stone wins. The code includes a playing interface for both human and computer moves.

NIM Game Playing Interface in Java

This assignment requires a team of 2 students to write an algorithm/flowchart and then convert it to a Java program for a given game of NIM. The purpose of the assessment is to assess the students' ability to apply principles of abstraction and problem solving in an object-oriented programming language, apply knowledge of programming constructs, and create programs based on incremental development processes.

   Added on 2023-06-11

ShareRelated Documents
Algorithm / Flowchart
NIM Game Playing Interface in Java_1
Source Code
package nim;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
/**
* This clsss serves as the NIM game's playing interface.
* Each playing instance takes in the name of the first and the second players and converts them into
proper case.
* Furthermore, the players are allowed to take turns to make their moves alongside the computer.
* The player to remove the last remaining stone, wins.
* The players are allowed to play again or quit.
*/
public class NIM
{
static int remaining=0;
/**
* Gets the id of the player whose turn is to be represented (1 or 2)
* Asks the player if he wishes to make a random move or a manual.
* updates the remaining number of stones
*/
public static void human(char turn)
{
Scanner sc = new Scanner(System.in);
Random random = new Random();
while(true)
{
int toRemove, choice, flag=0;
while(true) {
System.out.println("Player "+turn+"\nEnter 1 to make a RANDOM MOVE\nEnter 2 to make a
manual move. Enter> ");
choice=sc.nextInt();
if(choice!=1 && choice!=2) {
System.out.println("Invalid Input!");
continue;
}
break;
}
if(choice==1) {
while(true) {
toRemove=random.nextInt((3 - 1) + 1) + 1;
if(toRemove>remaining)
continue;
remaining -= toRemove;
System.out.println("Player "+turn+" removes "+toRemove+" stones.");
flag=1;
break;
}
}
else {
System.out.println("Player "+turn+", please enter number of stones to remove (Range of 1 to
3): ");
toRemove=sc.nextInt();
if(toRemove<=3 && toRemove>remaining)
{
System.out.println("You do not have enough stones left! Try a lesser number.");
}
else if(toRemove>=1 && toRemove<=3)
{
remaining -= toRemove;
System.out.println("Player "+turn+" removes "+toRemove+" stones.");
break;
}
else
System.out.println("Invalid Input! You can remove either 1, 2 or 3 stones. Try
again!");
}
if(flag==1)
break;
}
}
//Implements the optimal playing algorithm to make the computer's move and update the remaining
stones.
public static void computer()
{
int toRemove;
NIM Game Playing Interface in Java_2
if(remaining%3==0)
{
toRemove=2;
}
else
{
toRemove=1;
}
remaining-=toRemove;
System.out.println("Computer removes "+toRemove+" stones.");
}
//Driver method of the program
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("\tTHE GAME OF NIM\n\t---------------\nGiven a number of stones.\nIn each turn,
one of the three players can choose between 1-3 stones to be removed from that pile.\nThe player who take
the last stone is the winner!!");
//Computer's welcome message
System.out.println("\nHey there! I am AI-101 and I will be your AI opponent for the match!\
nPlease enter the name of Player 1: ");
String player1 = sc.nextLine();
System.out.println("Please enter the MIT ID of Player 1: ");
String MIT1 = sc.nextLine();
System.out.println("Please enter the name of the Player 2: ");
String player2 = sc.nextLine();
System.out.println("Please enter the MIT ID of the Player 2: ");
String MIT2 = sc.nextLine();
//Rectifies the Case of Player-1's name
String[] temp = player1.split(" ");
player1="";
for(String word : temp) {
char cap = Character.toUpperCase(word.charAt(0));
word=cap+word.substring(1, word.length()).toLowerCase();
player1+=word+" ";
}
//Rectifies the Case of Player-1's name
temp = player2.split(" ");
player2="";
for(String word : temp) {
char cap = Character.toUpperCase(word.charAt(0));
word=cap+word.substring(1, word.length()).toLowerCase();
player2+=word+" ";
}
player1=player1.substring(0, player1.length()-1);
player2=player2.substring(0, player2.length()-1);
String[] players = {player1,player2};
//Players welcome message
System.out.printf("\nWelcome %s and %s to the NIM game! Best of luck!\n",player1,player2);
int stones=0;
int flag=1;
char turn='0';
String choice;
//Starts a new game everytime the player wishes to continue
while(flag==1)
{
stones=0;
System.out.print("\n"+player1+", would you like to go first (YES,Y/NO,N)? Enter> ");
choice=sc.next().toUpperCase();
if(choice.equals("YES") || choice.equals("Y"))
{
System.out.println("\n"+player1+" goes first!");
turn='1';
}
else if(choice.equals("NO") || choice.equals("N"))
{
System.out.println("\n"+player2+" goes first!");
turn='2';
}
else if(!choice.equals("YES") && !choice.equals("Y") && !choice.equals("NO") && !
choice.equals("N"))
{
System.out.println("Invalid Input!");
continue;
}
Check validity of number of stones
while(stones<30||stones>50)
NIM Game Playing Interface in Java_3

End of preview

Want to access all the pages? Upload your documents or become a member.