NIM game (10%) NIM game is also a two players game that is more sophisticated th
ID: 3606892 • Letter: N
Question
NIM game (10%)
NIM game is also a two players game that is more sophisticated than Tic-Tac. You will work on the simpler version of one pile only (the more sophisticated version consists of multiple piles).
There is one pile of matchsticks. Two players take turns removing 1 or 2 sticks from this pile. The person getting the last stick wins. There is a strategy to win for some combination. Notice if there is only one stick, the first player wins. If there are 2 sticks, the first player wins because s(he) can take 2 sticks. The first player of course can ply stupid by taking just 1 stick, and then the 2nd player will win.
(10%) Write a C# program that simulates two players playing this game. Your output should show: whose turn, how many sticks removed by that player, and how many are left. At the end your program shows who wins.
Develop your own case for two players with 4 sticks. You do not need to let any player play smart, but they have to follow the rules (you play both the first and the second player, just like you did in the tic-tac-toe game). Show the output. Who wins?
Develop your own case for two players with 5 sticks. You do not need to let any player play smart, but they have to follow the rules (you play both the first and the second player, just like you did in the tic-tac-toe game). Show the output. Who wins?
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define PLAYERA 1
#define PLAYERB 2
struct move
{
int pile_index;
int stones_removed;
};
void showPiles (int piles[], int n)
{
int i;
printf ("Current Game Status -> ");
for (i=0; i<n; i++)
printf ("%d ", piles[i]);
printf(" ");
return;
}
bool gameOver(int piles[], int n)
{
int i;
for (i=0; i<n; i++)
if (piles[i]!=0)
return (false);
return (true);
}
void declareWinner(int whoseTurn)
{
if (whoseTurn == PLAYERA)
printf (" PLAYERB won ");
else
printf(" PLAYERA won ");
return;
}
int calculateNimSum(int piles[], int n)
{
int i, nimsum = piles[0];
for (i=1; i<n; i++)
nimsum = nimsum ^ piles[i];
return(nimsum);
}
void makeMove(int piles[], int n, struct move * moves)
{
int i, nim_sum = calculateNimSum(piles, n);
if (nim_sum != 0)
{
for (i=0; i<n; i++)
{
if ((piles[i] ^ nim_sum) < piles[i])
{
(*moves).pile_index = i;
(*moves).stones_removed =
piles[i]-(piles[i]^nim_sum);
piles[i] = (piles[i] ^ nim_sum);
break;
}
}
}
else
{
int non_zero_indices[n], count;
for (i=0, count=0; i<n; i++)
if (piles[i] > 0)
non_zero_indices [count++] = i;
(*moves).pile_index = (rand() % (count));
(*moves).stones_removed =
1 + (rand() % (piles[(*moves).pile_index]));
piles[(*moves).pile_index] =
piles[(*moves).pile_index] - (*moves).stones_removed;
if (piles[(*moves).pile_index] < 0)
piles[(*moves).pile_index]=0;
}
return;
}
void playGame(int piles[], int n, int whoseTurn)
{
printf(" GAME STARTS ");
struct move moves;
while (gameOver (piles, n) == false)
{
showPiles(piles, n);
makeMove(piles, n, &moves);
if (whoseTurn == PLAYERA)
{
printf("PLAYERA removes %d stones from pile "
"at index %d ", moves.stones_removed,
moves.pile_index);
whoseTurn = PLAYERB;
}
else
{
printf("PLAYERB removes %d stones from pile at "
"index %d ", moves.stones_removed,
moves.pile_index);
whoseTurn = PLAYERA;
}
}
showPiles(piles, n);
declareWinner(whoseTurn);
return;
}
void knowWinnerBeforePlaying(int piles[], int n,
int whoseTurn)
{
printf("Prediction before playing the game -> ");
if (calculateNimSum(piles, n) !=0)
{
if (whoseTurn == PLAYERA)
printf("PLAYERA will win ");
else
printf("PLAYERB will win ");
}
else
{
if (whoseTurn == PLAYERA)
printf("PLAYERB will win ");
else
printf("PLAYERA will win ");
}
return;
}
int main()
{
int piles[] = {3, 4, 5};
int n = sizeof(piles)/sizeof(piles[0]);
knowWinnerBeforePlaying(piles, n, PLAYERA);
playGame(piles, n, PLAYERA);
int piles[] = {3, 4, 7};
int n = sizeof(piles)/sizeof(piles[0]);
knowWinnerBeforePlaying (piles, n, PLAYERA);
playGame (piles, n, PLAYERB);
return(0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.