Skip to main content
GameDev.net gamedev.net
🔒 Locked

Coding Help VB

Started by AncientHunter Jul 18, 2007 at 3:59 AM 2 replies 550+ views
Original Post
AncientHunter
AncientHunter
hi im having a problem with a realy simple piece of code. Its sad and frustrating. im making a noughts and crosses game for my yr 11 programming project. the problem is that my program wont change the players turns from player 1 to player 2. what ive done is set up a system where ive created a variable called turn (which goes up by 1 everytime a player makes a move) and what im doing is if the number is even then its player 1's turn but if its odd then its player 2's turn. Simple, i know! it wont work. so my question is: is there a EVEN command? or an ODD command? here is that snippet of code: Private Function DeterminTurn(ByVal turn As Integer) As Boolean If turn / 2 = 0 Or 2 Or 4 Or 6 Or 8 Or 10 Then DeterminTurn = True pic_ShowWhosTurn.Image = Image.FromFile(Application.StartupPath & "\Visual basic\Nought" & ".bmp") Else : DeterminTurn = False pic_ShowWhosTurn.Image = Image.FromFile(Application.StartupPath & "\Visual basic\Cross" & ".bmp") End If End Function Is the OR command what i think it is? cos i wasnt tought it, i found it by accident. so any help would be realy appreciated.
The wheel is spinning, but the hampster is dead!
jbadams
jbadams
You have a couple of options here.

One is to use amodulo operation, which will return the remainder of the division of two numbers. If you divide a given integer by 2 and the remainder is 1 your number is an odd number.

So
if((turn % 2) = 1)then\\ odd numberelse\\ even numberend if

Not that the above may not be correct VB syntax, that's from the top of my head and may not be exactly correct but should give you an idea of what needs to be done.

An alternative is bitwise comparison; some_number And 1 should return 0 for even numbers or 1 for odd.


I'd suggest the first method.
- Jason Astle-Adams
PhilHalf
PhilHalf
Kazgoroth is right, your best bet would be Mod.
In VB, the syntax would be:
If turn Mod 2 = 0 Then 'Player 1's turnElse 'Player 2's turnEnd If


Just to explain why what you had wasn't working, after your first Or operation, you're not doing any kind of comparison.
You would have needed to write something along the lines of:
If turn / 2 = 0 Or turn / 2 = 2 Or turn / 2 = 4 etc....

Each expression after the Or must be a Boolean (i.e. return True or False). VB works such that any number that isn't zero is true, so what you have at the moment is always returning true (2, 4, 6 etc.) and so always staying on Player 1's turn.

Hope that helps,

PhilHalf
AncientHunter
AncientHunter
Thank you so much. that was iritating.
The wheel is spinning, but the hampster is dead!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.