game theory intro ak

19
                   Introduction to                       Combinatorial (not economics)                   Game Theory Anil Kishore ] student, IIIT-Hyderabad, India.

Upload: mir-wasi-ahmed

Post on 08-Mar-2015

700 views

Category:

Documents


3 download

DESCRIPTION

Uploaded from Google Docs

TRANSCRIPT

Page 1: Game Theory Intro Ak

   

                  Introduction to                      Combinatorial (not economics)    

               Game Theory

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

Page 2: Game Theory Intro Ak

   

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 ?

Page 3: Game Theory Intro Ak

   

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 ?

Page 4: Game Theory Intro Ak

   

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 :)

Page 5: Game Theory Intro Ak

   

● 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

Page 6: Game Theory Intro Ak

   

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” );

Page 7: Game Theory Intro Ak

   

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; }

Page 8: Game Theory Intro Ak

   

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 ?

Page 9: Game Theory Intro Ak

   

Solution : Take away 1­3­4 

● 11 is winning position

Page 10: Game Theory Intro Ak

   

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 ?

Page 11: Game Theory Intro Ak

   

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

Page 12: Game Theory Intro Ak

   

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 ?

Page 13: Game Theory Intro Ak

   

// 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

Page 14: Game Theory Intro Ak

   

● 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

Page 15: Game Theory Intro Ak

   

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” );

Page 16: Game Theory Intro Ak

   

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 )

Page 17: Game Theory Intro Ak

   

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

Page 18: Game Theory Intro Ak

   

   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 ?

    

Page 19: Game Theory Intro Ak

   

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 ! ☺