乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 10|回复: 6

[编程交流] Tic-Tac-Toe

[复制链接]

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 15:28:22 | 显示全部楼层 |阅读模式
CADTutor正在进行维护,这提醒了我想做什么:
 
 
不是最漂亮的编码,但经过测试,没有失败:
 
  1. (defun C:test nil
  2. (TicTacToe)
  3. )
  4. ; Tic Tac Toe
  5. ; Written By Grrr
  6. ; Credits: Lee Mac
  7. (defun TicTacToe ( / SubstNth GroupByN kL vL *error* dcl des dch dcf tmp )
  8. (defun SubstNth ( n v L / i ) (setq i -1) (mapcar (function (lambda (x) (if (eq n (setq i (1+ i))) v x))) L))
  9. (defun GroupByN ( n L / r ) ; Grrr
  10.    (repeat n (and L (setq r (cons (car L) r))) (setq L (cdr L)) r)
  11.    (if L (cons (reverse r) (GroupByN n L)) (list (reverse r)))
  12. ); defun GroupByN
  13. (setq kL
  14.    '(
  15.      ("A1" "B1" "C1")
  16.      ("A2" "B2" "C2")
  17.      ("A3" "B3" "C3")
  18.    ); list
  19. ); setq kL
  20. (setq vL
  21.    '(
  22.      (nil nil nil)
  23.      (nil nil nil)
  24.      (nil nil nil)
  25.    )
  26. )
  27. (defun *error* ( msg )
  28.    (and (< 0 dch) (unload_dialog dch))
  29.    (and (eq 'FILE (type des)) (close des))
  30.    (and (eq 'STR (type dcl)) (findfile dcl) (vl-file-delete dcl))
  31.    (and msg (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\nError: " msg)) ))
  32.    (princ)
  33. ); defun *error*
  34. (cond
  35.    (
  36.      (progn
  37.        (while (not (member dcf '(0 1))) (*error* nil)
  38.          (cond
  39.            (
  40.              (not ; Rewrite and Reload the dialog continiously
  41.                (and (setq dcl (vl-filename-mktemp nil nil ".dcl")) (setq des (open dcl "w"))
  42.                  (vl-every (function (lambda (x) (princ (strcat "\n" x) des)))
  43.                    (list
  44.                      "test : dialog "
  45.                      "{ label = "Tic Tac Toe"; spacer;"
  46.                      "  : column "
  47.                      "  {"
  48.                      (apply (function strcat)
  49.                        (mapcar
  50.                          (function
  51.                            (lambda (kL vL)
  52.                              (strcat "\n"
  53.                                "    : row "
  54.                                "    { fixed_width = true; alignment = centered; "
  55.                                (apply (function strcat)
  56.                                  (mapcar
  57.                                    (function
  58.                                      (lambda (k v)
  59.                                        (strcat "\n" "      : button { key = "" k ""; label = "" (cond (v)("")) ""; width = 3; fixed_width = true; height = 1.5; }")
  60.                                      ); lambda (xx)
  61.                                    ); function
  62.                                    kL
  63.                                    vL
  64.                                  ); mapcar
  65.                                ); apply 'strcat
  66.                                "    }"
  67.                              ); strcat
  68.                            ); lambda (r)
  69.                          ); function
  70.                          kL
  71.                          vL
  72.                        ); mapcar
  73.                      ); apply 'strcat
  74.                      "  }"
  75.                      "  spacer_1; ok_only; : text { key = "error"; }"
  76.                      "}"
  77.                    ); list
  78.                  ); vl-every
  79.                  (not (setq des (close des))) (< 0 (setq dch (load_dialog dcl)))
  80.                ); and
  81.              ); not
  82.              (princ "\nUnable to write or load the DCL file.") (setq dcf 0)
  83.            )
  84.            ( (not (new_dialog "test" dch)) (princ "\nUnable to display the dialog") (setq dcf 0) )
  85.            (T
  86.              (eval (read (apply 'strcat (mapcar 'chr '(40 83 69 84 95 84 73 76 69 32 34 101 114 114 111 114 34 32 34 87 114 105 116 116 101 110 32 98 121 32 71 114 114 114 34 41)))))
  87.              (mapcar
  88.                (function
  89.                  (lambda (rk rv)
  90.                    (mapcar
  91.                      (function
  92.                        (lambda ( k v )
  93.                          (action_tile k
  94.                            (vl-prin1-to-string
  95.                              (quote
  96.                                (
  97.                                  (lambda ( / i n tmp )
  98.                                    
  99.                                    (cond
  100.                                      ( (not (nth (setq i (vl-position $key (apply 'append kL))) (apply 'append vL)))
  101.                                        (setq vL (GroupByN 3 (SubstNth i "O" (apply 'append vL))))
  102.                                        (cond
  103.                                          ( (vl-some 'null (apply 'append vL))
  104.                                            ; SubstNth ( n v L )
  105.                                            (cond ; apply a little logic (but not alot, since we don't want to draw everytime)
  106.                                              ( ; row
  107.                                                (and
  108.                                                  (setq n -1)
  109.                                                  (setq n
  110.                                                    (vl-some
  111.                                                      '(lambda (row) (setq n (1+ n))
  112.                                                        (if
  113.                                                          (and
  114.                                                            (= 2 (length (vl-remove-if-not '(lambda (x) (= x "O")) row)))
  115.                                                            (vl-some 'null row)
  116.                                                          ); and
  117.                                                          n
  118.                                                        ); if
  119.                                                      ); lambda (row)
  120.                                                      vL
  121.                                                    ); vl-some
  122.                                                  ); setq n
  123.                                                ); and
  124.                                                (setq vL
  125.                                                  (SubstNth n
  126.                                                    (SubstNth (vl-position nil (nth n vL)) "X" (nth n vL))
  127.                                                    vL
  128.                                                  ); SubstNth
  129.                                                ); setq vL
  130.                                              ) ; row
  131.                                              ( ; col
  132.                                                (and
  133.                                                  (setq n -1)
  134.                                                  (setq n
  135.                                                    (vl-some
  136.                                                      '(lambda (col) (setq n (1+ n))
  137.                                                        (if
  138.                                                          (and
  139.                                                            (= 2 (length (vl-remove-if-not '(lambda (x) (= x "O")) col)))
  140.                                                            (vl-some 'null col)
  141.                                                          ); and
  142.                                                          n
  143.                                                        ); if
  144.                                                      ); lambda (col)
  145.                                                      (apply 'mapcar (cons 'list vL))
  146.                                                    ); vl-some
  147.                                                  ); setq n
  148.                                                ); and
  149.                                                (setq vL (apply 'mapcar (cons 'list vL)))
  150.                                                (setq vL
  151.                                                  (SubstNth n
  152.                                                    (SubstNth (vl-position nil (nth n vL)) "X" (nth n vL))
  153.                                                    vL
  154.                                                  ); SubstNth
  155.                                                ); setq vL
  156.                                                (setq vL (apply 'mapcar (cons 'list vL)))
  157.                                              ) ; col
  158.                                              ( ; mid
  159.                                                (null (nth 1 (nth 1 vL)))
  160.                                                (setq vL
  161.                                                  (SubstNth 1
  162.                                                    (SubstNth 1 "X" (nth 1 vL))
  163.                                                    vL
  164.                                                  ); SubstNth
  165.                                                ); setq vL
  166.                                              ) ; mid
  167.                                              (t ; random!
  168.                                                (while (nth (setq tmp (LM:randrange 0 ) (apply 'append vL)))
  169.                                                (setq vL (GroupByN 3 (SubstNth tmp "X" (apply 'append vL))))
  170.                                              ); t
  171.                                            ); cond
  172.                                           
  173.                                           
  174.                                          ); vl-some 'null
  175.                                        ); cond
  176.                                        (cond
  177.                                          (
  178.                                            (or
  179.                                              (vl-some '(lambda (x) (equal x '("O" "O" "O"))) (append vL (apply 'mapcar (cons 'list vL))))
  180.                                              (vl-some '(lambda (x) (equal (mapcar 'nth '(0 1 2) x) '("O" "O" "O"))) (list vL (reverse vL)))
  181.                                            ); or
  182.                                            (done_dialog 3)
  183.                                          )
  184.                                          (
  185.                                            (or
  186.                                              (vl-some '(lambda (x) (equal x '("X" "X" "X"))) (append vL (apply 'mapcar (cons 'list vL))))
  187.                                              (vl-some '(lambda (x) (equal (mapcar 'nth '(0 1 2) x) '("X" "X" "X"))) (list vL (reverse vL)))
  188.                                            ); or
  189.                                            (done_dialog 4)
  190.                                          )
  191.                                          ( (apply 'and (apply 'append vL)) (done_dialog 5) )
  192.                                          ( (done_dialog 2) )
  193.                                        ); cond
  194.                                      )
  195.                                      ( (set_tile "error" "Already taken!") )
  196.                                    ); cond
  197.                                  ); lambda
  198.                                )
  199.                              ); quote
  200.                            ); vl-prin1-to-string
  201.                          ); action_tile k
  202.                        ); lambda
  203.                      ); function
  204.                      rk rv
  205.                    ); mapcar
  206.                  ); lambda
  207.                ); function
  208.                kL vL
  209.              ); mapcar
  210.              (cond
  211.                ( (= dcf 3) (set_tile "error" "You win!") (mapcar '(lambda (x) (mapcar '(lambda (xx) (mode_tile xx 1)) x)) kL) )
  212.                ( (= dcf 4) (set_tile "error" "You loose!") (mapcar '(lambda (x) (mapcar '(lambda (xx) (mode_tile xx 1)) x)) kL) )
  213.                ( (= dcf 5) (set_tile "error" "Draw!") (mapcar '(lambda (x) (mapcar '(lambda (xx) (mode_tile xx 1)) x)) kL) )
  214.              ); cond
  215.              (setq dcf (start_dialog))
  216.             
  217.            ); T
  218.          ); cond
  219.        ); while
  220.        (= 0 dcf)
  221.      ); progn
  222.      (princ "\nUser cancelled the dialog.")
  223.    )
  224. ); cond
  225. (*error* nil) (princ)
  226. ); defun
  227. ;; Rand  -  Lee Mac
  228. ;; PRNG implementing a linear congruential generator with
  229. ;; parameters derived from the book 'Numerical Recipes'
  230. (defun LM:rand ( / a c m )
  231. (setq m   4294967296.0
  232.    a   1664525.0
  233.    c   1013904223.0
  234.    $xn (rem (+ c (* a (cond ($xn) ((getvar 'date))))) m)
  235. )
  236. (/ $xn m)
  237. )
  238. ;; Random in Range  -  Lee Mac
  239. ;; Returns a pseudo-random integral number in a given range (inclusive)
  240. (defun LM:randrange ( a b )
  241. (+ (min a b) (fix (* (LM:rand) (1+ (abs (- a b))))))
  242. )

 
玩得高兴伊玛·戈纳现在做我的起草工作。。
回复

使用道具 举报

rlx

21

主题

1505

帖子

1551

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
81
发表于 2022-7-5 15:41:03 | 显示全部楼层
美好的等不及v2.0:4连成一行或v3.0:5连成一行:-)甚至更好:麻将
 
 
gr.Rlx
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 15:52:14 | 显示全部楼层
 
谢谢,我甚至不知道4x4/5x5。。存在的。
回复

使用道具 举报

5

主题

1334

帖子

1410

银币

限制会员

铜币
-20
发表于 2022-7-5 16:07:36 | 显示全部楼层
这是我的老歌,但必须有两个玩家——我没有考虑过人工智能:
https://www.theswamp.org/index.php?topic=49245.0
 
(必须记录)
P、 我已经将我的firefox设置为使用https-如果你无法访问,请将https更改为http。。。
回复

使用道具 举报

pBe

32

主题

2722

帖子

2666

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
211
发表于 2022-7-5 16:15:50 | 显示全部楼层
 
难道这不应该是“没有起草工作,这提醒了我想做什么:……”
 
你手头的时间太多了
 
但是有一件事,把人工智能放在第一位,如果我从中间开始,我总是赢或平局。想想看,Once可以编写一个代码,使游戏始终以平局结束。总是,没有胜利,只有平局。
 
好吧,我改变主意了。是赢是平,但决不输。
 
有没有办法在井字游戏中不输
Tic-tac-toe#组合数学
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 16:27:39 | 显示全部楼层
 
 
哇,太疯狂了。。2D和3D两种变体!
现在我记得我以前在寻找tic-tac-toe,找到了你的,但我更想要一个DCL版本。
 
 
 
 
空闲时间。。嗯,我只是为了工作或兴趣而分发。
谢天谢地,这篇文章花了我30-40分钟的时间。
 
 
 
起初,人工智能是从随机单元开始的,但后来我认为最好的逻辑位置应该是中间中心(如果它是自由的)。
我可以多收些支票,但每次以平局或失利结束都有点无聊。
 
根本没有考虑对人工智能进行任何检查。。但我真的很困扰什么dumb@ss是的,
当它不在(O nil O)/(O nil)/(nil O)行或列上阻止我时。
 
 
 
如果我想赢的话,我可以用它们。。但现在你带来了下一个想法“赌博游戏”。
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 16:30:10 | 显示全部楼层
Grrr查看游戏“连接4”
162827gwcw4w7e6nwb0e0x.jpg
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-14 00:23 , Processed in 0.471499 second(s), 68 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表