Sign in to follow this  
ARC inc

SDL Question

Recommended Posts

ARC inc    100
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[i] == GRID_TYPE_X) {
        CSurface::OnDraw(Surf_Display, Surf_X, X, Y);
    }else
    if(Grid[i] == 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 this post


Link to post
Share on other sites
fng    154
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

Share this post


Link to post
Share on other sites
ArKano22    650
% 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 this post


Link to post
Share on other sites
ARC inc    100
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 this post


Link to post
Share on other sites
Cradelet    101
% 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 this post


Link to post
Share on other sites
ARC inc    100
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 this post


Link to post
Share on other sites
theOcelot    498
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this