Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // HW3
- //
- // Created by Muhammad Hleihel on 5/1/15.
- // Copyright (c) 2015 Muhammad Hleihel. All rights reserved.
- //
- #include <stdio.h>
- void InitiateGame();
- void PrintBoard(char Board[6][7]);
- void InitiateBoard(char Board[6][7]);
- void PrintTurn(char turn);
- void StartGame(char Board[6][7]);
- int ScanMove(char Board[6][7], char turn, int MovesArr[42], int *MovesCnt);
- int CheckMove(char Board[6][7], char turn, int move, int MovesArr[42], int *MovesCnt);
- int InRange(int move);
- int IsFull(char Board[6][7], int column, char turn);
- int EmptyCell(char Board[6][7], int column);
- void Undo(char Board[6][7], int MovesArr[42], int *MovesCnt);
- int Last_Move(char Board[6][7], int MovesArr[42], int *MovesCnt);
- char CheckWin(char Board[6][7]);
- char CheckWin_Vertical(char Board[6][7]);
- char CheckWin_RDiagonal(char Board[6][7]);
- char CheckWin_DDiagonal(char Board[6][7]);
- int main()
- {
- InitiateGame();
- return 0;
- }
- void InitiateGame()
- {
- char Board[6][7];;
- printf("Welcome to Connect-4!\n\n");
- InitiateBoard(Board);
- PrintBoard(Board);
- StartGame(Board);
- }
- void StartGame(char Board[6][7])
- {
- char last_turn = 'o';
- int MovesArr[43] = {0};
- int MovesCnt = 0;
- int tmp = 0;
- while(1)
- {
- if(MovesCnt == 42)
- {
- printf("The game has ended! It's a tie.");
- break;
- }
- PrintTurn((last_turn == 'o')?('*'):('o'));
- tmp = ScanMove(Board, (last_turn == 'o')?('*'):('o'), MovesArr, &MovesCnt);
- if(!tmp)
- {
- printf("You should have entered a number! We are doomed...");
- break;
- }
- else if(tmp == '*' || tmp == 'o')
- {
- printf("The game has ended! Player %c has won!!!!!", tmp);
- break;
- }
- last_turn = (last_turn == 'o')?('*'):('o');
- }
- }
- int InRange(int move)
- {
- return (move >= 0 && move <= 7)?1:0;
- }
- int ScanMove(char Board[6][7], char turn, int MovesArr[42], int *MovesCnt)
- {
- int move;
- if(scanf("%d", &move) != 1)
- {
- return 0;
- }
- CheckMove(Board, turn, move-1, MovesArr, MovesCnt);
- char win = CheckWin(Board);
- if(win == '*' || win == 'o') return win;
- return 1;
- }
- char CheckWin(char Board[6][7])
- {
- char tmp = CheckWin_Vertical(Board);
- if(tmp != 0) return tmp;
- tmp = CheckWin_RDiagonal(Board);
- if(tmp != 0) return tmp;
- return CheckWin_DDiagonal(Board);
- }
- char CheckWin_Vertical(char Board[6][7])
- {
- int s_count = 0;
- int o_count = 0;
- for(int i = 0; i < 7; i++)
- {
- for(int j = 0; j < 3; j++)
- {
- for(int k = 0; k <= 3; k++)
- {
- if(Board[j+k][i] == '*') s_count ++;
- if(Board[j+k][i] == 'o') o_count ++;
- if(s_count == 4) return Board[j+k][i];
- if(o_count == 4) return Board[j+k][i];
- }
- s_count = 0;
- o_count = 0;
- }
- }
- return 0;
- }
- char CheckWin_RDiagonal(char Board[6][7])
- {
- int s_count = 0;
- int o_count = 0;
- for(int i = 0; i < 4; i++)
- {
- for(int j = 5; j >= 3; j--)
- {
- for(int k = 0; k <= 3; k++)
- {
- if(Board[j-k][i+k] == '*') s_count ++;
- if(Board[j-k][i+k] == 'o') o_count ++;
- if(s_count == 4 || o_count == 4) return Board[j-k][k+i];
- }
- s_count = 0;
- o_count = 0;
- }
- }
- return 0;
- }
- char CheckWin_DDiagonal(char Board[6][7])
- {
- int s_count = 0;
- int o_count = 0;
- for(int i = 0; i < 4; i++)
- {
- for(int j = 0; j <= 2; j++)
- {
- for(int k = 0; k <= 3; k++)
- {
- if(Board[j+k][i+k] == '*') s_count ++;
- if(Board[j+k][i+k] == 'o') o_count ++;
- if(s_count == 4 || o_count == 4) return Board[j+k][k+i];
- }
- s_count = 0;
- o_count = 0;
- }
- }
- return 0;
- }
- void Undo(char Board[6][7], int MovesArr[42], int *MovesCnt)
- {
- if(*MovesCnt > 0)
- {
- Board[Last_Move(Board, MovesArr, MovesCnt)][MovesArr[*MovesCnt]] = ' ';
- --*MovesCnt;
- }
- }
- int Last_Move(char Board[6][7], int MovesArr[42], int *MovesCnt)
- {
- int last = 0;
- for(int i = 0; i < 6; ++i)
- {
- if(Board[i][MovesArr[*MovesCnt]] != ' ')
- {
- last = i;
- break;
- }
- }
- return last;
- }
- int CheckMove(char Board[6][7], char turn, int move, int MovesArr[42], int *MovesCnt)
- {
- if(!(move+1))
- {
- Undo(Board, MovesArr, MovesCnt);
- }
- else
- {
- if(!InRange(move+1))
- {
- printf("Invalid move. Input move is out of range.\nPlease try again:");
- ScanMove(Board, turn, MovesArr, MovesCnt);
- return 0;
- }
- if(IsFull(Board, move, turn) == 1)
- {
- printf("Invalid move. Column %d is full.\nPlease try again:", move+1);
- ScanMove(Board, turn, MovesArr, MovesCnt);
- return 0;
- }
- Board[EmptyCell(Board, move)][move] = turn;
- MovesArr[++*MovesCnt] = move;
- }
- PrintBoard(Board);
- return 1;
- }
- int EmptyCell(char Board[6][7], int column)
- {
- int count = 0;
- for(int i = 0; i < 6; i++)
- {
- if(Board[i][column] != ' ') count ++;
- }
- return (5-count);
- }
- int IsFull(char Board[6][7], int column, char turn)
- {
- int count = 0;
- for(int i = 0; i < 6; i++)
- {
- if(Board[i][column] != ' ') count ++;
- }
- return (!(6-count)) ? 1 : 0;
- }
- void InitiateBoard(char Board[6][7])
- {
- for(int i = 0; i < 6; i++)
- for(int j = 0; j < 7; j++)
- Board[i][j] = ' ';
- }
- void PrintBoard(char Board[6][7])
- {
- for(int i = 0; i < 6; ++i)
- {
- printf("|");
- for(int j = 0; j < 7; ++j)
- {
- printf("%c|", Board[i][j]);
- }
- printf("\n");
- }
- printf("---------------\n ");
- for(int i = 0; i < 7; i++)
- printf("%c ", '1'+i);
- printf("\n");
- }
- void PrintTurn(char turn)
- {
- printf("Player %c, in which column would you like to place your token? (enter 0 to undo)\n", turn);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement