A modified game of Nim in C ?

The modified game of Nim is a strategic game played on an array where two players take turns removing elements based on divisibility rules. This game determines the winner based on optimal play and starting advantage.

Game Rules

In this modified Nim game −

  • Player A removes elements divisible by 3
  • Player B removes elements divisible by 5
  • Elements divisible by both 3 and 5 (i.e., divisible by 15) can be removed by either player
  • Player A moves first
  • The last player to make a move wins

Syntax

// Count moves for each player
int movesA = 0, movesB = 0, movesBoth = 0;
// Apply optimal strategy based on counts

Strategy

The optimal strategy considers three types of elements −

  • movesA: Elements only Player A can remove (divisible by 3, not 5)
  • movesB: Elements only Player B can remove (divisible by 5, not 3)
  • movesBoth: Elements either player can remove (divisible by 15)

Since Player A goes first, they have a slight advantage when competing for shared elements.

Example: Modified Nim Game Implementation

#include <stdio.h>

int main() {
    int arr[] = {1, 5, 75, 2, 65, 7, 25, 6};
    int n = sizeof(arr) / sizeof(arr[0]);
    int movesA = 0, movesB = 0, movesBoth = 0;
    
    printf("Array elements: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("<br><br>");
    
    /* Count moves for each player */
    for (int i = 0; i < n; i++) {
        if (arr[i] % 3 == 0 && arr[i] % 5 == 0) {
            movesBoth++;
            printf("%d can be removed by either player (divisible by 15)<br>", arr[i]);
        }
        else if (arr[i] % 3 == 0) {
            movesA++;
            printf("%d can be removed by Player A (divisible by 3)<br>", arr[i]);
        }
        else if (arr[i] % 5 == 0) {
            movesB++;
            printf("%d can be removed by Player B (divisible by 5)<br>", arr[i]);
        }
    }
    
    printf("\nPlayer A exclusive moves: %d<br>", movesA);
    printf("Player B exclusive moves: %d<br>", movesB);
    printf("Shared moves: %d<br>", movesBoth);
    
    /* Determine winner using optimal strategy */
    if (movesBoth == 0) {
        /* No shared elements - simple comparison */
        if (movesA >= movesB) {
            printf("\nPlayer A wins!<br>");
        } else {
            printf("\nPlayer B wins!<br>");
        }
    } else {
        /* With shared elements, A gets first-move advantage */
        if (movesA + (movesBoth + 1) / 2 >= movesB + movesBoth / 2) {
            printf("\nPlayer A wins!<br>");
        } else {
            printf("\nPlayer B wins!<br>");
        }
    }
    
    return 0;
}
Array elements: 1 5 75 2 65 7 25 6 

75 can be removed by either player (divisible by 15)
5 can be removed by Player B (divisible by 5)
65 can be removed by Player B (divisible by 5)
25 can be removed by Player B (divisible by 5)
6 can be removed by Player A (divisible by 3)

Player A exclusive moves: 1
Player B exclusive moves: 3
Shared moves: 1

Player B wins!

How the Strategy Works

The winning condition considers that when there are shared elements (divisible by 15), Player A can claim (movesBoth + 1) / 2 of them due to moving first, while Player B gets movesBoth / 2. The player with the higher total move count wins.

Conclusion

The modified Nim game combines divisibility rules with optimal play strategy. Player A's first-move advantage is crucial when shared elements exist, making the game more complex than simple move counting.

Updated on: 2026-03-15T11:20:47+05:30

751 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements