查看: 1969|回复: 7
|
有人知道怎样用C++不要用array做tic tac toe吗?
[复制链接]
|
|
有人知道怎样用C++不要用array做tic tac toe吗?
教教我。。。可以吗? |
|
|
|
|
|
|
|
发表于 29-8-2006 01:08 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 29-8-2006 04:42 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 31-8-2006 10:08 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 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就够了
今天下午无聊时写的
- #include <stdio.h>
- #include <stdlib.h>
- class ttt
- {
- public:
- int board;
- bool put(unsigned short mark,short int position);
- unsigned short int check(int tempboard);
- unsigned short int compute();
- };
- bool ttt::put(unsigned short int mark,short int p)
- {
- if (!((board >> (p * 2)) %4))
- {
- int mask=mark << (p*2);
- board=board | mask;
- return true;
- }
- else
- return false;
- }
- short unsigned int ttt::check(int board)
- {
- return
- ( (board & 65793)==65793 ) | ((board & 4368)==4368) |
- ( ( (board % 64) & 21)==21 ) | ( ( (board>>6 % 64) & 21)==21 ) |
- ( ( (board>>12 % 64) & 21)==21 ) | ( ( board & 4161)==4161 ) |
- ( ( board & (4161<<2))==(4161<<2) ) | ( ( board & (4161<<4) )==(4161<<4) )? 1:
- ( (board & (65793<<1))==(65793<<1) ) | ((board & (4368<<1))==(4368<<1)) |
- ( ( (board % 64) & (21<<1))==(21<<1) ) | ( ( (board>>6 % 64) & (21<<1))==(21<<1) ) |
- ( ( (board>>12 % 64) & (21<<1))==(21<<1) ) |
- ( ( board & (4161<<1))==(4161<<1) ) | ( ( board & (4161<<3))==(4161<<3) ) |
- ( ( board & (4161<<5) )==(4161<<5) )? 2: 0;
- }
- unsigned short int ttt::compute()
- {
- for(unsigned short int i = 0;i<9;i++)
- {
- if (!((board >> (i * 2)) % 4))
- {
- if ((check(board | (2 << (i * 2)))) == 2)
- {
- return i;
- }
- }
- }
- for(unsigned short int i = 0;i<9;i++)
- {
- if (!((board >> (i * 2)) % 4))
- {
- if ((check(board | (1 << (i * 2)))) == 1)
- {
- return i;
- }
- }
- }
- if (!((board >> 8) % 4))
- return 4;
- else
- {
- for (unsigned short int i = 0; i<=4 ; i++)
- {
- if (!((board >> (i * 4)) % 4 ))
- {
- if (!(i % 2))
- {
- if ((board & 4112) != 4112)
- {
- return i * 2;
- }
- }
- else
- {
- if ((board & 65537) != 65537)
- {
- return i * 2;
- }
- }
- }
- }
- for (unsigned short int i = 0; i<=4 ; i++)
- {
- if (!(board >> (i * 4 - 2)) % 4)
- {
- return (i * 2+1);
-
- }
- }
- }
- return -1;
- }
- int drawboard(ttt *a,bool showgridnumber)
- {
- for (unsigned short int i=0;i<3;i++)
- {
- for (unsigned short int j=0;j<3;j++)
- {
- if (((a->board)>>((i*3+j)*2)&3)==1)
- printf(" o |");
- else if (((a->board)>>((i*3+j)*2)&3)==2)
- printf(" x |");
- else
- showgridnumber? printf(" (%d) ",i*3+j): printf(" |");
- }
- printf("\n");
- }
- return 0;
- }
- int main()
- {
- ttt *a = new ttt();
- bool finish=0;
- drawboard(a,true);
- while (!(a->check(a->board)) || (a->compute()>0) && !finish)
- {
- drawboard(a,false);
- printf("You marked at position: ");
- int urmove,valid=0;
- do
- {
- scanf("%d",&urmove);
- if (a->put(1,urmove))
- {
- int pcmove=0;
- pcmove=a->compute();
- printf("PC marked position: %d\n",pcmove);
- a->put(2,pcmove);
- if (a->check(a->board)==2) { printf("Computer won\n");drawboard(a,false);finish=1;}
- valid=1;
- }
- else
- {
- valid=0;
- printf("Invalid position (position is not vacant)\n Re-mark at position: ");
- }
- }while (!valid);
- }
- system("pause");
- return 0;
- }
复制代码
|
|
|
|
|
|
|
|
发表于 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。。。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|