Open In App

Game of Chocolates | Wythoff’s Game

Last Updated : 20 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Bunty and Dolly are playing a game, described as follows:
There are two boxes having A and B number of chocolates respectively. Both can eat L (L ? 1) chocolates from any one box or L chocolates from both boxes in one move. They play the game alternatively and the last one to eat the chocolate will be the winner.

You have to help Bunty in deciding who should play first such that Dolly is always the winner. Assume that both players play optimally.

Note: This game is also known as Wythoff’s Game.

Examples:

Input: A = 1 and B = 2
Output: Bunty
Explanation: If Bunty starts first, all the possible states after Bunty eats chocolates are (0, 2), (1, 1), (1, 0).  These are all wining states for Dolly

Input: A = 1 and B = 3
Output: Dolly

Any state can be uniquely identified using two integers (n, m) which are the number of chocolates in the boxes. Now each state can be classified into two categories: cold state (the player whose turn it is will lose) and hot state (the player whose turn it is will win). The classification can be done as follows:

  • (0, 0) is a cold position
  • Any state from which a cold state can be reached in a single move is a hot state.
  • The state from which any possible move leads to a hot state is a cold state.

The task is to check if the given state (A, B) is a hot state or cold state and based on that the one to start will be Dolly or Bunty respectively.

Follow the below steps to solve the problem:

  • If A > B, then swap A and B, so that, B is always greater than or equal to A.
  • Initialize k = B – A and d = 1 + sqrt(5).
  • Set, d = d/2, and again d = d*k.
  • Make a variable c = d after typecasting it into int.
  • Now, if a and c are equal then return 0 else return 1.
  • If the answer is 0 then print “Dolly” else print “Bunty”.

Below is the implementation of the above approach:

C++
// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Function to decide who should play first
bool game(int a, int b)
{
    // Swap the value
    if (a > b)
        swap(a, b);

    int k = b - a;
    long double d = 1 + sqrt(5);

    d /= 2;
    d *= k;

    int c = (int)d;

    // Return answer
    return (a == c) ? 0 : 1;
}

// Driver Code
int main()
{
    int A = 1, B = 2;

    // Function call
    cout << (game(A, B) ? "Dolly" : "Bunty") << endl;

    return 0;
}
Java
import java.util.*;

class GFG {

    // Function to decide who should play first
    static boolean game(int a, int b)
    {
        // Swap the value
        if (a > b)
        {
            int t=a;
            a=b;
            b=t;
        }
            
        int k = b - a;
        double d = (1 + Math.sqrt(5)) / 2.0;

        d *= k;

        int c = (int)d;

        // Return answer
        return (a == c) ? false : true;
    }

    // Driver Code
    public static void main (String[] args) {

        int A = 4, B = 7;

        // Function call
        System.out.println((game(A, B) ? "Dolly" : "Bunty"));
    }
}
Python3
import math
class Solution:
    # Function to decide who should play first
    def game(self,A, B):
        golden_ratio = (1 + math.sqrt(5)) / 2
        nimber = math.floor(abs(B - A) * golden_ratio)
        if nimber == min(A,B):
            return "Bunty"
        else:
            return "Dolly"
# Test function
A = 1
B = 2
print(Solution().game(A,B))

# Test function
A = 1
B = 3
print(Solution().game(A,B))

# Test function
A = 4
B = 7
print(Solution().game(A,B))
C#
using System;

public class GFG {

    // Function to decide who should play first
    static bool game(int a, int b)
    {
        // Swap the value
        if (a > b) {
            int t = a;
            a = b;
            b = t;
        }

        int k = b - a;
        double d = (1 + Math.Sqrt(5)) / 2.0;

        d *= k;

        int c = (int)d;

        // Return answer
        return (a == c) ? false : true;
    }

    static public void Main()
    {

        // Code
        int A = 4, B = 7;

        // Function call
        Console.WriteLine((game(A, B) ? "Dolly" : "Bunty"));
    }
}
Javascript
function game(a, b)
{
    // Swap the value
    if (a > b) {
        let temp = a;
        a = b;
        b = temp;
    }
    let k = b - a;
    let d = (1 + Math.sqrt(5)) / 2;
    d *= k;
    let c = Math.floor(d);
    
    // Return answer
    return (a == c) ? false : true;
}

let A = 4;
let B = 7;

// Function call
console.log(game(A, B) ? "Dolly" : "Bunty");

Output:

Bunty
Dolly
Bunty

Time Complexity: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads