Jump to content

Wow, thats a glitch!

- - - - -

  • Please log in to reply
5 replies to this topic

#1
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 548 posts
  • Location:/etc/passwd
Hello there.
I recently stated writing my first ever C++ game, a humble game of tic tac toe! Anyway I have hit a weird and frustrating problem- when I set player to 1 (Manually, I have yet to add switching gameplay) it all runs fine, and even the win system works.

But once I (manually) set it to player 2 the game instantly acts up, placing the hover/circle tiles 100 pixels off their proper location! I have spent weeks trying to work this out.
[ATTACH]3932[/ATTACH]

Attached Files

  • Attached File  main.cpp   12.85K   55 downloads

Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Haven't found the glitch (yet) but I seriously advise you that you move your tiles to an array and use loops to initialize/perform checks. That way you could really easily extend your game to have more/less tiles without that much code modification. Also, if you could provide a *.png file, that'd be lovely since I'm not much of an artist.

EDIT: I believe I've narrowed the problem down to blitting problem.

+---+---+---+

|0,0|0,1|0,2|

+---+---+---+

|1,0|1,1|1,2|

+---+---+---+

|2,0|2,1|2,2|

+---+---+---+

When I clicked on 1,2 and 2,2 fields the entire png file got blitted.

Edited by Flying Dutchman, 10 June 2011 - 02:46 AM.

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
The mouse_click() method is the only one that utilizes the global value player so far. Here's what it does if player is 2:
        if(player == 2) {

            state = 3;

        }
state is defined in the Tile object, and that is only utilized here:
void Tile::show()   {

    if (state == 0) {

        applySurface(box.x, box.y, squares, screen, &clip[0]);

    }

    if (state == 1) {

        applySurface(box.x, box.y, squares, screen, &clip[2]);

        log("Stat52");

    }

    if (state == 2) {

        applySurface(box.x, box.y, squares, screen, &clip[1]);

    }

    if (state == 3) {

        applySurface(box.x, box.y, squares, screen, &clip[3]);

    }

}
If state == 3 is what should be executed if the player is player 2. The only real problem I see is when you use &clip[3]. You define clip as another global array, here:
SDL_Rect clip[3];
Notice that you only declared clip to have 3 elements. If you use &clip[3], you're actually calling a fourth element, not the third, as arrays are 0-indexed. This is an undefined value, and could lead to all sorts of erratic behavior. You'll need to define and initialize it!

At least, that's all I found so far.
Wow I changed my sig!

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Ah yes, for example you could do this rather than nine separate initializations with an array.

for(each tile as i) 

   tile[i].show();

You can freely create arrays of objects or structures and use them as so.

Your compiler can also assume a lot of things when you do this for better optimization.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Few suggestions; use 0 and 1 values for player, that way you can simply switch players like this:

player = !player;


In Tile::show() method you can replace all that code

void Tile::show()   {

    if (state == 0) 

        applySurface(box.x, box.y, squares, screen, &clip[0]);

    if (state == 1)

        applySurface(box.x, box.y, squares, screen, &clip[2]);

    if (state == 2)

        applySurface(box.x, box.y, squares, screen, &clip[1]);

    if (state == 3)

        applySurface(box.x, box.y, squares, screen, &clip[3]);

}

to something as simple as this

void Tile::show()   {

    applySurface(box.x, box.y, squares, screen, &clip[state]);

}


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 548 posts
  • Location:/etc/passwd
Shizle sticks!
I have got the array thing mixed up!
Thanks tons the program now works fine!
Flying Dutchmen
I see how inefficient the code is getting,but oh well I'm learning aren't I?
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users