佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1969|回复: 7

有人知道怎样用C++不要用array做tic tac toe吗?

[复制链接]
发表于 28-8-2006 09:45 PM | 显示全部楼层 |阅读模式
有人知道怎样用C++不要用array做tic tac toe吗?
教教我。。。可以吗?
回复

使用道具 举报


ADVERTISEMENT

发表于 29-8-2006 01:08 AM | 显示全部楼层
不要array就用bit string 咯。
回复

使用道具 举报

发表于 29-8-2006 04:42 PM | 显示全部楼层
为什么有array不要用? 用CLASS 吗?
回复

使用道具 举报

发表于 31-8-2006 10:08 AM | 显示全部楼层
不要用array的话就用pointer啰。
回复

使用道具 举报

发表于 1-9-2006 01:58 AM | 显示全部楼层
用 9 個 char 的 string 吧.

這樣的問法有點怪... 可以說明不能用 array 的原因嗎 ?
回复

使用道具 举报

发表于 14-9-2006 12:13 AM | 显示全部楼层
2 个bit 就可以绸四个可能性,那9个格,每格各2 bits..那最多麻18 bits..我看一个integer都卓卓有余了。
algorithm 方面,。简单的heuristic就够了

今天下午无聊时写的



  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. class ttt
  4. {
  5.         public:
  6.                 int board;
  7.                 bool put(unsigned short mark,short int position);
  8.                 unsigned short int check(int tempboard);
  9.                 unsigned short int compute();
  10. };


  11. bool ttt::put(unsigned short int mark,short int p)
  12. {
  13.         if (!((board >> (p * 2)) %4))
  14.         {
  15.                 int mask=mark << (p*2);
  16.                 board=board | mask;
  17.                 return true;
  18.         }
  19.         else
  20.                 return false;
  21. }


  22. short unsigned int ttt::check(int board)
  23. {
  24.         return
  25.         ( (board & 65793)==65793  ) |  ((board & 4368)==4368) |      
  26.         ( (  (board % 64) & 21)==21 ) |  ( (  (board>>6 % 64) & 21)==21 ) |
  27.         ( (  (board>>12 % 64) & 21)==21 ) |        ( (  board  & 4161)==4161 ) |
  28.         ( (  board  & (4161<<2))==(4161<<2) ) | ( (  board  & (4161<<4) )==(4161<<4) )? 1:
  29.         ( (board & (65793<<1))==(65793<<1)  ) |  ((board & (4368<<1))==(4368<<1)) |      
  30.         ( (  (board % 64) & (21<<1))==(21<<1) ) |  ( (  (board>>6 % 64) & (21<<1))==(21<<1) ) |
  31.         ( (  (board>>12 % 64) & (21<<1))==(21<<1) ) |
  32.         ( (  board  & (4161<<1))==(4161<<1) ) |  ( (  board  & (4161<<3))==(4161<<3) ) |
  33.         ( (  board  & (4161<<5) )==(4161<<5) )? 2: 0;
  34. }


  35. unsigned short int ttt::compute()
  36. {
  37.         for(unsigned short int i = 0;i<9;i++)
  38.         {
  39.                 if (!((board >> (i * 2)) % 4))
  40.                 {
  41.                         if ((check(board | (2 << (i * 2)))) == 2)
  42.                         {
  43.                                 return i;
  44.                         }
  45.                 }
  46.         }

  47.         for(unsigned short int i = 0;i<9;i++)
  48.         {
  49.                 if (!((board >> (i * 2)) % 4))
  50.                 {
  51.                         if ((check(board | (1 << (i * 2)))) == 1)
  52.                         {
  53.                                 return i;
  54.                         }
  55.                 }
  56.         }

  57.         if (!((board >> 8) % 4))
  58.             return 4;
  59.     else
  60.         {
  61.        for (unsigned short int i = 0; i<=4 ; i++)
  62.            {
  63.             if (!((board >> (i * 4)) % 4 ))
  64.                         {
  65.                     if (!(i % 2))
  66.                                         {
  67.                         if ((board & 4112) != 4112)
  68.                                                 {
  69.                             return i * 2;
  70.                                                 }
  71.                                         }
  72.                     else
  73.                                         {
  74.                         if ((board & 65537) != 65537)
  75.                                                 {
  76.                             return i * 2;
  77.                                                 }
  78.                                         }
  79.                         }
  80.            }

  81.                 for (unsigned short int i = 0; i<=4 ; i++)
  82.                 {
  83.                 if (!(board >> (i * 4 - 2)) % 4)
  84.                                 {

  85.                     return (i * 2+1);
  86.                                        
  87.                                 }
  88.                 }
  89.         }

  90.         return -1;
  91. }






  92. int drawboard(ttt *a,bool showgridnumber)
  93. {
  94.         for (unsigned short int  i=0;i<3;i++)
  95.         {
  96.                         for (unsigned short int j=0;j<3;j++)
  97.                         {
  98.                                 if (((a->board)>>((i*3+j)*2)&3)==1)
  99.                                         printf("  o |");
  100.                                 else if (((a->board)>>((i*3+j)*2)&3)==2)
  101.                                         printf("  x |");
  102.                                 else
  103.                                         showgridnumber? printf(" (%d) ",i*3+j): printf("    |");
  104.                         }
  105.                         printf("\n");
  106.                 }
  107.                 return 0;
  108. }




  109. int main()
  110. {
  111.         ttt *a = new ttt();
  112.         bool finish=0;
  113.         drawboard(a,true);

  114.         while (!(a->check(a->board)) || (a->compute()>0) && !finish)
  115.         {
  116.                 drawboard(a,false);
  117.                 printf("You marked at position: ");
  118.                 int urmove,valid=0;

  119.                 do
  120.                 {
  121.                         scanf("%d",&urmove);
  122.                         if (a->put(1,urmove))
  123.                         {
  124.                                 int pcmove=0;
  125.                                 pcmove=a->compute();
  126.                                 printf("PC marked position: %d\n",pcmove);
  127.                                 a->put(2,pcmove);
  128.                                 if (a->check(a->board)==2) { printf("Computer won\n");drawboard(a,false);finish=1;}
  129.                                 valid=1;
  130.                         }
  131.                         else
  132.                         {
  133.                                 valid=0;
  134.                                 printf("Invalid position (position is not vacant)\n Re-mark at position: ");
  135.                         }
  136.                 }while (!valid);
  137.         }

  138.         system("pause");
  139.         return 0;
  140. }


复制代码


回复

使用道具 举报

Follow Us
发表于 15-9-2006 11:26 PM | 显示全部楼层
原帖由 tensaix2j 于 14-9-2006 12:13 AM 发表
2 个bit 就可以绸四个可能性,那9个格,每格各2 bits..那最多麻18 bits..我看一个integer都卓卓有余了。
algorithm 方面,。简单的heuristic就够了

今天下午无聊时写的

#include <stdio.h>
#i ...


這樣寫回很亂, 而且很難除錯

乾脆寫一個 function 來取某 bit 到某 bit, 或者每兩個 bit 為一個單位
回复

使用道具 举报

发表于 2-12-2006 12:00 AM | 显示全部楼层
原帖由 exiang 于 29-8-2006 04:42 PM 发表
为什么有array不要用? 用CLASS 吗?


我记得当年我拿 Computer Programming 时第一个 assignmnet 就是不要能用 array 来写一个 tic tac toe,因为还没教到 array。。。
回复

使用道具 举报


ADVERTISEMENT

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

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 24-9-2024 07:21 AM , Processed in 0.126730 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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