Guest User

Untitled

a guest
Apr 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define n 16
  4.  
  5. static int board[n][n];
  6. int i, j, x, y;
  7. int choice;
  8.  
  9.  
  10.  
  11. void main_menu(void)
  12. {
  13. printf("\n==========5 chess game==========\n");
  14. printf(" 1.Start a new game\n");
  15. printf(" 2.Continue\n");
  16. printf(" 3.Exit");
  17. printf("\n================================\n");
  18. printf("Enter : \n");
  19.  
  20. }
  21.  
  22. void initialize_board() /* reset a blank board */
  23. {
  24. for(i=0; i<16; i++)
  25. for(j=0; j<16; j++)
  26. board[i][j]=0;
  27. }
  28.  
  29. void read_board(void)
  30. {
  31. FILE *file = fopen("board.txt" , "r");
  32. for(i=0; i<17; i++)
  33. for(j=0; j<17; j++)
  34. fscanf(file, "%d", &board[i][j]);
  35. fclose(file);
  36. }
  37.  
  38. void write_board(void)
  39. {
  40. FILE *file = fopen("board.txt" , "w");
  41. for(i=0; i<16; i++)
  42. for(j=0; j<16; j++)
  43. fprintf(file, "%d", board[i][j]);
  44. fclose(file);
  45. }
  46.  
  47. void output_board(void) /* print the chess board */
  48. {
  49. int a=0, b=0;
  50.  
  51. for(i=0; i<16; i++) /* print the 5 chess x-coordinates */
  52. printf("%3d", a++);
  53. printf("\n");
  54.  
  55. for(i=0; i<16; i++)
  56. {
  57. printf("%2d", b++); /* print the 5 chess y-coordinates */
  58. for(j=0; j<16; j++)
  59. {
  60. if(board[i][j]==0)
  61. printf("+ ");
  62. else if(board[i][j]==1)
  63. printf("o ");
  64. else if(board[i][j]==2)
  65. printf("x ");
  66. }
  67. printf("\n");
  68. }
  69. }
  70.  
  71. void range_check(int p) /* check if the position is in the board range */
  72. {
  73. if(x>=0 && x<16 && y>=0 && y<16); //in the borad range, continue
  74.  
  75. else if(x<0 || x>=16 || y<0 || y>=16) //not in the board range, run the following statements
  76. {
  77. printf("\nError.\n");
  78. printf("\n player%d--enter the position (y/x) : ", p);
  79. scanf("%d %d", &x, &y);
  80. range_check(p);
  81. }
  82. }
  83.  
  84. void occupasion_check(int p) /* check if the position has already occupied */
  85. {
  86. if(board[x][y]==0); //not occupied, continue
  87.  
  88. else if(board[x][y]==1 || board[x][y]==2) //occupied, run the following statements
  89. {
  90. printf("\nError.\n");
  91. printf("\n player%d--enter the position (y/x) : ", p);
  92. scanf("%d %d", &x, &y);
  93. occupasion_check(p);
  94. }
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101. int main(void)
  102. {
  103. int p, k;
  104.  
  105. main_menu();
  106. scanf("%d", &choice);
  107. switch(choice)
  108. {
  109. case 1: read_board();
  110. do
  111. {
  112. /* player1 playing */
  113. system("cls");
  114. p=1;
  115. output_board();
  116. printf("\n player%d--enter the position (y/x) : ", p);
  117. scanf("%d %d", &x, &y);
  118. range_check(p);
  119. occupasion_check(p);
  120. board[x][y]=1;
  121. write_board();
  122.  
  123. /* player2 playing */
  124. system("cls");
  125. p=2;
  126. output_board();
  127. printf("\n player2--enter the position (y/x) : ", p);
  128. scanf("%d %d", &x, &y);
  129. range_check(p);
  130. occupasion_check(p);
  131. board[x][y]=2;
  132. write_board();
  133.  
  134. k=1;
  135. //k=2;
  136. }while(k==1);
  137.  
  138. break;
  139.  
  140. case 2: read_board();
  141. output_board();
  142. break;
  143.  
  144. case 0: break;
  145. }
  146. }
Add Comment
Please, Sign In to add comment