Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "stdbool.h"
  5.  
  6. #pragma region Constants
  7.  
  8. #define ROWS 6
  9. #define COLS 7
  10. char board[ROWS][COLS];
  11.  
  12. #define FIRST_PLAYER_ICON 'X'
  13. #define SECOND_PLAYER_ICON 'O'
  14.  
  15. #pragma endregion
  16.  
  17. #pragma region Structs
  18.  
  19. struct Point {
  20. int row;
  21. int col;
  22. int icon;
  23. };
  24.  
  25. typedef struct Point Point;
  26.  
  27. #pragma endregion
  28.  
  29. #pragma region Declarations
  30.  
  31. /// This funciton checks for tie.
  32. bool isTie(int turnsCounter);
  33.  
  34. /// This function handles a tie situation.
  35. void handleTie();
  36.  
  37. /// This function checks if the point is out of the board bounds.
  38. bool isPointOutOfBoardBounds(Point point);
  39.  
  40. /// This function places a point on the board.
  41. void placePointOnBoard(int playerNumber);
  42.  
  43. /// This function returns an player's icon.
  44. char getPlayerIcon(int playerNumber);
  45. /// This function initializes the game board by assigning each cell
  46. /// with ' ' (resulting with an empty game board).
  47. void initBoard();
  48.  
  49. /// This function gets a valid column number from the user.
  50. Point getPlayerPoint(int playerNumber);
  51.  
  52. /// This function initializes the game by initalling new board.
  53. void initGame();
  54.  
  55. /// This function manages the whole game.
  56. void playGame();
  57.  
  58. /// This function prints the current state of the board.
  59. void printBoard();
  60.  
  61. /// This funciton receives a player number and returns the next player's number;
  62. int getNextPlayerNumber(int currentPlayerNumber);
  63.  
  64. /// This function manages one round full of action!
  65. void playOneRound(int playerNumber);
  66.  
  67. /// This function sets a player's requested point on the board.
  68. void setPlayerPointOnBoard(Point playerPoint);
  69.  
  70. /// This function checks if a selected column is outside the bounds of the board.
  71. bool pointIsOutOfBoardColumns(int columnNumber);
  72.  
  73. /// This function gets the max row height of a selected column.
  74. int getHighestRowInSelectedColumn(int columnNumber);
  75.  
  76. /// This function checks if we can place a point on a specified board column.
  77. bool pointIsOutOfBoardRows(int row);
  78.  
  79. /// This function gets a row number and a column number (a cell),
  80. /// and returns the character in that cell (could be 'X', 'O' or ' ').
  81. /// For example:
  82. /// char c = getCell(1, 1);
  83. char getCell(int row, int col);
  84.  
  85. /// This function gets a row number, a column number and a sign,
  86. /// and assigns the cell with the given sign.
  87. /// For example:
  88. /// setCell(1, 1, 'X');
  89. void setCell(int row, int col, char sign);
  90.  
  91. /// This function clears the screen.
  92. void clearScreen();
  93.  
  94. #pragma endregion
  95.  
  96. #pragma region Main
  97.  
  98. void main() {
  99. initGame();
  100. playGame();
  101. }
  102.  
  103. #pragma endregion
  104.  
  105. #pragma region Implementations
  106.  
  107. void initGame() {
  108. printf("Welcome to Connect Four! \nLet's play.\n");
  109.  
  110. initBoard();
  111. }
  112.  
  113. bool isTie(int playsCounter) {
  114. return playsCounter == 42;
  115. }
  116.  
  117. void handleTie() {
  118. clearScreen();
  119. initBoard();
  120. printBoard();
  121. }
  122.  
  123. void playGame() {
  124. int currentPlayerNumber = 1;
  125. int playsCounter = 0;
  126.  
  127. printBoard();
  128.  
  129. while (true) {
  130. if (isTie(playsCounter)) {
  131. handleTie();
  132. playsCounter = 0;
  133. }
  134.  
  135. else {
  136. playOneRound(currentPlayerNumber);
  137. playsCounter++;
  138. }
  139.  
  140. currentPlayerNumber = getNextPlayerNumber(currentPlayerNumber);
  141. }
  142. }
  143.  
  144. void playOneRound(int playerNumber) {
  145. placePointOnBoard(playerNumber);
  146. clearScreen();
  147. printBoard();
  148. }
  149.  
  150. int getNextPlayerNumber(int currentPlayerNumber) {
  151. if (currentPlayerNumber == 1)
  152. return 2;
  153.  
  154. return 1;
  155. }
  156.  
  157. char getPlayerIcon(int playerNumber) {
  158. printf("Player number %d: \n", playerNumber);
  159.  
  160. if (playerNumber == 1)
  161. return FIRST_PLAYER_ICON;
  162.  
  163. return SECOND_PLAYER_ICON;
  164. }
  165.  
  166. void placePointOnBoard(int playerNumber) {
  167. Point playerPoint;
  168.  
  169. playerPoint = getPlayerPoint(playerNumber);
  170. setPlayerPointOnBoard(playerPoint);
  171. }
  172.  
  173. void setPlayerPointOnBoard(Point playerPoint) {
  174. setCell(playerPoint.row, playerPoint.col, playerPoint.icon);
  175. }
  176.  
  177. bool pointIsOutOfBoardColumns(int columnNumber) {
  178. bool isColOfOfBounds = columnNumber <= 0 || columnNumber > COLS;
  179. if (isColOfOfBounds)
  180. printf("The col you entered is not between 1-7.\n");
  181. return isColOfOfBounds;
  182. }
  183.  
  184. int getValidColumn() {
  185. int validCol;
  186.  
  187. printf("Please enter column input (a number between 1-7): ");
  188. scanf("%d", &validCol);
  189.  
  190. while (pointIsOutOfBoardColumns(validCol)) {
  191. printf("Please enter column input (a number between 1-7): ");
  192. scanf("%d", &validCol);
  193. }
  194.  
  195. return validCol;
  196. }
  197.  
  198. Point getPlayerPoint(int playerNumber) {
  199. Point point;
  200.  
  201. point.col = getValidColumn();
  202. point.row = getHighestRowInSelectedColumn(point.col);
  203. point.icon = getPlayerIcon(playerNumber);
  204.  
  205. while (isPointOutOfBoardBounds(point)) {
  206. point.col = getValidColumn();
  207.  
  208. if (!pointIsOutOfBoardColumns(point.col))
  209. point.row = getHighestRowInSelectedColumn(point.col);
  210. }
  211.  
  212. return point;
  213. }
  214.  
  215. bool isPointOutOfBoardBounds(Point point) {
  216. return pointIsOutOfBoardColumns(point.col) || pointIsOutOfBoardRows(point.row);
  217. }
  218.  
  219. int getHighestRowInSelectedColumn(int columnNumber)
  220. {
  221. int i;
  222.  
  223. for (i = ROWS; i > 0; i--) {
  224. if (getCell(i, columnNumber) == ' ')
  225. return i;
  226. }
  227.  
  228. printf("The row you selected is full! Please try another one. \n");
  229. return -1; // If we couldn't find an empty row, return -1.
  230. }
  231.  
  232. bool pointIsOutOfBoardRows(int row) { // A Function that checks if we got out of bounds of the board.
  233. return row == -1;
  234. }
  235.  
  236. void printBoard() {
  237. int i, j;
  238.  
  239. printf("The current board looks like this: \n\n");
  240.  
  241. printf(" ");
  242.  
  243. for (i = 1; i <= COLS; i++) {
  244. printf(" %d", i);
  245. }
  246.  
  247. printf("\n\n");
  248.  
  249. for (i = 1; i <= ROWS; i++) {
  250. printf("%c ", i + 64);
  251.  
  252. for (j = 1; j <= COLS; j++) {
  253. printf(" %c", getCell(i, j));
  254. //printf(" %d, %d", i, j);
  255. }
  256.  
  257. printf("\n\n");
  258. }
  259.  
  260. }
  261.  
  262. char getCell(int row, int col) {
  263. return board[row - 1][col - 1];
  264. }
  265.  
  266. void setCell(int row, int col, char sign) {
  267. board[row - 1][col - 1] = sign;
  268. }
  269.  
  270. void initBoard() {
  271. int i, j;
  272.  
  273. for (i = 0; i < ROWS; i++) {
  274. for (j = 0; j < COLS; j++) {
  275. setCell(i + 1, j + 1, ' ');
  276. }
  277. }
  278. }
  279.  
  280. void clearScreen() {
  281. system("cls");
  282. }
  283.  
  284. bool isPlayerWinVerticalRow(Point point) {
  285. int i, j;
  286. int identicalIconsCounter = 0;
  287.  
  288. for (i = 1; i <= ROWS; i++) {
  289. for (j = 1; j <= COLS; j++) {
  290. if (board[point.row][point.col] == board[i + 1][j] == board[i + 2][j] == board[i + 3][j])
  291. {
  292. return true;
  293. }
  294. }
  295. }
  296. }
  297.  
  298. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement