-
Jul 19th, 2009, 04:48 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Check For Win Help
I have recently taken up .net game programming and have been through a few tutorials but have now decided on a project of my own. I've drawn up a plan for my project but cant figure out the best way to check for a win.
Game Background
Basically each level will have a certain amount of random coloured blocks at the top of the screen. These blocks are created in a class I have created which has four parameters (Size, Location, Colour1, Colour2).
Each block on the screen is stored in a two dimensional array of my block class.
The user will control a shooter at the bottom of the screen which will generate a random coloured block. When the block is shot and stops i need to test the two blocks above and two blocks to the left and right to see if they are the same colour. Then if they are this will be a win and the blocks will disappear.
The problem i have is how to check for the win.
Any help will be appreciated
-
Jul 19th, 2009, 04:52 PM
#2
Re: Check For Win Help
How would you define a state where the player has won?
-
Jul 19th, 2009, 04:56 PM
#3
Thread Starter
Addicted Member
Re: Check For Win Help
It would be three of the same colour block either vertically or horizontaly
-
Jul 21st, 2009, 09:50 AM
#4
Re: Check For Win Help
Brute-force wise, you can loop through each element in your array and check the surrounding elements. Here's a simple example:
Code:
Private Sub CheckForWin()
Dim SizeOfBoard = 10 'For a 10x10 board
Dim block(SizeOfBoard - 1, SizeOfBoard - 1) As Color 'make a 10x10 board
For i As Integer = 0 To SizeOfBoard - 1
For j As Integer = 0 To SizeOfBoard - 3
'Check horizontal the first line, check vertical the second line.
If (block(i, j) = block(i, j + 1) AndAlso block(i, j) = block(i, j + 2)) OrElse _
(block(j, i) = block(j + 1, i) AndAlso block(j, i) = block(j + 2, i)) Then
Console.Write("YOU WIN!!!")
Exit Sub
End If
Next
Next
End Sub
Notice the first loop cycles 10 elements but the second only cycles 8. This is because you're always checking j+1 and j+2. If the second loop went to 10 as well, then it would crash because j+1 would eventually = 11. Our array doesn't have an 11th element.
-
Jul 22nd, 2009, 12:22 PM
#5
Thread Starter
Addicted Member
Re: Check For Win Help
Thanks for your help on this one Jenner
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|