Question

One variation of the game of Nim, described in Chapter 5 Exercise 12, is played with...

One variation of the game of Nim, described in Chapter 5 Exercise 12, is played with four
piles of stones. Initially, the piles contain 1, 2, 3, and 4 stones. On each turn, a player may
take 1, 2, or 3 stones from a single pile. The player who takes the last stone loses.
a) Write a program that allows two players to play Nim. Be sure to allow only legal
moves. The program should be written so that it can easily be modified for more
or less than four piles. The program output should look similar to:

Name for player #1: Petra
Name for player #2: Elaine
Board: 1 2 3 4
Petra
Which pile? 3
How many? 2
Board: 1 2 1 4
Elaine
Which pile? 4
How many? 3
Board: 1 2 1 1

Petra

...

When writing this JAVA program only use arrays, keyboardreader functions and no other function. You can only use Arrays.toString but you cannot use any other. Please put down below if you have any other functions that you want to use and I will look at it.

Homework Answers

Answer #1

hey there ! i update the code .. Please give me a like to appreciate my work and efforts...

here is the code -


import java.util.Arrays;
import java.util.Scanner;


/**
*
* @author Aditya kumar
*/
public class BoardGame {

public static void main(String args[])
{
//declaring varibales
int pile,howmany;
String playername="";
//for taking user input
Scanner sc=new Scanner(System.in);
//create array
int board[]={1,2,3,4};
//showing output
System.out.println("Player #1 : ");
playername=sc.next();
  
System.out.println(Arrays.toString(board));
System.out.println(playername+" : ");
  
boolean va=true;
while(va)
{
if(board[0]==1 && board[1]==1 && board[2]==1 && board[3]==1)
{
va=false;
}
else
{
  
System.out.println("Which Pile ? ");
//take input as pile number and as array start from zero
//so we need to -1 value from the pile
//for example - if user enter 4 , so the position will be 3 in the array
pile=sc.nextInt()-1;
System.out.println("How Many ? ");
//take how many wants to remove by user
howmany=sc.nextInt();
  
//taking the value from the pile's position
int v=board[pile];
//substract v from the howmay
howmany=v-howmany;
//set the new value
board[pile]=howmany;
//print the array
System.out.println(Arrays.toString(board));
}
  
}

  
  
}
  
  
}

and the snapshot of the output is -

Thank You ! Dont Forget to like !

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
One of the most popular games of chance is a dice game known as “craps”, played...
One of the most popular games of chance is a dice game known as “craps”, played in casinos around the world. Here are the rules of the game: A player rolls two six-sided die, which means he can roll a 1, 2, 3, 4, 5 or 6 on either die. After the dice come to rest they are added together and their sum determines the outcome. If the sum is 7 or 11 on the first roll, the player wins....
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a 3x3 grid. The winner is the first player to place 3 consecutive marks in a horizontal, vertical or diagonal row. Understanding the Problem: In this assignment, you will: 1) Create a tic tac toe board data structure. 2) Check for game over. 3) Clear the tic tac toe game for each game. 4) Display the tic tac toe board to screen. 5) Simulate playing...
R-S-P Requirement: - write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask...
R-S-P Requirement: - write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor:          20 -- user's paper:            30 -- comp's rock:            1 -- comp's scissor:        2 -- comp's...
Problem 4)A simplified version of the dice game 10,000 is played using 5 dice. The player...
Problem 4)A simplified version of the dice game 10,000 is played using 5 dice. The player rolls the 5 six-sided dice, each 1 that is rolled, the player achieves a score of 100. A) How many possible ways are there to roll i l's over these 5 dice? (Hint: use combinations) B) The probability of any one dice rolling to a value of 1 is 1/5 (since each dice has six sides). Use the binomial probability distribution. Calculate the probability...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
For C++: (Use only conditions, loops, user defined functions and arrays) Suppose you had data in...
For C++: (Use only conditions, loops, user defined functions and arrays) Suppose you had data in a file (input.txt) like this 1 2 6 11 4 8 12 3 16 5 6 14 ……… Find the multiples of 2 in order and store it in the output file (output.txt) like this 2 4 .......... The number 6 is in the wrong position so the order was disturbed. 6 only comes after 4. Therefore 6 and the other multiples of 2...
Only use C for this problem. To start, create a structure with arrays. Any structure you...
Only use C for this problem. To start, create a structure with arrays. Any structure you decide on is ok, just create your own. That said, you are required to meet these guidelines: 1. You must give the structure you make a name. Any is ok 2. Your structure must have at least 3 members. 3. Two of those members must be arrays. 4. Your members need to be of at least two different data-types. To clarify, your members cannot...
The premise of this project is to make a basic GUI and code for the game...
The premise of this project is to make a basic GUI and code for the game Keno. The specifics of the gui and game are below. Description: In this project you will implement the popular casino and state lottery game, Keno. This is a somewhat simple game to understand and play which should allow you to focus on learning GUI development in JavaFX and trying your hand at event driven programing. Implementation Details: You may add as many classes, data...
COSC 1436 Programming Assignment 2 Programming Assignment 2 Refactoring is the process of taking existing code...
COSC 1436 Programming Assignment 2 Programming Assignment 2 Refactoring is the process of taking existing code and improving its design without changing the functionality. In this programming assignment, you are going to take your code from Programming Assignment 1 and refactor it so that it uses functions. The program should still do the following:  Get the name of a student  Get the name of three assignments  Get the grade of three assignments as integers  Calculates the...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT