diff options
Diffstat (limited to 'player_mcwin.cpp')
-rw-r--r-- | player_mcwin.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/player_mcwin.cpp b/player_mcwin.cpp new file mode 100644 index 0000000..1306f92 --- /dev/null +++ b/player_mcwin.cpp @@ -0,0 +1,75 @@ +#include <iostream> +#include <string> +#include <sys/time.h> +#include "common.h" + +using namespace std; + +const int NPLAYOUTS=300; + +//returns +turns if `me` wins, -turns if an opponent wins +int mcplayout(Board &bd,int me){ + bool lost[NPLAYERS]; + memset(lost,0,NPLAYERS*sizeof(bool)); + int nlost=0; + int win; + int turnidx; + for(turnidx=1;;turnidx++){ //turnidx=1: one after `me` + const int onturn=(me+turnidx)%NPLAYERS; + if(lost[onturn])continue; + if(bd.ballcount(onturn)==0){ + lost[onturn]=true; + nlost++; + if(nlost==NPLAYERS)break; //? someone would have won... + continue; + } + bd.put(randommove(bd,onturn).idx(),onturn); + win=bd.checkwin(); + if(win!=-1)break; + } + if(win==-1)return 0; //? + if(win==me)return turnidx; + else return -turnidx; +} + +Move calcmove(Board &bd,int me){ + int i,j; + int score,maxscore=INT_MIN,maxat=0; + for(i=0;i<WID*HEI;i++){ + Board bd2=bd; + if(!bd2.put(i,me))continue; + score=0; + for(j=0;j<NPLAYOUTS;j++) + score+=mcplayout(bd2,me); + if(score>maxscore){ + maxscore=score; + maxat=i; + } + } + return Move(maxat%WID,maxat/WID); +} + +int main(void){ + struct timeval tv; + gettimeofday(&tv,NULL); + srand(tv.tv_sec*1000000+tv.tv_usec); + + Board bd; + char c; + Move mv; + cin>>c; cin.ignore(1024,'\n'); + int me=c-'A'; + int x,y,i; + while(true){ + c=cin.peek(); + if(c=='q'||c=='Q')break; + for(i=me+1;i%NPLAYERS!=me;i++){ + cin>>x>>y; + if(x!=-1&&y!=-1)bd.put(x,y,i%NPLAYERS); + } + cin.ignore(1024,'\n'); + mv=calcmove(bd,me); + cout<<mv.x<<' '<<mv.y<<endl; + bd.put(mv.idx(),me); + } +} |