无意中看到这个跟电脑玩剪刀石头布的游戏,
,其实就是根据你之前的习惯来的。但是不够强大,这个的新手推理只是根据我的出拳历史来的,找最大匹配模式,老手则是根据我跟他的出拳的最近4次历史,然后根据统计数据判断我要出什么。其实只要我随机出,还有很很大机会赢的。于是我自己写了一个,感觉比他的强一点。我的假设是,人出拳会根据自己和对方的历史出拳状态来决定下次出拳,因此我就找最大的重复历史,然后判断我当时的下一次怎么出的,然后电脑就赢我。如果对于同时判断双方的出拳历史找不到重复的话,就根据人出拳历史的重复模式预测,如果还找不到,就根据人对这三种拳的概率预测,如果概率相等,就随机出。这里面我是用0,1,2来代替的,1赢0,2赢1,0赢2。
如果感兴趣,你也可以跟我的电脑PK一下。哈哈,记得把结果告诉我哦
代码有点乱,没什么优化,就不要喷我了。。。
- #include <iostream>
-
#include <queue>
-
#include <time.h>
-
#include <string>
-
#include <fstream>
-
#include <vector>
-
#include <ctime>
-
#include <stdlib.h>
-
using namespace std;
-
-
//#define DBG
-
-
enum hand{A=0,B,C};
-
-
int win(int a , int b)
-
{
-
if(a==b)
-
return 0;
-
if(a > b)
-
{
-
if( a - b == 1 )
-
return 1;
-
if( a - b == 2 )
-
return -1;
-
}
-
if( a < b )
-
{
-
if( b - a ==1)
-
return -1;
-
if( b - a ==2)
-
return 1;
-
}
-
}
-
-
struct node
-
{
-
int a;
-
int b;
-
};
-
-
node rdq[20000];
-
int onecnt[3]={0,0,0};
-
int rdcnt[3]={0,0,0};
-
int size=0;
-
bool equ(node& aa, node& bb, bool useone)
-
{
-
if(!useone)
-
return aa.a ==bb.a && aa.b==bb.b;
-
return aa.b == bb.b;
-
}
-
int winptn(int me)
-
{
-
if(me == A)
-
return B;
-
if(me == B)
-
return C;
-
return A;
-
}
-
int getrand()
-
{
-
srand(time(NULL));
-
int a = rand()%3;
-
return a;
-
}
-
int getbig(int * onecnt)
-
{
-
if(onecnt[0]> onecnt[1] && onecnt[0]>onecnt[2])return 0;
-
if(onecnt[1]> onecnt[2] && onecnt[1]>onecnt[0])return 1;
-
if(onecnt[2]> onecnt[1] && onecnt[2]>onecnt[0])return 2;
-
-
}
-
-
int find_pattern(bool useone)
-
{
-
int sz = size;
-
int maxx = 0;
-
int place = -1;
-
rdcnt[0]= rdcnt[1] = rdcnt[2] = 0;
-
-
for(int i = sz-2;i>=0; --i)
-
{
-
int idx = sz-1;
-
int cnt = 0;
-
if(place == -1)
-
place = i;
-
while(1)
-
{
-
#ifdef DBG
-
cout<<i-cnt<<" : "<<rdq[i-cnt].a<<" "<<rdq[i-cnt].b<<" | "<<rdq[idx].a<<" "<<rdq[idx].b<<endl;
-
#endif
-
bool res = equ(rdq[i-cnt],rdq[idx - cnt],useone);
-
if(res==0)break;
-
-
cnt++;
-
if(idx<0)break;
-
}
-
#ifdef DBG
-
cout<<"at "<<i<<" and best is "<<cnt<<endl;
-
#endif
-
if(cnt == maxx)
-
{
-
rdcnt[winptn(rdq[place+1].b)]++;
-
}
-
if(cnt > maxx)
-
{
-
rdcnt[0] = rdcnt[1] = rdcnt[2] = 0;
-
maxx = cnt;
-
place = i;
-
rdcnt[winptn(rdq[place+1].b)]++;
-
}
-
}
-
if( maxx ==0 )
-
{
-
#ifdef DBG
-
cout<<"can not find repeated ptn ,use onecnt "<<endl;
-
#endif
-
if(onecnt[0]==onecnt[1]&& onecnt[1] == onecnt[2])
-
{
-
return getrand();
-
}
-
if(!useone)
-
return find_pattern(1);
-
else
-
return winptn(getbig(onecnt));
-
}
-
#ifdef DBG
-
cout<<"find best ptn at "<<place<<" and cnt is "<<maxx<<endl;
-
#endif
-
return getbig(rdcnt);
-
-
-
}
-
-
int guess()
-
{
-
if(size==0 || size==1)
-
{
-
#ifdef DBG
-
cout<<"size is not bigger than 1 ,so guess"<<endl;
-
#endif
-
return getrand();
-
}
-
-
return find_pattern(0);
-
}
-
void print()
-
{
-
cout<<"------------------------------------------------------\nAI: ";
-
for(int i=0;i<size;++i)
-
{
-
cout<<rdq[i].a<<" ";
-
}
-
cout<<"\nman:";
-
for(int i=0;i<size;++i)
-
cout<<rdq[i].b<<" ";
-
cout<<endl;
-
cout<<"one cnt"<<endl;
-
-
cout<<onecnt[0]<<" "<<onecnt[1]<<" "<<onecnt[2]<<endl;
-
cout<<"rdcnt"<<endl;
-
cout<<rdcnt[0]<<" "<<rdcnt[1]<<" "<<rdcnt[2];
-
cout<<"\n----------------------------------------------------------"<<endl;
-
}
-
int main()
-
{
-
int man;
-
int AI;
-
node tem;
-
int aiwin = 0 ;
-
int manwin=0;
-
int tiewin = 0;
-
while(cin>>man)
-
{
-
man%=3;
-
AI = guess();
-
onecnt[man]++;
-
tem.a = AI;
-
tem.b = man;
-
rdq[size++] = tem;
-
cout<<"you input "<<man<<" \nAI input "<<AI<<endl;
-
int re = win(AI,man);
-
if(re == 1)
-
cout<<"AI win"<<endl,aiwin++;
-
if(re==-1)
-
cout<<"you win"<<endl,manwin++;
-
if(re==0)
-
cout<<"tie"<<endl,tiewin++;
-
cout<<"AI win: "<<aiwin<<" you win: "<<manwin<<" tie: "<<tiewin<<endl;
-
cout<<endl;
-
#ifdef DBG
-
print();
-
#endif
-
}
-
-
}
Insert mode
阅读(3113) | 评论(0) | 转发(0) |