Got a little problem here! :D

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
KGangsta
Posts: 3
Joined: July 30th, 2012, 2:51 am
Location: Portugal
Contact:

Got a little problem here! :D

Post by KGangsta » July 30th, 2012, 3:29 am

Well hello guys, above all thank you to who cared to open this post with the intention of helping me :)

Well Im currently on lesson 12, and I'm getting a bug, i already tried to match the code from the video and i can't detect any difference, well I'm not going to say "but i copied the code EXACTLY blabla" because if I'm getting errors It's because I failed somewhere, now whats my problem,
Heres the Grid

Draws|Draws|Draws
.Here.|.Here.|.Here.
-----.|.-----|-----
Draws|Draws|Draws
.Here.|.Here.|.Here.
-----.|.-----|-----
Draws|DoNot|Draws
.Here.|.Here.|.Here.

The others boxes work perfectly and detect if a combination is done.

I've managed to put the Grid drawn correctly, all is working well despite 1 box, which i cannot draw my X or O, but the cursor can go to those boxes, but when i press ENTER to draw the objects, it doesnt do anything.
The box that is "DoNot here" , is a bit bitchy, when debugging(or executing on Release) sometimes it will let me draw on it, sometimes not, even if it is always the first box to be drawn.

Could someone help me please? I just started programming C++ due to Chili's tutorials wich I find amazing, and he tries to EXPLAIN things, not those tutors who give the program so people can just copy and paste the code and say "hey mommy! look at the game i *made*! ", actually this was the first video where i think "shit, NOW I know why I need some maths >.<"

EDIT 1 : I managed to find the problem, the last box wasnt being attributed the EMPTY status, so when the drawing function didnt detected the EMPTY state(because it was NULL, didnt had any type of value), the IF statement would ignore it, but the DoNot box still has the same random error, some times it will draw, sometimes it not, but more often not drawing.

EDIT 2 : So using the debugger, i managed to find the error, s7 = -858993460, while the other sNumbers are all = EMPTY despite the initialization functios of these values seem correct.

BrokenBackspaceKey
Posts: 9
Joined: July 29th, 2012, 3:09 pm

Re: Got a little problem here! :D

Post by BrokenBackspaceKey » July 31st, 2012, 2:22 am

Yes, the dreaded wild pointer. Worse than hunting the wild boar.

Your value for that box is being overwritten by an uninitialized pointer that can be anywhere in your code. I would say that you can track it down with the debugger, but unifitialized pointers can change the behavior when you add or subract code (for example, the debugger adds lots of overhead), so if, for example, you were unintentionally overwriting a place in memory we'll call address ABCD, it will have one type of odd behavior, but if you add or subtract code, the same spot in memory might be now affecting a different place in your program, or even, a completely different program. And even the place in memory that you are overwriting may have a different value in it; like now it overwrites the memory location DDAA.

Better than using the debugger then, is to first go through your program with a fine tooth comb to find the bugs, and pay specific attention to pointers. All pointers should be initialized to something. Initializing them all to NULL is better than nothing.

Then if you still can't find the pointer that is giving you problems, my next suggestion is to post the code, and maybe someone else will spot the problem. It's amazing how even veteran programmers will make an obvious mistake that they cannot find, but a newbie will spot immediately.

Then if that doesn't work, you will have to use the debugger. Please don't read me wrong, the debugger is great for finding bugs that don't mutate, and you should always try that first, but an uninitialized pointer can escape detection of some of the best debuggers (although some really good lint programs will actually catch most bad pointers).

Hope you find some use in all of my suggestions.

<-- Backspace

BrokenBackspaceKey
Posts: 9
Joined: July 29th, 2012, 3:09 pm

Re: Got a little problem here! :D

Post by BrokenBackspaceKey » July 31st, 2012, 2:34 am

Oh, I forgot!

Along with uninitialized pointers, you should also watch out for writing to an array element that is outside the declared size. Like for example, you might be writing to element 98 of an array that was declared to have only 50 elements. Both C and C++ allow you to do crazy stuff like that. A lot of power, but with it comes a lot of responsibility (paraphrasing Chili).

<-- Backspace

KGangsta
Posts: 3
Joined: July 30th, 2012, 2:51 am
Location: Portugal
Contact:

Re: Got a little problem here! :D

Post by KGangsta » July 31st, 2012, 6:06 pm

hey ya there BrokenBackspaceKey, thanks for the help! But on the part where I'm at, I didn't started working with pointers or arrays, so I "think" that may not be the problem, but im uploading my code so that someone can seem to find the problem ^^

The thing is that when i debug and break, the values of the boxes are EMPTY( not EMPTY of having nothing, but EMPTY to say that i can draw there), but if I try to write on the box, it will not work DESPITE of on debug it says its EMPTY, but if I draw on the boxes that work, the value of the box that doesnt work goes to -858993460 value, and the boxes are random.

Heres the code to say that the boxes are EMPTY, it's exactly how Chili wrote it.

for( int index = 0; index < 9; index++)
{
SetSquareState( index,EMPTY );
}


Here I uploaded it on Dropbox http://db.tt/nwppH25p

BrokenBackspaceKey
Posts: 9
Joined: July 29th, 2012, 3:09 pm

Re: Got a little problem here! :D

Post by BrokenBackspaceKey » July 31st, 2012, 7:03 pm

I've looked at your code, and I think I see the problem. The variable activePlayer is not initialized. So the variable may initially have a value that is well outside the three enums, EMPTY, X, or O, which would make the function call to set the first square state to act abnormally. (After the first turn, the variable then flops back and forth between X and O, which is normal.)

That would be best guess, so initialize activePlayer in the game constructor to X or O, and please let me know if it worked.

<-- Backspace

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Got a little problem here! :D

Post by LuX » July 31st, 2012, 7:14 pm

Dude, why upload a 12 Mb file on another site. Read the "READ THIS FIRST" post. You could have uploaded a 75 Kb file here on this site.
ʕ •ᴥ•ʔ

KGangsta
Posts: 3
Joined: July 30th, 2012, 2:51 am
Location: Portugal
Contact:

Re: Got a little problem here! :D

Post by KGangsta » July 31st, 2012, 7:49 pm

BrokenBackspaceKey wrote:I've looked at your code, and I think I see the problem. The variable activePlayer is not initialized. So the variable may initially have a value that is well outside the three enums, EMPTY, X, or O, which would make the function call to set the first square state to act abnormally. (After the first turn, the variable then flops back and forth between X and O, which is normal.)

That would be best guess, so initialize activePlayer in the game constructor to X or O, and please let me know if it worked.

<-- Backspace
It worked! I initialized it to "X", and it works like a charm now, thank you very much! Now i can continue the lesson to the part where we put an AI, many thanks :)

@Lux sorry about it :p next time i'll know :)

Post Reply