(defclass chessboard ()
((board-position :accessor state
:initform (make-array '(8 8) :initial-element '0))))
(defmethod put-piece ((board chessboard) x y p)
(setf (aref (state board) x y) p))
(defun get-four-unique-random-numbers ()
(loop
:with results = '()
:for alea = (random 64)
:while (< (length results) 4)
:do (pushnew alea results)
:finally (return results)))
(defun rank-file (position)
(multiple-value-bind (quotient remainder)
(truncate position 8)
(list quotient remainder)))
(defun reverse-rank-file (lst)
(let ((x (first lst))
(y (second lst)))
(+ y (* 8 x))))
(defun get-positions ()
(mapcar #'cons '(wk wn wb bk) (get-four-unique-random-numbers)))
(defun neighbor (x)
(list (1- x) x (1+ x)))
(defun bad-squares (wk)
(let ((bad-squares (loop for x in (neighbor (first wk))
append (loop for y in (neighbor (second wk))
collect (list x y)))))
bad-squares))
(defun illegalp (wk bk)
(member (reverse-rank-file bk)
(mapcar #'reverse-rank-file (bad-squares wk))))
(defun new-ill ()
(let* ((lst (get-positions))
(wk (cdr (assoc 'wk lst)))
(bk (cdr (assoc 'bk lst))))
(member (reverse-rank-file bk)
(mapcar #'reverse-rank-file (bad-squares wk)))))
I would tend to make the function correct, instead of ignoring the
off-board positions.
For you current application, this doesn't matter, but by taking the time
to do a well-formed function definition now, you save the need to go
back and fix bugs if you suddenly decide that you can use this same
function to, for example, show where the king can legally move.
So, part of good software engineering and design is to try to think
beyond the immediate problem and make modular pieces (functions) that
encapsulate some domain-relevant and more general function. That allows
your code to be extensible and reusable without the need to do a lot of
re-writing (not to mention forgetting about those shortcuts 3 months
from now when you try to make changes.)
At the very least, make sure you document any of these assumptions or
shortcuts that you take in the code comments.
--
Thomas A. Russ, USC/Information Sciences Institute
> i attempt to rewrite function illegalp as below. is not working.
> please help.
> (defun illegalp (wk bk)
> (member (reverse-rank-file bk)
> (mapcar #'reverse-rank-file (bad-squares wk))))
You need to specify the :TEST argument to MEMBER.
You are testing for list equality, and the default test of EQL will not
do what you want. (Hmmm. I didn't notice that in my other reply).
Try
(defun illegalp (wk bk)
(member (reverse-rank-file bk)
(mapcar #'reverse-rank-file (bad-squares wk))
:test #'equal))
or more simply:
(defun illegalp (wk bk)
(member (rank-file bk) (bad-squares wk) :test #'equal))
>
> (defun get-four-unique-random-numbers ()
> (loop
> :with results = '()
> :for alea = (random 64)
> :while (< (length results) 4)
> :do (pushnew alea results)
> :finally (return results)))
Insufficient bloat. Make it
(defun
find-and-return-four-unique-random-numbers-between-zero-and-sixty-four
find-and-return-four-unique-random-numbers-between-zero-and-sixty-four-
inclusive-and-exclusive-respectively
> find-and-return-four-unique-random-numbers-between-zero-and-sixty-four-
> inclusive-and-exclusive-respectively
Be nice with your computer...
find-and-return-four-unique-random-numbers-between-zero-and-
sixty-four-inclusive-and-exclusive-respectively-PLEASE
--
Jacques.
where's your damn homework, punk? are you saying Ruby cannot scale to a
25-word spec?
This must be terribly embarrassing for you. Look on the bright side:
imagine if I had posted something non-trivial!
The Ruby Guy has not discovered Intercal yet. Hence his
impoliteness :)
Cheers
--
Marco
when i program in common lisp i feel like i'm skiing down
a frozen mountain, the icy wind blowing in my face, as i eat
a peppermint patty sandwiched between two more peppermint patties.
can ruby make me feel like that?
thanks, david