Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define ROWS 5
  4. #define COLUMNS 5
  5. #define marker1 'X'
  6. #define marker2 '0'
  7. #define WINNER_1 1
  8. #define WINNER_2 2
  9. #define DRAW 3
  10. #define NO_WINNER 0
  11. void reset_board(char(*board)[COLUMNS]);
  12. void draw_board(char(*board)[COLUMNS]);
  13. //void user_position();
  14. int check_board_position(int i,int j,char (*board)[COLUMNS]);
  15. void random_setup(char (*board)[COLUMNS]);
  16. void humanvshuman(char (*board)[COLUMNS]);
  17. int check_winner(char (*board)[COLUMNS]);
  18. void humanvscomputer(int gamemode,char (*board)[COLUMNS]);
  19. //void check_computer_wins(char (*board)[COLUMNS]);
  20. //void check_human_wins(char (*board)[COLUMNS]);
  21.  
  22. int main(void)
  23. {
  24. int game_mode, firstplayer;
  25. char myboard[ROWS][COLUMNS], player1, player2;
  26. printf("Select a game mode:\n");
  27. printf("1 - Human vs computer\n");
  28. printf("2 - Human vs human\n");
  29. fflush(stdin);
  30. scanf("%d", &game_mode);
  31. if (game_mode==1)
  32. {
  33. printf("Would you like to go: \n1 - First \n2 - Second\n");
  34. fflush(stdin);
  35. scanf("%d", &firstplayer);
  36.  
  37. reset_board(myboard);
  38. draw_board(myboard);
  39.  
  40. if(firstplayer == 1)
  41. {
  42. player1 = marker1;
  43. player2 = marker2;
  44. humanvscomputer(game_mode,myboard);
  45. }
  46. else
  47. {
  48. player1 = marker2;
  49. player2 = marker1;
  50. }
  51.  
  52. }
  53.  
  54.  
  55. if(game_mode ==2)
  56. {
  57. do
  58. {
  59. humanvshuman(myboard);
  60. draw_board(myboard);
  61. check_winner(myboard);
  62.  
  63. } while (check_winner(myboard) != -1);
  64.  
  65.  
  66. }
  67.  
  68. // if (game_mode == 1)
  69. // {
  70. // humanvscomputer(game_mode,myboard);
  71. // draw_board(myboard);
  72. // check_winner(myboard);
  73. }
  74.  
  75.  
  76. void reset_board(char(*board)[COLUMNS])
  77. {
  78. int i,j;
  79. for(i=0; i<ROWS; i++)
  80. {
  81. for(j=0; j<COLUMNS; j++)
  82. {
  83. board[i][j]=' ';
  84. }
  85. }
  86. }
  87.  
  88. void draw_board(char (*board)[COLUMNS])
  89. {
  90. int i, j;
  91. printf(" ");
  92. for(j=0; j<COLUMNS; j++)
  93. {
  94. printf(" %d ", j);
  95. }
  96. printf("\n");
  97.  
  98. for(i=0; i<ROWS; i++)
  99. {
  100. printf(" ");
  101. for(j=0; j<COLUMNS; j++)
  102. {
  103. printf("+---");
  104. }
  105. printf("+\n");
  106. printf("%d ", i);
  107.  
  108. for(j=0; j<COLUMNS; j++)
  109. {
  110. printf("| %c ", board[i][j]);
  111. }
  112. printf("|\n");
  113. }
  114. printf(" ");
  115. for(j=0; j<COLUMNS; j++)
  116. {
  117. printf("+---");
  118. }
  119. printf("+\n");
  120. }
  121.  
  122.  
  123. int check_board_position(int i,int j,char (*board)[COLUMNS])
  124. {
  125. int n;
  126. if(board[i][j] != ' ')
  127. {
  128. draw_board(board);
  129. printf("\nInvalid position %d, %d, %c \n", i, j, board[i][j]);
  130. //humanvshuman(board);
  131. n=0; // Indicates an invalid position
  132. }
  133. else{
  134. n=1;
  135. }
  136. return n;
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. void random_setup(char (*myboard)[COLUMNS])
  145. {
  146. reset_board(myboard);
  147.  
  148. myboard[0][1] = 'X';
  149. myboard[1][1] = 'X';
  150. myboard[2][1] = 'X';
  151. myboard[3][1] = 'O';
  152. myboard[4][1] = 'O';
  153. }
  154.  
  155. void humanvshuman (char (*board)[COLUMNS])
  156. {
  157.  
  158. int a,b;
  159.  
  160.  
  161.  
  162. printf("Player 1 enter which row you would like to go in: ");
  163. fflush(stdin);
  164. scanf("%d", &a);
  165. printf("Player 1 enter which column you would like to go in: ");
  166. fflush(stdin);
  167. scanf("%d", &b);
  168.  
  169. board[a][b]='X';
  170.  
  171.  
  172.  
  173.  
  174. check_winner(board);
  175. int c,d;
  176. printf("\nPlayer 2 enter which row you would like to go in: ");
  177. fflush(stdin);
  178. scanf("%d", &c);
  179. printf("Player 2 enter which column you would like to go in: ");
  180. fflush(stdin);
  181. scanf("%d", &d);
  182. if (check_board_position(c,d,board))
  183. {
  184. board[c][d]='0';
  185. }
  186.  
  187.  
  188. check_board_position(c,d,board);
  189. check_winner(board);
  190. }
  191.  
  192. int check_winner(char (*board)[COLUMNS])
  193. {
  194. int i, j, nof_markers_1, nof_markers_2, empty_board_positions;
  195.  
  196. // Check rows
  197. for(i=0; i<ROWS; i++)
  198. {
  199. nof_markers_1 = nof_markers_2 = 0;
  200. for(j=0; j<COLUMNS; j++)
  201. {
  202. switch(board[i][j])
  203. {
  204. case marker1:
  205. nof_markers_1++;
  206. break;
  207. case marker2:
  208. nof_markers_2++;
  209. break;
  210. }
  211. }
  212. if(nof_markers_1 == COLUMNS)
  213. return WINNER_1;
  214. if(nof_markers_2 == COLUMNS)
  215. return WINNER_2;
  216. }
  217.  
  218. // Check cols
  219. for(j=0; j<COLUMNS; j++)
  220. {
  221. nof_markers_1 = nof_markers_2 = 0;
  222. for(i=0; i<ROWS; i++)
  223. {
  224. switch(board[i][j])
  225. {
  226. case marker1:
  227. nof_markers_1++;
  228. break;
  229. case marker2:
  230. nof_markers_2++;
  231. break;
  232. }
  233. }
  234. if(nof_markers_1 == COLUMNS)
  235. return WINNER_1;
  236. if(nof_markers_2 == COLUMNS)
  237. return WINNER_2;
  238. }
  239.  
  240. // Check main diagonal
  241. nof_markers_1 = nof_markers_2 = 0;
  242. for(j=0; j<COLUMNS; j++)
  243. {
  244. switch(board[j][j])
  245. {
  246. case marker1:
  247. nof_markers_1++;
  248. break;
  249. case marker2:
  250. nof_markers_2++;
  251. break;
  252. }
  253. }
  254. if(nof_markers_1 == COLUMNS)
  255. return WINNER_1;
  256. if(nof_markers_2 == COLUMNS)
  257. return WINNER_2;
  258.  
  259. // Check the other diagonal
  260. nof_markers_1 = nof_markers_2 = 0;
  261. for(j=0; j<COLUMNS; j++)
  262. {
  263. switch(board[j][COLUMNS-1-j])
  264. {
  265. case marker1:
  266. nof_markers_1++;
  267. break;
  268. case marker2:
  269. nof_markers_2++;
  270. break;
  271. }
  272. }
  273. if(nof_markers_1 == COLUMNS)
  274. return WINNER_1;
  275. if(nof_markers_2 == COLUMNS)
  276. return WINNER_2;
  277.  
  278. // Check if board is full
  279. empty_board_positions = 0;
  280. for(i=0; i<ROWS; i++)
  281. {
  282. for(j=0; j<COLUMNS; j++)
  283. {
  284. if(board[i][j] == ' ')
  285. {
  286. empty_board_positions++;
  287. }
  288. }
  289. }
  290. if(empty_board_positions == 0)
  291. return DRAW;
  292. else
  293. return NO_WINNER; // The game goes on
  294.  
  295. }
  296.  
  297. void humanvscomputer(int gamemode,char (*board)[COLUMNS])
  298. {
  299. int firstplayer;
  300. firstplayer=gamemode;
  301.  
  302. if(firstplayer == 1)
  303. {
  304. int a,b;
  305.  
  306. printf("Enter which row you would like to go in: ");
  307. fflush(stdin);
  308. scanf("%d", &a);
  309. printf("Enter which column you would like to go in: ");
  310. fflush(stdin);
  311. scanf("%d", &b);
  312. if (check_board_position(a, b, board))
  313. {
  314. board[a][b]='X';
  315.  
  316. }
  317. }
  318. draw_board(board);
  319.  
  320. // Computer's turn
  321.  
  322. //check_computer_wins(myboard);
  323. // check_human_wins(myboard);
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement