-
Notifications
You must be signed in to change notification settings - Fork 0
/
larrys-winning-checker.lisp
46 lines (40 loc) · 1.05 KB
/
larrys-winning-checker.lisp
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
(defpackage :larrys-winning-checker
(:use :cl)
(:export
:make-empty-board
:make-board-from-list
:all-the-same-p
:row
:column))
(in-package :larrys-winning-checker)
;;; returns an empty 3x3 array
(defun make-empty-board ()
(make-array '(3 3) :initial-element nil)
) ; => #2A((NIL NIL NIL) (NIL NIL NIL) (NIL NIL NIL))
;;; returns a 3x3 array intialized with a list
(defun make-board-from-list (list)
(make-array '(3 3) :initial-contents list)
) ; => #2A((1 2 3) (4 5 6) (7 8 9))
;;; return true if one of the arrays has 3 of the same element
(defun all-the-same-p (list)
(and
(eq (aref list 0) (aref list 1))
(eq (aref list 0) (aref list 2))
)
) ; => t or nil
;;; returns the elements in the requested row
(defun row (board row-num)
(vector
(aref board row-num 0)
(aref board row-num 1)
(aref board row-num 2)
)
) ; => #(x y z)
;;; returns the elements in the requested column
(defun column (board col-num)
(vector
(aref board 0 col-num)
(aref board 1 col-num)
(aref board 2 col-num)
)
) ; => #(x y z)