Thread: Explain a section of code

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    5

    Explain a section of code

    Can anyone explain the function bool check to me and how its working? Im just really confused trying to understand it in context with the program.
    Code:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    
    using namespace std;
    
    void display();
    bool check(int a, int b);
    int drop(int b, char player);
    
    char place[6][7];  //available for whole program
    
    int main()
    {
        // put white spaces
        for(int a =0; a <= 5; a++){        
            for(int b = 0; b <= 6; b++)    
                place[a][b] = ' ';        
        }    
                                    
        display();//Displays empty board
        int hold;//user row choice
        int hold2 = 0;//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 = 'B';//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 == 'B'){//if player 2 lasted dropped a piece so its player 1s turn
                    cout<<"player 1 drop where?";
                    player = 'A';//char of players piece
                }
                else{
                    cout<<"player 2 drop where?";
                    player = 'B';//char of player piece
                }
            }
            while(true){//will run untill 'break;'
                if(charsPlaced == 42) break;//if draw
                cin>>hold;//get user input
                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 cout<< "\nplease enter a value between 1 and 7 :";//ask for input and loop again
                if (cin.fail())    //catch a non number
                {                        //
                    cin.clear();        //Stops cin trying to put its value in to hold
                    char c;            //Try entering a non number without this, 2 see what this does
                    cin>>c;            //
                }                        //Catch a non number
    
            }
            if(charsPlaced == 42) break;//if draw
            hold2 = drop(hold,player);//drop the player store the row in hold2
            if(hold2 == -1)    cout<<"Colom 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("cls");//this clears the screen
        
        if(charsPlaced == 42){//if draw
            cout<<"No winner, Game was draw\n";
            system("pause");
            return 0;
        }
        
        if(player == 15)//if won by player 2
            cout<<"gamewon by : player 2\n";
        else cout<<"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
    }
    
    void display()
    {
        cout<<" 1   2   3   4   5   6   7\n";
        for(int a = 0; a <= 5; a++)
        {
            for(int b =0; b <= 6; b++) cout<<"-"<<"-"<<"-"<<"-";
            cout<<'\n';
            for(int b =0; b <= 6; b++) cout<<"|"<<place[a][b]<<" "<<"|";
            cout<<'\n';
            for(int b =0; b <= 6; b++) cout<<"-"<<"-"<<"-"<<"-";
            cout<<'\n';
        }
    }
    
    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 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;
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char place[6][7]; //available for whole program
    It would be a lot better if you had some constants to define the size of the array.

    const int ROWS = 6;
    const int COLS = 7;
    char place[ROWS][COLS]; //available for whole program


    It would also be a good idea if you localised this array in main, and passed a parameter to all the functions which need access to it.
    Making things global is a lazy habit.

    If you want to understand check, then rewrite all of the compressed loops into long form
    > for(i = a +1;place[i][b] == player && i <= 5;i++,vertical++);//Check down
    As
    Code:
        for(i = a +1;i <= 5;i++) {
            if ( place[i][b] == player ) {
                vertical++;
            }
        }
    1. It is clearer.
    2. It removes an overflow bug in the original code, caused by testing the array before testing the subscript.

    You might then try say this, to observe how it runs.
    Code:
        for(i = a +1;i <= 5;i++) {
            if ( place[i][b] == player ) {
                vertical++;
                cout << "DEBUG: Incrementing vertical at subscript i=" << i << endl;
            }
        }
    I guess that's what happens when you randomly grab 'found' code from the web. Cite your references when you copy/paste.

    Here's one, ConnectFour/connect4.cpp at master * thatSunshineKid/ConnectFour * GitHub
    There are others.
    GIYF
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone explain what this section code is doing ?
    By zidangus in forum C Programming
    Replies: 8
    Last Post: 06-22-2010, 05:25 AM
  2. Help with mprotect and writing on code section
    By raghu2383 in forum Linux Programming
    Replies: 3
    Last Post: 06-20-2008, 02:40 AM
  3. Need help with section of Code
    By Harkin1987 in forum C Programming
    Replies: 9
    Last Post: 08-27-2006, 01:55 PM
  4. Help with section of code.
    By unejam2005 in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2005, 06:38 PM
  5. How would you go back to a section of code?
    By XenoForce in forum C++ Programming
    Replies: 23
    Last Post: 10-20-2004, 10:51 PM

Tags for this Thread