Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #define ROW_NUM 6
- #define COLS_NUM 7
- //function that prints the board
- void board_print(char board[ROW_NUM][COLS_NUM]){
- int i=0,j=0;
- for(i=0;i<ROW_NUM;i++){
- for(j=0;j<COLS_NUM;j++){
- printf("|%c",board[i][j]);
- }
- printf("|");
- printf("\n");
- }
- for(i=0;i<2*COLS_NUM+1;i++){
- printf("-");
- }
- printf("\n");
- for(i=1;i<COLS_NUM+1;i++){
- printf(" %d",i);
- }
- printf("\n");
- }
- //function that takes the column number from the player
- void getting_token(int Col_Mem[1],int scanf_val[1]){
- int col_num=0;
- scanf_val[0]=scanf("%d",&col_num);
- Col_Mem[0]=col_num;
- }
- //function that checks the range
- int check_range(int Col_Mem[1],int scanf_val[1]){
- if((Col_Mem[0]<0)||(Col_Mem[0]>COLS_NUM)){
- printf("Invalid move. Input move is out of range.\n");
- printf("Please try again:\n");
- return 1;
- }
- if(scanf_val[0]!=1){
- printf("You should have entered a number! We are doomed...\n");
- }
- return 0;
- }
- //function that flips the turn of the player to play
- void player_turn(int z[1]){
- z[0]=1-z[0];
- if(z[0]){
- printf("Player *, in which column would you like to place your");
- printf(" token? (enter 0 to undo)\n");
- }
- else{
- printf("Player o, in which column would you like to place your");
- printf(" token? (enter 0 to undo)\n");
- }
- }
- //function that fills the board with tokens,in the right place
- int filling_board(char board[ROW_NUM][COLS_NUM],
- int z[1],int Col_Mem[1]){
- int u=Col_Mem[0];
- int i=0,s_counter=0;
- for(i=ROW_NUM-1;i>-1;i--){
- if((board[i][u-1]!='*')&&(board[i][u-1]!='o')&&(u!=0)){
- if(z[0]==1){
- board[i][u-1]='*';
- s_counter++;
- break;
- }
- else{
- board[i][u-1]='o';
- s_counter++;
- break;
- }
- }
- }
- return s_counter;
- }
- //function that checks if the column is full
- int full_check(char board[ROW_NUM][COLS_NUM],int Col_Mem[1],int z[1]){
- int u=Col_Mem[0];
- if((board[0][u-1]=='*')||(board[0][u-1]=='o')){
- printf("Invalid move. Column %d is full.\n",u);
- printf("Please try again:\n");
- z[0]=1-z[0];
- return 1;
- }
- return 0;
- }
- //undo terms function - checks the terms for undo
- int undo_terms(char board[ROW_NUM][COLS_NUM],int Col_Mem[1]){
- int i=0,j=0,counter=0;
- if(Col_Mem[0]==0){ //if the player enters 0,the function works
- for(i=0;i<ROW_NUM;i++){
- for(j=0;j<COLS_NUM;j++){
- if((board[i][j]!='o')&&(board[i][j]!='*')){counter++;}
- }
- }
- if(counter==42){ //checks if the board is empty
- return 1;
- }
- else{
- return 0;
- }
- }
- return 2;
- }
- //undo function- function that do the undo process
- void undo_function(char board[ROW_NUM][COLS_NUM],int Moves_Mem[42],
- int Moves_loc[1],int b){
- if(b==0){ //if the player entered 0 and the board isn't empty
- Moves_loc[0]-=1; /*we move 1 backwards in the Moves_Mem,to know
- the value before the player entered 0 */
- int i=0;
- int x=Moves_loc[0];
- int u=Moves_Mem[x]; /*the value in the right place,where we
- want to make the undo */
- for(i=0;i<ROW_NUM;i++){
- if((board[i][u-1]=='*')||(board[i][u-1]=='o')){
- board[i][u-1]=' ';
- Moves_loc[0]-=1; /*we move 1 more backwards,because in
- the next turn we move 1 forward-by this move backwards
- we prevent from moving twice in the Moves_Mem*/
- break;
- }
- }
- }
- }
- /*undo function-empty board:when the player press undo and the board
- is empty*/
- int undo_empty_board(int b){
- if(b==1){ //if the player entered 0 and the board is empty
- printf("Invalid move. Cannot undo when board is empty.\n");
- printf("Please try again:\n");
- return 1;
- }
- return 0;
- }
- //check if a player wins , in column!!!
- int check_winner_col(char board[ROW_NUM][COLS_NUM]){
- int i=0,j=0;
- /* when identify o-*,checks if there is another 3 token in a row
- from the same type */
- for(i=ROW_NUM-1;i>ROW_NUM-4;i--){
- for(j=0;j<COLS_NUM;j++){
- if((board[i][j]==board[i-1][j])&&(board[i-1][j]==board[i-2][j])
- &&(board[i-2][j]==board[i-3][j])&&(board[i-3][j]=='*')){
- printf("The game has ended! Player * has won!!!!!\n");
- return 1;
- }
- if((board[i][j]==board[i-1][j])&&(board[i-1][j]==board[i-2][j])
- &&(board[i-2][j]==board[i-3][j])&&(board[i-3][j]=='o')){
- printf("The game has ended! Player o has won!!!!!\n");
- return 1;
- }
- }
- }
- return 0;
- }
- //check if a player wins in row!!!
- int check_winner_row(char board[ROW_NUM][COLS_NUM]){
- int i=0,j=0;
- /* when identify o-*,checks if there is another 3 token in a column
- from the same type */
- for(i=ROW_NUM-1;i>-1;i--){
- for(j=0;j<COLS_NUM-3;j++){
- if((board[i][j]==board[i][j+1])&&(board[i][j+1]==board[i][j+2])
- &&(board[i][j+2]==board[i][j+3])&&(board[i][j+3]=='*')){
- printf("The game has ended! Player * has won!!!!!\n");
- return 1;
- }
- if((board[i][j]==board[i][j+1])&&(board[i][j+1]==board[i][j+2])
- &&(board[i][j+2]==board[i][j+3])&&(board[i][j+3]=='o')){
- printf("The game has ended! Player o has won!!!!!\n");
- return 1;
- }
- }
- }
- return 0;
- }
- //checks if a player wins in diagonal from left to right
- int check_winner_diagonal_lr(char board[ROW_NUM][COLS_NUM]){
- int i=0,j=0;
- /* when identify o-*,checks if there is another 3 token in a diagonal
- from left to right ,from the same type */
- for(i=ROW_NUM-1;i>2;i--){
- for(j=0;j<COLS_NUM-3;j++){
- if((board[i][j]==board[i-1][j+1])&&(board[i-1][j+1]==board[i-2][j+2])
- &&(board[i-2][j+2]==board[i-3][j+3])&&(board[i-3][j+3]=='*')){
- printf("The game has ended! Player * has won!!!!!\n");
- return 1;
- }
- if((board[i][j]==board[i-1][j+1])&&(board[i-1][j+1]==board[i-2][j+2])
- &&(board[i-2][j+2]==board[i-3][j+3])&&(board[i-3][j+3]=='o')){
- printf("The game has ended! Player o has won!!!!!\n");
- return 1;
- }
- }
- }
- return 0;
- }
- //checks if a player wins in diagonal from right to left
- int check_winner_diagonal_rl(char board[ROW_NUM][COLS_NUM]){
- int i=0,j=0;
- /* when identify o-*,checks if there is another 3 token in a diagonal
- from right to left ,from the same type */
- for(i=ROW_NUM-1;i>2;i--){
- for(j=COLS_NUM-1;j>2;j--){
- if((board[i][j]==board[i-1][j-1])&&(board[i-1][j-1]==board[i-2][j-2])
- &&(board[i-2][j-2]==board[i-3][j-3])&&(board[i-3][j-3]=='*')){
- printf("The game has ended! Player * has won!!!!!\n");
- return 1;
- }
- if((board[i][j]==board[i-1][j-1])&&(board[i-1][j-1]==board[i-2][j-2])
- &&(board[i-2][j-2]==board[i-3][j-3])&&(board[i-3][j-3]=='o')){
- printf("The game has ended! Player o has won!!!!!\n");
- return 1;
- }
- }
- }
- return 0;
- }
- //function that checks if there is a tie
- int tie_function(char board[ROW_NUM][COLS_NUM]){
- int i=0,j=0,counter=0;
- for(i=0;i<ROW_NUM;i++){
- for(j=0;j<COLS_NUM;j++){
- if((board[i][j]=='o')||(board[i][j]=='*')){counter++;}
- if(counter==42){
- printf("The game has ended! It's a tie.\n");
- return 1;
- }
- }
- }
- return 0;
- }
- //open function - starts the program
- void open_board_function(char board[ROW_NUM][COLS_NUM]){
- printf("Welcome to Connect-4!\n\n");
- board_print(board);
- printf("Player *, in which column would you like to place your");
- printf(" token? (enter 0 to undo)\n");
- }
- //undo combined functions
- int undo(char board[ROW_NUM][COLS_NUM],int Col_Mem[1],
- int Moves_Mem[42],int Moves_loc[1]){
- int b=0;
- b=undo_terms(board,Col_Mem);
- undo_function(board,Moves_Mem,Moves_loc,b);
- if(undo_empty_board(b)){return 1;}
- return 0;
- }
- //win and tie combined functions
- int win_tie(char board[ROW_NUM][COLS_NUM]){
- int m=0,t=0,r=0,w=0,s=0;
- if((m=check_winner_col(board))==1){return 1;}
- if((t=check_winner_row(board))==1){return 1;}
- if((r=check_winner_diagonal_lr(board))==1){return 1;}
- if((w=check_winner_diagonal_rl(board))==1){return 1;}
- if(((s=tie_function(board))==1)&&(m!=1)&&(t!=1)&&(r!=1)&&(w!=1)){
- return 1;
- }
- return 0;
- }
- //full column function
- int full_col_check(char board[ROW_NUM][COLS_NUM], int z[1],
- int Col_Mem[1],int Moves_loc[1]){
- int v=0;
- if((v=full_check(board,Col_Mem,z))==1){
- Moves_loc[0]-=1;
- z[0]=1-z[0];
- return 1;
- }
- return 0;
- }
- //board combined functions
- void board_function(char board[ROW_NUM][COLS_NUM], int z[1],
- int Col_Mem[1]){
- filling_board(board,z,Col_Mem);
- board_print(board);
- }
- //function that puts data into the board
- void board_first_fill(char board[ROW_NUM][COLS_NUM]){
- int i=0,j=0;
- for(i=0;i<ROW_NUM;i++){
- for(j=0;j<COLS_NUM;j++){
- board[i][j]=32;
- }
- }
- }
- int main()
- {
- char board[ROW_NUM][COLS_NUM]; //array that remembers the board
- board_first_fill(board);
- open_board_function(board);
- int Col_Mem[1]={0},z[1]={1},scanf_val[1]={0},Moves_Mem[42]={0};
- int Moves_loc[1]={0};
- /* -Col_Mem-it's array that remembers the column number entered by
- -a player , along the whole program
- -z-array that flips , used in player_turn and another functions
- -scanf_val-array that remembers the value that scanf got , used
- to know if the input is a number or not
- -Moves_Mem-array that remembers the moves played
- -Moves_loc-array that remember the location inside the Moves_Mem
- array
- */
- while(1){
- getting_token(Col_Mem,scanf_val);
- if(check_range(Col_Mem,scanf_val)){continue;}
- if(scanf_val[0]!=1){break;}
- Moves_Mem[Moves_loc[0]+=1]=Col_Mem[0];
- if(full_col_check(board,z,Col_Mem,Moves_loc)){continue;}
- if(undo(board,Col_Mem,Moves_Mem,Moves_loc)){continue;}
- board_function(board,z,Col_Mem);
- if(win_tie(board)){break;}
- player_turn(z);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement