-1

I work on a connect four game in c i compiled my code but i still have these 2 problems first this line on my terminal sh: cls: command not found i search on forum (may be an segmentation fault)but i don' know what to do to fix it enter code here and second when i compile the row don't appear on the screen can you help me please ?Thank you in advance :)

This is it :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
void display();
bool check(int a, int b);

int drop(int b, char player);
char place[6][7];//available for the entire code

    void display(){ //empty 

    printf(" 1   2   3   4   5   6   7\n");                     


    int i,j;

    for (i=0; i<6; i++) {
    printf ("+");
    for (j=0; j<7; j++)
        printf("---+");
    printf ("\n");
    printf ("|");
    for (j=0; j<7; j++)
        printf("   |");
    printf ("\n");
  }

printf ("+");
for (j=0; j<7; j++)
    printf("---+");
printf ("\n");
}

int drop(int b, char player){
        if(b >=0 && b<= 6){
            if(place[0][b] == ' '){
            int i;
            for(i = 0;place[i][b] == ' ';i++)
                if(i == 5){place[i][b] = player;
            return i;}
            i--;
            place[i][b] =player;
            return i;
            }else{
            return -1;
            }
        }else{
            return -1;
        }
    }
    bool check(int a, int b){
    int vertical = 1;//(|)
    int horizontal = 1;//(-)
    int diagonal1 = 1;//(\)
    int diagonal2 = 1;//(/)
    char player = place[a][b];
    int i;//vertical
    int ii;//horizontal
    //check for vertical(|)
    for(i = a +1;place[i][b] == player && i <= 5;i++,vertical++);//Check down
    for(i = a -1;place[i][b] == player && i >= 0;i--,vertical++);//Check up
    if(vertical >= 4)return true;
    //check for horizontal(-)
    for(ii = b -1;place[a][ii] == player && ii >= 0;ii--,horizontal++);//Check left
    for(ii = b +1;place[a][ii] == player && ii <= 6;ii++,horizontal++);//Check right
    if(horizontal >= 4) return true;
    //check for diagonal 1 (\)
    for(i = a -1, ii= b -1;place[i][ii] == player && i>=0 && ii >=0; diagonal1 ++, i --, ii --);//up and left
    for(i = a +1, ii = b+1;place[i][ii] == player && i<=5 && ii <=6;diagonal1 ++, i ++, ii ++);//down and right
    if(diagonal1 >= 4) return true;
    //check for diagonal 2(/)
    for(i = a -1, ii= b +1;place[i][ii] == player && i>=0 && ii <= 6; diagonal2 ++, i --, ii ++);//up and right
    for(i = a +1, ii= b -1;place[i][ii] == player && i<=5 && ii >=0; diagonal2 ++, i ++, ii --);//up and left
    if(diagonal2 >= 4) return true;
    return false;
}
 int main(){
    for(int a =0;a <= 5; a++){      //fill place with whitespace
        for(int b = 0; b<=6; b++)   //
            place[a][b] = ' ';      //
    }                               //
    display();//Displays for first time so players can see the board
    //declarations
    int hold;//Will house user row choice
    int hold2 = 0;//will hold drop value
    int charsPlaced = 0;//Number of peices dropped so can end game if a draw
    bool gamewon = false;//Will be changed to true when game is won and will exit while loop
    char player = 15;//start as player 2 will change back 2 player 1

    while(!gamewon){//will stop when game is won, ! means NOT makes the oppisite be checked
        if(hold2 != -1){//check if there was a error in the last drop
            if(player == 15){//if player 2 lasted dropped a piece so its player 1s turn
                printf("player 1 drop where?");
                player = 254;//char of players piece
            }
            else{
                printf("player 2 drop where?");
                player = 15;//char of player piece
            }
        }
        while(true){//will run untill 'break;'
            if(charsPlaced == 42) break;//if draw
//          cin>>hold;//get user input
            scanf("%d",&hold);
            hold--;//take off 1 to account for arrays starting at 0 not 1
            if(hold <=6 && hold>= 0) break;//if within valid range stop loop
            else printf( "\nplease enter a value between 1 and 7 :");//ask for input and loop again


        }

        if(charsPlaced == 42) break;//if draw 6X7
        hold2 = drop(hold,player);//drop the player store the row in hold2

        if(hold2 == -1) printf("Column is full\nPlease enter anothor number between 1 and 7:");//if error -1 row is full
        else{
            gamewon = check(hold2,hold);//check if game is run
            charsPlaced ++;//another character has been succesfully placed
            system("cls");//This clears the screen works with windows, not nesscery to run game
            display();//displayed updated board
        }
    }
    system("clear");//this clears the screen
    if(charsPlaced == 42){//if draw
        printf("No winner, Game was draw\n");
    system("pause");
    return 0;
    }
    if(player == 15)//if won by player 2
        printf("gamewon by : player 2\n");
    else printf("gamewon by : player 1\n");//Else won by player 1
    system("pause");//pauses before exit so players can see who won, works with windows
    return 0;//Exit application
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nevada
  • 1
  • 2
  • 1
    Windows or Linux OS? – Idos Mar 06 '16 at 19:49
  • You have `system("cls");` which you comment "works with windows", you also have `system("clear");` which you comment "this clears the screen". Try both but be consistent. – Weather Vane Mar 06 '16 at 19:51
  • Your code is full of meaningless var names, (i,j,ii etc) and for loops with almost-incomprehensible 'clever code' inside. This is of very little use whatsoever to anyone trying to fix problems. – Martin James Mar 06 '16 at 20:08

1 Answers1

1

You are using system("clear") which is native to Linux OS. But also using system("cls") which is native to Windows OS. Make up your mind, since this is causing your error.

You can actually detect which OS you are running with some predefined macros.

Community
  • 1
  • 1
Idos
  • 15,053
  • 14
  • 60
  • 75
  • I have el capitan os so the same that linux os and i test the two solutions but i still have the same error of segmentation and for the display of row in my game any suggestions ?:) – Nevada Mar 06 '16 at 20:02
  • Remove the `system("cls")` and any other Windows commands (`system("pause")`) and it will fix your error, like I stated – Idos Mar 06 '16 at 20:03
  • I did it and its works !Thanks its my first post and very thankful of this community ,Thanks a lot ! – Nevada Mar 06 '16 at 20:09
  • @Nevada please [accept](http://meta.stackexchange.com/a/5235) this answer if it helped you, by clicking the green check mark next to it. – Idos Mar 15 '16 at 09:26