Advertisement
Guest User

Untitled

a guest
May 6th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. //
  2. // main.c
  3. // HW3
  4. //
  5. // Created by Muhammad Hleihel on 5/1/15.
  6. // Copyright (c) 2015 Muhammad Hleihel. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10.  
  11. void InitiateGame();
  12. void PrintBoard(char Board[6][7]);
  13. void InitiateBoard(char Board[6][7]);
  14. void PrintTurn(char turn);
  15. void StartGame(char Board[6][7]);
  16. int ScanMove(char Board[6][7], char turn, int MovesArr[42], int *MovesCnt);
  17. int CheckMove(char Board[6][7], char turn, int move, int MovesArr[42], int *MovesCnt);
  18. int InRange(int move);
  19. int IsFull(char Board[6][7], int column, char turn);
  20. int EmptyCell(char Board[6][7], int column);
  21. void Undo(char Board[6][7], int MovesArr[42], int *MovesCnt);
  22. int Last_Move(char Board[6][7], int MovesArr[42], int *MovesCnt);
  23. char CheckWin(char Board[6][7]);
  24. char CheckWin_Vertical(char Board[6][7]);
  25. char CheckWin_RDiagonal(char Board[6][7]);
  26. char CheckWin_DDiagonal(char Board[6][7]);
  27.  
  28. int main()
  29. {
  30. InitiateGame();
  31. return 0;
  32. }
  33.  
  34. void InitiateGame()
  35. {
  36. char Board[6][7];;
  37. printf("Welcome to Connect-4!\n\n");
  38. InitiateBoard(Board);
  39. PrintBoard(Board);
  40. StartGame(Board);
  41. }
  42.  
  43. void StartGame(char Board[6][7])
  44. {
  45. char last_turn = 'o';
  46. int MovesArr[43] = {0};
  47. int MovesCnt = 0;
  48. int tmp = 0;
  49. while(1)
  50. {
  51. if(MovesCnt == 42)
  52. {
  53. printf("The game has ended! It's a tie.");
  54. break;
  55. }
  56. PrintTurn((last_turn == 'o')?('*'):('o'));
  57. tmp = ScanMove(Board, (last_turn == 'o')?('*'):('o'), MovesArr, &MovesCnt);
  58. if(!tmp)
  59. {
  60. printf("You should have entered a number! We are doomed...");
  61. break;
  62. }
  63. else if(tmp == '*' || tmp == 'o')
  64. {
  65. printf("The game has ended! Player %c has won!!!!!", tmp);
  66. break;
  67. }
  68. last_turn = (last_turn == 'o')?('*'):('o');
  69. }
  70. }
  71.  
  72. int InRange(int move)
  73. {
  74. return (move >= 0 && move <= 7)?1:0;
  75. }
  76.  
  77. int ScanMove(char Board[6][7], char turn, int MovesArr[42], int *MovesCnt)
  78. {
  79. int move;
  80. if(scanf("%d", &move) != 1)
  81. {
  82. return 0;
  83. }
  84. CheckMove(Board, turn, move-1, MovesArr, MovesCnt);
  85. char win = CheckWin(Board);
  86. if(win == '*' || win == 'o') return win;
  87. return 1;
  88. }
  89.  
  90.  
  91. char CheckWin(char Board[6][7])
  92. {
  93. char tmp = CheckWin_Vertical(Board);
  94. if(tmp != 0) return tmp;
  95. tmp = CheckWin_RDiagonal(Board);
  96. if(tmp != 0) return tmp;
  97. return CheckWin_DDiagonal(Board);
  98. }
  99.  
  100. char CheckWin_Vertical(char Board[6][7])
  101. {
  102. int s_count = 0;
  103. int o_count = 0;
  104. for(int i = 0; i < 7; i++)
  105. {
  106. for(int j = 0; j < 3; j++)
  107. {
  108. for(int k = 0; k <= 3; k++)
  109. {
  110. if(Board[j+k][i] == '*') s_count ++;
  111. if(Board[j+k][i] == 'o') o_count ++;
  112. if(s_count == 4) return Board[j+k][i];
  113. if(o_count == 4) return Board[j+k][i];
  114. }
  115. s_count = 0;
  116. o_count = 0;
  117. }
  118. }
  119. return 0;
  120. }
  121.  
  122. char CheckWin_RDiagonal(char Board[6][7])
  123. {
  124. int s_count = 0;
  125. int o_count = 0;
  126. for(int i = 0; i < 4; i++)
  127. {
  128. for(int j = 5; j >= 3; j--)
  129. {
  130. for(int k = 0; k <= 3; k++)
  131. {
  132. if(Board[j-k][i+k] == '*') s_count ++;
  133. if(Board[j-k][i+k] == 'o') o_count ++;
  134. if(s_count == 4 || o_count == 4) return Board[j-k][k+i];
  135. }
  136. s_count = 0;
  137. o_count = 0;
  138. }
  139. }
  140. return 0;
  141. }
  142.  
  143. char CheckWin_DDiagonal(char Board[6][7])
  144. {
  145. int s_count = 0;
  146. int o_count = 0;
  147. for(int i = 0; i < 4; i++)
  148. {
  149. for(int j = 0; j <= 2; j++)
  150. {
  151. for(int k = 0; k <= 3; k++)
  152. {
  153. if(Board[j+k][i+k] == '*') s_count ++;
  154. if(Board[j+k][i+k] == 'o') o_count ++;
  155. if(s_count == 4 || o_count == 4) return Board[j+k][k+i];
  156. }
  157. s_count = 0;
  158. o_count = 0;
  159. }
  160. }
  161. return 0;
  162. }
  163.  
  164. void Undo(char Board[6][7], int MovesArr[42], int *MovesCnt)
  165. {
  166. if(*MovesCnt > 0)
  167. {
  168. Board[Last_Move(Board, MovesArr, MovesCnt)][MovesArr[*MovesCnt]] = ' ';
  169. --*MovesCnt;
  170. }
  171. }
  172.  
  173. int Last_Move(char Board[6][7], int MovesArr[42], int *MovesCnt)
  174. {
  175. int last = 0;
  176. for(int i = 0; i < 6; ++i)
  177. {
  178. if(Board[i][MovesArr[*MovesCnt]] != ' ')
  179. {
  180. last = i;
  181. break;
  182. }
  183. }
  184. return last;
  185. }
  186.  
  187. int CheckMove(char Board[6][7], char turn, int move, int MovesArr[42], int *MovesCnt)
  188. {
  189. if(!(move+1))
  190. {
  191. Undo(Board, MovesArr, MovesCnt);
  192. }
  193. else
  194. {
  195. if(!InRange(move+1))
  196. {
  197. printf("Invalid move. Input move is out of range.\nPlease try again:");
  198. ScanMove(Board, turn, MovesArr, MovesCnt);
  199. return 0;
  200. }
  201. if(IsFull(Board, move, turn) == 1)
  202. {
  203. printf("Invalid move. Column %d is full.\nPlease try again:", move+1);
  204. ScanMove(Board, turn, MovesArr, MovesCnt);
  205. return 0;
  206. }
  207. Board[EmptyCell(Board, move)][move] = turn;
  208. MovesArr[++*MovesCnt] = move;
  209. }
  210. PrintBoard(Board);
  211. return 1;
  212. }
  213.  
  214. int EmptyCell(char Board[6][7], int column)
  215. {
  216. int count = 0;
  217. for(int i = 0; i < 6; i++)
  218. {
  219. if(Board[i][column] != ' ') count ++;
  220. }
  221. return (5-count);
  222. }
  223.  
  224. int IsFull(char Board[6][7], int column, char turn)
  225. {
  226. int count = 0;
  227. for(int i = 0; i < 6; i++)
  228. {
  229. if(Board[i][column] != ' ') count ++;
  230. }
  231. return (!(6-count)) ? 1 : 0;
  232. }
  233.  
  234. void InitiateBoard(char Board[6][7])
  235. {
  236. for(int i = 0; i < 6; i++)
  237. for(int j = 0; j < 7; j++)
  238. Board[i][j] = ' ';
  239. }
  240.  
  241. void PrintBoard(char Board[6][7])
  242. {
  243. for(int i = 0; i < 6; ++i)
  244. {
  245. printf("|");
  246. for(int j = 0; j < 7; ++j)
  247. {
  248. printf("%c|", Board[i][j]);
  249. }
  250. printf("\n");
  251. }
  252. printf("---------------\n ");
  253. for(int i = 0; i < 7; i++)
  254. printf("%c ", '1'+i);
  255. printf("\n");
  256. }
  257.  
  258. void PrintTurn(char turn)
  259. {
  260. printf("Player %c, in which column would you like to place your token? (enter 0 to undo)\n", turn);
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement