summaryrefslogtreecommitdiff
path: root/_player_mcmaxb.cpp
diff options
context:
space:
mode:
Diffstat (limited to '_player_mcmaxb.cpp')
-rw-r--r--_player_mcmaxb.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/_player_mcmaxb.cpp b/_player_mcmaxb.cpp
new file mode 100644
index 0000000..4148ba5
--- /dev/null
+++ b/_player_mcmaxb.cpp
@@ -0,0 +1,77 @@
+#include <iostream>
+#include <string>
+#include <sys/time.h>
+#include "common.h"
+
+using namespace std;
+
+const int NPLAYOUTS=300,PLAYOUTLEN=20;
+
+//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;
+ int maxballs=-1;
+ int bc;
+ for(turnidx=1;turnidx<=PLAYOUTLEN;turnidx++){ //turnidx=1: one after `me`
+ const int onturn=(me+turnidx)%NPLAYERS;
+ if(lost[onturn])continue;
+ bc=bd.ballcount(onturn);
+ if(bc==0){
+ lost[onturn]=true;
+ nlost++;
+ if(nlost==NPLAYERS)break; //? someone would have won...
+ continue;
+ }
+ if(onturn==me)maxballs=max(maxballs,bc);
+ bd.put(randommove(bd,onturn).idx(),onturn);
+ win=bd.checkwin();
+ if(win!=-1)break;
+ }
+ return maxballs;
+}
+
+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);
+ }
+}