fork download
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. const int n= 8; // n >= 4
  8. const int cons= 500000000;
  9. int a[15][15], lx= 100, ly= 100;
  10. int dx[]= {0, 0, -1, 1, -1, -1, 1, 1};
  11. int dy[]= {1, -1, 0, 0, 1, -1, 1, -1};
  12. int his[105][2][2], cnt, com;
  13.  
  14. bool ok(int x, int y){
  15. if(x<=0 || y<=0 || x>n || y>n) return false;
  16. return true;
  17. }
  18.  
  19. bool suit(int x, int y, int turn){
  20. if(a[x][y]!=2) return false;
  21. bool out= true;
  22. for(int k=0;k<8;k++){
  23. if(ok(x+dx[k],y+dy[k])==true && a[x+dx[k]][y+dy[k]]==1-turn){
  24. out= false; break;
  25. }
  26. }
  27. return out;
  28. }
  29.  
  30. void delay(int timer){
  31. for(int i=1;i<=timer;) i++;
  32. }
  33.  
  34. bool check(int turn){
  35. for(int i=1;i<=n;i++){
  36. for(int j=1;j<=n;j++){
  37. if(suit(i,j,turn)==true) return true;
  38. }
  39. }
  40. return false;
  41. }
  42.  
  43. void print_board(){
  44. for(int i=1;i<=n;i++){
  45. for(int j=1;j<=25;j++) printf(" ");
  46. for(int j=1;j<=n;j++){
  47. if(a[i][j]==2) printf("* ");
  48. else printf("%d ", a[i][j]);
  49. }
  50. printf("\n");
  51. }
  52. }
  53.  
  54. void print_history(){
  55. printf("\n -----------------------\nLast moves:\n\n");
  56. printf(" TURN | YOU | COMPUTER\n");
  57. printf("---------------------------------\n");
  58. for(int i=1;i<=cnt;i++){
  59. printf(" %2d |", i);
  60. if(his[i][1-com][0]==-1) printf(" |");
  61. else printf(" %d - %d |", his[i][1-com][0], his[i][1-com][1]);
  62. if(his[i][com][0]==-1) printf("\n");
  63. else printf(" %d - %d \n", his[i][com][0], his[i][com][1]);
  64. }
  65. printf("\n");
  66. printf("Status: ");
  67. }
  68.  
  69. void print_rule(){
  70. printf("There is a %dx%d board with all cells are empty (*) like above\n", n, n);
  71. printf("1. First, you have to choose your bit: white (0) or black (1)\n");
  72. printf("2. Second, white plays first. You and the computer will play one-by-one.\n");
  73. 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");
  74. 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");
  75. 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");
  76. printf("Computer also has to obey the rules like you.\n");
  77. printf("Player who can't make a move loses the game.\n");
  78. printf("Good luck!\n");
  79. }
  80.  
  81. void init(){
  82. printf("I call this game 'Ryo_gamma_chess'\n");
  83. for(int i=1;i<=n;i++){
  84. for(int j=1;j<=n;j++){
  85. a[i][j]= 2;
  86. }
  87. }
  88. memset(his, -1, sizeof his);
  89. }
  90.  
  91. int main(){
  92. init();
  93. int turn= 0;
  94. print_board(); print_rule();
  95. printf("Choose your bit (0/1): "); scanf("%d", &com); com= 1-com;
  96. for(;;){
  97. system("cls");
  98. printf(" CURRENT BOARD\n\n");
  99. print_board(); print_history(); if(turn==0) cnt++;
  100. if(cnt==n*n/2+1){printf("DRAW\n"); break;}
  101. if(check(turn)==false){
  102. if(turn==com) printf("You won\n");
  103. else printf("Computer won\n");
  104. break;
  105. }
  106. if(com==turn){
  107. printf("Computer's turn, thinking...\n");
  108. int timer= rand()%(n*n+lx*ly)+1, z= 0, x= 1, y= 1;
  109. for(;;){
  110. if(suit(x,y,turn)==true){
  111. z++; if(z==timer){lx= x; ly= y; a[x][y]= turn; break;}
  112. }
  113. if(x==n && y==n){x= 1; y= 1;}
  114. else if(y==n){x++; y= 1;}
  115. else y++;
  116. }
  117. delay(cons); his[cnt][com][0]= x; his[cnt][com][1]= y;
  118. }
  119. else{
  120. printf("Your turn, choose your cell (row,column): ");
  121. int x, y;
  122. for(;;){
  123. scanf("%d%d", &x, &y);
  124. if(suit(x,y,turn)==false){printf("Wrong input, retype(row,column): "); continue;}
  125. else{
  126. lx= x; ly= y; a[x][y]= turn;
  127. his[cnt][1-com][0]= x; his[cnt][1-com][1]= y;
  128. break;
  129. }
  130. }
  131. }
  132. turn= (turn+1)%2;
  133. }
  134. printf("I'm Nguyen Viet Dung and THANKS FOR PLAYING !!!\n");
  135. scanf("%*d");
  136. return 0;
  137. }
  138.  
Runtime error #stdin #stdout #stderr 0s 3104KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
sh: 1: cls: not found