【问题标题】:please explain this piece of python code and how to write equivalent in lisp请解释这段 python 代码以及如何在 lisp 中编写等效代码
【发布时间】:2012-05-12 06:24:09
【问题描述】:

请解释 [[[0]*64 for i in range(7)] for j in range(2)] 部分以及如何编写 lisp 等效项。

pieceHashes = [[[0]*64 for i in range(7)] for j in range(2)]
    for color in WHITE, BLACK:
        for piece in PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING:
            for cord in range(64):
                pieceHashes[color][piece][cord] = randint(0, maxint)

【问题讨论】:

    标签: python common-lisp


    【解决方案1】:
    (let ((piece-array (make-array '(2 7 64) :initial-element 0)))
      (dolist (color `(,white ,black))
        (dolist (piece `(,pawn ,knight ,bishop ,rook ,queen ,king))
          (loop for cord below 64
                do (setf (aref piece-array color piece cord)
                         (random maxint))))))
    

    【讨论】:

      【解决方案2】:

      它生成一个包含 64 x 7 x 2 元素的三维数组。 然后在最后一行填充数组。

      【讨论】:

      • 该代码显然会为棋盘不同位置的不同类型棋子生成随机哈希值,并将它们存储在一个大数组中。 WHITE、BLACK、PAWN 等是映射到整数的符号常量。您可以使用 (defconstant WHITE 0) (defconstant BLACK 1) 等来定义 CL 中的符号常量,并使用 (make-array 64 7 2) 创建相应的数组。
      猜你喜欢
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2013-08-24
      • 2015-07-31
      相关资源
      最近更新 更多