Chinaunix首页 | 论坛 | 博客
  • 博客访问: 485879
  • 博文数量: 137
  • 博客积分: 3874
  • 博客等级: 中校
  • 技术积分: 1475
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-05 10:50
文章分类

全部博文(137)

文章存档

2011年(37)

2010年(100)

分类: C/C++

2011-03-11 02:08:06

无意中看到这个跟电脑玩剪刀石头布的游戏,
,其实就是根据你之前的习惯来的。但是不够强大,这个的新手推理只是根据我的出拳历史来的,找最大匹配模式,老手则是根据我跟他的出拳的最近4次历史,然后根据统计数据判断我要出什么。其实只要我随机出,还有很很大机会赢的。于是我自己写了一个,感觉比他的强一点。我的假设是,人出拳会根据自己和对方的历史出拳状态来决定下次出拳,因此我就找最大的重复历史,然后判断我当时的下一次怎么出的,然后电脑就赢我。如果对于同时判断双方的出拳历史找不到重复的话,就根据人出拳历史的重复模式预测,如果还找不到,就根据人对这三种拳的概率预测,如果概率相等,就随机出。这里面我是用0,1,2来代替的,1赢0,2赢1,0赢2。
如果感兴趣,你也可以跟我的电脑PK一下。哈哈,记得把结果告诉我哦
代码有点乱,没什么优化,就不要喷我了。。。
  1. #include <iostream>
  2. #include <queue>
  3. #include <time.h>
  4. #include <string>
  5. #include <fstream>
  6. #include <vector>
  7. #include <ctime>
  8. #include <stdlib.h>
  9. using namespace std;

  10. //#define DBG

  11. enum hand{A=0,B,C};

  12. int win(int a , int b)
  13. {
  14.   if(a==b)
  15.     return 0;
  16.   if(a > b)
  17.   {
  18.     if( a - b == 1 )
  19.       return 1;
  20.     if( a - b == 2 )
  21.       return -1;
  22.   }
  23.   if( a < b )
  24.   {
  25.     if( b - a ==1)
  26.       return -1;
  27.     if( b - a ==2)
  28.       return 1;
  29.   }
  30. }

  31. struct node
  32. {
  33.   int a;
  34.   int b;
  35. };

  36. node rdq[20000];
  37. int onecnt[3]={0,0,0};
  38. int rdcnt[3]={0,0,0};
  39. int size=0;
  40. bool equ(node& aa, node& bb, bool useone)
  41. {
  42.   if(!useone)
  43.     return aa.a ==bb.a && aa.b==bb.b;
  44.   return aa.b == bb.b;
  45. }
  46. int winptn(int me)
  47. {
  48.   if(me == A)
  49.     return B;
  50.   if(me == B)
  51.     return C;
  52.   return A;
  53. }
  54. int getrand()
  55. {
  56.   srand(time(NULL));
  57.   int a = rand()%3;
  58.   return a;
  59. }
  60. int getbig(int * onecnt)
  61. {
  62.   if(onecnt[0]> onecnt[1] && onecnt[0]>onecnt[2])return 0;
  63.   if(onecnt[1]> onecnt[2] && onecnt[1]>onecnt[0])return 1;
  64.   if(onecnt[2]> onecnt[1] && onecnt[2]>onecnt[0])return 2;
  65.   
  66. }

  67. int find_pattern(bool useone)
  68. {
  69.   int sz = size;
  70.   int maxx = 0;
  71.   int place = -1;
  72.   rdcnt[0]= rdcnt[1] = rdcnt[2] = 0;
  73.   
  74.   for(int i = sz-2;i>=0; --i)
  75.   {
  76.     int idx = sz-1;
  77.     int cnt = 0;
  78.     if(place == -1)
  79.       place = i;
  80.     while(1)
  81.     {
  82. #ifdef DBG
  83.       cout<<i-cnt<<" : "<<rdq[i-cnt].a<<" "<<rdq[i-cnt].b<<" | "<<rdq[idx].a<<" "<<rdq[idx].b<<endl;
  84. #endif
  85.       bool res = equ(rdq[i-cnt],rdq[idx - cnt],useone);
  86.       if(res==0)break;
  87.       
  88.       cnt++;
  89.       if(idx<0)break;
  90.     }
  91. #ifdef DBG
  92.     cout<<"at "<<i<<" and best is "<<cnt<<endl;
  93. #endif
  94.     if(cnt == maxx)
  95.     {
  96.       rdcnt[winptn(rdq[place+1].b)]++;
  97.     }
  98.     if(cnt > maxx)
  99.     {
  100.       rdcnt[0] = rdcnt[1] = rdcnt[2] = 0;
  101.       maxx = cnt;
  102.       place = i;
  103.       rdcnt[winptn(rdq[place+1].b)]++;
  104.     }
  105.   }
  106.   if( maxx ==0 )
  107.   {
  108. #ifdef DBG
  109.     cout<<"can not find repeated ptn ,use onecnt "<<endl;
  110. #endif
  111.     if(onecnt[0]==onecnt[1]&& onecnt[1] == onecnt[2])
  112.     {
  113.       return getrand();
  114.     }
  115.     if(!useone)
  116.       return find_pattern(1);
  117.     else
  118.       return winptn(getbig(onecnt));
  119.   }
  120. #ifdef DBG
  121.   cout<<"find best ptn at "<<place<<" and cnt is "<<maxx<<endl;
  122. #endif
  123.   return getbig(rdcnt);
  124.   
  125.     
  126. }

  127. int guess()
  128. {
  129.    if(size==0 || size==1)
  130.    {
  131. #ifdef DBG
  132.      cout<<"size is not bigger than 1 ,so guess"<<endl;
  133. #endif
  134.      return getrand();
  135.    }
  136.   
  137.   return find_pattern(0);
  138. }
  139. void print()
  140. {
  141.   cout<<"------------------------------------------------------\nAI: ";
  142.   for(int i=0;i<size;++i)
  143.   {
  144.     cout<<rdq[i].a<<" ";
  145.   }
  146.   cout<<"\nman:";
  147.   for(int i=0;i<size;++i)
  148.     cout<<rdq[i].b<<" ";
  149.   cout<<endl;
  150.   cout<<"one cnt"<<endl;
  151.   
  152.   cout<<onecnt[0]<<" "<<onecnt[1]<<" "<<onecnt[2]<<endl;
  153.   cout<<"rdcnt"<<endl;
  154.   cout<<rdcnt[0]<<" "<<rdcnt[1]<<" "<<rdcnt[2];
  155.   cout<<"\n----------------------------------------------------------"<<endl;
  156. }
  157. int main()
  158. {
  159.   int man;
  160.   int AI;
  161.   node tem;
  162.   int aiwin = 0 ;
  163.   int manwin=0;
  164.   int tiewin = 0;
  165.   while(cin>>man)
  166.   {
  167.     man%=3;
  168.     AI = guess();
  169.     onecnt[man]++;
  170.     tem.a = AI;
  171.     tem.b = man;
  172.     rdq[size++] = tem;
  173.     cout<<"you input "<<man<<" \nAI input "<<AI<<endl;
  174.     int re = win(AI,man);
  175.     if(re == 1)
  176.       cout<<"AI win"<<endl,aiwin++;
  177.     if(re==-1)
  178.       cout<<"you win"<<endl,manwin++;
  179.     if(re==0)
  180.       cout<<"tie"<<endl,tiewin++;
  181.     cout<<"AI win: "<<aiwin<<" you win: "<<manwin<<" tie: "<<tiewin<<endl;
  182.     cout<<endl;
  183. #ifdef DBG
  184.     print();
  185. #endif
  186.   }
  187.   
  188. }
Insert mode
阅读(3045) | 评论(0) | 转发(0) |
0

上一篇:python 装饰器

下一篇:Android 的 Linux内核

给主人留下些什么吧!~~