This repository was archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_state.lisp~
More file actions
89 lines (58 loc) · 2.65 KB
/
game_state.lisp~
File metadata and controls
89 lines (58 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(defclass game_state ()
;;; Represents whether or not if each space on the board is ocupied by a piece.
((all_pieces :accessor all_pieces :initarg :all_pieces :initform (blank-bb))
;; Current Game-state: an array type, that holds all the pieces for each
;; player by name (ex. Wp Bk...) according to thier respective positions
;; on the board.
(game_state :accessor game_state :initarg :game_state :initform (make-array 64 :element-type 'string :initial-element "__"))
;; Players
(player1 :accessor player1 :initarg :player1 :initform (new-player1))
(player2 :accessor player2 :initarg :player2 :initform (new-player2))))
#|---------------------------
FUNCTION DEFINITIONS
----------------------------|#
;==;Update-all-pieces()
;;; Desc: Updates all_pieces bitboard to represent all currently held positions.
;; Calls functions: Update-player-pieces {player}
;; clear-bb {bb-util}
(defmethod update-all-pieces ((gs game_state))
(clear-bb (all_pieces gs))
(update-player-pieces (player1 gs))
(update-player-pieces (player2 gs))
(loop for i from 0 to 63 do
(setf (all_pieces gs) (bit-ior (player_pieces (player1 gs)) (player_pieces (player2 gs))))))
;==;Update-game-state()
;;; Desc: For both players iterate through each piece's pos bb and for
;; each bit that == 1 set the corresponding index of game-state to
;; the pieces name.
;; Calls functions: update-all-pieces {game}
(defmethod update-game-state ((gs game_state))
(update-all-pieces gs)
;; Player1 (white)
(loop for i from 0 to 5 do
(loop for j from 0 to 63 do
(if (eq (bit (pos (nth i (pieces (player1 gs)))) j) 1)
(setf (aref (game_state gs) j) (name (nth i (pieces (player1 gs))))))))
;; Player2 (black)
(loop for i from 0 to 5 do
(loop for j from 0 to 63 do
(if (eq (bit (pos (nth i (pieces (player2 gs)))) j) 1)
(setf (aref (game_state gs) j) (name (nth i (pieces (player2 gs)))))))))
;==;Clear-game-state()
;;; Desc: Clear the game-state
(defmethod clear-game-state ((gs game_state))
(loop for i from 0 to 63 do
(setf (aref (game_state gs) i) '__)))
;==;Print-game-state()
;; Desc: Print current game state as a board
;; Calls Functions: clear-game-state {game}
;; update-game-state {game}
(defmethod print-game-state ((gs game_state))
(clear-game-state gs)
(update-game-state gs)
(let ((curState (reverse (game_state gs))))
(loop for i from 0 to 7 do
(let ((x))
(loop for j from 0 to 7 do
(setf x (cons (aref curState (+ j (* i 8))) x)))
(print x)))))