Search code examples
c++returnreturn-value

Need Clarification On Return 0, 1, -1 (Not in Main)


I am beginner at C++ (or all coding in general). That being said, I am trying to understand what return 0/return 1/return -1 defines outside of the main() function.

What I do know about Return values is that it allows one to end a statement and get a "return" integer value. I was told (by my professor) that return 0 is like a "achieved" status. Telling the user that everything is working fine.

However what is confusing me is the code below. Inside "int checkwin()" function it states that if a player wins, then the code will input "return 1". If no one wins, then "return 0". And the last one I don't understand completely (assuming it means keep going) because again, I haven't figured the definitions for return 0/1/-1.

#include <iostream>

using namespace std;

char square[10] {'0','1','2','3','4','5','6','7','8','9'};

int checkwin();
void board();

int main()

{ // main function OPEN

    int player;
    int i;
    int choice;

    char mark;

    do { // do while loop open

            board();

            player=(player%2)?1:2;

            cout << "Player " << player << ", enter a number: ";
            cin >> choice;

            mark =(player ==1) ? 'X' : 'O';

            if(choice==1 && square[1]=='1')

                    square[1] = mark;

            else if(choice==2 && square[2]=='2')

                    square[2] = mark;

            else if(choice==3 && square[3]=='3')

                    square[3] = mark;

            else if(choice==4 && square[4]=='4')

                    square[4] = mark;

            else if(choice==5 && square[5]=='5')

                    square[5] = mark;

            else if(choice==6 && square[6]=='6')


            else if(choice==7 && square[7]=='7')

                    square[7] = mark;

            else if(choice==8 && square[8]=='8')

                    square[8] = mark;

            else if(choice==9 && square[9]=='9')

                    square[9] = mark;
            else
            { // else statment OPEN

                    cout << "That is an invalid choice" <<endl;

                    player--;
                    cin.ignore();
                    cin.get();
            } // else statement CLOSED

            i=checkwin();
            player ++;

    } // do while loop CLOSED
    while (i==-1);

            board();
    if(i==1)

            cout << "==> Player " << --player << "win";
    else
            cout << "==> Game Draw" ;

    cin.ignore();
    cin.get();

    return 0;

} // main function CLOSED



 //****************************************************************
//**********************checkwin function *************************
//*****************************************************************

int checkwin()

{ // checkwin function bracket OPEN

   if(square[1] == square[2] && square[2] == square[3])

            return 1;

    else if(square[4] == square[5] && square[5] == square [6] )

            return 1;

    else if(square[7] ==  square[8] && square[8] == square [9] )

            return 1;

    else if(square[1] == square[4] && square[4] == square[7])

            return 1;

    else if(square[2] == square[5] && square[5] == square[8])

            return 1;

    else if(square[3] == square[6] && square[6] == square[9])

            return 1;

    else if(square[1] == square[5] && square[5] == square[9])

            return 1;

    else if(square[3] == square[5] && square[5] == square[7])

            return 1;

    else if(square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'
            && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8'
            && square[9] != '9' )

            return 0;

    else
            return -1;



} // checkwin function bracekt CLOSED

//*******************************************************************
//************************ board function ***************************
//*******************************************************************

void board()

{ // board function bracket OPEN

    system("cls");
    cout <<"\n\nTic Tac Toe\n\n";

    cout<< "Player 1 ( X )  -  Player 2 ( O )\n\n\n" ;

    cout<< "     |     |     " << endl;
    cout<< "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;

    cout<< "_____|_____|_____" << endl;
    cout<< "     |     |     " << endl;

    cout<< "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;

    cout<< "_____|_____|_____" << endl;
    cout<< "     |     |     " << endl;

    cout<< "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;

    cout<< "     |     |     "  << endl;

} // board function bracket CLOSED

Sorry if my question is a bit confusing or if I'm not being clear enough.


Solution

  • -1 in checkwin means that not all board fields are filled and no one wins at the moment, so game is not yet finished

    1) at initial position, any square[i] != X or O

    char square[10] {'0','1','2','3','4','5','6','7','8','9'}; or, equivalently,

    square[0] = '0'; // dummy value -- not used at all
    square[1] = '1';
    square[2] = '2';
    square[3] = '3';
    square[4] = '4';
    square[5] = '5';
    square[6] = '6';
    square[7] = '7';
    square[8] = '8';
    square[9] = '9';
    

    2) they place mars on board mark =(player ==1) ? 'X' : 'O';

    3) game contunues, while state of the game is -1:

        do { // do while loop open
            ...
            i=checkwin();
            ...
        } while (i == -1)  
    

    -1 means that not all board fields are filled and no one wins at the moment, so game is not yet finished