Question
Some two-player games are played with a pile of pebbles. The first player can take a certain number of pebbles that depends on the current
Some two-player games are played with a pile of pebbles. The first player can take a certain number of pebbles that depends on the current number of pebbles, leaving a smaller number of pebbles. The permissible moves depend on the game rules. Then the second player takes a turn, and again the first, until one of them is unable to make another legal move and loses.
The Game interface below describes the game rules-the number of pebbles that are permitted after making a legal move from a given number of pebbles.
A common game of this type is called Nim. A player must take at least one pebble and can take at most half of the pebbles. The NimGame class below provides the game rules for this game. The TwoThreeGame class provides rules for a different game. As long as there are at least four pebbles, a player can take two or three of them.
A position (that is, a given number of pebbles) is winning if it isn't an immediate loss, and at least one of the moves yields a losing position for the other player.
A position is losing if it is an immediate loss, or all of the moves lead to a winning position for the other player.
In the Nim game, it happens that positions of the form 21 are losing positions, and all others are winning positions.
Implement the mutually recursive methods isWinningPosition and isLosingPosition.
The nextPositions method of the Game interface returns the positions that result from legal moves.
code :
import java.util.Scanner;
public class Games { public static boolean isWinningPosition(int n, Game g) { int[] positions = g.nextPositions(n);
if (positions.length == 0) { return false; } for (int i : positions) { if (!isWinningPosition(i, g)) { return true; } } return false; }
public static boolean isLosingPosition(int n, Game g) { int[] positions = g.nextPositions(n); if (positions.length == 0) { return true; } for (int i : positions) { if (!isLosingPosition(i, g)) { return true; } } return false;
} public static void main (String [] args) { Scanner in = new Scanner(System.in); String gameType = in.nextLine(); String positionType = in.nextLine(); int n = in.nextInt(); if (gameType.equals("NimGame")) { if(positionType.equals("isWinningPosition")) { System.out.println(isWinningPosition(n, new NimGame())); } else // isLosingPosition { System.out.println(isLosingPosition(n, new NimGame())); } } else //TwoThreeGame { if(positionType.equals("isWinningPosition")) // isWinningPosition { System.out.println(isWinningPosition(n, new TwoThreeGame())); } else // isLosingPosition { System.out.println(isLosingPosition(n, new TwoThreeGame())); } } } }
outputs all good except 9
Not all tests passed.
check1: Compare outputkeyboard_arrow_up
Input
NimGame isWinningPosition 1
Your output
false
check2: Compare outputkeyboard_arrow_up
Input
NimGame isWinningPosition 2
Your output
true
check3: Compare outputkeyboard_arrow_up
Input
NimGame isWinningPosition 3
Your output
false
check4: Compare outputkeyboard_arrow_up
Input
NimGame isWinningPosition 4
Your output
true
check5: Compare outputkeyboard_arrow_up
Input
NimGame isWinningPosition 5
Your output
true
check6: Compare outputkeyboard_arrow_up
Input
NimGame isLosingPosition 1
Your output
true
check7: Compare outputkeyboard_arrow_up
Input
NimGame isLosingPosition 2
Your output
false
check8: Compare outputkeyboard_arrow_up
Input
NimGame isLosingPosition 3
Your output
true
clear9: Compare outputkeyboard_arrow_up
Output differs. See highlights below.
Input
NimGame isLosingPosition 4
Your output
true
Expected output
false
check10: Compare outputkeyboard_arrow_up
Input
NimGame isLosingPosition 5
Your output
false
check11: Compare outputkeyboard_arrow_up
Input
NimGame isLosingPosition 7
Your output
true
check12: Compare outputkeyboard_arrow_up
Input
NimGame isLosingPosition 15
Your output
true
check13: Compare outputkeyboard_arrow_up
Input
TwoThreeGame isLosingPosition 5
Your output
false
check14: Compare outputkeyboard_arrow_up
Input
TwoThreeGame isLosingPosition 7
Your output
true
check15: Compare outputkeyboard_arrow_up
Input
TwoThreeGame isLosingPosition 15
Your output
false
other java files
public interface Game { int[] nextPositions(int n); }
public class NimGame implements Game { public int[] nextPositions(int n) { int[] result = new int[n / 2]; for (int i = 0; i < result.length; i++) result[i] = n - i - 1; return result; } }
public class TwoThreeGame implements Game { public int[] nextPositions(int n) { if (n >= 4) return new int[] { n - 3, n - 2 }; else return new int[] {}; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started