吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1098|回复: 2
收起左侧

[其他原创] 几行代码实现一个象棋变体--122象棋

[复制链接]
长空牧雁 发表于 2024-5-11 11:10
发现一个棋类软件Zillions Of Games,收录了大量棋类,还可自行编写代码,保存为zrf(即 Zillions Rule File)后缀即可运行。

以中国象棋为例,核心代码如下:

[Asm] 纯文本查看 复制代码
(define shift-myside   ($1 (verify empty?) $1 (verify not-friend?) (verify (in-zone? myside)) add) )
(define shift-fortress ($1 (verify not-friend?) (verify (in-zone? fortress)) add) )
(define shift2         ($1 (verify empty?) $2 (verify not-friend?) add) )
(define slide          ($1 (while empty? add $1) (verify enemy?) add))
(define slide-cannon   ($1 (while empty? add $1) $1 (while empty? $1) (verify enemy?) add))
(define opposed-kings  (n (while empty? n) (verify (piece? General)) add) )

(game
   (title "Chinese Chess")
   (description "Object: Checkmate the opponent's General by attacking it so 
      that it has no safe positions to move to.  It's also a win to stalemate
      your opponent so that he can't move -- this usually only happens when a
      player is reduced to a lone king.  A player may not force a repetition of
	  moves.\\
	  The horizontal space across the center of the board is the river separating
	  the territories of the two sides.  Elephants are not allowed to cross the
	  river, whereas Soldiers promote once they cross it.  The 3x3 boxes marked with
	  an `X` at the top and bottom of the board are the Generals' imperial palaces
	  or fortresses.  Each General and his Mandarins may not leave their fortress.\\
	  Right-click on the pieces to see how they move.")
   (history "Chinese Chess, or Xiangqi (Elephant Chess), derives from the same source as
        Western Chess, though there is much debate over whether the earliest form of chess
        began in India or China.   The first definite reference to the game is from the
        8th century; the present form dates from about the beginning of the 12th century.
        Xiangqi is still firmly embedded in culture in China and viewed as a folk game.
        Among the Chinese, the rules are said to be universally known, and it is claimed
        that Xiangqi is the world's most popular game, with perhaps 200 million players.")
   (strategy "Unlike Western Chess, having an extra piece is not as important as having
      a strong attack.  Attack on the General can come at any stage of the game.\\
	  Chariots are the most valuable pieces, being worth about twice as much as a
	  Cannon.   The Horse is slightly less valuable than the Cannon in the opening,
        but becomes stronger as the game progresses, as it becomes more mobile and
        the Cannon less so (due to the lack of `screens`).  Mandarins and Elephants
        are purely defensive pieces.   Soldiers are very weak until they cross the river
        and promote, where their increased mobility makes them useful in attack.\\
        The most common opening moves are: moving either Cannon behind the central
        Soldier, moving a Horse to defend the central Soldier (this also frees the
        Chariot to occupy the adjacent file), pushing a c-file or g-file Soldier to
        give a Horse a path to advance to the river's edge, or bringing either Elephant
        to the front of the fortress to protect its partner.\\
        Endgames have been studied even more deeply than in Western chess, and much
        is known about mating possibilities with various combinations of pieces.
        A lone General can be mated (remember that checkmate and stalemate both win,
        and Generals cannot face each other a file with no intervening pieces) by
        General and Soldier, General and Horse, or General and Chariot.   A Chariot
        can even win against two Elephants or two Mandarins; two Soldiers can defeat
        a single Elephant or two Mandarins.   Many detailed endgame examples are
        given in H. T. Lau's book `Chinese Chess`.\\
        Literature on the game is plentiful in Chinese (and game transcripts can be
        understood with a little study, by learning the characters for the pieces,
        Chinese numbers, and a few miscellaneous characters).    English-language
        sources are less abundant; some good books and magazines can be found at
        <http://www.zillions-of-games.com/xiangqi.html>.")
   (music "Audio\\China.mid")
   (win-sound "Audio\\Pan.wav")
   (loss-sound "Audio\\Pan.wav")
   (opening-sound "Audio\\Pan.wav")
   (release-sound "Audio\\BongPercussive.wav")
   (move-sound "Audio\\Bong5th.wav")
   (capture-sound "Audio\\WoodBells.wav")
   (players Red Black)
   (turn-order Red Black)

   (board
      (image "images\Chinese_Chess\ChineseChess.bmp")
      (grid
         (start-rectangle 4 5 46 47)
         (dimensions
             ("a/b/c/d/e/f/g/h/i" (45 0)) ; files
             ("10/9/8/7/6/5/4/3/2/1" (0 45)) ; ranks
         )
         (directions (n 0 -1) (e 1 0) (s 0 1) (w -1 0)
			         (ne 1 -1) (nw -1 -1) (se 1 1) (sw -1 1)
         )
      )
      (symmetry Black (n s)(s n) (nw sw)(sw nw) (ne se)(se ne))
      (zone
         (name fortress)
         (players Red)
         (positions d1 e1 f1 d2 e2 f2 d3 e3 f3)
      )
      (zone
         (name fortress)
         (players Black)
         (positions d10 e10 f10 d9 e9 f9 d8 e8 f8)
      )
      (zone
         (name myside)
         (players Red)
         (positions a1 b1 c1 d1 e1 f1 g1 h1 i1
		            a2 b2 c2 d2 e2 f2 g2 h2 i2
					a3 b3 c4 d3 e3 f3 g3 h3 i3
					a4 b4 c4 d4 e4 f4 g4 h4 i4
					a5 b5 c5 d5 e5 f5 g5 h5 i5)
      )
      (zone
         (name myside)
         (players Black)
         (positions a10 b10 c10 d10 e10 f10 g10 h10 i10
		            a9 b9 c9 d9 e9 f9 g9 h9 i9
					a8 b8 c8 d8 e8 f8 g8 h8 i8
					a7 b7 c7 d7 e7 f7 g7 h7 i7
					a6 b6 c6 d6 e6 f6 g6 h6 i6)
      )
   )

   (board-setup
      (Red
         (Soldier a4 c4 e4 g4 i4)
         (Horse b1 h1)
         (Elephant c1 g1)
         (Chariot a1 i1)
         (Mandarin d1 f1)
		 (Cannon b3 h3)
         (General e1)
      )
      (Black
         (Soldier a7 c7 e7 g7 i7)
         (Horse b10 h10)
         (Elephant c10 g10)
         (Chariot a10 i10)
         (Mandarin d10 f10)
		 (Cannon b8 h8)
         (General e10)
      )
   )

   (piece
      (name Soldier)
	  (help "Soldier/Pawn: only moves forward until it crosses the river, then also left and right")
	  (description "Soldier/Pawn (zu/tsut, bing/ping = foot soldier)\\
	     Soldiers can move forward until they cross the center section of the board
		 (called `crossing the river`) where they gain the ability to move left and
		 right.")
      (image Red "images\Chinese_Chess\RSoldier.bmp" "images\Chinese_Chess\Shaag\RSoldier.bmp" Black "images\Chinese_Chess\BSoldier.bmp" "images\Chinese_Chess\Shaag\BSoldier.bmp")
      (moves
         (n (verify not-friend?) add)
		 ((verify (not-in-zone? myside)) e (verify not-friend?) add)
		 ((verify (not-in-zone? myside)) w (verify not-friend?) add)
      )
   )
   (piece
      (name Horse)
      (help "Horse/Knight: step orthogonally 1 square then diagonally outward 1 square")
	  (description "Horse/Knight (ma = horse)\\
	    Horses move like a Knight in Chess, except that they can't jump over other pieces.
          They step outward on a row or column, then diagonally outward one step.  If
          something is adjacent to a Horse on a row or column, it can't move in that
          direction.")
      (image Red "images\Chinese_Chess\RHorse.bmp" "images\Chinese_Chess\Shaag\RHorse.bmp" Black "images\Chinese_Chess\BHorse.bmp" "images\Chinese_Chess\Shaag\BHorse.bmp")
      (moves
	     (shift2 n ne)
	     (shift2 n nw)
	     (shift2 s se)
	     (shift2 s sw)
	     (shift2 e ne)
	     (shift2 e se)
	     (shift2 w nw)
	     (shift2 w sw)
      )
   )
   (piece
      (name Elephant)
      (help "Elephant: moves two steps diagonally without jumping, can't cross the river")
	  (description "Elephant (xiang/tseung = elephant; xiang/sheung = minister/premier)\\
	     Elephants move diagonally two steps.  However, Elephants cannot jump over other
	     pieces, so an Elephant is blocked in any direction where another piece is
           diagonally next to it.  Elephants are defensive pieces: they must stay on their
           side of the board and cannot cross the 'river.'\\
           The Elephant is similar (but without the ability to leap) to the Alfil in
           Shatranj, the precursor to the modern Bishop.")
      (image Red "images\Chinese_Chess\RElephant.bmp" "images\Chinese_Chess\Shaag\RElephant.bmp" Black "images\Chinese_Chess\BElephant.bmp" "images\Chinese_Chess\Shaag\BElephant.bmp")
      (moves
         (shift-myside ne)
         (shift-myside nw)
         (shift-myside se)
         (shift-myside sw)
      )
   )
   (piece
      (name Chariot)
      (help "Chariot/Rook: slides any number of squares along a row or column")
	  (description "Chariot/Rook (ju/kui = chariot)\\
	     Chariots move like the Rook in Western Chess, that is, any number of squares along a
		 row or column.")
      (image Red "images\Chinese_Chess\RChariot.bmp" "images\Chinese_Chess\Shaag\RChariot.bmp" Black "images\Chinese_Chess\BChariot.bmp" "images\Chinese_Chess\Shaag\BChariot.bmp")
      (moves
         (slide n)
         (slide e)
         (slide s)
         (slide w)
      )
   )
   (piece
      (name Cannon)
      (help "Cannon: slides on row or column, captures by jumping")
	  (description "Cannon (pao = cannon)\\
	     Cannons move like Chariots/Rooks, by sliding any number of squares along
		 a row or column, but they can capture an enemy only if there is another
		 piece (of either side) in between.  Thus to capture they leap over the
		 intervening piece and land on the enemy piece, like a cannonball.\\
             One account of Xiangqi dates the introduction of the cannon at 839 A.D.")
      (image Red "images\Chinese_Chess\RCannon.bmp" "images\Chinese_Chess\Shaag\RCannon.bmp" Black "images\Chinese_Chess\BCannon.bmp" "images\Chinese_Chess\Shaag\BCannon.bmp")
      (moves
         (slide-cannon n)
         (slide-cannon e)
         (slide-cannon s)
         (slide-cannon w)
      )
   )
   (piece
      (name Mandarin)
      (image Red "images\Chinese_Chess\RMandarin.bmp" "images\Chinese_Chess\Shaag\RMandarin.bmp"
           Black "images\Chinese_Chess\BMandarin.bmp" "images\Chinese_Chess\Shaag\BMandarin.bmp")
	  (description "Mandarin (shi/see = counsellor)\\
	     The Mandarin must stay confined to the fortress, and can only move a single step
		 along the diagonal lines shown.  This gives it only 5 possible positions.\\
		 This piece is often translated into other names such as Assistant, Guard,
             Counsellor, and Officer.")
      (help "Mandarin: steps along diagonal lines in fortress")
      (moves
         (shift-fortress ne)
         (shift-fortress nw)
         (shift-fortress se)
         (shift-fortress sw)
      )
   )
   (piece
      (name General)
      (image Red "images\Chinese_Chess\RGeneral.bmp" "images\Chinese_Chess\Shaag\RGeneral.bmp" Black "images\Chinese_Chess\BGeneral.bmp" "images\Chinese_Chess\Shaag\BGeneral.bmp")
      (help "General/King: steps orthogonally within fortress, can't oppose opponent's General")
	  (description "General/King (jiang/cheung = general, shuai/sui = general)\\
	     The General is confined to the fortress and can only move a step at a time
           horizontally or vertically.  It also has the special power to threaten an enemy
           General across the board along an open column.  For this reason, it is not
           permitted to make a move that leaves the two Generals facing each other with
           nothing in between.\\
	     The object of the game is to checkmate the opponent's General.")
      (moves
	     (opposed-kings)
         (shift-fortress n)
         (shift-fortress e)
         (shift-fortress s)
         (shift-fortress w)		 
      )
   )
   (loss-condition (Red Black) (checkmated General)  )
   (loss-condition (Red Black) stalemated )
   (loss-condition (Red Black) repetition )
)


简单介绍下代码

代码包含在(game )里面
title 即棋名
description 棋的描述
history 历史
strategy 策略

music、win-sound、loss-sound、opening-sound等等 定义各种声音 赢棋声音、输棋声音、走子声音、被吃声音等等

(players Red Black) 定义了下棋双方为红方、黑方
(turn-order Red Black) 下棋顺序 先红后黑

  (board
      (image "images\Chinese_Chess\ChineseChess.bmp")
      (grid
         (start-rectangle 4 5 46 47)
         (dimensions
             ("a/b/c/d/e/f/g/h/i" (45 0)) ; files
             ("10/9/8/7/6/5/4/3/2/1" (0 45)) ; ranks
         )
         (directions (n 0 -1) (e 1 0) (s 0 1) (w -1 0)
                                 (ne 1 -1) (nw -1 -1) (se 1 1) (sw -1 1)
         )
      )
这段划分棋盘,规定了八个方向。

zone即区域 用来规定那些是红/黑地盘,那些是红/黑方九宫格

board-setup 棋子的布置 例如
(Red
         (Soldier a4 c4 e4 g4 i4)
即红方的兵在a4 c4 e4 g4 i4这几个位置


piece 定义了各种棋子

loss-condition 输棋条件

(loss-condition (Red Black) (checkmated General)  ) --将/帅被吃
(loss-condition (Red Black) stalemated )            --困毙 无子可走
(loss-condition (Red Black) repetition )            --重复

以上是正常象棋的代码,如果要增加变体,需要加上(variant),设计了122象棋(不知道起啥名,即第一个人走1步棋,接下来双方每回合都走两步)

变体代码如下:

(variant
   (title "122-Xq")
   (turn-order Red Black Black Red)
)

即只需起个名字,定义下棋顺序,其它全部跟正常象棋一样。

测试了一下,没下几步就输了{:1_905:}




测试对战视频:
链接:https://pan.baidu.com/s/1zQ2w4Ehiw93TetRhUZkLBg?pwd=orfg
提取码:orfg

对棋类感兴趣的可以去Zillionsofgames官网看看

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

shuangjiang163 发表于 2024-5-13 08:55
厉害厉害,学习&#128209;
lygz05 发表于 2024-5-16 15:46
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 15:27

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表