SDL Question

Started by
6 comments, last by theOcelot 14 years, 5 months ago
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?
Advertisement
Quote:Original post by ARC inc
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?


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

-- blog: www.fysx.org
% 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.
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!
% 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!
SDL is not required but that's the Graphics lib I am most comfortable at the moment An thanks for the information
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
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.

This topic is closed to new replies.

Advertisement