Download presentation
Presentation is loading. Please wait.
1
AI – Week 12 AI and Games (4) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk http://scom.hud.ac.uk/scomtlm/cha2555/
2
AI in Games To build AI into a game one method is to: u Design a representation of the world (game) state u Design a static state evaluation function u Design Move application and Move generation simulation functions (possibly use an explicit representation for moves) u Design a search algorithm that searches through states to find the optimum move - for two player/turns/perfect info games, a good search techniques is Minimax
3
Artform Research Group Minimax Design – WITH a-b pruning n Layer with Fox to move: u Generate Next Fox Move u Initialise Level Value to Worst u Minimax with goose to move u Eventually get value for Fox move u If it is better than current stored move then change current stored best move u check whether it is worth going on - If it is not worth it then end procedure, otherwise backtrack (generate next fox move) n Layer with Goose to move - same
4
Minimax Design – WITH a-b pruning % RECURSIVE case - we are NOT at the bottom of the search tree % Board = BOARD, fox's turn, Depth = current depth of search minimax(Board,fox,Depth) :- Depth1 is Depth+1, apply(x(SX,SY,f), x(FX,FY,V), Board,Board1), assert(level(Depth1,-10000,_)), minimax(Board1,goose,Depth1), retract(level(Depth1,ValBoard1,_)), level(Depth,ValBoard,Move), % if the new value recorded is better (less) that the % current value of this node then record new value ValBoard1 < ValBoard, retract(level(Depth,ValBoard,Move)), assert(level(Depth,ValBoard1, [x(SX,SY,f), x(FX,FY,V)])), % check whether it is worth going on by checking ValBoard1 % against the current Depth-1 value if there is one. If this is the % case then make sure minimax does not continue searching. Depth2 is Depth-1, level(Depth2,ValueAbove,_), ifthenelse(ValBoard1 =< ValueAbove,true,fail),!. Goose to go Fox to go Board ValBoard Board1 ValBoard1 Move x(SX,SY,f) TO x(FX,FY,V) Depth Depth+1
5
Another Example of AI in Games: Chess Same architecture as the Fox and Goose 1. Board (state) representation 2. Board (state) evaluation 3. Move application 4. Move generation 5. Move choice
6
Representation of Game State / Board [x(1,8,-60), x(2,8,-40), x(3,8,-50), x(4,8,-90), x(5,8,-1000), x(6,8,-50), x(7,8,-40), x(8,8,-60), x(1,7,-10), x(2,7,-10), x(3,7,-10), x(4,7,-10), x(5,7,-10), x(6,7,-10), x(7,7,-10), x(8,7,-10), x(1,6,0), x(2,6,0), x(3,6,0), x(4,6,0), x(5,6,0), x(6,6,0), x(7,6,0), x(8,6,0), x(1,5,0), x(2,5,0), x(3,5,0), x(4,5,0), x(5,5,0), x(6,5,0), x(7,5,0), x(8,5,0), x(1,4,0), x(2,4,0), x(3,4,0), x(4,4,0), x(5,4,0), x(6,4,0), x(7,4,0), x(8,4,0), x(1,3,0), x(2,3,0), x(3,3,0), x(4,3,0), x(5,3,0), x(6,3,0), x(7,3,0), x(8,3,0), x(1,2,10), x(2,2,10), x(3,2,10), x(4,2,10), x(5,2,10), x(6,2,10), x(7,2,10), x(8,2,10), x(1,1,60), x(2,1,40), x(3,1,50), x(4,1,90), x(5,1,1000), x(6,1,50), x(7,1,40), x(8,1,60) ] NB ORDER of co-ordinate data in list is NOT RELEVANT
7
Board Evaluation % Add up values of pieces. Take into account advanced pawns. % EXAMPLE OF HOW TO TAKE INTO ACCOUNT FEATURES ---- board_evalX([x(X,6,10)|R],S,VAL,white) :- VALA is VAL+ 20, board_evalX(R,S,VALA,white),!. board_evalX([x(X,7,10)|R],S,VAL,white) :- VALA is VAL+ 30, board_evalX(R,S,VALA,white),!. % THIS IS THE MAIN PART ------ board_evalX([x(X,Y,W)|R],S,VAL,white) :- VALA is VAL+W, board_evalX(R,S,VALA,white),!. board_evalX([],S,S,_) :- !.
8
Chess: Move Generation n Chess Move generator **outline** apply_white( x(X,Y,Z), x(X1,Y1,Z1), B_IN, B_OUT) :- member(x(X,Y,Z), B_IN), member(x(X1,Y1,Z1),B_IN), check_geometry_white(Z,X,Y,X1,Y1), \+ something_in_way_white(Z,X,Y,X1,Y1,B_IN), % make move remove(x(X,Y,Z), B_IN, B1), remove(x(X1,Y1,Z1), B1, B2), B_OUT = [x(X1,Y1,Z),x(X,Y,0)|B2].
9
Example of Geometry check For a white knight.. check_geometry_white(40,X,Y,X1,Y1) :- X1 is X+1, Y1 is Y+2. check_geometry_white(40,X,Y,X1,Y1) :- X1 is X+1, Y1 is Y-2. check_geometry_white(40,X,Y,X1,Y1) :- X1 is X-1, Y1 is Y+2. check_geometry_white(40,X,Y,X1,Y1) :- X1 is X-1, Y1 is Y-2. check_geometry_white(40,X,Y,X1,Y1) :- X1 is X+2, Y1 is Y+1. check_geometry_white(40,X,Y,X1,Y1) :- X1 is X+2, Y1 is Y-1. check_geometry_white(40,X,Y,X1,Y1) :- X1 is X-2, Y1 is Y+1. check_geometry_white(40,X,Y,X1,Y1) :- X1 is X-2, Y1 is Y-1.
10
Another Game – Connect 3 Use x-y co-ordinates again.. [ x(1,6,0), x(2,6,0), x(3,6,0), x(4,6,0), x(1,5,0), x(2,5,0), x(3,5,0), x(4,5,0), x(1,4,0), x(2,4,0), x(3,4,0), x(4,4,0), x(1,3,0), x(2,3,0), x(3,3,0), x(4,3,0), x(1,2,0), x(2,2,0), x(3,2,0), x(4,2,0), x(1,1,0), x(2,1,0), x(3,1,0), x(4,1,0), ]).
11
Connect 3 (or Connect N): Move Generation n Move generator **outline** apply_red( x(X,Y,0), B_IN, B_OUT) :- member(x(X,Y,0), B_IN), check_geometry(X,Y,B_IN), % make move remove(x(X,Y,0), B_IN, B1), B_OUT = [x(X,Y,red)|B1].
12
Connect 3 Geometry Check check_geometry(X,Y,B_IN) :- Y = 1. check_geometry(X,Y,B_IN) :- Y = 2, member(x(X,1,Piece), B_IN), \+ (Piece = 0). check_geometry(X,Y,B_IN) :- Y = 3, member(x(X,1,Piece), B_IN), member(x(X,2,Piece2), B_IN), \+ (Piece = 0), \+ (Piece2 = 0). check_geometry(X,Y,B_IN) :- Y = 4, member(x(X,1,Piece), B_IN), member(x(X,2,Piece2), B_IN), member(x(X,3,Piece3), B_IN), \+ (Piece = 0), \+ (Piece2 = 0). \+ (Piece3 = 0).
13
Chess: Minimax with alpha-beta pruning minimax(BD,black,D,LIMIT) :- D1 is D+1, getpiece(PIECE), apply_black(x(SX,SY,PIECE), x(FX,FY,V), BD,BD1), assert(level(D1,-10000,_)), minimax(BD1,white,D1,LIMIT), retract(level(D1,ValBD1,_)), level(D,ValBD,Move), ValBD1 < ValBD, retract(level(D,ValBD,Move)), assert(level(D,ValBD1,[x(SX,SY,PIECE), x(FX,FY,V)])), D2 is D-1, level(D2,Above,_), ifthenelse(ValBD1 =< Above,true,fail),!. % for Alpha-Beta, check whether it is worth going on by checking ValDB1 % against the current D-1 value if there is one. If this is the % case then make sure minimax does not continue searching.
14
Conclusions (AI and Games) Using the basic structure: n board representation, n move generation / application, n board evaluation and n best move search (minimax), you can encode many turn-based games very efficiently. The best move search algorithm makes the computer appear intelligent.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.