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