#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int n= 4; // 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;
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("The cell in which a piece is picked up last turn will have '#' symbol.\n");
printf("3. 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 '#' cell.\n");
printf("4. 1 cell only contains 1 piece, and it will be empty (X) if a player picked up its piece.\n");
printf("Computer also has to obey the rules like you.\n");
printf("If all the pieces are picked up, it will be a DRAW.\n");
printf("Else, player who can't make a move loses the game.\n");
printf("Good luck!\n");
}
void init(){
printf("I call this game 'Ryo_alpha_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);
}
void delay(int timer){
for(int i=1;i<=timer;) i++;
}
bool ok(int x, int y){
if(x<=0 || y<=0 || x>n || y>n) return false;
return true;
}
bool check(int turn){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
bool out= false;
int x= i, y= j;
if(a[i][j]==turn){
for(int k=0;k<4;k++){
if(ok(x+dx[k],y+dy[k])==true && lx==x+dx[k] && ly==y+dy[k]){
out= true; break;
}
}
if(!out) return true;
}
}
}
}
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(i==lx && j==ly) printf("# ");
else 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: ");
}
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(;;){
bool have= false;
for(int i=0;i<4;i++){
if(ok(x+dx[i],y+dy[i])==true && lx==x+dx[i] && ly==y+dy[i]){
have= true; break;
}
}
if(a[x][y]==turn && have==false){
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);
bool have= false;
if(a[x][y]!=turn) have= true;
else{
for(int i=0;i<4;i++){
if(ok(x+dx[i],y+dy[i])==true && lx==x+dx[i] && ly==y+dy[i]){
have= true; break;
}
}
}
if(have){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 NVD and THANKS FOR PLAYING !!!\n");
scanf("%*d");
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjc3RyaW5nPgoKdXNpb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AgICAgZm9yKGludCBqPTE7ajw9bjtqKyspewogICAgICAgICAgICBib29sIG91dD0gZmFsc2U7CiAgICAgICAgICAgIGludCB4PSBpLCB5PSBqOwogICAgICAgICAgICBpZihhW2ldW2pdPT10dXJuKXsKICAgICAgICAgICAgICAgIGZvcihpbnQgaz0wO2s8NDtrKyspewogICAgICAgICAgICAgICAgICAgIGlmKG9rKHgrZHhba10seStkeVtrXSk9PXRydWUgJiYgbHg9PXgrZHhba10gJiYgbHk9PXkrZHlba10pewogICAgICAgICAgICAgICAgICAgICAgICBvdXQ9IHRydWU7IGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmKCFvdXQpIHJldHVybiB0cnVlOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIHByaW50X2JvYXJkKCl7CiAgICBmb3IoaW50IGk9MTtpPD1uO2krKyl7CiAgICAgICAgZm9yKGludCBqPTE7ajw9MjU7aisrKSBwcmludGYoIiAiKTsKICAgICAgICBmb3IoaW50IGo9MTtqPD1uO2orKyl7CiAgICAgICAgICAgIGlmKGk9PWx4ICYmIGo9PWx5KSBwcmludGYoIiMgIik7CiAgICAgICAgICAgIGVsc2UgaWYoYVtpXVtqXT09MikgcHJpbnRmKCJYICIpOwogICAgICAgICAgICBlbHNlIHByaW50ZigiJWQgIiwgYVtpXVtqXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0KfQoKdm9pZCBwcmludF9oaXN0b3J5KCl7CiAgICBwcmludGYoIlxuICAgICAgICAgICAgICAgICAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tXG5MYXN0IG1vdmVzOlxuXG4iKTsKICAgIHByaW50ZigiIFRVUk4gfCAgICAgWU9VICAgICB8ICAgQ09NUFVURVJcbiIpOwogICAgcHJpbnRmKCItLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS1cbiIpOwogICAgZm9yKGludCBpPTE7aTw9Y250O2krKyl7CiAgICAgICAgcHJpbnRmKCIgICUyZCAgfCIsIGkpOwogICAgICAgIGlmKGhpc1tpXVsxLWNvbV1bMF09PS0xKSBwcmludGYoIiAgICAgICAgICAgICB8Iik7CiAgICAgICAgZWxzZSBwcmludGYoIiAgICAlZCAtICVkICAgIHwiLCBoaXNbaV1bMS1jb21dWzBdLCBoaXNbaV1bMS1jb21dWzFdKTsKICAgICAgICBpZihoaXNbaV1bY29tXVswXT09LTEpIHByaW50ZigiXG4iKTsKICAgICAgICBlbHNlIHByaW50ZigiICAgICVkIC0gJWQgICBcbiIsIGhpc1tpXVtjb21dWzBdLCBoaXNbaV1bY29tXVsxXSk7CiAgICB9CiAgICBwcmludGYoIlxuIik7CiAgICBwcmludGYoIlN0YXR1czogIik7Cn0KCmludCBtYWluKCl7CiAgICBpbml0KCk7CiAgICBpbnQgdHVybj0gMDsKICAgIHByaW50X2JvYXJkKCk7IHByaW50X3J1bGUoKTsKICAgIHByaW50ZigiQ2hvb3NlIHlvdXIgdGVhbSAoMC8xKTogIik7IHNjYW5mKCIlZCIsICZjb20pOyBjb209IDEtY29tOwogICAgZm9yKDs7KXsKICAgICAgICBzeXN0ZW0oImNscyIpOwogICAgICAgIHByaW50ZigiICAgICAgICAgICAgICAgICAgICAgIENVUlJFTlQgQk9BUkRcblxuIik7CiAgICAgICAgcHJpbnRfYm9hcmQoKTsgcHJpbnRfaGlzdG9yeSgpOyBpZih0dXJuPT0wKSBjbnQrKzsKICAgICAgICBpZihjbnQ9PW4qbi8yKzEpe3ByaW50ZigiRFJBV1xuIik7IGJyZWFrO30KICAgICAgICBpZihjaGVjayh0dXJuKT09ZmFsc2UpewogICAgICAgICAgICBpZih0dXJuPT1jb20pIHByaW50ZigiWW91IHdvblxuIik7CiAgICAgICAgICAgIGVsc2UgcHJpbnRmKCJDb21wdXRlciB3b25cbiIpOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgICAgaWYoY29tPT10dXJuKXsKICAgICAgICAgICAgcHJpbnRmKCJDb21wdXRlcidzIHR1cm4sIHRoaW5raW5nLi4uXG4iKTsKICAgICAgICAgICAgaW50IHRpbWVyPSByYW5kKCklKG4qbitseCpseSkrMSwgej0gMCwgeD0gMSwgeT0gMTsKICAgICAgICAgICAgZm9yKDs7KXsKICAgICAgICAgICAgICAgIGJvb2wgaGF2ZT0gZmFsc2U7CiAgICAgICAgICAgICAgICBmb3IoaW50IGk9MDtpPDQ7aSsrKXsKICAgICAgICAgICAgICAgICAgICBpZihvayh4K2R4W2ldLHkrZHlbaV0pPT10cnVlICYmIGx4PT14K2R4W2ldICYmIGx5PT15K2R5W2ldKXsKICAgICAgICAgICAgICAgICAgICAgICAgaGF2ZT0gdHJ1ZTsgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYoYVt4XVt5XT09dHVybiAmJiBoYXZlPT1mYWxzZSl7CiAgICAgICAgICAgICAgICAgICAgeisrOyBpZih6PT10aW1lcil7bHg9IHg7IGx5PSB5OyBhW3hdW3ldPSAyOyBicmVhazt9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZih4PT1uICYmIHk9PW4pe3g9IDE7IHk9IDE7fQogICAgICAgICAgICAgICAgZWxzZSBpZih5PT1uKXt4Kys7IHk9IDE7fQogICAgICAgICAgICAgICAgZWxzZSB5Kys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZGVsYXkoY29ucyk7IGhpc1tjbnRdW2NvbV1bMF09IHg7IGhpc1tjbnRdW2NvbV1bMV09IHk7CiAgICAgICAgfQogICAgICAgIGVsc2V7CiAgICAgICAgICAgIHByaW50ZigiWW91ciB0dXJuLCBjaG9vc2UgeW91ciBwaWVjZSAocm93LGNvbHVtbik6ICIpOwogICAgICAgICAgICBpbnQgeCwgeTsKICAgICAgICAgICAgZm9yKDs7KXsKICAgICAgICAgICAgICAgIHNjYW5mKCIlZCVkIiwgJngsICZ5KTsKICAgICAgICAgICAgICAgIGJvb2wgaGF2ZT0gZmFsc2U7CiAgICAgICAgICAgICAgICBpZihhW3hdW3ldIT10dXJuKSBoYXZlPSB0cnVlOwogICAgICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgICAgICBmb3IoaW50IGk9MDtpPDQ7aSsrKXsKICAgICAgICAgICAgICAgICAgICAgICAgaWYob2soeCtkeFtpXSx5K2R5W2ldKT09dHJ1ZSAmJiBseD09eCtkeFtpXSAmJiBseT09eStkeVtpXSl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBoYXZlPSB0cnVlOyBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmKGhhdmUpe3ByaW50ZigiV3JvbmcgaW5wdXQsIHJldHlwZShyb3csY29sdW1uKTogIik7IGNvbnRpbnVlO30KICAgICAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICAgICAgbHg9IHg7IGx5PSB5OyBhW3hdW3ldPSAyOwogICAgICAgICAgICAgICAgICAgIGhpc1tjbnRdWzEtY29tXVswXT0geDsgaGlzW2NudF1bMS1jb21dWzFdPSB5OwogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHR1cm49ICh0dXJuKzEpJTI7CiAgICB9CiAgICBwcmludGYoIkknbSBOVkQgYW5kIFRIQU5LUyBGT1IgUExBWUlORyAhISFcbiIpOwogICAgc2NhbmYoIiUqZCIpOwogICAgcmV0dXJuIDA7Cn0K