C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-06-2002, 02:47 PM   #1
Unregistered
Guest
 
Posts: n/a
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.
  Reply With Quote
Old 05-06-2002, 02:52 PM   #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;
    }

    ...
}
Shiro is offline   Reply With Quote
Old 05-06-2002, 02:53 PM   #3
Registered User
 
Join Date: Feb 2002
Posts: 591
Yes there is ,add a "i < 3;" To line 28 in your code.
Barjor is offline   Reply With Quote
Old 05-06-2002, 02:59 PM   #4
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 5,034
Sebastiani is offline   Reply With Quote
Old 05-06-2002, 03:18 PM   #5
Unregistered
Guest
 
Posts: n/a
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();

}
}
  Reply With Quote
Old 05-06-2002, 04:04 PM   #6
Unregistered
Guest
 
Posts: n/a
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();
}
}

}
  Reply With Quote
Old 05-06-2002, 05:14 PM   #7
End Of Line
 
Hammer's Avatar
 
Join Date: Apr 2002
Posts: 6,240
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]
Hammer is offline   Reply With Quote
Old 05-06-2002, 05:26 PM   #8
Unregistered
Guest
 
Posts: n/a
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);
}
  Reply With Quote
Old 05-06-2002, 08:00 PM   #9
Unregistered
Guest
 
Posts: n/a
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?
  Reply With Quote
Old 05-07-2002, 03:34 AM   #10
End Of Line
 
Hammer's Avatar
 
Join Date: Apr 2002
Posts: 6,240
Quote:
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]
Hammer is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Link to DLL with LoadLibrary corrupts control loop Ramses800 C Programming 8 12-01-2008 10:09 AM
Mutiliplcation Program that uses a (do, while, and for loop)? Debbie Bremer C++ Programming 4 10-11-2008 06:04 PM
Can a "switch" be inside a loop? gmk0351 C Programming 5 03-28-2008 05:47 PM
syntax question cyph1e C Programming 19 03-31-2006 12:59 AM
How to change recursive loop to non recursive loop ooosawaddee3 C Programming 1 06-24-2002 08:15 AM


All times are GMT -6. The time now is 04:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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