#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int n= 8; // n >= 4
const int cons= 500000000;
int a[15][15], lx= 100, ly= 100;
int dx[]= {0, 0, -1, 1};
int dy[]= {1, -1, 0, 0};
int his[105][2][2], cnt, com;
bool ok(int x, int y){
if(x<=0 || y<=0 || x>n || y>n) return false;
return true;
}
bool suit(int x, int y){
bool out= true;
for(int k=0;k<4;k++){
if(ok(x+dx[k],y+dy[k])==true && a[x+dx[k]][y+dy[k]]==2){
out= false; break;
}
}
return out;
}
void delay(int timer){
for(int i=1;i<=timer;) i++;
}
bool check(int turn){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]==turn && suit(i,j)==true) return true;
}
}
return false;
}
void print_board(){
for(int i=1;i<=n;i++){
for(int j=1;j<=25;j++) printf(" ");
for(int j=1;j<=n;j++){
if(a[i][j]==2) printf("X ");
else printf("%d ", a[i][j]);
}
printf("\n");
}
}
void print_history(){
printf("\n -----------------------\nLast moves:\n\n");
printf(" TURN | YOU | COMPUTER\n");
printf("---------------------------------\n");
for(int i=1;i<=cnt;i++){
printf(" %2d |", i);
if(his[i][1-com][0]==-1) printf(" |");
else printf(" %d - %d |", his[i][1-com][0], his[i][1-com][1]);
if(his[i][com][0]==-1) printf("\n");
else printf(" %d - %d \n", his[i][com][0], his[i][com][1]);
}
printf("\n");
printf("Status: ");
}
void print_rule(){
printf("There is a %dx%d board in which %d white pieces and %d black pieces are set like above\n", n, n, n*n/2, n*n/2);
printf("1. First, you have to choose your team: white (0) or black (1)\n");
printf("2. Second, white plays first. You and the computer will play one-by-one.\n");
printf("In your turn, you have to choose a piece to pick up by entering its coordinates, row and column.\n");
printf("3. 1 cell only contains 1 piece, and it will be empty (X) if a player picked up its piece.\n");
printf("4. If you choose team white, you can't pick up a black piece and vice versa.\n");
printf("In addition, you can't pick up a piece in a cell which shares border with a 'X' cell.\n");
printf("Computer also has to obey the rules like you.\n");
printf("Player who can't make a move loses the game.\n");
printf("Good luck!\n");
}
void init(){
printf("I call this game 'Ryo_beta_chess'\n");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
a[i][j]= (i+j)%2;
}
}
memset(his, -1, sizeof his);
}
int main(){
init();
int turn= 0;
print_board(); print_rule();
printf("Choose your team (0/1): "); scanf("%d", &com); com= 1-com;
for(;;){
system("cls");
printf(" CURRENT BOARD\n\n");
print_board(); print_history(); if(turn==0) cnt++;
if(cnt==n*n/2+1){printf("DRAW\n"); break;}
if(check(turn)==false){
if(turn==com) printf("You won\n");
else printf("Computer won\n");
break;
}
if(com==turn){
printf("Computer's turn, thinking...\n");
int timer= rand()%(n*n+lx*ly)+1, z= 0, x= 1, y= 1;
for(;;){
if(a[x][y]==turn && suit(x,y)==true){
z++; if(z==timer){lx= x; ly= y; a[x][y]= 2; break;}
}
if(x==n && y==n){x= 1; y= 1;}
else if(y==n){x++; y= 1;}
else y++;
}
delay(cons); his[cnt][com][0]= x; his[cnt][com][1]= y;
}
else{
printf("Your turn, choose your piece (row,column): ");
int x, y;
for(;;){
scanf("%d%d", &x, &y);
if(a[x][y]!=turn || suit(x,y)==false){printf("Wrong input, retype(row,column): "); continue;}
else{
lx= x; ly= y; a[x][y]= 2;
his[cnt][1-com][0]= x; his[cnt][1-com][1]= y;
break;
}
}
}
turn= (turn+1)%2;
}
printf("I'm Nguyen Viet Dung and THANKS FOR PLAYING !!!\n");
scanf("%*d");
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjc3RyaW5nPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBuPSA4OyAvLyBuID49IDQKY29uc3QgaW50IGNvbnM9IDUwMDAwMDAwMDsKaW50IGFbMTVdWzE1XSwgbHg9IDEwMCwgbHk9IDEwMDsKaW50IGR4W109IHswLCAwLCAtMSwgMX07CmludCBkeVtdPSB7MSwgLTEsIDAsIDB9OwppbnQgaGlzWzEwNV1bMl1bMl0sIGNudCwgY29tOwoKYm9vbCBvayhpbnQgeCwgaW50IHkpewogICAgaWYoeDw9MCB8fCB5PD0wIHx8IHg+biB8fCB5Pm4pIHJldHVybiBmYWxzZTsKICAgIHJldHVybiB0cnVlOwp9Cgpib29sIHN1aXQoaW50IHgsIGludCB5KXsKICAgIGJvb2wgb3V0PSB0cnVlOwogICAgZm9yKGludCBrPTA7azw0O2srKyl7CiAgICAgICAgaWYob2soeCtkeFtrXSx5K2R5W2tdKT09dHJ1ZSAmJiBhW3grZHhba11dW3krZHlba11dPT0yKXsKICAgICAgICAgICAgb3V0PSBmYWxzZTsgYnJlYWs7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIG91dDsKfQoKdm9pZCBkZWxheShpbnQgdGltZXIpewogICAgZm9yKGludCBpPTE7aTw9dGltZXI7KSBpKys7Cn0KCmJvb2wgY2hlY2soaW50IHR1cm4pewogICAgZm9yKGludCBpPTE7aTw9bjtpKyspewogICAgICAgIGZvcihpbnQgaj0xO2o8PW47aisrKXsKICAgICAgICAgICAgaWYoYVtpXVtqXT09dHVybiAmJiBzdWl0KGksaik9PXRydWUpIHJldHVybiB0cnVlOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBmYWxzZTsKfQoKdm9pZCBwcmludF9ib2FyZCgpewogICAgZm9yKGludCBpPTE7aTw9bjtpKyspewogICAgICAgIGZvcihpbnQgaj0xO2o8PTI1O2orKykgcHJpbnRmKCIgIik7CiAgICAgICAgZm9yKGludCBqPTE7ajw9bjtqKyspewogICAgICAgICAgICBpZihhW2ldW2pdPT0yKSBwcmludGYoIlggIik7CiAgICAgICAgICAgIGVsc2UgcHJpbnRmKCIlZCAiLCBhW2ldW2pdKTsKICAgICAgICB9CiAgICAgICAgcHJpbnRmKCJcbiIpOwogICAgfQp9Cgp2b2lkIHByaW50X2hpc3RvcnkoKXsKICAgIHByaW50ZigiXG4gICAgICAgICAgICAgICAgICAgICAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tL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AgICBwcmludGYoIkNob29zZSB5b3VyIHRlYW0gKDAvMSk6ICIpOyBzY2FuZigiJWQiLCAmY29tKTsgY29tPSAxLWNvbTsKICAgIGZvcig7Oyl7CiAgICAgICAgc3lzdGVtKCJjbHMiKTsKICAgICAgICBwcmludGYoIiAgICAgICAgICAgICAgICAgICAgICAgICAgQ1VSUkVOVCBCT0FSRFxuXG4iKTsKICAgICAgICBwcmludF9ib2FyZCgpOyBwcmludF9oaXN0b3J5KCk7IGlmKHR1cm49PTApIGNudCsrOwogICAgICAgIGlmKGNudD09bipuLzIrMSl7cHJpbnRmKCJEUkFXXG4iKTsgYnJlYWs7fQogICAgICAgIGlmKGNoZWNrKHR1cm4pPT1mYWxzZSl7CiAgICAgICAgICAgIGlmKHR1cm49PWNvbSkgcHJpbnRmKCJZb3Ugd29uXG4iKTsKICAgICAgICAgICAgZWxzZSBwcmludGYoIkNvbXB1dGVyIHdvblxuIik7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICBpZihjb209PXR1cm4pewogICAgICAgICAgICBwcmludGYoIkNvbXB1dGVyJ3MgdHVybiwgdGhpbmtpbmcuLi5cbiIpOwogICAgICAgICAgICBpbnQgdGltZXI9IHJhbmQoKSUobipuK2x4Kmx5KSsxLCB6PSAwLCB4PSAxLCB5PSAxOwogICAgICAgICAgICBmb3IoOzspewogICAgICAgICAgICAgICAgaWYoYVt4XVt5XT09dHVybiAmJiBzdWl0KHgseSk9PXRydWUpewogICAgICAgICAgICAgICAgICAgIHorKzsgaWYoej09dGltZXIpe2x4PSB4OyBseT0geTsgYVt4XVt5XT0gMjsgYnJlYWs7fQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYoeD09biAmJiB5PT1uKXt4PSAxOyB5PSAxO30KICAgICAgICAgICAgICAgIGVsc2UgaWYoeT09bil7eCsrOyB5PSAxO30KICAgICAgICAgICAgICAgIGVsc2UgeSsrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGRlbGF5KGNvbnMpOyBoaXNbY250XVtjb21dWzBdPSB4OyBoaXNbY250XVtjb21dWzFdPSB5OwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICBwcmludGYoIllvdXIgdHVybiwgY2hvb3NlIHlvdXIgcGllY2UgKHJvdyxjb2x1bW4pOiAiKTsKICAgICAgICAgICAgaW50IHgsIHk7CiAgICAgICAgICAgIGZvcig7Oyl7CiAgICAgICAgICAgICAgICBzY2FuZigiJWQlZCIsICZ4LCAmeSk7CiAgICAgICAgICAgICAgICBpZihhW3hdW3ldIT10dXJuIHx8IHN1aXQoeCx5KT09ZmFsc2Upe3ByaW50ZigiV3JvbmcgaW5wdXQsIHJldHlwZShyb3csY29sdW1uKTogIik7IGNvbnRpbnVlO30KICAgICAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICAgICAgbHg9IHg7IGx5PSB5OyBhW3hdW3ldPSAyOwogICAgICAgICAgICAgICAgICAgIGhpc1tjbnRdWzEtY29tXVswXT0geDsgaGlzW2NudF1bMS1jb21dWzFdPSB5OwogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHR1cm49ICh0dXJuKzEpJTI7CiAgICB9CiAgICBwcmludGYoIkknbSBOZ3V5ZW4gVmlldCBEdW5nIGFuZCBUSEFOS1MgRk9SIFBMQVlJTkcgISEhXG4iKTsKICAgIHNjYW5mKCIlKmQiKTsKICAgIHJldHVybiAwOwp9Cg==