Você está na página 1de 23

Play chess with god

Arun Pratik
th
25 February 2008
Agenda
● Introduction
● Evaluating a position
● Search Algorithms
● Minimax Algorithm
● Alpha-Beta Pruning Algorithm
● Board Representation
● Conclusion
● References
Introduction
● Structure of the game
● Opening, Middle Game, End Game
● Opening

Large number of choices. How to limit?


Introduction : Middle Game
● Middle Game

Decide strategy. Check for tactical possibilities.


Evaluate a position ?
Introduction : End Game
● End Game

Techniques. Calculations?
Introduction : Making the machine
play
● Add a mechanism to allow the machine to eval-
uate any given position
● Thousands of combinations of moves
● Calculate ??
● Search through!
● Represent the 8x8 board in memory : represent
the hundreds of sub-variations for a move
● The challenge : Speed and Efficiency
Representing a game
● Game Trees 1.e4 White

e.g : A chess game –


1..e5 1..d5 Black
White plays 1.e4
Variations @ Move 1 for Black : W
1...e5 or 1...d5. If Black plays 2.Nf3 2.Nc3 2.d4 2.e5
1..d5
Variations @ Move 2 for White :
1..e5 1..d5 B
2.d4 or 2.e5. If White plays 2.d4
Variations @ Move 2 for Black : Standard algebraic chess notation:
Assume each row in chess board to be
2... ed or 2...c6. denoted as “a-h” and each column as “1-
8”.
Game Trees
● Node : Position of the pieces in board
● Leaf Node : Final result of the game
● Ply : A move made by a player in one turn
● For chess, a ply is half a move
● Complete Game Tree :
● Tree with leaves present in the last level only
● Represents the game in full. For chess??
● Given a complete game tree, How to find the
“winning” variations?
Solving a game tree

Player 1

Player 2

Player 1

Player 1 wins Player 2 wins


Game tree : The Algorithm
For each level from n-1 to 1
For each node in the level
// check the child nodes
case 1: /* one of the child nodes is a win
/* for the opposing player
/* return “win for opponent”
case 2: /* all the child nodes are a win
/* for the current player
/* return “win for current player”
case 3: /* for all other cases :
/* return “draw”
End of For#2
End of For#1
Evaluation Function
● Assign value to a position (node in game tree)
● Parameters: How to decide?
● Material Balance : Total value of pieces a side has
● Mobility and Board Control
● Development : Are all pieces on board playing?
● King Safety : Is the king out of attack?
● Take a weighted sum of all factors
Eval(posn) = value1*Material Bal+value2*Mobility+....
Some characteristics of chess
● 2 player game
● Zero sum : Gains for white = Losses for black
● Discrete : All game states and decisions are
discrete values
● Finite : Finite number of decisions and states
● Deterministic : No “chance factor”
● Perfect decision : Both players can see the state,
and each decision is made sequentially
Minimax Algorithm
● Assume 2 players : MIN and MAX
● MAX : Maximize the “value”
● MIN : Minimize the “value”
● Value = +INFINITY , MAX wins and
Value = -INFINITY, MIN wins
● Used with a parameter “depth”
● Recursive definition
Minimax Algorithm
/* Find the best move for white
* Looks for moves “depth” plies ahead */
Function max(int depth)
best = INFINITY
if(depth <= 0) return Eval(current_posn)

For each possible move 'm'


val_black = min(depth-1)
// get the worst move for black
if(val_black > best)
best = val_black
end if
End For
return best
End Function
Negamax Algorithm
/* Instead of 2 functions : min and max, define 1
* Optimized since min = -max */
Function negamax(int depth)
best = INFINITY
if(depth <= 0) return Eval(current_posn)

For each possible move 'm'


val_opp = -negamax(depth-1) // -ve sign!
// get the worst move for opponent
if(val_opp > best)
best = val_opp
end if
End For
return best
End Function
Alpha-beta pruning algorithm
● Optimize by minimizing the number of choices
● Keep track of the best move (stored as alpha)
● Store the opponent's worst move as beta
● Discard away a node when:
● Value < alpha because we have a better move
● Value >= beta
● Initially,
● Alpha = -INFINITY, beta = +INFINITY for white
Alpha-beta pruning algorithm
/* call as AlphaBeta(depth, -INFI, +INFI) */
Function AlphaBeta(depth, alpha, beta)
if(depth == 0) return Eval(current_posn)

For each possible move 'm'


value = -AlphaBeta(depth-1, -alpha, -beta)
// get the worst move for black
if(value >= beta) return beta end if
if(value > alpha) alpha = value end if
End For
return best
End Function
//
Alpha-beta pruning example

Item 1 : Watch Item 1 : A Pen

Item 2 : An ipod Item 2 : Laptop


alpha = Watch Pen < Watch
Discard
Bag 1 Bag 2

● You can get only one item off any one bag. You
choose the bag, opponent choses the item to give.
● Your Aim : Choose the most profitable bag?
● Opponent's Aim : Give you the cheapest item.
Board Representation
● Array Based
● 8x8 array with each square containing value
represents a piece
● Efficiency? Improvement ?
● 0x88 Method
● Use an array of 16x8 = 128 squares
● 2 adjacent boards, main board on left
● ANDing a square with 0x88 tells if the square in
inside the board
● Difference between 2 squares' : gives a relation
between the squares
Board Representation : Huffman
Coding
● Represent each piece with certain bit patterns
● Most used piece : lesser bits
Empty square = 0 ● c represents the color of the
piece
Pawn = 10c
● complete board can be
Bishop = 1100c represented with 23 bytes
Knight = 1101c ● CPU intensive, but final
Rook = 1110c memory footprint is very less
Queen = 11110c ● Used to store opening books
King = 11111c
Conclusion
● Took a look at how the game is organized
● How to play the Opening, Middle Game and End
Game
● Understood Game Trees and how to solve them
● Realized the concept behind Minimax, Negamax
and Alpha-beta search algorithms
● Finally we learned how to represent the board in
main memory
References
● http://www.seanet.com/~brucemo/topics/topics.ht
m
● http://ce.sharif.edu/~ahmadinejad/gametree/
● http://www.gamedev.net/reference/programming/
features/chess1/
● Title “Play Chess With God” was coined by Ken
Thompson, Bell Labs.
http://plan9.bell-labs.com/who/ken/
Thank You

Created with OOo 2.4 Impress / Arch Linux

Você também pode gostar