Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: Practice using control structures Due on Thursday June 29, 2017 11:59

ID: 3853116 • Letter: O

Question

Objective: Practice using control structures Due on Thursday June 29, 2017 11:59 PM (about 7 hours) The assignment is to write a program to play the well-known game of Nim. The game starts with some number of stones in a pile. The players take turns removing either one or two stones at each move. The player who takes the last stone loses. The game should be played between the program and the user. The game should be setup to be friendly to the user, letting them know on each move how many stones are left in the pile and how many stones the computer removes on each of its turns. STRATEGY It is easy to program an optimal strategy for Nim: Take the remainder of dividing the number of stones left by three; this will be 0, 1, or 2. If it is 0 then take 2 stones; if it is 2, take one stone. If the remainder is 1, then the game cannot be won assuming the opponent is also playing the optimal strategy; however, the program should still put up a good show of it and take one stone (delaying the inevitable and giving the opponent more chances to make a mistake)

Explanation / Answer

The code for the game in the C language is as follows:

Nim.c

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#define CMP 1

#define MAN 2

struct mv

{

int index;

int rmStone;

};

void dispPiles (int pl[], int no)

{

int uu;

printf ("Current Game Status -> ");

for (uu=0; uu<no; uu++)

printf ("%d ", pl[uu]);

printf(" o");

return;

}

bool isGameOver(int pl[], int no)

{

int uu;

for (uu=0; uu<no; uu++)

if (pl[uu]!=0)

return (false);

return (true);

}

void dispWin(int turn)

{

if (turn == CMP)

printf (" HUMAN won o o");

else

printf(" COMPUTER won o o");

return;

}

int calcSum(int pl[], int no)

{

int uu, sum = pl[0];

for (uu=1; uu<no; uu++)

sum = sum ^ pl[uu];

return(sum);

}

void makeMV(int pl[], int no, struct mv * moves)

{

int uu, nim_sum = calcSum(pl, no);

if (nim_sum != 0)

{

for (uu=0; uu<no; uu++)

{

if ((pl[uu] ^ nim_sum) < pl[uu])

{

(*moves).index = uu;

(*moves).rmStone =

pl[uu]-(pl[uu]^nim_sum);

pl[uu] = (pl[uu] ^ nim_sum);

break;

}

}

}

else

{

int nonZero[no], cnt;

for (uu=0, cnt=0; uu<no; uu++)

if (pl[uu] > 0)

nonZero [cnt++] = uu;

(*moves).index = (rand() % (cnt));

(*moves).rmStone =

1 + (rand() % (pl[(*moves).index]));

pl[(*moves).index] =

pl[(*moves).index] - (*moves).rmStone;

if (pl[(*moves).index] < 0)

pl[(*moves).index]=0;

}

return;

}

void startGame(int pl[], int no, int turn)

{

printf(" GAME STARTS o o");

struct mv moves;

while (isGameOver (pl, no) == false)

{

dispPiles(pl, no);

makeMV(pl, no, &moves);

if (turn == CMP)

{

printf("CMP removes %d stones from pile "

"at index %d o", moves.rmStone,

moves.index);

turn = MAN;

}

else

{

printf("MAN removes %d stones from pile at "

"index %d o", moves.rmStone,

moves.index);

turn = CMP;

}

}

dispPiles(pl, no);

dispWin(turn);

return;

}

void GuessWinner(int pl[], int no,

int turn)

{

printf("Prediction before playing the game -> ");

if (calcSum(pl, no) !=0)

{

if (turn == CMP)

printf("CMP will win o");

else

printf("MAN will win o");

}

else

{

if (turn == CMP)

printf("MAN will win o");

else

printf("CMP will win o");

}

return;

}

int main()

{

int pl[] = {3, 4, 5};

int no = sizeof(pl)/sizeof(pl[0]);

GuessWinner(pl, no, CMP);

startGame(pl, no, CMP);

return(0);

}

Please rate the answer if it helped.....Thankyou

Hope it helps.....