Created
May 19, 2017 12:04
-
-
Save MayankPratap/491cfc9f62d244b73096c667005fa54f to your computer and use it in GitHub Desktop.
Code for AI bot which got 2nd rank in CodeWarriors event in Avishkar 2k16.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
int dir[8][2] = { -1,-1 , -1,0 , -1,1 , 0,-1 , 0,1 , 1,-1 , 1,0 , 1,1 } ; // For direction | |
int x , y ; // x for amazon movement and y for arrow movement | |
int sx , sy , s; // selected amazon x and y location | |
int p , arena[15][15] , pos[3][5][3] , p1=1 , p2=1 , tmp[15][15] ; | |
int cons[40] = {100,65,48,40,36,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37} ; | |
/* | |
p for player selected | |
arena stores arena matrix | |
pos for player positions | |
*/ | |
int ax , ay , arx , ary ; // ax and ay for amazon x and y move ; arx and ary for arrow x and y move | |
int bpts = -20000 , bmov[3][2] ; // bpts stores max point bmov stores best move | |
set< pair<int , int > > ts1 , ts2 , s1 , s2; | |
void loc(int x , int y){ | |
char ch[2]; | |
ch[0] = 'A' + x - 1; | |
ch[1] = 'A' + y - 1; | |
cout<<ch[0]<<ch[1]; | |
return ; | |
} | |
void territory(){ | |
// CREATING TEMPORARY ARENA | |
for(int i = 1 ; i<=12 ; i++) for( int j = 1 ; j<=12 ; j++) tmp[i][j] = arena[i][j]; | |
if( arena[arx][ary] == 5 ) for(int i=0; i<8 ;i++) if( arena[ arx + dir[i][0] ][ ary + dir[i][1] ] == -1 ) | |
tmp[ arx + dir[i][0] ][ ary + dir[i][1] ] = 0; | |
tmp[sx][sy] = 0 ; | |
tmp[ax][ay] = p ; | |
tmp[arx][ary] = -1 ; | |
// INITIALIZING s1 , s2 , ts1 , ts2 ; | |
s1.clear(); | |
s2.clear(); | |
ts1.clear(); | |
ts2.clear(); | |
for(int i=1 ; i<=4 ; i++){ | |
if(p==1&&s==i) | |
s1.insert( make_pair( ax , ay ) ); | |
else | |
s1.insert( make_pair( pos[1][i][1] , pos[1][i][2] ) ); | |
} | |
for(int i=1 ; i<=4 ; i++) | |
{ | |
if(p==2&&s==i) | |
s2.insert( make_pair( ax , ay ) ); | |
else | |
s2.insert( make_pair( pos[2][i][1] , pos[2][i][2] ) ); | |
} | |
//DEBUG CODE | |
/* cout<<"Player 1 postions :"<<endl; | |
for(int i=1 ; i<=4 ; i++) cout<<pos[1][i][1]<<" "<<pos[1][i][2]<<endl ; | |
cout<<"\nPlayer 2 postions :"<<endl; | |
for(int i=1 ; i<=4 ; i++) cout<<pos[2][i][1]<<" "<<pos[2][i][2]<<endl ; | |
cout<<"\n Arena:"<<endl; | |
for(int i = 1 ; i<=12 ; i++){ | |
for( int j = 1 ; j<=12 ; j++) cout<<tmp[i][j]<<" "; | |
cout<<endl; | |
} | |
cout<<"S1 :"<<endl; | |
for(auto px:s1) cout<<px.first<<" "<<px.second<<endl; | |
cout<<"S2 :"<<endl; | |
for(auto px:s2) cout<<px.first<<" "<<px.second<<endl; | |
*/ | |
// MAIN CODE | |
int pts = 0 ; | |
while( ( !s1.empty() )||( !s2.empty() ) ) | |
{ | |
for(auto px:s1) | |
{ | |
for(int i=0 ; i<8 ; i++) | |
{ | |
int t1 = px.first + dir[i][0] ; | |
int t2 = px.second + dir[i][1] ; | |
if( t1>0 && t1<=12 && t2>0 && t2<=12 && tmp[ t1 ][ t2 ] == 0 ) | |
{ | |
ts1.insert( make_pair( t1 , t2 ) ) ; | |
tmp[ t1 ][ t2 ] = -1 ; | |
if(p==2) | |
pts -= cons[i]; | |
else pts += cons[i]; | |
} | |
} | |
} | |
for( auto px:s2 ) | |
{ | |
for(int i=0 ; i<8 ; i++) | |
{ | |
int t1 = px.first + dir[i][0] ; | |
int t2 = px.second + dir[i][1] ; | |
if( t1>0 && t1<=12 && t2>0 && t2<=12 && tmp[ t1 ][ t2 ] == 0 ) | |
{ | |
ts2.insert( make_pair( t1 , t2 ) ) ; | |
tmp[ t1 ][ t2 ] = -1 ; | |
if(p==1) | |
pts -= cons[i]; | |
else | |
pts += cons[i]; | |
} | |
if( ts1.find( make_pair( t1 , t2 ) )!=ts1.end() ) | |
if(p==1) | |
pts -= cons[i]; | |
else | |
pts += cons[i]; | |
} | |
} | |
s1.clear(); | |
s2.clear(); | |
s1 = ts1 ; | |
s2 = ts2 ; | |
ts1.clear(); | |
ts2.clear(); | |
} | |
// IF SELECTED MOVE IS CURRENT BEST | |
if(pts>bpts){ | |
bpts = pts ; | |
bmov[0][0] = sx ; | |
bmov[0][1] = sy ; | |
bmov[1][0] = ax ; | |
bmov[1][1] = ay ; | |
bmov[2][0] = arx ; | |
bmov[2][1] = ary ; | |
} | |
return; | |
} | |
int main() | |
{ | |
ios_base::sync_with_stdio(false); | |
// freopen("input.txt","r",stdin); | |
// freopen("output.txt","w",stdout); | |
cin>>p; | |
for(int i=1;i<=12;i++) for(int j=1 ; j<=12 ;j++) cin>>arena[i][j] ; | |
for(int i=1;i<=12;i++) for(int j=1; j<=12 ;j++) | |
{ | |
if(arena[i][j] == 1) pos[1][p1][1] = i , pos[1][p1][2] = j , p1++ ; | |
else if(arena[i][j] == 2) pos[2][p2][1] = i , pos[2][p2][2] = j , p2++ ; | |
} | |
for( s = 1 ; s<=4 ; s++ ) { | |
sx = pos[p][s][1] ; | |
sy = pos[p][s][2] ; | |
for( int j = 0 ; j<8 ; j++) | |
{ | |
for( int x = 1 ; ; x++ ){ | |
ax = sx + x*dir[j][0] , ay = sy + x*dir[j][1] ; | |
if( ax<1 || ax>12 || ay<1 || ay>12 || arena[ax][ay] != 0 ) break; | |
for( int k = 0 ; k<8 ; k++) | |
for( int y = 1 ; ; y++) | |
{ | |
arx = ax + y*dir[k][0] , ary = ay + y*dir[k][1] ; | |
if( (arx<1 || arx>12 || ary<1 || ary>12 || ( arena[arx][ary]!=0 && arena[arx][ary]!=5) ) && !( arx==sx && ary==sy ) ) break; | |
territory(); | |
if( arena[arx][ary] == 5 ) break; | |
} | |
} | |
} | |
} | |
loc(bmov[0][0] , bmov[0][1]); | |
loc(bmov[1][0] , bmov[1][1]); | |
loc(bmov[2][0] , bmov[2][1]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment