我知道这似乎是微不足道的,但是只使用过声明/命令式语言,我发现自己遇到了这个问题。
我有一个棋盘(基本上是8x8列表/状态数组),我需要能够抓住棋盘的第(xy)个元素。
我认为给予董事会剩余部分作为董事会,如果我能(休息休息)y-1次,然后返回(第一次休息),我将获得该行。然后,如果我(休息行)x-1次,然后返回(第一行),我将拥有正确的元素。然后我带着(结果)返回?
#lang scheme
(define new-board '((P2 OFF P2 OFF P2 OFF P2 OFF)
(OFF P2 OFF P2 OFF P2 OFF P2)
(P2 OFF P2 OFF P2 OFF P2 OFF)
(OFF BLANK OFF BLANK OFF BLANK OFF BLANK)
(BLANK OFF BLANK OFF BLANK OFF BLANK OFF)
(OFF P1 OFF P1 OFF P1 OFF P1)
(P1 OFF P1 OFF P1 OFF P1 OFF)
(OFF P1 OFF P1 OFF P1 OFF P1)))
(define (get-state board x y))
答案 0 :(得分:2)
为此创建一个抽象:
(define (make-board)
(make-vector 64 'blank))
(define (board-ref board x y)
(vector-ref board (+ (* y 8) x)))
(define (board-set! board x y new)
(vector-set! board (+ (* y 8) x) new))
从那里开始。
(define (board-fill board list-of-64-values) ...) ;; for example
答案 1 :(得分:1)
这里的vector向量可能是更好的代表:
(define new-board
(list->vector
(map list->vector
'((P2 OFF P2 OFF P2 OFF P2 OFF)
(OFF P2 OFF P2 OFF P2 OFF P2)
(P2 OFF P2 OFF P2 OFF P2 OFF)
(OFF BLANK OFF BLANK OFF BLANK OFF BLANK)
(BLANK OFF BLANK OFF BLANK OFF BLANK OFF)
(OFF P1 OFF P1 OFF P1 OFF P1)
(P1 OFF P1 OFF P1 OFF P1 OFF)
(OFF P1 OFF P1 OFF P1 OFF P1)))))
(define (get-state board x y)
(vector-ref (vector-ref board x) y))
(define (set-state! board x y val)
(vector-set! (vector-ref board x) y val))
然后
> (get-state new-board 0 1)
OFF
> (get-state new-board 1 1)
P2
> (set-state! new-board 1 1 'hi)
> (get-state new-board 1 1)
'hi