#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 , - 1 , - 1 , 1 , 1 } ;
int dy[ ] = { 1 , - 1 , 0 , 0 , 1 , - 1 , 1 , - 1 } ;
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, int turn) {
if ( a[ x] [ y] ! = 2 ) return false ;
bool out= true ;
for ( int k= 0 ; k< 8 ; k++ ) {
if ( ok( x+ dx[ k] ,y+ dy[ k] ) == true && a[ x+ dx[ k] ] [ y+ dy[ k] ] == 1 - turn) {
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 ( suit( i,j,turn) == 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 ( "* " ) ;
else printf ( "%d " , a[ i] [ j] ) ;
}
printf ( "\n " ) ;
}
}
void print_history( ) {
printf ( "\n -----------------------\n Last 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 with all cells are empty (*) like above\n " , n, n) ;
printf ( "1. First, you have to choose your bit: 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 an empty cell and write your bit in that cell by entering its coordinates, row and column.\n " ) ;
printf ( "3. You can't choose a cell that shares at least one point with a cell which has a different bit from yours.\n " ) ;
printf ( "i.e. If you choose bit 0, then you can't write bit 0 in a cell which shares at least one point with a cell which already has bit 1 and vice versa.\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_gamma_chess'\n " ) ;
for ( int i= 1 ; i<= n; i++ ) {
for ( int j= 1 ; j<= n; j++ ) {
a[ i] [ j] = 2 ;
}
}
memset ( his, - 1 , sizeof his) ;
}
int main( ) {
init( ) ;
int turn= 0 ;
print_board( ) ; print_rule( ) ;
printf ( "Choose your bit (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 ( suit( x,y,turn) == true ) {
z++ ; if ( z== timer) { lx= x; ly= y; a[ x] [ y] = turn; 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 cell (row,column): " ) ;
int x, y;
for ( ;; ) {
scanf ( "%d%d" , & x, & y) ;
if ( suit( x,y,turn) == false ) { printf ( "Wrong input, retype(row,column): " ) ; continue ; }
else {
lx= x; ly= y; a[ x] [ y] = turn;
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+CiNpbmNsdWRlIDxjc3RyaW5nPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBuPSA4OyAvLyBuID49IDQKY29uc3QgaW50IGNvbnM9IDUwMDAwMDAwMDsKaW50IGFbMTVdWzE1XSwgbHg9IDEwMCwgbHk9IDEwMDsKaW50IGR4W109IHswLCAwLCAtMSwgMSwgLTEsIC0xLCAxLCAxfTsKaW50IGR5W109IHsxLCAtMSwgMCwgMCwgMSwgLTEsIDEsIC0xfTsKaW50IGhpc1sxMDVdWzJdWzJdLCBjbnQsIGNvbTsKCmJvb2wgb2soaW50IHgsIGludCB5KXsKICAgIGlmKHg8PTAgfHwgeTw9MCB8fCB4Pm4gfHwgeT5uKSByZXR1cm4gZmFsc2U7CiAgICByZXR1cm4gdHJ1ZTsKfQoKYm9vbCBzdWl0KGludCB4LCBpbnQgeSwgaW50IHR1cm4pewogICAgaWYoYVt4XVt5XSE9MikgcmV0dXJuIGZhbHNlOwogICAgYm9vbCBvdXQ9IHRydWU7CiAgICBmb3IoaW50IGs9MDtrPDg7aysrKXsKICAgICAgICBpZihvayh4K2R4W2tdLHkrZHlba10pPT10cnVlICYmIGFbeCtkeFtrXV1beStkeVtrXV09PTEtdHVybil7CiAgICAgICAgICAgIG91dD0gZmFsc2U7IGJyZWFrOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBvdXQ7Cn0KCnZvaWQgZGVsYXkoaW50IHRpbWVyKXsKICAgIGZvcihpbnQgaT0xO2k8PXRpbWVyOykgaSsrOwp9Cgpib29sIGNoZWNrKGludCB0dXJuKXsKICAgIGZvcihpbnQgaT0xO2k8PW47aSsrKXsKICAgICAgICBmb3IoaW50IGo9MTtqPD1uO2orKyl7CiAgICAgICAgICAgIGlmKHN1aXQoaSxqLHR1cm4pPT10cnVlKSByZXR1cm4gdHJ1ZTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gZmFsc2U7Cn0KCnZvaWQgcHJpbnRfYm9hcmQoKXsKICAgIGZvcihpbnQgaT0xO2k8PW47aSsrKXsKICAgICAgICBmb3IoaW50IGo9MTtqPD0yNTtqKyspIHByaW50ZigiICIpOwogICAgICAgIGZvcihpbnQgaj0xO2o8PW47aisrKXsKICAgICAgICAgICAgaWYoYVtpXVtqXT09MikgcHJpbnRmKCIqICIpOwogICAgICAgICAgICBlbHNlIHByaW50ZigiJWQgIiwgYVtpXVtqXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0KfQoKdm9pZCBwcmludF9oaXN0b3J5KCl7CiAgICBwcmludGYoIlxuICAgICAgICAgICAgICAgICAgICAgLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS1cbkxhc3QgbW92ZXM6XG5cbiIpOwogICAgcHJpbnRmKCIgVFVSTiB8ICAgICBZT1UgICAgIHwgICBDT01QVVRFUlxuIik7CiAgICBwcmludGYoIi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLVxuIik7CiAgICBmb3IoaW50IGk9MTtpPD1jbnQ7aSsrKXsKICAgICAgICBwcmludGYoIiAgJTJkICB8IiwgaSk7CiAgICAgICAgaWYoaGlzW2ldWzEtY29tXVswXT09LTEpIHByaW50ZigiICAgICAgICAgICAgIHwiK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YnJlYWs7fQogICAgICAgIGlmKGNoZWNrKHR1cm4pPT1mYWxzZSl7CiAgICAgICAgICAgIGlmKHR1cm49PWNvbSkgcHJpbnRmKCJZb3Ugd29uXG4iKTsKICAgICAgICAgICAgZWxzZSBwcmludGYoIkNvbXB1dGVyIHdvblxuIik7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICBpZihjb209PXR1cm4pewogICAgICAgICAgICBwcmludGYoIkNvbXB1dGVyJ3MgdHVybiwgdGhpbmtpbmcuLi5cbiIpOwogICAgICAgICAgICBpbnQgdGltZXI9IHJhbmQoKSUobipuK2x4Kmx5KSsxLCB6PSAwLCB4PSAxLCB5PSAxOwogICAgICAgICAgICBmb3IoOzspewogICAgICAgICAgICAgICAgaWYoc3VpdCh4LHksdHVybik9PXRydWUpewogICAgICAgICAgICAgICAgICAgIHorKzsgaWYoej09dGltZXIpe2x4PSB4OyBseT0geTsgYVt4XVt5XT0gdHVybjsgYnJlYWs7fQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYoeD09biAmJiB5PT1uKXt4PSAxOyB5PSAxO30KICAgICAgICAgICAgICAgIGVsc2UgaWYoeT09bil7eCsrOyB5PSAxO30KICAgICAgICAgICAgICAgIGVsc2UgeSsrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGRlbGF5KGNvbnMpOyBoaXNbY250XVtjb21dWzBdPSB4OyBoaXNbY250XVtjb21dWzFdPSB5OwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICBwcmludGYoIllvdXIgdHVybiwgY2hvb3NlIHlvdXIgY2VsbCAocm93LGNvbHVtbik6ICIpOwogICAgICAgICAgICBpbnQgeCwgeTsKICAgICAgICAgICAgZm9yKDs7KXsKICAgICAgICAgICAgICAgIHNjYW5mKCIlZCVkIiwgJngsICZ5KTsKICAgICAgICAgICAgICAgIGlmKHN1aXQoeCx5LHR1cm4pPT1mYWxzZSl7cHJpbnRmKCJXcm9uZyBpbnB1dCwgcmV0eXBlKHJvdyxjb2x1bW4pOiAiKTsgY29udGludWU7fQogICAgICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgICAgICBseD0geDsgbHk9IHk7IGFbeF1beV09IHR1cm47CiAgICAgICAgICAgICAgICAgICAgaGlzW2NudF1bMS1jb21dWzBdPSB4OyBoaXNbY250XVsxLWNvbV1bMV09IHk7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgdHVybj0gKHR1cm4rMSklMjsKICAgIH0KICAgIHByaW50ZigiSSdtIE5ndXllbiBWaWV0IER1bmcgYW5kIFRIQU5LUyBGT1IgUExBWUlORyAhISFcbiIpOwogICAgc2NhbmYoIiUqZCIpOwogICAgcmV0dXJuIDA7Cn0K