Results 1 to 5 of 5

Thread: [RESOLVED] Check For Win Help

  1. #1

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Resolved [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

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Check For Win Help

    How would you define a state where the player has won?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Check For Win Help

    It would be three of the same colour block either vertically or horizontaly

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    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
  •  



Click Here to Expand Forum to Full Width