main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #define WHITE 1
  4. #define BLACK -1
  5. #define BOARD_SIZE 8
  6. typedef struct {
  7. int board[BOARD_SIZE][BOARD_SIZE];
  8. int current_player;
  9. int human_color;
  10. } game_state;
  11. bool isLegalMove(int board[BOARD_SIZE][BOARD_SIZE], int row, int col, int player);
  12. void init_game(game_state* state) {
  13. // Fill the board with 0
  14. for (int i = 0; i < BOARD_SIZE; i++) {
  15. for (int j = 0; j < BOARD_SIZE; j++) {
  16. state->board[i][j] = 0;
  17. }
  18. }
  19. // Place the initial pieces
  20. int mid = BOARD_SIZE / 2;
  21. state->board[mid - 1][mid - 1] = WHITE;
  22. state->board[mid][mid] = WHITE;
  23. state->board[mid - 1][mid] = BLACK;
  24. state->board[mid][mid - 1] = BLACK;
  25. }
  26. bool isGameOver(int board[BOARD_SIZE][BOARD_SIZE]) {
  27. /* Check if the game is over by checking if there are no more moves left for either player */
  28. for (int i = 0; i < BOARD_SIZE; i++) {
  29. for (int j = 0; j < BOARD_SIZE; j++) {
  30. if (isLegalMove(board, i, j, BLACK) ||
  31. isLegalMove(board, i, j, WHITE)) {
  32. return false;
  33. }
  34. }
  35. }
  36. return true;
  37. }
  38. bool isLegalMove(int board[BOARD_SIZE][BOARD_SIZE], int row, int col, int player) {
  39. /* Check if the move is legal by checking if it results in any opponent pieces being captured */
  40. if (board[row][col] != 0) return false;
  41. int opponent = -player;
  42. for (int i = -1; i <= 1; i++) {
  43. for (int j = -1; j <= 1; j++) {
  44. if (i == 0 && j == 0) continue;
  45. int r = row + i, c = col + j;
  46. if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE && board[r][c] == opponent) {
  47. int k = 2;
  48. while (true) {
  49. r += i;
  50. c += j;
  51. if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE) break;
  52. if (board[r][c] == player) return true;
  53. if (board[r][c] == 0) break;
  54. k++;
  55. }
  56. }
  57. }
  58. }
  59. return false;
  60. }
  61. int heuristic(int board[BOARD_SIZE][BOARD_SIZE]) {
  62. /* Calculate the heuristic value of the current board. This function can
  63. be replaced with a more complex evaluation function that takes into
  64. account factors such as mobility, piece square tables, etc. */
  65. int white = 0, black = 0;
  66. for (int i = 0; i < BOARD_SIZE; i++) {
  67. for (int j = 0; j < BOARD_SIZE; j++) {
  68. if (board[i][j] == 1) white++;
  69. if (board[i][j] == -1) black++;
  70. }
  71. }
  72. return white - black;
  73. }
  74. void makeMove(game_state* state, int x, int y, int player) {
  75. /* Make a move on the board and capture any opponent pieces */
  76. state->board[x][y] = player;
  77. int opponent = -player;
  78. for (int i = -1; i <= 1; i++) {
  79. for (int j = -1; j <= 1; j++) {
  80. if (i == 0 && j == 0) continue;
  81. int r = x + i, c = y + j;
  82. if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE && state->board[r][c] == opponent) {
  83. int k = 2;
  84. while (true) {
  85. r += i;
  86. c += j;
  87. if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE) break;
  88. if (state->board[r][c] == player) {
  89. r -= i;
  90. c -= j;
  91. while (r != x || c != y) {
  92. state->board[r][c] = player;
  93. r -= i;
  94. c -= j;
  95. }
  96. break;
  97. }
  98. if (state->board[r][c] == 0) break;
  99. k++;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. // function to print the board to show black and white pieces for current game state
  106. // and display numbers for the columns and letters for the rows
  107. void print_board(int board[BOARD_SIZE][BOARD_SIZE]) {
  108. printf(" ");
  109. for (int i = 0; i < BOARD_SIZE; i++) {
  110. printf("%d ", i);
  111. }
  112. printf("\n");
  113. for (int i = 0; i < BOARD_SIZE; i++) {
  114. printf("%d ", i);
  115. for (int j = 0; j < BOARD_SIZE; j++) {
  116. if (board[i][j] == 0) {
  117. printf(". ");
  118. } else if (board[i][j] == 1) {
  119. printf("W ");
  120. } else {
  121. printf("B ");
  122. }
  123. }
  124. printf("\n");
  125. }
  126. }
  127. int main() {
  128. game_state state;
  129. init_game(&state);
  130. state.human_color = WHITE;
  131. state.current_player = WHITE;
  132. while (true) {
  133. if (isGameOver(state.board)) {
  134. // caluclate number of black and white pieces on the board
  135. // and determine the winner
  136. // print the winner
  137. int white = 0, black = 0;
  138. for (int i = 0; i < BOARD_SIZE; i++) {
  139. for (int j = 0; j < BOARD_SIZE; j++) {
  140. if (state.board[i][j] == 1) white++;
  141. if (state.board[i][j] == -1) black++;
  142. }
  143. }
  144. if (white > black) {
  145. printf("White wins!\n");
  146. } else if (black > white) {
  147. printf("Black wins!\n");
  148. } else {
  149. printf("Tie game!\n");
  150. }
  151. printf("%d - %d\n", white, black);
  152. break;
  153. }
  154. print_board(state.board);
  155. if (state.current_player == state.human_color) {
  156. int row, col;
  157. printf("Enter row and column (or -1 -1 to skip; -2 -2 to exit): ");
  158. scanf("%d %d", &row, &col);
  159. if (row == -2 && col == -2) {
  160. break;
  161. }
  162. if (row == -1 && col == -1) {
  163. state.current_player = -state.current_player;
  164. continue;
  165. }
  166. if (isLegalMove(state.board, row, col, state.current_player)) {
  167. makeMove(&state, row, col, state.current_player);
  168. state.current_player = -state.current_player;
  169. } else {
  170. printf("Illegal move!\n");
  171. }
  172. } else {
  173. int best_row = -1, best_col = -1, best_score = -1000000;
  174. for (int i = 0; i < BOARD_SIZE; i++) {
  175. for (int j = 0; j < BOARD_SIZE; j++) {
  176. if (isLegalMove(state.board, i, j, state.current_player)) {
  177. int score = heuristic(state.board);
  178. if (score > best_score) {
  179. best_score = score;
  180. best_row = i;
  181. best_col = j;
  182. }
  183. }
  184. }
  185. }
  186. if (best_row != -1) {
  187. makeMove(&state, best_row, best_col, state.current_player);
  188. } else {
  189. printf("No legal moves for computer!\n");
  190. }
  191. state.current_player = -state.current_player;
  192. }
  193. }
  194. return 0;
  195. }