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};
  11. int dy[]= {1, -1, 0, 0};
  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){
  20. bool out= true;
  21. for(int k=0;k<4;k++){
  22. if(ok(x+dx[k],y+dy[k])==true && a[x+dx[k]][y+dy[k]]==2){
  23. out= false; break;
  24. }
  25. }
  26. return out;
  27. }
  28.  
  29. void delay(int timer){
  30. for(int i=1;i<=timer;) i++;
  31. }
  32.  
  33. bool check(int turn){
  34. for(int i=1;i<=n;i++){
  35. for(int j=1;j<=n;j++){
  36. if(a[i][j]==turn && suit(i,j)==true) return true;
  37. }
  38. }
  39. return false;
  40. }
  41.  
  42. void print_board(){
  43. for(int i=1;i<=n;i++){
  44. for(int j=1;j<=25;j++) printf(" ");
  45. for(int j=1;j<=n;j++){
  46. if(a[i][j]==2) printf("X ");
  47. else printf("%d ", a[i][j]);
  48. }
  49. printf("\n");
  50. }
  51. }
  52.  
  53. void print_history(){
  54. printf("\n -----------------------\nLast moves:\n\n");
  55. printf(" TURN | YOU | COMPUTER\n");
  56. printf("---------------------------------\n");
  57. for(int i=1;i<=cnt;i++){
  58. printf(" %2d |", i);
  59. if(his[i][1-com][0]==-1) printf(" |");
  60. else printf(" %d - %d |", his[i][1-com][0], his[i][1-com][1]);
  61. if(his[i][com][0]==-1) printf("\n");
  62. else printf(" %d - %d \n", his[i][com][0], his[i][com][1]);
  63. }
  64. printf("\n");
  65. printf("Status: ");
  66. }
  67.  
  68. void print_rule(){
  69. 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);
  70. printf("1. First, you have to choose your team: white (0) or black (1)\n");
  71. printf("2. Second, white plays first. You and the computer will play one-by-one.\n");
  72. printf("In your turn, you have to choose a piece to pick up by entering its coordinates, row and column.\n");
  73. printf("3. 1 cell only contains 1 piece, and it will be empty (X) if a player picked up its piece.\n");
  74. printf("4. If you choose team white, you can't pick up a black piece and vice versa.\n");
  75. printf("In addition, you can't pick up a piece in a cell which shares border with a 'X' cell.\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_beta_chess'\n");
  83. for(int i=1;i<=n;i++){
  84. for(int j=1;j<=n;j++){
  85. a[i][j]= (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 team (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(a[x][y]==turn && suit(x,y)==true){
  111. z++; if(z==timer){lx= x; ly= y; a[x][y]= 2; 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 piece (row,column): ");
  121. int x, y;
  122. for(;;){
  123. scanf("%d%d", &x, &y);
  124. if(a[x][y]!=turn || suit(x,y)==false){printf("Wrong input, retype(row,column): "); continue;}
  125. else{
  126. lx= x; ly= y; a[x][y]= 2;
  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