Skip to main content

Game Theory with examples

Game Theory with examples

Introduction

In this article I will be covering problems related to two players game in which it is assumed that both players play optimally and we have to find out the winner of the game.

First we will look at the basic division of positions to winning and losing. Then we will see the Game of Nim and then see how it will be used to solve the Composite games .

Basic Division of positions to winning and losing

Problem Statement: Consider a simple game played by two players A and B . There are n stones on the table. Each player can pick 1 , 2 or 5 stones in each turn. Both players pick the stones alternately until the total number of stones left on the table is 0. The player unable to make the move will lose. Assuming that both the players play optimally, output the winner of the game.

Solution: As you can see positions 1 , 2 and 5 are winning positions since the player can pick up all the stones and other player will not be able to make a move . 0 is a losing position since the player can not pick any stones. Also 3 is a losing position as there is only two options that is to pick 1 stone or two stones . In both the cases the other player will pick the remaining stones and win.

So , from the following observation we can conclude that :

· Terminal positions are losing positions.

· If a player can move to a losing position the he is on a winning position.

· If a player can only move to a winning position than he is on a losing position.

This can be implemented using simple 1-D dp code.

  1. bool dp[n]; 
  2. dp[0] = dp[3] = 0; 
  3. dp[1] = dp[2] = dp[4] = dp[5]=1; 
  4. for(int i=6;i<=n;i++){ 
  5. if(dp[i-1] == 0 or dp[i-2] == 0 or dp[i-5] == 0) 
  6. dp[i]=1; 
  7. else 
  8. dp[i]=0; 

Here ‘1’ represent winning position and ‘0’ represent losing position.

The Game of Nim

Problem statement: There are k piles of stones . In each turn a player chooses a pile and takes out atleast one stone from it. If someone is unable to make a move , he loses .

Solution: Let there be n1,n2,n3….nk no. of stones in the 1,2,3….kth pile respectively. Now, a player is in a losing position if n1 xor n2 xor n3 xor…..nk = 0 . Else he is in a winning position .

Composite Games - Grundy numbers

Problem Statement(link):

Bob recently invented a new game. The game is played on an

chessboard. The rules of the game are:

  • Initially, there are k kings in some of the cells. A cell can contain more than one king.
  • A king is only allowed to move in one of three directions: up, left or up-left.
  • Some of the cells on the board might be damaged. A king is not allowed to move to a damaged cell.
  • A king cannot be moved outside the board.
  • There are two players in the game. They make moves alternately.
  • In a single move, a player must choose a single king and move it to a new cell.
  • If a player can't make a move, they lose the game.

Bob is playing the game with his friend Alice. Bob always makes the first move. Given the configuration of the board, Find the number of ways Bob can make the first move to ensure that he will win the game, assuming that both players will play optimally.

Solution: This is same as if we have k different chessboards each having exaxctly one king. Now , we can solve the k sub problems independently. Each sub problem can be solved using grundy numbers. Algorithm for finding grudy numbers is as follows:

  1. int grundyNumber(position pos) { 
  2. moves[] = possible positions to which I can move from pos 
  3. set s; 
  4. for (all x in moves) insert into s grundyNumber(x); 
  5. //return the smallest non-negative integer not in the set s;  
  6. int ret=0; 
  7. while (s.contains(ret)) ret++; 
  8. return ret; 

Through this algo we can compute grundy number for each position of the chess board. Now, this problem can be solved using the Game of Nim concept. The player is in a losing position if the xor of all values at the positions of kings is 0. Else , he is in a winning position.

You can find the code for above problem here.

How these two problems are equivalent?

  • In the game of nim we can take any number of stones out of the pile (atleast one) , same is the case with king problem as we can move to index with less grundy number than that index.
  • Also, if we move the king to any other index , that index must have grundy number less than the the previous index , which is same as decreasing number of stones from a pile.

Practice problem: SNACK01

Comments

Popular posts from this blog

Big O Notation — Time & Space Complexity in Ruby!

  Big O Cheat Sheet In this post, we will look at the Big O Notation both time and space complexity! For all our examples we will be using Ruby. What is Big O Notation? Big O notation is just a fancy way of describing how your code’s performance depends on the amount of data its processing. It allows us to calculate the worst possible runtime of an algorithm, or how long it takes the algorithm to complete. In other words, it informs us of how much it will slow down based on the input size. It’s usually talked about in two ways the  time complexity ( how long an algorithm takes )and  space complexity ( how much space an algorithm uses ) Let me put this graph here we can refer to it as we go on, it’s a graph showing the complexities compare against each other. Big O complexity chart Time Complexity Before we get try wrapping our heads around the different big O notations here is a nice table that I am borrowing to show how speed will slow down based on the size of the datas...

NT Part 2: Generating Primes, Prime Test, Prime Factorization

  Generating primes fast is very important in some problems. Let's cut to the chase and introduce Eratosthenes's Sieve. The main idea is the following. Suppose we want to find all primes between 2 and 50. Iterate from 2 to 50. We start with 2. Since it is not checked, it is a prime number. Now check all numbers that are multiple of    except  2. Now we move on, to number 3. It's not checked, so it is a prime number. Now check all numbers that are multiple of  ,   except  3. Now move on to 4. We see that this is checked - this is a multiple of 2! So 4 is not a prime. We continue doing this. Here's the implementation. #include <stdio.h> int primechk [ 21000 ] ;   void preprocess ( void ) { int i, j ; for ( i = 2 ; i <= 20000 ; i ++ ) { primechk [ i ] = 1 ; } for ( i = 2 ; i <= 20000 ; i ++ ) { if ( primechk [ i ] == 1 ) { for ( j = 2 ; i * j <= 20000...

NT Part 6: Sieve of Eratosthenes(Details)

  About 2300 years ago from today, famous Greek mathematician Euclid proved that there are an infinite number of prime numbers. Since then people have been searching for these prime numbers. In 1849, one of the greatest mathematician of all time, Carl Fredrick Gauss, had identified almost all of the prime numbers within the first 3 hundred thousand whole numbers. In the age of computers, we can find large prime numbers in the blink of an eye. But to do that, we need to know a bit of programming and a 2000 year old algorithm. By the end of this tutorial, you will be able to figure out a solution on your own to Gauss’s problem. What is a Prime Number? A prime number is an integer number that is divisible by 1 and the number itself only. For example, 7 is divisible by 1 and 7 only. But 6 is not a prime number because 6 is be divisible by 2 and 3 as well. It is worth mentioning that 1 itself is not a prime number. Now if you are asked to determine if a number is a prime number, you can...