Results 1 to 7 of 7

Thread: How to exit...?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Cleveland, Ohio
    Posts
    263
    Ok, I'm learning C++, and here's the code for a little tic-tac-toe game I've written. At the end, I'd like to be able to provide the user with a choice of yes or no to keep playing, but I have problems telling my app to quit right then and there. Here's the code; please someone show me how to do this!
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    char board[3][3];
    int hAxis;
    int vAxis;
    int lineCheck;
    char uRes;
    char cWin;
    int pTurn = 0;
    long xWins = 0;
    long oWins = 0;
    long cWins = 0;
    
    void endGame(char);
    char isWinner();
    void mainGame();
    
    
    void main()
    {
    srand(time(NULL));
    board[0][0] = ' '; 
    board[0][1] = ' ';
    board[0][2] = ' ';
    board[1][0] = ' ';
    board[1][1] = ' ';
    board[1][2] = ' ';
    board[2][0] = ' ';
    board[2][1] = ' ';
    board[2][2] = ' ';
    cout << "Welcome to Tic-Tac-Toe!\nYou are player 'X'" << endl;
    mainGame();
    cout << "\n\nThanks for playing!";
    } 
    
    void mainGame()
    {
    	cout << endl << "MainGame" << endl;
    	int hComp;
    	int vComp;
    
    		do{
    			do{
    				cout << "\n 1 2 3\n" <<
    				"1" << board[0][0] << "|" << board[0][1] << "|" << board[0][2] <<
    				"\n -+-+-\n" << 
    				"2" << board[1][0] << "|" << board[1][1] << "|" << board[1][2] <<
    				"\n -+-+-\n" << 
    				"3" << board[2][0] << "|" << board[2][1] << "|" << board[2][2] << endl << endl;
    				
    				do{
    					cout << "Enter Horizontal: ";
    					cin >> hAxis;
    				}while (hAxis > 3 && hAxis < 1);
    				do{
    					cout << "Enter Vertical: ";
    					cin >> vAxis;
    				}while (vAxis > 3 && vAxis < 1);
    				
    				if (board[vAxis - 1][hAxis - 1] != ' '){cout << "\nThat space is already occupied by '" << board[vAxis - 1][hAxis - 1] << "'!" << endl;};
    			}while(board[vAxis - 1][hAxis - 1] != ' ');
    			
    			board[vAxis - 1][hAxis - 1] = 'X';
    
    			cWin = isWinner();
    			if(cWin != ' ') endGame(cWin);
    			else{ 
    
    				if (pTurn < 4){
    				do{
    					hComp = rand() % 3;
    					vComp = rand() % 3;
    				}while (board[vComp][hComp] != ' ');
    				board[vComp][hComp] = 'O';
    				};
    			
    				cWin = isWinner();
    				if(cWin != ' ') endGame(cWin);
    				else pTurn = pTurn + 1;
    			}
    		}while(pTurn < 5);
    	endGame(isWinner());
    }
    
    char isWinner()
    {
    	lineCheck = (int)board[0][0] + (int)board[0][1] + (int)board[0][2];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	lineCheck = (int)board[1][0] + (int)board[1][1] + (int)board[1][2];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	lineCheck = (int)board[2][0] + (int)board[2][1] + (int)board[2][2];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	
    	lineCheck = (int)board[0][0] + (int)board[1][0] + (int)board[2][0];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	lineCheck = (int)board[0][1] + (int)board[1][1] + (int)board[2][1];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	lineCheck = (int)board[0][2] + (int)board[1][2] + (int)board[2][2];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    
    	lineCheck = (int)board[0][0] + (int)board[1][1] + (int)board[2][2];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    
    	lineCheck = (int)board[0][2] + (int)board[1][1] + (int)board[2][0];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	return ' ';
    }
    
    void endGame(char winner)
    {
    	cout << "\n 1 2 3\n" <<
    	"1" << board[0][0] << "|" << board[0][1] << "|" << board[0][2] <<
    	"\n -+-+-\n" << 
    	"2" << board[1][0] << "|" << board[1][1] << "|" << board[1][2] <<
    	"\n -+-+-\n" << 
    	"3" << board[2][0] << "|" << board[2][1] << "|" << board[2][2] << "\n\n";
    
    	switch(cWin)
    	{case 'X': cout << "'X' wins the game!"; xWins = xWins + 1; break;
    	case 'O': cout << "'O' wins the game!"; oWins = oWins + 1; break;
    	case ' ': cout << "It's a cat's game!"; cWins = cWins + 1; break;}
    	
    	cout << "\n\nScoreboard:\n-----------\nX Wins: " << xWins << "\nO Wins: " << oWins << "\nCat's Games: " << cWins << 
    		"\n\nWould you like to play again? (Y/N): ";
    
    	board[0][0] = ' '; 
    	board[0][1] = ' ';
    	board[0][2] = ' ';
    	board[1][0] = ' ';
    	board[1][1] = ' ';
    	board[1][2] = ' ';
    	board[2][0] = ' ';
    	board[2][1] = ' ';
    	board[2][2] = ' ';
    
    	cin >> winner;
    	if (uRes != 'Y' || uRes != 'y') //now what?
    }
    Also note that a lot of this code has been written pretty beginner-like, so please feel free to show me other ways of doing this stuff. Please mark code you create or edit by adding //* or something so I will know! Thanks!

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Use exit(0).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Cleveland, Ohio
    Posts
    263
    This code always exits no matter what the user enters:
    Code:
    //This quality application allows you to play Tic-Tac-Toe and not pay attention in class.
    
    
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    char board[3][3];
    int hAxis;
    int vAxis;
    int lineCheck;
    char uRes;
    char cWin;
    int pTurn = 0;
    long xWins = 0;
    long oWins = 0;
    long cWins = 0;
    
    void endGame(char);
    char isWinner();
    void mainGame();
    
    
    void main()
    {
    srand(time(NULL));
    board[0][0] = ' '; 
    board[0][1] = ' ';
    board[0][2] = ' ';
    board[1][0] = ' ';
    board[1][1] = ' ';
    board[1][2] = ' ';
    board[2][0] = ' ';
    board[2][1] = ' ';
    board[2][2] = ' ';
    cout << "Welcome to Tic-Tac-Toe!\nYou are player 'X'" << endl;
    mainGame();
    cout << "\n\nThanks for playing!";
    } 
    
    void mainGame()
    {
    	cout << endl << "MainGame" << endl;
    	int hComp;
    	int vComp;
    
    		do{
    			do{
    				cout << "\n 1 2 3\n" <<
    				"1" << board[0][0] << "|" << board[0][1] << "|" << board[0][2] <<
    				"\n -+-+-\n" << 
    				"2" << board[1][0] << "|" << board[1][1] << "|" << board[1][2] <<
    				"\n -+-+-\n" << 
    				"3" << board[2][0] << "|" << board[2][1] << "|" << board[2][2] << endl << endl;
    				
    				do{
    					cout << "Enter Horizontal: ";
    					cin >> hAxis;
    				}while (hAxis > 3 && hAxis < 1);
    				do{
    					cout << "Enter Vertical: ";
    					cin >> vAxis;
    				}while (vAxis > 3 && vAxis < 1);
    				
    				if (board[vAxis - 1][hAxis - 1] != ' '){cout << "\nThat space is already occupied by '" << board[vAxis - 1][hAxis - 1] << "'!" << endl;};
    			}while(board[vAxis - 1][hAxis - 1] != ' ');
    			
    			board[vAxis - 1][hAxis - 1] = 'X';
    
    			cWin = isWinner();
    			if(cWin != ' ') endGame(cWin);
    			else{ 
    
    				if (pTurn < 4){
    				do{
    					hComp = rand() % 3;
    					vComp = rand() % 3;
    				}while (board[vComp][hComp] != ' ');
    				board[vComp][hComp] = 'O';
    				};
    			
    				cWin = isWinner();
    				if(cWin != ' ') endGame(cWin);
    				else pTurn = pTurn + 1;
    			}
    		}while(pTurn < 5);
    	endGame(isWinner());
    	mainGame();
    }
    
    char isWinner()
    {
    	lineCheck = (int)board[0][0] + (int)board[0][1] + (int)board[0][2];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	lineCheck = (int)board[1][0] + (int)board[1][1] + (int)board[1][2];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	lineCheck = (int)board[2][0] + (int)board[2][1] + (int)board[2][2];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	
    	lineCheck = (int)board[0][0] + (int)board[1][0] + (int)board[2][0];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	lineCheck = (int)board[0][1] + (int)board[1][1] + (int)board[2][1];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	lineCheck = (int)board[0][2] + (int)board[1][2] + (int)board[2][2];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    
    	lineCheck = (int)board[0][0] + (int)board[1][1] + (int)board[2][2];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    
    	lineCheck = (int)board[0][2] + (int)board[1][1] + (int)board[2][0];
    	if(lineCheck == 264 || lineCheck == 237) return (char)(lineCheck / 3);
    	return ' ';
    }
    
    void endGame(char winner)
    {
    	cout << "\n 1 2 3\n" <<
    	"1" << board[0][0] << "|" << board[0][1] << "|" << board[0][2] <<
    	"\n -+-+-\n" << 
    	"2" << board[1][0] << "|" << board[1][1] << "|" << board[1][2] <<
    	"\n -+-+-\n" << 
    	"3" << board[2][0] << "|" << board[2][1] << "|" << board[2][2] << "\n\n";
    
    	switch(cWin)
    	{case 'X': cout << "'X' wins the game!"; xWins = xWins + 1; break;
    	case 'O': cout << "'O' wins the game!"; oWins = oWins + 1; break;
    	case ' ': cout << "It's a cat's game!"; cWins = cWins + 1; break;}
    	
    	cout << "\n\nScoreboard:\n-----------\nX Wins: " << xWins << "\nO Wins: " << oWins << "\nCat's Games: " << cWins << 
    		"\n\nWould you like to play again? (Y/N): ";
    
    	board[0][0] = ' '; 
    	board[0][1] = ' ';
    	board[0][2] = ' ';
    	board[1][0] = ' ';
    	board[1][1] = ' ';
    	board[1][2] = ' ';
    	board[2][0] = ' ';
    	board[2][1] = ' ';
    	board[2][2] = ' ';
    	pTurn = 0;
    
    	cin >> uRes;
    	if (uRes != 'Y' || uRes != 'y') exit(0); else mainGame();
    }

  4. #4
    Lively Member
    Join Date
    Jul 2000
    Posts
    94
    First of all, I don't recommend using exit(0);. This violates one of the 3 rules of C++. For those who haven't heard them here they are:

    1. Don't use global variables
    2. Don't use goto statements
    3. Every program should have one and only one entrance and exit.

    I would structure my endgame function to return a bool. Then return their choice.(as true or false obviously)

    Something like this:

    void main (void){
    //do stuff
    MainGame();
    }

    bool EndGame(){
    cin >> answer;
    if (answer == 'Y' || answer == 'y')
    return true;
    return false;
    }

    MainGame(){
    //do stuff
    do {
    //more suff
    }
    while EndGame(char);
    }

    Now if you return false (i.e. they do not wish to continue playing) the program will exit the MainGame function and exit the program normally. No need for the exit(0);

    Calling the MainGame function inside the EndGame function will eventually cause a stack overflow (it would take a long time, but it would happen), because you never let the functions return, each call to MainGame would leave an EndGame and MainGame behind it on the stack.

    Hope this helps.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Good point - I've never seen those rules before, although they seem very sensible. If I'd spent longer on it, I probably would have used my normal program method which is - only ever have one global function (main).

    Code:
    class MyApp {
    public:
        MyApp() { }
        MyApp(int argc, char** argv) { }
        virtual ~MyApp { }
    
        bool Run() {
            // Set the program going
            // Whole app is controlled from here
            // Usually only returns false, to end,
            // but occasionally returns true, to restart.
        }
    }
    
    int main(int argc, char** argv) {
        MyApp theApp(argc, argv);
    
        while(theApp.Run());
    }
    This is the way a lot of Java programs are written, too.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Member
    Join Date
    Jul 2000
    Location
    Ontario, Canada
    Posts
    61
    am I stupid or something but when there done make a message box come up with a yes and no button
    then have a case so if the user hit yes it restarts the game no it ends the game

    int iResults
    iResults = MessageBox("Would you like to exit or play again", "What would you like to do", MB_YESNO | MB_ICONQUESTION)
    switch (iResults)
    {
    case IDYES :
    Goto start of code, code
    break;
    case IDNO :
    OnOK();
    break;
    }
    ---~^ Absalom ^~---

    There is nobody in the world who knows everything there is no one his/her workforce who knows everything what really makes the person smart is that he/she is not affraid to ask for help.

  7. #7
    Lively Member
    Join Date
    Jul 2000
    Posts
    94
    umm ... this is a console app. Meaning, no message boxes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width