Silverlight Sliding Puzzle Problem?

trying to create a sliding puzzle game in Silverlight

Page 1 of 1

0 Replies - 2612 Views - Last Post: 01 December 2010 - 10:54 AM

#1 inDireNeed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-December 10

Silverlight Sliding Puzzle Problem?

Posted 01 December 2010 - 10:54 AM

hi, i am trying to create a silverlight application that runs a working sliding puzzle game.
the game should slice a picture(any picture) into 9, 16 or 25 pieces respectively depending on grid size which user chooses, 3x3, 4x4 or 5x5. at the moment i cannot get the program to run with anything but 4x4.

Imports System.Windows.Media.Imaging

Partial Public Class MainPage
    Inherits UserControl
    ' class level variables.
    Dim _iTotalPieces As Integer
    Dim _cvsPartial(_iTotalPieces) As Canvas
    Dim _imgThePicture(_iTotalPieces) As Image
    Dim _iBoardPieces(_iTotalPieces) As Integer
    Dim _iGridSize As Integer


    Public Sub New()
        InitializeComponent()
        'InitialiseCanvas()
        'LoadPicture()
        InitialiseBoard()
        _iGridSize = 4


    End Sub


    Public Sub LoadPicture()
        '    Dim iCount As Integer
        '    Dim myURI As New Uri("test.jpg", UriKind.Relative)
        '    imgFinal.Source = New BitmapImage(myURI)

        '    For iCount = 0 To 15
        '        _imgThePicture(iCount) = New Image  ' create image object
        '        _imgThePicture(iCount).Width = 400
        '        _imgThePicture(iCount).Height = 400
        '        _imgThePicture(iCount).Stretch = Stretch.UniformToFill
        '        _imgThePicture(iCount).Source = New BitmapImage(myURI)

        '        ' add to parent canvas
        '        _cvsPartial(iCount).Children.Add(_imgThePicture(iCount))

        '    Next
    End Sub
    Public Sub InitialiseCanvas()
        '    Dim iCount As Integer

        '    For iCount = 0 To 15
        '        _cvsPartial(iCount) = New Canvas
        '        _cvsPartial(iCount).Width = 100
        '        _cvsPartial(iCount).Height = 100
        '        ' create a string for the name.
        '        ' C + iCount
        '        _cvsPartial(iCount).Name = "C" & iCount.ToString()
        '        GameImageContainer.Children.Add(_cvsPartial(iCount))

        '    Next
    End Sub

    Public Sub InitialiseBoard()
        ' have to set the X,Y coords of each on the board
        Dim iCount, iX, iY As Integer

        ' set the background for the GameImageContainer
        Dim myBrush As New SolidColorBrush()
        myBrush.Color = Colors.Blue
        GameImageContainer.Background = myBrush

        ' adds the small image
        Dim myURI As New Uri("test.jpg", UriKind.Relative)
        imgFinal.Source = New BitmapImage(myURI)

        For iY = 0 To _iGridSize - 1
            For iX = 0 To _iGridSize - 1
                ' work out the count to index the array
                iCount = (_iGridSize * iY) + iX

                ' set up the image boxes
                _imgThePicture(iCount) = New Image  ' create image object
                _imgThePicture(iCount).Width = _iGridSize * 100
                _imgThePicture(iCount).Height = _iGridSize * 100
                _imgThePicture(iCount).Stretch = Stretch.UniformToFill
                _imgThePicture(iCount).Source = New BitmapImage(myURI)
                ' set the 16 image X,Y values
                _imgThePicture(iCount).SetValue(Canvas.TopProperty, _
                                                Convert.ToDouble(iY * 100 * -1))
                _imgThePicture(iCount).SetValue(Canvas.LeftProperty, _
                                                Convert.ToDouble(iX * 100 * -1))
                ' add the clip and set the X,Y values
                Dim r As New RectangleGeometry
                r.Rect = New Rect((iX * 100), (iY * 100), 100, 100)
                _imgThePicture(iCount).Clip = r

                ' create the 16 small canvas
                _cvsPartial(iCount) = New Canvas
                _cvsPartial(iCount).Width = 100
                _cvsPartial(iCount).Height = 100
                ' create a string for the name - C + iCount
                _cvsPartial(iCount).Name = "C" & iCount.ToString()
                _cvsPartial(iCount).SetValue(Canvas.LeftProperty, Convert.ToDouble(-200))
                _cvsPartial(iCount).SetValue(Canvas.TopProperty, Convert.ToDouble(-200))
                ' add the event handler for the mouse click
                AddHandler _cvsPartial(iCount).MouseLeftButtonDown, _
                            AddressOf Canvas_MouseLeftButtonDown


                ' add the image to the canvas
                _cvsPartial(iCount).Children.Add(_imgThePicture(iCount))

                ' add the canvas to the GameImageContainer
                GameImageContainer.Children.Add(_cvsPartial(iCount))
            Next
        Next



    End Sub

    Public Sub DrawBoard()
        Dim iPosition, iCount, iX, iY As Integer

        For iY = 0 To _iGridSize - 1
            For iX = 0 To _iGridSize - 1
                iCount = (_iGridSize * iY) + iX
                iPosition = _iBoardPieces(iCount)

                ' sets the 16 canvas X,Y values
                If iCount < _iTotalPieces - 1 Then
                    _cvsPartial(iPosition).SetValue(Canvas.LeftProperty, _
                                                 Convert.ToDouble(iX * 100))

                    _cvsPartial(iPosition).SetValue(Canvas.TopProperty, _
                                                 Convert.ToDouble(iY * 100))
                End If
            Next
        Next

    End Sub

    Public Sub ShufflePieces()
        Dim iCount, iPos1, iPos2, iTmp As Integer
        Dim myRandom As New Random(System.DateTime.Now.Second)

        ' need to set the values in the _iBoardPieces array
        For iCount = 0 To _iTotalPieces - 1
            _iBoardPieces(iCount) = iCount
        Next
        _iBoardPieces(_iTotalPieces) = -1

        ' shuffle the array by going through it 100 time.
        ' on each pass, generate 2 random numbers
        ' compare them, if not equal then swap.
        For iCount = 0 To 100
            iPos1 = myRandom.Next(_iTotalPieces - 1)
            iPos2 = myRandom.Next(_iTotalPieces - 1)
            If iPos1 <> iPos2 Then
                ' swap the numbers
                iTmp = _iBoardPieces(iPos1)
                _iBoardPieces(iPos1) = _iBoardPieces(iPos2)
                _iBoardPieces(iPos2) = iTmp
            End If
        Next

    End Sub

    Public Sub Canvas_MouseLeftButtonDown(ByVal sender As Object, _
                                          ByVal e As MouseButtonEventArgs)
        Dim cvs As New Canvas()
        Dim iCanvasID As Integer
        Dim iLocation As Integer
        Dim iEmpty As Integer
        Dim iCount, iX, iY As Integer
        cvs = sender

        ' find who got clicked
        ' the sender has a name property
        ' find it and parse
        iCanvasID = Val(cvs.Name.Substring(1, (cvs.Name.Length - 1)))

        For iCount = 0 To _iTotalPieces - 1
            If _iBoardPieces(iCount) = iCanvasID Then
                iLocation = iCount
            ElseIf _iBoardPieces(iCount) = -1 Then
                iEmpty = iCount
            End If
        Next

        testText.Text = "You clicked canvas " & iCanvasID & _
                        " in position " & iLocation & _
                        ". Empty is " & iEmpty

        ' move the piece if possible
        ' check if squares iEmpty +-1 and +-4 
        ' is the iLocation
        If (iEmpty + 1 = iLocation) Or
            (iEmpty - 1 = iLocation) Or
            (iEmpty + _iGridSize = iLocation) Or
            (iEmpty - _iGridSize = iLocation) Then
            ' its a valid move, so change the coordinates of the 
            ' canvas and move the empty square marker.
            ' set the coordinates
            iX = iEmpty Mod _iGridSize
            iY = iEmpty \ _iGridSize
            _cvsPartial(iCanvasID).SetValue(Canvas.LeftProperty, _
                                            Convert.ToDouble(iX * 100))
            _cvsPartial(iCanvasID).SetValue(Canvas.TopProperty, _
                                            Convert.ToDouble(iY * 100))
            ' set the empty spot to where I moved the piece FROM.
            _iBoardPieces(iLocation) = -1
            ' set the empty spot to hold the canvas
            _iBoardPieces(iEmpty) = iCanvasID

        End If

    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, _
                               ByVal e As System.Windows.RoutedEventArgs) Handles btnStart.Click
        ShufflePieces()
        DrawBoard()
    End Sub

    Private Sub RadioButton1_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RadioButton1.Checked
        GameImageContainer.Children.Clear()
        _iGridSize = 3
        _iTotalPieces = _iGridSize * _iGridSize

        InitialiseBoard()
    End Sub

    Private Sub RadioButton2_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RadioButton2.Checked
        GameImageContainer.Children.Clear()
        _iGridSize = 4
        _iTotalPieces = _iGridSize * _iGridSize

        InitialiseBoard()
    End Sub

    Private Sub RadioButton3_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RadioButton3.Checked
        GameImageContainer.Children.Clear()
        _iGridSize = 5
        _iTotalPieces = _iGridSize * _iGridSize

        InitialiseBoard()
    End Sub
End Class 


the design page just has 3 radio buttons for grid size, start button and canvas for picture and pieces
i've attached the file just in case
i admit i'm useless at this, but i have been trying to get this working for quite a while and it just does not like me.
any help would be greatly appreciated!


p.s. i was not sure what section to post this so apologies if this isn't an appropriate selection.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1