Game Theory Intro Ak

Preview:

DESCRIPTION

Uploaded from Google Docs

Citation preview

   

                  Introduction to                      Combinatorial (not economics)    

               Game Theory

[ Anil Kishore ]student, IIIT­Hyderabad, India.

   

General Game Setting

● Two players A and B ● Rules of the game :

– Possible moves a player can take in his/her turn– Winning condition

● Impartial : A and B have same set of moves● Both play 'optimally'

If A starts first, who will win ?

   

First Game : Bowling Pins

● N bowling pins arranged in a row● Possible Moves

– Hit exactly one pin– Hit in middle of any 

two adjacent pins

● Winner : One who  knocks down the last pin● Given N, who wins if A takes the first move ?

   

Solution : First Game – Bowling Pins

● A can knock down the middle pin(s) ( 1 for odd N & 2 for even N ) and make two independent games of same size

● Now, just mimic what B does, in the other half.

● A always wins :)

   

● Alice and Bob play the following game : There are N piles of stones with Si stones in the ith pile. Piles are numbered from 1 to N. Alice and Bob play alternately, with Alice starting. In a turn, the player chooses any pile i which has atleast i stones in it, and removes exactly i stones from it. The game ends when there is no such pile. The player who plays last wins the game. Assuming Alice and Bob play optimally, who will win the game? 

Stone Game : RESN04 on CodeChef

   

Solution : Stone Game 

● Idea:  Exactly ( S[i] / i )  moves can be made at pile i

● Total moves in whole game T = Sum over i (  S[i] / i )

● Nothing A or B can do clever. Just make those T moves alternatively

● Depends only on the parity of T

int nummoves = 0;

for (int i=1; i<=N; i++){

nummoves += piles[i]/i;}

puts( nummoves%2 ==0 ? ”BOB” : ”ALICE” );

   

Win­Lose Bruteforce Strategy

1.All terminal positions are losing.

2.If a player is able to move to a losing position then he is in a winning position.

3.If a player is able to move only to the winning positions then he is in a losing position.

boolean isWinning( position X ) { for (all positions Y that I can go to) if ( !isWinning(Y) ) return true; return false; }

   

Game : Take away 1­3­4

● There are n coins. When it is a player's turn he can take away 1 or 3 or 4 coins. The player who takes the last one away is declared the winner (in other words, the player who can not make a move is the loser).

● Question : Is n = 11 a winning or losing position ?

   

Solution : Take away 1­3­4 

● 11 is winning position

   

Game : Take away 1 to K

● There are n coins. When it is a player's turn he can take away at least 1 and at most K coins. The player who takes the last one away is declared the winner (in other words, the player who can not make a move is the loser).

● Question : Though exhaustively enumerating WL      works, can we find some pattern for L positions ?

   

Solution : Take away 1 to K

LOSE

W IN

●In our problem, the property P can be : mod (K+1) = 0

● From X ( X mod (K+1) ==0 ), all moves lead to a Y ( Y mod (K+1) != 0 )

● From X ( X mod (K+1) !=0 ), there exsits a move to Y ( Y mod (K+1) = 0 )

● So, N is a losing position if N % (K+1) == 0 , else its winning

   

The Game of NIM

● Given sizes of N heaps of stones

● Possible move : Pick any heap and remove non­zero stones from it

● Winner : One who removes the last stone

● Given int Size[n] , who wins if A starts first ?

   

// read input Size[]

r = 0; for(i=0;i<n;i++) r = r ^ Size[i]; puts( r!=0 ? "A Wins" : "B Wins");

Solution : NIM

LOSE

W IN

   

● Almost same as previous Stone Game except for the bold part

● Alice and Bob play the following game : There are N piles of stones with Si stones in the ith pile. Piles are numbered from 1 to N. Alice and Bob play alternately, with Alice starting. In a turn, the player chooses any pile i which has atleast i stones in it, and removes k*i stones from it, for any positive integer k and of course k*i <= Si. The game ends when there is no such pile. The player who plays last wins the game. Assuming Alice and Bob play optimally, who will win the game? 

New Stone Game

   

Solution : New Stone Game 

● Idea:  From pile i, we can remove 1*i or 2*i or 3*i .... or ( S[i] / i )*i stones

● Looks exactly like NIM with heap size ( S[i] / i )

int r = 0;

for (int i=1; i<=N; i++){

r ^= piles[i]/i;}

puts( r==0 ? ”BOB” : ”ALICE” );

   

THE GAME : Quark'10 Bits Goa

● Tom and Hanks play the following game. On a game board having a line of squares labelled from 0,1,2 ... certain number of coins are placed with possibly more than one coin on a single square. In each turn a player can move exactly one coin to any square to the left i.e, if a player wishes to remove a coin from square i, he can then place it in any square which belongs to the set (0,1, ... i­1) . Given the description of the board and also assuming that Tom always makes the first move you have to tell who wins the game (Assuming Both play Optimally). 

● Board size N ( 1<= N <= 105 ). 

● Number of coins on each square : X ( 0 <= X <= 105 )

   

scanf("%d",&n); r=0;

for(i=1;i<=n;i++){

scanf("%d",&x);if(x&1) r ^= i;

}

puts(r==0?"Hanks Wins":"Tom Wins");

Solution : THE GAME ( QCJ6 in CodeChef )

● Idea: Each stone at position P, corresponds to heap of size P in NIM  r = x ^ x ^ x ^ x . . . . . ^ x ( n times ) 

● n even : r = 0● n odd   : r = x

   

   Take Home Games   (Not some game category :p, try these out in your rooms )

● Take away 1 + Primes ( = {1,2,3,5,7,11,...} )

● Identify a pattern in Take away {1,3,8}

● Ordered Nim :  The heaps are numbered 1 through N , and a player can only remove stones from a heap if all the lower­numbered heaps are empty.

● Two piles with p & q stones respectively. A move can be   1.) Take one stone from a pile, 2.) Take one stone from each pile, 3.) Move a stone from one pile to other. 

Winner : Who takes the last stone.  A wins or B wins ?

    

   

http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=algorithmGames

http://www.cut­the­knot.org/Curriculum/index.shtml#games● You can see a variety of games here and also play with computer :)

● TopCoder Tutorial

● http://www.madras.fife.sch.uk/maths/games/

● CodeChef Tutorial on Wythoff's Game http://www.codechef.com/wiki/tutorial­game­theory

References & further playing

Thats all folks... Happy Gaming ! ☺