Skip to content

Commit dfa3749

Browse files
committed
Merge branch 'master' of https://github.com/trueagi-io/metta-wam
2 parents 307da89 + 46aeff3 commit dfa3749

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

examples/games/GreedyChess.metta

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
; first created at the outset of each side's move based upon the chessboard list.
6666
;
6767
; Possible moves are evaluated by rapidly adding and removing these 'square' atoms. As a design
68-
; consideration use of such atoms harmonizes well with the 'atomic' structure of Metta which is
68+
; consideration use of such atoms harmonizes well with the 'atomic' structure of MeTTa which is
6969
; not really the same as LISP type list processing. At the start of the move, the transient 'square' atoms
7070
; are loaded into atomspace and at the conclusion of the move the 'square' atoms are used to
7171
; recreate the chessboard. Take care that after "envisioning" moves you move the 'square' atoms
@@ -81,7 +81,7 @@
8181
;
8282
; During programming tail recursion functions were often created, but it was discovered that these
8383
; were often faster instead using metta calls to 'match'. The original functions have sometimes been retained for
84-
; comparison. In general the defining features of Metta are the use of atomspace, 'match' 'superpose' and
84+
; comparison. In general the defining features of MeTTa are the use of atomspace, 'match' 'superpose' and
8585
; 'collapse'. Occasionally it is wise to use 'xor' in lieu of 'or' (if possible) to avoid unexpected
8686
; nondeterministic behavior. In general it is not necessary to think in GOFAI chaining terms (forward, backward).
8787
;
@@ -1353,6 +1353,10 @@
13531353
(match &self (square $x1 $y1 g $AI_rank)
13541354
; if piece under threat by human
13551355
(if (== (take_dest ($x1 $y1) s) True)
1356+
; if square not covered by a different AI piece that could in turn capture attacking human piece,
1357+
(if (xor (== (take_dest ($x1 $y1) g) False)
1358+
; except if threatened piece is a queen. If queen is threatened play defense regardless...
1359+
(and (== (take_dest ($x1 $y1) g) True) (== $AI_rank q)))
13561360
; find all empty squares to flee to...
13571361
(match &self (square $x2 $y2)
13581362
; if piece can escape to an empty square
@@ -1374,7 +1378,38 @@
13741378
(empty)
13751379
)
13761380
)
1377-
)))
1381+
(empty)))))
1382+
1383+
; This variation will play defense if high ranking pieces are ever threatened by a pawn.
1384+
(= (playdefense_match $AI_rank)
1385+
; find all AI pieces
1386+
(match &self (square $x1 $y1 g $AI_rank)
1387+
; find human pawns...
1388+
(match &self (square $PAWNx1 $PAWNy1 s p)
1389+
; if there is a capture route for the human pawn to capture higher rank AI piece
1390+
(if (== (clear_route ($PAWNx1 $PAWNy1 s p) ($x1 $y1)) True)
1391+
; find all empty squares to flee to...
1392+
(match &self (square $x2 $y2)
1393+
; if piece can escape to an empty square
1394+
(if (== (clear_route ($x1 $y1 g $AI_rank) ($x2 $y2)) True)
1395+
; if AI piece is not exposed in new position
1396+
(let*
1397+
( ;envision the move to empty square, see if exposed after moves
1398+
($__1 (move_piece ($x1 $y1 g $AI_rank) ($x2 $y2)))
1399+
($exposed (take_dest ($x2 $y2) s))
1400+
; make sure king safe
1401+
($king_status (checkking g))
1402+
($__2 (reset_pieces ($x1 $y1 g $AI_rank) ($x2 $y2)))
1403+
)
1404+
(if (and (== $exposed False) (== $king_status False))
1405+
; success!
1406+
(($x1 $y1 g $AI_rank) ($x2 $y2))
1407+
; whoops, the piece is exposed to capture
1408+
(empty)))
1409+
(empty)
1410+
)
1411+
)
1412+
(empty)))))
13781413

13791414
;*************************************************************************
13801415
; Function: takehighestopen_scored
@@ -2552,13 +2587,15 @@
25522587
(progn
25532588
; Retrieve the source box details.
25542589
(let $h (return_entire_box $f) ())
2590+
; Get source box color, assign NIL if no piece
2591+
(let $source_color (if (== (size-atom $h) 4) (nth 3 $h) NIL) ())
25552592
; Retrieve the destination box details.
25562593
(let $i (return_entire_box $g) ())
25572594
(let $dest_color (if (== (size-atom $i) 4)
25582595
(nth 3 $i)
25592596
NIL) ())
2560-
; Check there is a piece to move.
2561-
(if (and (== (size-atom $h) 4)
2597+
; Check there is a human piece to move.
2598+
(if (and (== $source_color s)
25622599
(xor
25632600
; OK to move to open square size 2...
25642601
(== (size-atom $i) 2)

0 commit comments

Comments
 (0)