Rules of NIM The game board consists of three rows of sticks

Rules of NIM:

The game board consists of three rows of sticks with three, five, and seven sticks in each row.

A: | | |
B: | | | | |
C: | | | | | | |
Two players take turns. Each player may cross off as many sticks in a row as he/she wants. The last player to cross off a stick, LOSES.

For example, Player 1 chooses to cross off 2 sticks in Row A. The player will enter the value, A2

A: + + |
B: | | | | |
C: | | | | | | |

Player 2 chooses to finish off Row A, and cross off the other stick. Player 2 enters, A1

A: + + +
B: | | | | |
C: | | | | | | |

Player 1 enters B5

A: + + +
B: + + + + +
C: | | | | | | |

Player 2 enters C6

A: + + +
B: + + + + +
C: - - - - - - |

Player 1 one only has one valid move C1

A: + + +
B: + + + + +
C: - - - - - - +

Player 1 loses.
HW5: (100 Points) Due 1 week.
Write a program that allows two player operation or NIM.

Requirements:
1)   The program will accept only valid player moves, entered as row and number of sticks (C3) to cross of three sticks from row C. The program must verify that Row C has 3 sticks to cross off.
2)   The game board will be displayed at the beginning of the game, and after each move.
3)   The game will be repeated until the players choose to quit.
4)   The program will track the number of wins for both Player1 one and Player 2. This will be displayed above the game board.
5)   Player 1 will go first on the first game. Afterward, the player to lose the last match goes first.

NOTE: I need the code for this in C. Thank you for your help.

Solution

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#define COMPUTER 1

#define HUMAN 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 == COMPUTER)

        printf (\"\ HUMAN won\ \ \");

    else

        printf(\"\ COMPUTER 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 == COMPUTER)

        {

            printf(\"COMPUTER removes %d stones from pile \"

                   \"at index %d\ \", moves.stones_removed,

                   moves.pile_index);

            whoseTurn = HUMAN;

        }

        else

        {

            printf(\"HUMAN removes %d stones from pile at \"

                   \"index %d\ \", moves.stones_removed,

                   moves.pile_index);

            whoseTurn = COMPUTER;

        }

    }

    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 == COMPUTER)

            printf(\"COMPUTER will win\ \");

        else

            printf(\"HUMAN will win\ \");

    }

    else

    {

        if (whoseTurn == COMPUTER)

            printf(\"HUMAN will win\ \");

        else

            printf(\"COMPUTER will win\ \");

    }

    return;

}

int main()

{

    int piles[] = {3, 5,7};

    int n = sizeof(piles)/sizeof(piles[0]);

        knowWinnerBeforePlaying(piles, n, COMPUTER);

        playGame(piles, n, COMPUTER);

         

    return(0);

}

Rules of NIM: The game board consists of three rows of sticks with three, five, and seven sticks in each row. A: | | | B: | | | | | C: | | | | | | | Two players
Rules of NIM: The game board consists of three rows of sticks with three, five, and seven sticks in each row. A: | | | B: | | | | | C: | | | | | | | Two players
Rules of NIM: The game board consists of three rows of sticks with three, five, and seven sticks in each row. A: | | | B: | | | | | C: | | | | | | | Two players
Rules of NIM: The game board consists of three rows of sticks with three, five, and seven sticks in each row. A: | | | B: | | | | | C: | | | | | | | Two players
Rules of NIM: The game board consists of three rows of sticks with three, five, and seven sticks in each row. A: | | | B: | | | | | C: | | | | | | | Two players

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site