Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "stdio.h"
- #include "stdlib.h"
- #include "stdbool.h"
- #pragma region Constants
- #define ROWS 6
- #define COLS 7
- char board[ROWS][COLS];
- #define FIRST_PLAYER_ICON 'X'
- #define SECOND_PLAYER_ICON 'O'
- #pragma endregion
- #pragma region Structs
- struct Point {
- int row;
- int col;
- int icon;
- };
- typedef struct Point Point;
- #pragma endregion
- #pragma region Declarations
- /// This funciton checks for tie.
- bool isTie(int turnsCounter);
- /// This function handles a tie situation.
- void handleTie();
- /// This function checks if the point is out of the board bounds.
- bool isPointOutOfBoardBounds(Point point);
- /// This function places a point on the board.
- void placePointOnBoard(int playerNumber);
- /// This function returns an player's icon.
- char getPlayerIcon(int playerNumber);
- /// This function initializes the game board by assigning each cell
- /// with ' ' (resulting with an empty game board).
- void initBoard();
- /// This function gets a valid column number from the user.
- Point getPlayerPoint(int playerNumber);
- /// This function initializes the game by initalling new board.
- void initGame();
- /// This function manages the whole game.
- void playGame();
- /// This function prints the current state of the board.
- void printBoard();
- /// This funciton receives a player number and returns the next player's number;
- int getNextPlayerNumber(int currentPlayerNumber);
- /// This function manages one round full of action!
- void playOneRound(int playerNumber);
- /// This function sets a player's requested point on the board.
- void setPlayerPointOnBoard(Point playerPoint);
- /// This function checks if a selected column is outside the bounds of the board.
- bool pointIsOutOfBoardColumns(int columnNumber);
- /// This function gets the max row height of a selected column.
- int getHighestRowInSelectedColumn(int columnNumber);
- /// This function checks if we can place a point on a specified board column.
- bool pointIsOutOfBoardRows(int row);
- /// This function gets a row number and a column number (a cell),
- /// and returns the character in that cell (could be 'X', 'O' or ' ').
- /// For example:
- /// char c = getCell(1, 1);
- char getCell(int row, int col);
- /// This function gets a row number, a column number and a sign,
- /// and assigns the cell with the given sign.
- /// For example:
- /// setCell(1, 1, 'X');
- void setCell(int row, int col, char sign);
- /// This function clears the screen.
- void clearScreen();
- #pragma endregion
- #pragma region Main
- void main() {
- initGame();
- playGame();
- }
- #pragma endregion
- #pragma region Implementations
- void initGame() {
- printf("Welcome to Connect Four! \nLet's play.\n");
- initBoard();
- }
- bool isTie(int playsCounter) {
- return playsCounter == 42;
- }
- void handleTie() {
- clearScreen();
- initBoard();
- printBoard();
- }
- void playGame() {
- int currentPlayerNumber = 1;
- int playsCounter = 0;
- printBoard();
- while (true) {
- if (isTie(playsCounter)) {
- handleTie();
- playsCounter = 0;
- }
- else {
- playOneRound(currentPlayerNumber);
- playsCounter++;
- }
- currentPlayerNumber = getNextPlayerNumber(currentPlayerNumber);
- }
- }
- void playOneRound(int playerNumber) {
- placePointOnBoard(playerNumber);
- clearScreen();
- printBoard();
- }
- int getNextPlayerNumber(int currentPlayerNumber) {
- if (currentPlayerNumber == 1)
- return 2;
- return 1;
- }
- char getPlayerIcon(int playerNumber) {
- printf("Player number %d: \n", playerNumber);
- if (playerNumber == 1)
- return FIRST_PLAYER_ICON;
- return SECOND_PLAYER_ICON;
- }
- void placePointOnBoard(int playerNumber) {
- Point playerPoint;
- playerPoint = getPlayerPoint(playerNumber);
- setPlayerPointOnBoard(playerPoint);
- }
- void setPlayerPointOnBoard(Point playerPoint) {
- setCell(playerPoint.row, playerPoint.col, playerPoint.icon);
- }
- bool pointIsOutOfBoardColumns(int columnNumber) {
- bool isColOfOfBounds = columnNumber <= 0 || columnNumber > COLS;
- if (isColOfOfBounds)
- printf("The col you entered is not between 1-7.\n");
- return isColOfOfBounds;
- }
- int getValidColumn() {
- int validCol;
- printf("Please enter column input (a number between 1-7): ");
- scanf("%d", &validCol);
- while (pointIsOutOfBoardColumns(validCol)) {
- printf("Please enter column input (a number between 1-7): ");
- scanf("%d", &validCol);
- }
- return validCol;
- }
- Point getPlayerPoint(int playerNumber) {
- Point point;
- point.col = getValidColumn();
- point.row = getHighestRowInSelectedColumn(point.col);
- point.icon = getPlayerIcon(playerNumber);
- while (isPointOutOfBoardBounds(point)) {
- point.col = getValidColumn();
- if (!pointIsOutOfBoardColumns(point.col))
- point.row = getHighestRowInSelectedColumn(point.col);
- }
- return point;
- }
- bool isPointOutOfBoardBounds(Point point) {
- return pointIsOutOfBoardColumns(point.col) || pointIsOutOfBoardRows(point.row);
- }
- int getHighestRowInSelectedColumn(int columnNumber)
- {
- int i;
- for (i = ROWS; i > 0; i--) {
- if (getCell(i, columnNumber) == ' ')
- return i;
- }
- printf("The row you selected is full! Please try another one. \n");
- return -1; // If we couldn't find an empty row, return -1.
- }
- bool pointIsOutOfBoardRows(int row) { // A Function that checks if we got out of bounds of the board.
- return row == -1;
- }
- void printBoard() {
- int i, j;
- printf("The current board looks like this: \n\n");
- printf(" ");
- for (i = 1; i <= COLS; i++) {
- printf(" %d", i);
- }
- printf("\n\n");
- for (i = 1; i <= ROWS; i++) {
- printf("%c ", i + 64);
- for (j = 1; j <= COLS; j++) {
- printf(" %c", getCell(i, j));
- //printf(" %d, %d", i, j);
- }
- printf("\n\n");
- }
- }
- char getCell(int row, int col) {
- return board[row - 1][col - 1];
- }
- void setCell(int row, int col, char sign) {
- board[row - 1][col - 1] = sign;
- }
- void initBoard() {
- int i, j;
- for (i = 0; i < ROWS; i++) {
- for (j = 0; j < COLS; j++) {
- setCell(i + 1, j + 1, ' ');
- }
- }
- }
- void clearScreen() {
- system("cls");
- }
- bool isPlayerWinVerticalRow(Point point) {
- int i, j;
- int identicalIconsCounter = 0;
- for (i = 1; i <= ROWS; i++) {
- for (j = 1; j <= COLS; j++) {
- if (board[point.row][point.col] == board[i + 1][j] == board[i + 2][j] == board[i + 3][j])
- {
- return true;
- }
- }
- }
- }
- #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement