Question

Nim Game In java a) Write an Is-winning function in java such that: Input: pile size...

Nim Game In java a) Write an Is-winning function in java such that:

Input: pile size and the set of allowed moves.

Output: True if and only if the first player would win with the best play.

b) Make a computer player that will play a perfect game versus the user.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Main {

public static int input;
public static int piles = 12;
public static int com = (int) (Math.random() * 3 + 1);

@SuppressWarnings("resource")
public static void main(String[] args) throws InterruptedException {

System.out.println("There Are 12 piless, You Can Only Remove Up To 3 piless At A Time");
System.out.println("Your Goal Is To Remove The Last piles, piles 1");
System.out.println("To Remove A piles Either Enter 1, 2 or 3");
System.out.println("");

while (true) {

if (piles == 1) {
System.out.println("There Is " + piles + " piles Remaining");
} else {
System.out.println("There Are " + piles + " piless Remaining");
}

Scanner sc = new Scanner(System.in);
input = sc.nextInt();

if (input > 3 || input < 1) {
System.out.println("You Can Only Remove 3 piless At A Time");
} else {

piles = piles - input;

Thread.sleep(250);
int guess = 4 - input;
if (guess == 1) {
System.out.println("The Computer Has Removed " + guess + " piles");
} else {
System.out.println("The Computer Has Removed " + guess + " piless");
}
if (piles - guess == 0) {
System.out.println("There Are No piless Remaining, The Computer Has Won!!");
System.out.println("The Computer Has Won");
return;
}
if (piles - input == 0) {
System.out.println("There Are No piless Remaining, You Have Won!! True");
return;
}
piles = piles - guess;

}
}
}

}

Activities Terminal ▼ Tue 10:38 rams@rams-TravelMate-4740: -/Desktop File Edit View Search Terminal Help rams@rams-TravelMate-4740:-/Desktops java Main There Are 12 piless, You Can Only Renove Up To 3 piless At A Time Your Goal Is To Remove The Last piles, piles 1 To Remove A piles Either Enter 1, 2 or 3 There Are 12 piless Remaining The Computer Has Removed 3 piless There Are 8 piless Remaining You Can Only Remove 3 piless At A Tine There Are 8 piless Remaining The Computer Has Removed 3 piless There Are 4 piless Remaining The Computer Has Removed 3 piless There Are No piless Remaining, The Computer Has Won!! The Computer Has Won rams@rams-TravelMate-4740:~/DesktopS java Main There Are 12 piless, You can only Remove Up To 3 ptless At A Tine Your Goal Is To Remove The Last piles, piles 1 To Renove A piles Either Enter 1, 2 or 3 There Are 12 piless Remaining The Computer Has Removed 2 piless There Are 8 piless Remaining The Computer Has Removed 1 piles There Are 4 piless Remaining The Computer Has Removed 2 piless There Are No piless Remaining, The Computer Has Won!! The Computer Has Won rams@rams-TravelMate-4740:~/Desktop$ rams@rams-TravelMate-4740:/Desktop$ rams@rams-TravelMate-4740:~/Desktop$

Add a comment
Know the answer?
Add Answer to:
Nim Game In java a) Write an Is-winning function in java such that: Input: pile size...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. NIM game. This is a different version or easier version of NIM game Consider a pile of 5 matchsticks. Two people take turns removing 1 or 2 sticks each time from this pile. Suppose both players pl...

    1. NIM game. This is a different version or easier version of NIM game Consider a pile of 5 matchsticks. Two people take turns removing 1 or 2 sticks each time from this pile. Suppose both players play smartly (nobody plays a fool move trying to let the opponent wins. But there is only one winner anyway) a)If the person getting the last stick wins, will the first player win? Why? Show the steps the first and second player will...

  • Need Help with homework problem writing the game of nim in Python IDLE. Its a well...

    Need Help with homework problem writing the game of nim in Python IDLE. Its a well known game with a number of variants. The following variant has an interesting winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marble loses. Instructions...

  • A subtraction game Subtraction games are two-player games in which there is a pile of objects,...

    A subtraction game Subtraction games are two-player games in which there is a pile of objects, say coins. There are two players, Alice and Bob, who alternate turns subtracting 4.9. A SUBTRACTION GAME 19 from the pile some number of coins belonging to a set S (the subtraction set). Alice goes first. The first player who is unable to make a legal move loses. For example, suppose the initial pile contains 5 coins, and each player can, on his turn,...

  • Consider a variant of the Nim game called "Stones". Suppose that initially there is a single...

    Consider a variant of the Nim game called "Stones". Suppose that initially there is a single pile of 5 stones and two players, I and II. Each player takes turns picking up either 1 or 2 stones from the pile. Player I moves first, then Player II, then Player I, etc. until all stones have been picked up 3. Assuming that the loser is the player who picks up the last stone, write the game of Stones out in extensive...

  • Answer the following Nim game style questions. (Robert's Game) In this game, two players take turns removing stones from a pile that begins with n stones. The player who takes the last stone w...

    Answer the following Nim game style questions. (Robert's Game) In this game, two players take turns removing stones from a pile that begins with n stones. The player who takes the last stone wins. A player removes either one stone or p stones, where p is a prime dividing the number of stones in the pile at the start of the turn For which n does the First Player have a winning strategy? A winning strategy for the First Player...

  • The game of Nim: This is a well-known game with a number of variants. The following...

    The game of Nim: This is a well-known game with a number of variants. The following variant has an interesting winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marble loses. Write a C program in which the computer plays against...

  • Java Listed below is code to play a guessing game. In the game, two players attempt...

    Java Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player. boolean checkForWin (int guess, int answer) { System.out.println("You guessed" + guess +"."); if (answer == guess) { System.out.println( "You're right! You win!") ; return true; } else if (answer < guess) System.out.println ("Your guess is too high.") ; else System.out.println ("Your...

  • Problem 4. (20 points) Pebbles Game. Two players play a game, starting with three piles of...

    Problem 4. (20 points) Pebbles Game. Two players play a game, starting with three piles of n pebbles each. Players alternate turns. On each turn, a player may do one of the following: take one pebble from any pile take two pebbles each from two different piles The player who takes the last pebble(s) wins the game. Write an algorithm that, given n, determines whether the first player can "force" a win. That is, can the first player choose moves...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT