Almost done with the Tic Tac Toe, but I need 1 more easy thing. PLEASE HELP!!!

This is a discussion on Almost done with the Tic Tac Toe, but I need 1 more easy thing. PLEASE HELP!!! within the C++ Programming forums, part of the General Programming Boards category; Ok here is my entire Program below. BUT I cant figure out how to get the damn thing to quit! ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    38

    Unhappy Almost done with the Tic Tac Toe, but I need 1 more easy thing. PLEASE HELP!!!

    Ok here is my entire Program below. BUT I cant figure out how to get the damn thing to quit! Its having trouble finding the variable to end it. I can get it to end and find a winner but it wont quit properly. Ive spent about 16 hours at least in the past 2 days with my cousins help.

    I plead with everyone here to please look at this program, load it up if needed and help me figure out this seemingly small problem. It defenately needs some C Programming exprets here

    Thank you very much and my entire program is below so anyone and everyone can critique it and help. Thanks to anyone who helps. Its greatly appreciated.


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    void Initialize_Board(char [3][3]);
    void get_Player1_move(char [3][3]);
    void get_Player2_move(char [3][3]);
    void disp_board(char [3][3]);
    char check(char [3][3], int answer);
    int Ask_for_new_game(char [3][3], int answer);


    void main (int answer)
    {
    int newgame;
    char ttt[3][3]; /* Tic Tac Toe Board */
    char winner;

    printf("Welcome to Tic-Tac-Toe Game!\n\n");
    printf("Here is the board numbering (Row Column):\n");
    printf(" 0 1 2 \n");
    printf(" *************\n");
    printf("0 *0 0|0 1|0 2*\n");
    printf(" *---+---+---*\n");
    printf("1 *1 0|1 1|1 2*\n");
    printf(" *---+---+---*\n");
    printf("2 *2 0|2 1|2 2*\n");
    printf(" *************\n");

    printf("\nPlayer 1 will be 'X' Player 2 will be 'O'.\n");

    printf("Would you like to play a game? (Type a 1 for Yes and a 2 for No): ");
    scanf("%d", &newgame);

    if (newgame != 2)
    {
    printf("***************************\n");
    printf("** Let's start the Game. **\n");
    printf("***************************\n");


    Initialize_Board(ttt); /* Initialize the Game Board */

    /* Loop until Game Over */
    do
    {
    disp_board(ttt); /* display the Game Board */
    get_Player1_move(ttt); /* ask Player1 for a move */
    check(ttt, answer); /* check for winner */
    if(answer == 2) break;
    disp_board(ttt);
    get_Player2_move(ttt); /* ask Player2 for a move */
    check(ttt, answer); /* check for winner */
    } while(answer != 2);

    }
    else
    printf("\n\n **************************************\n");
    printf(" ** Thank you for Playing. GoodBye! **\n");
    printf(" **************************************\n");
    exit;

    }


    /* initialize the matrix to blanks */
    void Initialize_Board(char ttt[3][3])
    {
    int x, y;
    for (x = 0; x < 4; x++)
    {
    for (y = 0; y < 4; y++)
    ttt[x][y] = ' ';
    }
    }


    /* Get the move for player1 */
    void get_Player1_move(char ttt[3][3])
    {
    int x, y;

    /* for(;
    { */
    printf("Player1 please enter your move coordinates using the format (row column): ");
    scanf("%d%d", &x, &y);
    /* if ((x < 0) || (x > 3) || (y < 0) || (y > 3))
    {
    break;
    printf("\nYou have entered Illegal coordinates, Please try again\n");
    }
    }
    x--;
    y--;
    if(board[x][y] != ' ')
    {
    printf("Invalid move, Please try again.\n");
    get_Player1_move(board);
    }
    else */
    ttt[x][y] = 'X';
    }


    /* Get the move for player2 */
    void get_Player2_move(char ttt[3][3])
    {
    int x, y;

    /* for(;
    { */
    printf("Player2 please enter your move coordinates using the format (row column): ");
    scanf("%d%d", &x, &y);
    /* if ((x < 0) || (x > 3) || (y < 0) || (y > 3)) break;
    printf("\nYou have entered Illegal coordinates, Please try again\n");
    }
    x--;
    y--;
    if(board[x][y] != ' ')
    {
    printf("Invalid move, Please try again.\n");
    get_Player2_move(board);
    }
    else */
    ttt[x][y] = 'O';
    }


    /* Display the board on the screen */
    void disp_board(char ttt[3][3])
    {

    printf(" 0 1 2 \n");
    printf(" *************\n");
    printf("0 * %c | %c | %c *\n", ttt[0][0], ttt[0][1], ttt[0][2]);
    printf(" *---+---+---*\n");
    printf("1 * %c | %c | %c *\n", ttt[1][0], ttt[1][1], ttt[1][2]);
    printf(" *---+---+---*\n");
    printf("2 * %c | %c | %c *\n", ttt[2][0], ttt[2][1], ttt[2][2]);
    printf(" *************\n");

    }


    /* See if there is a winner. */
    char check(char ttt[3][3], int answer)
    {
    char winner;
    /* Check for a wining line - Diagonals first */
    if((ttt[0][0] == 'X') && (ttt[1][1] == 'X') && (ttt[2][2] == 'X') ||
    (ttt[0][2] == 'X') && (ttt[1][1] == 'X') && (ttt[2][0] == 'X'))
    {
    printf("\n\nThe winner is Player 1!\n\n");
    Ask_for_new_game(ttt, answer);

    }
    if ((ttt[0][0] == 'O') && (ttt[1][1] == 'O') && (ttt[2][2] == 'O') ||
    (ttt[0][2] == 'O') && (ttt[1][1] == 'O') && (ttt[2][0] == 'O'))
    {
    printf("\n\nThe winner is Player 2!\n\n");
    Ask_for_new_game(ttt, answer);

    }

    /* Check for a wining line - Horizontal Rows */
    if((ttt[0][0] == 'X') && (ttt[0][1] == 'X') && (ttt[0][2] == 'X') ||
    (ttt[1][0] == 'X') && (ttt[1][1] == 'X') && (ttt[1][2] == 'X') ||
    (ttt[2][0] == 'X') && (ttt[2][1] == 'X') && (ttt[2][2] == 'X'))
    {
    printf("\n\nThe winner is Player 1!\n\n");
    Ask_for_new_game(ttt, answer);

    }
    if ((ttt[0][0] == 'O') && (ttt[0][1] == 'O') && (ttt[0][2] == 'O') ||
    (ttt[1][0] == 'O') && (ttt[1][1] == 'O') && (ttt[1][2] == 'O') ||
    (ttt[2][0] == 'O') && (ttt[2][1] == 'O') && (ttt[2][2] == 'O'))
    {
    printf("\n\nThe winner is Player 2!\n\n");
    Ask_for_new_game(ttt, answer);

    }

    /* Check for a wining line - Vertical Rows */
    if((ttt[0][0] == 'X') && (ttt[1][0] == 'X') && (ttt[2][0] == 'X') ||
    (ttt[0][1] == 'X') && (ttt[1][1] == 'X') && (ttt[2][1] == 'X') ||
    (ttt[0][2] == 'X') && (ttt[1][2] == 'X') && (ttt[2][2] == 'X'))
    {
    printf("\n\nThe winner is Player 1!\n\n");
    Ask_for_new_game(ttt, answer);

    }
    if ((ttt[0][0] == 'O') && (ttt[1][0] == 'O') && (ttt[2][0] == 'O') ||
    (ttt[0][1] == 'O') && (ttt[1][1] == 'O') && (ttt[2][1] == 'O') ||
    (ttt[0][2] == 'O') && (ttt[1][2] == 'O') && (ttt[2][2] == 'O'))
    {
    printf("\n\nThe winner is Player 2!\n\n");
    Ask_for_new_game(ttt, answer);

    }
    }


    int Ask_for_new_game(char ttt[3][3], int answer)
    {
    printf("\n\nWould you like to play another game? (Enter a 1 for Yes and a 2 for No): \n");
    scanf("%d", &answer);
    if (answer == 1)
    {
    Initialize_Board(ttt);
    }

    if (answer == 2)
    {
    printf("\n\n\nthank you for playing Tic Tac Toe, Goodbye!\n\n\n");
    return answer;
    }
    }

  2. #2
    Registered User mfc2themax's Avatar
    Join Date
    Aug 2001
    Posts
    346
    too much code... help not possible..... programmers=lazy
    post less code....help will be possible.....programmers still=lazy, but willing to help more
    mfc2themax-Creator of all that is.

  3. #3
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224
    >> I can get it to end and find a winner but it wont quit properly.

    You say that you can get it 2 end, but it won't quit properly. What do you mean by "QUIT PROPERLY" ???

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    void Ask_for_new_game(char ttt[3][3], int answer)
    {
    printf("\n\nWould you like to play another game? (Enter a 1 for Yes and a 2 for No): \n");
    scanf("%d", &answer);
    if (answer == 1)
    {
    Initialize_Board(ttt);
    }

    if (answer == 2)
    {
    printf("\n\n\nthank you for playing Tic Tac Toe, Goodbye!\n\n\n");
    exit(0);
    }
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    This is foreign to me, and therefore suspicious:

    void main (int answer)

    I would place answer in the function body of main and pass it by reference to the various functions or, less recommended, make it a global variable by declaring it before void main(). To my knowledge main() can be void, or have two arguments, argc and argv, but not one argument as you have posted. Having said that I try to learn something new every day, and I am happy to have this be the lesson for the day if need be.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    38

    Unhappy

    Yea, I didnt make myself clear enough on the problem. It does everything OK, in my program and it says "goodbye" and everything , but right after it says bye, it goes back to the last move that was last played on the Tic Tac Toe game, and it includes the winning move. Than it prompts the next player to put in their next move. And the game continues. It wond end. I dont know why the hell its doing that.

    Thanks for the idea Stoned_Coder. But it didnt work It gave me an error saying: Type Mismatch of Redeclaration of 'Ask_for_new_game' and it has the first { ("sqiuggly" the one right under: int Ask_for_new_game(char ttt[3][3], int answer) at the end) highlighted.


    Anyone know whats wrong? Ive tried all I can think of but nothing works. This damn assignment is due tomorrow too so I hope someone can help with this little roadblock.

    Thanks for all your help guys. I really appreciate everything.

  7. #7
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Muahahahaha!!!

    Not to brag or anything, but men are men, and men want pride. I am a newbie and I just finished MY tic tac toe game. There is just one small bug that doesn't make it look as nice, but it works perfect other than that! You can play against another human, or against the computer. Then it asks if you want to play again. And YES, there is AI. Actually, if you play the computer you choose novice, moderate, or insane. And YES, there are differences. On novice, the computer chooses random spot. On moderate, it only blocks. On insane, it blocks, and if it can it will take its winning spot! Here is the source code, its an attachment.

    YOU MAY LOOK AT HOW I DESINGED MINE FOR TIPS ON YOURS!
    Attached Files Attached Files

  8. #8
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    get out of the way leeman, u dont know anything
    ::Shoves::
    my tic tac toe game is better. Look in the game programming board. Look under LEEMANS post. Mine is better. Very Much so.

  9. #9
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    Hey, i don't know if anyone said yet, but did you use the exit(0); function at where the program ends?
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  10. #10
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    you know

    you could use a bool and infinite loop

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    10

    I can't post a new thread for some reason...

    I can't post a new thread for some reason(page wont load). So I hope this message makes it somewhere someone can read it.

    I am trying to multiply two 3x3 arrays. All that I know how to do is add array's, so I am a little lost. Any help would be great. Also if anyone wants, if they can, please copy & post this message as a new thread, that would be great.

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    the reason you get a type mismatch is because there was no reason for that function to return a value to its caller so i made it void and you obviously didn't change the prototype. There are several mistakes in your code and maybe i would have spent 10 minutes sorting it out for you had you used code tags to make it look readable.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Talking errors

    Leeman_s: I downloaded your code and found a couple of errors. It would not compile so I changed the code to work correctly. Before you brag you might want to make sure the code works:


    corrected code:
    Code:
    #include "iostream.h"
    #include "conio.h"
    #include "windows.h"
    #include "stdlib.h"
    #include "time.h" 
    
    char board[3][3];
    char playerup;
    char playername[10];
    int y;
    int z;
    int win=0;
    char name1[10];
    char name2[10];
    int difficult;
    
    void getmove();
    void computermove();
    void showboard();
    void key();
    void checkforwin();
    void check_input();
    
    int main(int q, int w, int moves, char choose, char choose1)
    {
    	do
    	{
    		win=0;
    		moves=0;
    		choose='h';
    		cout<<"Do you want to play with a human (H) or the computer (C)? ";
    		cin>>choose;
    		system("CLS");
    		
    		//********************************PLAY 
    
    HUMAN*********************************
    		if (choose=='H' || choose=='h')
    		{
    			cout<<"Player 1, enter your name: ";
    			cin>>name1;
    			system("CLS");
    			cout<<"Player 2, enter your name: ";
    			cin>>name2;
    			for (q=1;q<4;q++)
    			{
    				for (w=1;w<4;w++)
    				{
    					board[q][w]='-';
    				}
    			}
    			
    			do
    			{
    			    showboard();
    			    key();
    			    playerup='X';
    				cout<<name1;
    			    playername[10]=name1[10];
    				cout<<"HELLO";
    				cout<<playername;
    				_getch();
    				getmove();
    			    moves++;
    			    system("CLS");
    			    checkforwin();
    			    if(win==0 && moves<9)
    				{
    				    showboard();
    				    key();
    				    playerup='O';
    				    playername[10]=name2[10];
    				    getmove();
    				    moves++;
    				    checkforwin();
    				}
    			    system("CLS");
    			    if(moves==9 && win==0)
    				{
    				    win=3;
    				} 
    			}while(win!=1 && win!=2 && win!=3);
    
    		    showboard();
    		    if(win==1)
    			{
    			    cout<<name1<<", you won!"<<endl;
    			}
    		    else if(win==2)
    			{
    			    cout<<name2<<", you won!"<<endl;
    			}
    		    else if(win==3)
    			{
    			    cout<<"Neither of you won, it's a tie!"<<flush<<endl;
    			}
    		    _getch();
    		    system("CLS");
    		    cout<<"Do you want to play another game? ('n' for no) ";
    		    cin>>choose1;
    		}
    		//****************************PLAY COMPUTER*******************************
    		else if(choose=='C' || choose=='c')
    		{
    			cout<<"1) Novice 2)Moderate 3)Insane"<<endl;
                cout<<"What level of difficulty do you want to play on?";
    			cin>>difficult;
    			cout<<"Player 1, enter your name: ";
    			cin>>name1;
    			system("CLS");
    			for (q=1;q<4;q++)
    			{
    				for (w=1;w<4;w++)
    				{
    					board[q][w]='-';
    				}
    			}
    
    			do
    			{
    				showboard();
    				key();
    				playerup='X';
    				playername[10]=name1[10];
    				getmove();
    				moves++;
    				system("CLS");
    				checkforwin();
    				if(win==0 && moves<9)
    				{
    					showboard();
    					key();
    					computermove();
    					moves++;
    					checkforwin();
    				}
    				system("CLS");
    				if(moves==9 && win==0)
    				{
    					win=3;
    				}
    			}while(win!=1 && win!=2 && win!=3);
    			system("CLS");
    			showboard();
    			if(win==1)
    			{
    			    cout<<name1<<", you won!"<<endl;
    			}
    		    else if(win==2)
    			{
    			    cout<<"HA! THE COMPUTER BEAT YOU!"<<endl;
                    _getch();
    				cout<<"GAME OVER"<<endl;
    				_getch();
    				system("CLS");
    			}
    		    else if(win==3)
    			{
    			    cout<<"Neither of you won, it's a tie!"<<flush<<endl;
    			}
    		    _getch();
    		    system("CLS");
    		    cout<<"Do you want to play another game? ('n' for no): ";
    		    cin>>choose1;
    		}
    	}while(choose1!='n' && choose1!='N');
    	return 0;
    }
    	//*********************************FUNCTION DEFINITIONS***********************
    
    	void getmove()
    	{
    		do
    		{
    			do
    			{
    			cout<<playername<<", you are the "<<playerup;
    			cout<<".Enter the x coordinate for your move: ";
    			cin>>y;
    			}while(y>3 || y<1);
    			cout<<""<<endl;
    			do
    			{
    			cout<<playername<<", you are the "<<playerup;
    			cout<<".Enter the y coordinate for your move: ";
    			cin>>z;
    			cout<<""<<endl;
    			}while(z>3 || z<1);
    			if(board[y][z]!='-')
    			{
    				cout<<"You're more stupid than my grandma! That spot's 
    
    taken!"<<flush<<endl;
    				_getch();
    			}
    		}while(board[y][z]!='-');
    		board[y][z]=playerup;
    	}
    
    	void showboard()
    	{
    		cout<<"CURRENT BOARD"<<endl;
    		cout<<"-----------------------"<<endl;
    		cout<<board[1][1]<<" "<<board[1][2]<<" "<<board[1][3]<<endl;
    		cout<<board[2][1]<<" "<<board[2][2]<<" "<<board[2][3]<<endl;
    		cout<<board[3][1]<<" "<<board[3][2]<<" "<<board[3][3]<<endl;
    		cout<<"-----------------------"<<endl;
    		cout<<""<<endl;
    		cout<<""<<endl;
    	}
    
    	void key()
    	{
    		cout<<"This is the key (x coordinate & y coordinate)"<<endl;
    		cout<<"--------------------------------------------------"<<endl;
    		cout<<"(1,1) (1,2) (1,3)"<<endl;
    		cout<<""<<endl;
    		cout<<"(2,1) (2,2) (2,3)"<<endl;
    		cout<<""<<endl;
    		cout<<"(3,1) (3,2) (3,3)"<<endl;
    		cout<<"--------------------------------------------------"<<endl;
    	}
    
    	void checkforwin()
    	{
    		for(int i=1;i<4;i++)
    		{
    			if(board[1][i]=='X' && board[2][i]=='X' && board[3][i]=='X')
    			{
    				win=1;
    			}
    		}
    		for(int j=1;j<4;j++)
    		{
    			if(board[j][1]=='X' && board[j][2]=='X' && board[j][3]=='X')
    			{
    				win=1;
    			}
    		}
    		if(board[1][1]=='X' && board[2][2]=='X' && board[3][3]=='X')
    		{
    			win=1;
    		}
    		if(board[3][1]=='X' && board[2][2]=='X' && board[1][3]=='X')
    		{
    			win=1;
    		}
    		//*****************************************************
    		for(int i=1;i<4;i++)
    		{
    			if(board[1][i]=='O' && board[2][i]=='O' && board[3][i]=='O')
    			{
    				win=2;
    			}
    		}
    		for(int j=1;j<4;j++)
    		{
    			if(board[j][1]=='O' && board[j][2]=='O' && board[j][3]=='O')
    			{
    				win=2;
    			}
    		}
    		if(board[1][1]=='O' && board[2][2]=='O' && board[3][3]=='O')
    		{
    			win=2;
    		}
    		if(board[3][1]=='O' && board[2][2]=='O' && board[1][3]=='O')
    		{
    			win=2;
    		}
    	}
    //***************************************************************************************
    	void computermove()
    	{
    		
    		y=0;
    		z=0;
    		char d;
    		if((difficult==2 || difficult==3) && difficult!=1)
    		{
    			for(int count=1;count<3;count++)
    			{
    				    d='X';
    			    if(difficult==3)
    				{
    				    d='O';
    				}
    			    for(int a=1;a<4;a++)
    				{
    				    if(board[a][1]==d && board[a][2]==d && 
    
    board[a][3]=='-')
    					{
    					    y=a;
    					    z=3;
    					}
    				    if(board[a][1]==d && board[a][2]=='-' && 
    
    board[a][3]==d)
    					{
    					    y=a;
    					    z=2;
    					}
    				    if(board[a][1]=='-' && board[a][2]==d && 
    
    board[a][3]==d)
    					{
    					    y=a;
    					    z=1;
    					}
    				    if(board[1][a]==d && board[2][a]==d && 
    
    board[3][a]=='-')
    					{
    					    y=3;
    					    z=a;
    					}
    				    if(board[1][a]==d && board[2][a]=='-' && 
    
    board[3][a]==d)
    					{
    					    y=2;
    					    z=a;
    					}
    				    if(board[1][a]=='-' && board[2][a]==d && 
    
    board[3][a]==d)
    					{
    				    	y=1;
    					    z=a;
    					}
    				    if(board[1][1]==d && board[2][2]==d && 
    
    board[3][3]=='-')
    					{
    					    y=3;
    					    z=3;
    					}
    				    if(board[1][1]==d && board[2][2]=='-' && 
    
    board[3][3]==d)
    					{
    					    y=2;
    					    z=2;
    					}
    				    if(board[1][1]=='-' && board[2][2]==d && 
    
    board[3][3]==d)
    					{
    					    y=1;
    					    z=1;
    					}
    				    if(board[1][3]==d && board[2][2]==d && 
    
    board[3][1]=='-')
    					{ 
    					    y=3;
    					    z=1;
    					}
    				    if(board[1][3]==d && board[2][2]=='-' && 
    
    board[3][1]==d)
    					{ 
    					    y=2;
    					    z=2;
    					}
    				    if(board[1][3]=='-' && board[2][2]==d && 
    
    board[3][1]==d)
    					{
    					    y=1;
    					    z=3;
    					}
    				}	
    			}
    		}
    
    				    if((y==0 && z==0) || difficult==1)
    					{
    						srand(time (NULL) );
    					    do
    						{
    					        y=1+rand() % 3;
    					        z=1+rand() % 3;
    						}while(board[y][z]!='-' || y==0 || z==0);
    					}
    		            board[y][z]='O';
    		 
    	}

  14. #14
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Well before...

    Before you try to prove someone wrong, use your head. I'm using MSVC++ 6.0, i don't know what you are using. All I know is that it works when I compile it. Perhaps you need some good tutorials on copying and pasting? Also there is this good place I know that has tutorials on how to 'think before you post'.

  15. #15
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    lol

    that was supposed to be a joke

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  2. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 08:58 PM
  3. Tic Tac Toe AI help please...
    By Rune Hunter in forum Game Programming
    Replies: 12
    Last Post: 11-05-2004, 03:24 PM
  4. my tic tac toe is done .. fear?
    By gunder in forum Game Programming
    Replies: 2
    Last Post: 09-14-2003, 11:33 PM
  5. Tic - Tac - Toe...
    By TheUnknowingOne in forum Game Programming
    Replies: 1
    Last Post: 09-10-2002, 06:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21