Can I get this to loop back?

This is a discussion on Can I get this to loop back? within the C Programming forums, part of the General Programming Boards category; I need to use a for loop so that the program will let the user play Tic Tac Toe 3 ...

  1. #1
    Unregistered
    Guest

    Can I get this to loop back?

    I need to use a for loop so that the program will let the user play Tic Tac Toe 3 times, problem is, once somebody wins, the program continues to ask for moves until somebody breaks the program. Is there anything that will stop the current game, but still take it back to the beginning of the for loop so that the second game can begin?

    Thanks for reading.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Try something like this:

    Code:
    for (i = 0; i < MAX; i++)
    {
        ...
    
        if (restart)
        {
            i = 0;
        }
    
        ...
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Yes there is ,add a "i < 3;" To line 28 in your code.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659



    ITSA
    Socket Library!

  5. #5
    Unregistered
    Guest
    This is the for loop right now... in 'checkforwin' if somebody wins 'win' will equal 1, to stop the while loop, so I'm trying to find a way to get it out of the while loop, and back to the beginning of the for loop.



    for (game = 0; game < 4; game = game + 1);
    {

    clrscr();
    board(300,80);


    while(win == 0)
    {
    xmove();
    checkforwin();
    omove();
    checkforwin();

    }
    }

  6. #6
    Unregistered
    Guest
    I managed to get it top stop asking for moves after somebody has won, but the program will exit, instead of going back to the to the beginning of the for loop..

    for (game = 0; game < 4; game = game + 1);
    {


    board(300,80);


    while (win < 1)
    {
    xmove();
    checkforwin();

    if (win < 1)
    {
    omove();
    checkforwin();
    }
    }

    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What are you calling to make the prog exit? Is it return or exit()? If so, you need to re-think how that works.

    If you get stuck, post a snippet of real code here, and don't forget to use code tags when doing so please.

    The basics in your psuedo code look OK to me, so you can't be far off the mark.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Unregistered
    Guest
    I'm using return to close the whole program...
    Here's the checkforwin function with the main again:

    Code:
    void checkforwin()		//Checks if either team has won.
    {
    	// X's win.
    	if( (square1==1 && square2==1 && square3==1) || (square4==1 && square5==1 && square6==1) || (square7==1 && square8==1 && square9==1) )  //Horizontal 
    
    wins.
    	{
    		printf("Tic Tac Toe! The X's win! X-cellent job! \n");
    		player1win = player1win + 1;
    		win = win + 1;
    	
    	}
    	if( (square1==1 && square4==1 && square7==1) || (square2==1 && square5==1 && square8==1) || (square3==1 && square6==1 && square9==1) )  //Vertical 
    
    wins.
    	{
    		printf("Tic Tac Toe! The X's win! X-cellant job! \n");
    		player1win = player1win + 1;
    		win = win + 1;
    	}
    	if( (square1==1 && square5==1 && square9==1) || (square3==1 && square5==1 && square7==1) )  //Diagonal Wins.
    	{
    		printf("Tic Tac Toe! The X's win! X-cellant job! \n");
    		player1win = player1win + 1;
    		win = win + 1;
    	}
    	
    	// O's win.
    	if( (square1==2 && square2==2 && square3==2) || (square4==2 && square5==2 && square6==2) || (square7==2 && square8==2 && square9==2) ) //Horizontal 
    
    wins.
    	{
    		printf("Tic Tac Toe! The O's win! Neat-O job! \n");
    		player2win = player2win + 1;
    		win = win + 1;
    	}
    	if( (square1==2 && square4==2 && square7==2) || (square2==2 && square5==2 && square8==2) || (square3==2 && square6==2 && square9==2) )  //Vertical 
    
    wins.
    	{
    		printf("Tic Tac Toe! The O's win! Neat-O job! \n");
    		player2win = player2win + 1;
    		win = win + 1;
    	}
    	if( (square1==2 && square5==2 && square9==2) || (square3==2 && square5==2 && square7==2) )  //Diagonal Wins
    	{
    		printf("Tic Tac Toe! The O's win! Neat-O job! \n");
    		player2win = player2win + 1;
    		win = win + 1;
    	}
    
    	// Cat's Game.
    	if ((square1 != 0 && square2 != 0) && (square3 != 0) && (square4 != 0) && (square5 != 0) && (square6 != 0) && (square7 != 0) && (square8 != 0) && 
    
    (square9 != 0) && (win == 0))
    	{
    		printf("Cat's Game! X's and O's are tied! \n");
    		win = win + 1;
    	}
    
    
    }
    int main()
    {
    	int gdriver = DETECT, gmode;  		//Determines graphics driver and mode to use.
    	initgraph(&gdriver, &gmode, " ");  	//Switches to graphics mode.
    
            player1win = 0;
    	player2win = 0; 
    	gamecount = 1;
    
    	for (game = 0; game <= 3; game = game + 1);	//Tic Tac Toe is played 3 times.
    	{
    		
    		win = 0;
    		square1 = 0;
    		square2 = 0;
    		square3 = 0;
    		square4 = 0;
    		square5 = 0;
    		square6 = 0;
    		square7 = 0;
    		square8 = 0;
    		square9 = 0;
    
    		board(300,80);
    
    		printf("\n \n \n \n \n \n \n");
    		printf("GAME %d \n", gamecount);
    
    		while (win < 1)
    		{
    			xmove();
                        	                checkforwin();
    			
    			if (win < 1)
    			{
    				omove();
    				checkforwin();
    			}
    		}
    			
    	}
    
    getchar();
    return(0);
    }

  9. #9
    Unregistered
    Guest
    I just found this out, my for loop works fine without graphics...
    The 'xmove' , 'omove' and 'board' are all graphics, does that affect the for loop?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    I just found this out, my for loop works fine without graphics...
    The 'xmove' , 'omove' and 'board' are all graphics, does that affect the for loop?
    Can't tell without actually knowing what they're doing! Suggest you read through them looking for exit() type functions. If they're not too big, post the source here and someone might help you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  2. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  3. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  4. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-30-2006, 11:59 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

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