# SDL Question

This topic is 3179 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello folks, I am making a Tic Tac Toe game for a class, an I am kinda stuck on something an class is not till next week an it's due then so need some help really fast. I was reading something on making a Tic Tac Toe game with SDL for reference an found one a pretty good one, but I am confused on this algorithm.
for(int i = 0;i < 9;i++) {
int X = (i % 3) * 200;
int Y = (i / 3) * 200;

if(Grid == GRID_TYPE_X) {
CSurface::OnDraw(Surf_Display, Surf_X, X, Y);
}else
if(Grid == GRID_TYPE_O) {
CSurface::OnDraw(Surf_Display, Surf_O, X, Y);
}
}

why do I need to take a percent of 3 from i I know I need to find the remander of 3 in a 9 section grid but I don't understand why it would be a percent an not like / any ideas?

##### Share on other sites
Quote:
 Original post by ARC incwhy do I need to take a percent of 3 from i I know I need to find the remander of 3 in a 9 section grid but I don't understand why it would be a percent an not like / any ideas?

The % sign is the modulo operator that tells you the remainder of a division. E.g. 4 % 3 would be 1, 2 % 3 would be 2 and 3 % 3 would be zero. You would also say "4 modulo 3 is 1" etc.

Cheers,
fng

##### Share on other sites
% is not a percentage operator, it is a remainder (also called the "mod" operator in some other languages) operator.

So 10%3=1, 4%2=0 , 24%7 = 3 and so on.

##### Share on other sites
I am still so confused lol...anyone care to explain a little more in depth? this never came up in class dammit!! grr stupid teachers!

##### Share on other sites
% is the modulo operator; basically it allows you to find the remainder of a division between two specified numbers. For example, 5 % 3 == 2; as 3 is able to go into 5 once, leaving a remainder of two. For more information on mod, read this: http://en.wikipedia.org/wiki/Modulo_operator]Wikipedia - Modulo Operator

In this instance, it is used to find the x positions and y positions as the code iterates through the loop for what I can only assume the rest is drawing the container section.

First Iteration:
x:0 y:0
Second Iteration:
x:200 y:0
Third Iteration:
x:400 y:0
Fourth iteration:
x:0 y:200

And so forth...

That said, is the use of SDL a requirement for your assignment? It seems a bit unnecessary if the goal of your assignment is to simply build a Tic-Tac-Toe game; and may be better off being done as a console application, or if graphics are required, using Win32's GDI.

EDIT: Fungai; beaten to the punch by quicker posters! ^-^ Oh well, hope this helped!

##### Share on other sites
SDL is not required but that's the Graphics lib I am most comfortable at the moment An thanks for the information

##### Share on other sites
Ok, this is kinda a new question but for some reason when I load a basic SDL_Surface it does not stay open on my screen it closes automatically any way to stop it from doing this? I thought GUI windows didn't do that

##### Share on other sites
The window closes when SDL_Quit is called, or after main returns if you don't clean up after yourself. You'll need to go into some kind of loop and only stop based on user input.

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631780
• Total Posts
3002314
×