Help with project! Puzzle in Gambas

Post

Posted
Rating:
#1 (In Topic #766)
Trainee
 My professor has asigned a project that I havent been able to do.
The project consists of making a 4x4 jigsaw puzzle competition between 2 players, the code must choose between 6 random images and alternate each image until a player finishes the puzzle.
I dont have a clue on how to do it.

Can anyone orient me/help me.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’

MB4055 said

…Can anyone orient me/help me.

Yes, I'm sure we can.

But before anyone jumps in and provides an 'oven-ready' project (this will not help MB4055 with his studies in the long run) lets see if we can point you in the right direction.

Have you written any code in Gambas? If not, you need to start by studying the language, following the documentation, and creating a few simple projects.

If you have worked with Gambas for a while, what part of your assignment are you struggling with? If its the concept of creating an algorithm or method of approaching the problem, you should go back to your tutor for advice.

If you have an idea of how to approach the problem, but are unsure how aspects of your algorithm could be implemented (e.g. generating random references to images) then ask away.

If you have some code that is not working as expected, post your code here.

I hope this is of some help.
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
 Thank you so much for replying!!
Yeah, i have some experience writing code in this language.Im taking ly first coding class in college. I started to struggle when we got into arrays.

I thought that I could start this algorithm by making a 1 dimension array and formatting it into a 4×4 chart. Create a second array and assign values from 1-16 that correspond to each piece of the image and with a bunch of IF's and some luck get it to work. Is this the right way? Or is there something else I could do?
Experience level: basic to low intermediate.
PS. This thing is due monday and its 40% of the final grade. Im going nuts.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
I think that doing this by Monday is a tall order and I don't want to do the work for you but, I was interested in how you might split an image into 16 pieces and came up with the code below that you can copy into a new Graphical Project.
You will need to change the picture to one you have. See line 20.

<IMG src="https://www.cogier.com/gambas/PictPieces1.png"> </IMG>

<IMG src="https://www.cogier.com/gambas/PictPieces2.png"> </IMG>

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Public pPicture As New Picture[16]
  4. hPictureBox As PictureBox
  5. PictureBox1 As PictureBox
  6.  
  7. Public Sub Form_Open()
  8.  
  9.   With Me
  10.     .border = False
  11.     .Maximized = True
  12.     .Show
  13.     .Arrangement = Arrange.Fill
  14.  
  15.   With PictureBox1 = New PictureBox(Me) As "PictureBox1"
  16.     .Alignment = Align.Center
  17.     .Mode = PictureBox.Fill
  18.     .Expand = True
  19.     .Picture = Picture.Load(User.home &/ "Pictures/APOD_Aurora over Clouds.png") ''CHANGE THIS TO A PICTURE THAT WILL FILL YOUR SCREEN e.g 1920 x 1080
  20.  
  21.   Wait 2 'Just to allow the picture to display fully
  22.   BreakupPictureInto16Pieces
  23.  
  24.  
  25. Public Sub BreakupPictureInto16Pieces()
  26.  
  27.   Dim iWidth, iHeight, iCount As Integer
  28.  
  29.   For iWidth = 1 To Desktop.Width Step Desktop.Width / 4
  30.     For iHeight = 1 To Desktop.Height Step Desktop.Height / 4
  31.       pPicture[iCount] = Desktop.Screenshot(iWidth, iHeight, Desktop.Width / 4, Desktop.Height / 4)
  32.       Inc iCount
  33.     Next
  34.   Next
  35.  
  36.   Pieces
  37.  
  38.  
  39. Public Sub Pieces()
  40.  
  41.   Dim iLoop As Integer
  42.   Dim iPadding As Integer = 5
  43.   Dim iOrder As Integer[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  44.  
  45.   iOrder.Shuffle()
  46.   PictureBox1.Delete
  47.  
  48.   With Me
  49.     .Maximized = False
  50.     .Border = True
  51.     .Height = Desktop.Height / 2
  52.     .Width = Desktop.Width / 2
  53.     .Padding = iPadding
  54.     .Arrangement = Arrange.Row
  55.     .Center
  56.  
  57.   For iLoop = 0 To 15
  58.     With hPictureBox = New PictureBox(Me) As "PictBoxes"
  59.       .Padding = iPadding
  60.       .Height = Me.Height / 4 - (iPadding * 4)
  61.       .Width = Me.Width / 4 - (iPadding * 4)
  62.       .Mode = PictureBox.Contain
  63.       .Alignment = Align.Center
  64.       .Picture = FMain.pPicture[iOrder[iLoop]]
  65.     End With
  66.   Next
  67.  
  68.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee

cogier said

I think that doing this by Monday is a tall order and I don't want to do the work for you but, I was interested in how you might split an image into 16 pieces and came up with the code below that you can copy into a new Graphical Project.
You will need to change the picture to one you have. See line 20.

<IMG src="https://www.cogier.com/gambas/PictPieces1.png"> </IMG>

<IMG src="https://www.cogier.com/gambas/PictPieces2.png"> </IMG>

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Public pPicture As New Picture[16]
  4. hPictureBox As PictureBox
  5. PictureBox1 As PictureBox
  6.  
  7. Public Sub Form_Open()
  8.  
  9.   With Me
  10.     .border = False
  11.     .Maximized = True
  12.     .Show
  13.     .Arrangement = Arrange.Fill
  14.  
  15.   With PictureBox1 = New PictureBox(Me) As "PictureBox1"
  16.     .Alignment = Align.Center
  17.     .Mode = PictureBox.Fill
  18.     .Expand = True
  19.     .Picture = Picture.Load(User.home &/ "Pictures/APOD_Aurora over Clouds.png") ''CHANGE THIS TO A PICTURE THAT WILL FILL YOUR SCREEN e.g 1920 x 1080
  20.  
  21.   Wait 2 'Just to allow the picture to display fully
  22.   BreakupPictureInto16Pieces
  23.  
  24.  
  25. Public Sub BreakupPictureInto16Pieces()
  26.  
  27.   Dim iWidth, iHeight, iCount As Integer
  28.  
  29.   For iWidth = 1 To Desktop.Width Step Desktop.Width / 4
  30.     For iHeight = 1 To Desktop.Height Step Desktop.Height / 4
  31.       pPicture[iCount] = Desktop.Screenshot(iWidth, iHeight, Desktop.Width / 4, Desktop.Height / 4)
  32.       Inc iCount
  33.     Next
  34.   Next
  35.  
  36.   Pieces
  37.  
  38.  
  39. Public Sub Pieces()
  40.  
  41.   Dim iLoop As Integer
  42.   Dim iPadding As Integer = 5
  43.   Dim iOrder As Integer[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  44.  
  45.   iOrder.Shuffle()
  46.   PictureBox1.Delete
  47.  
  48.   With Me
  49.     .Maximized = False
  50.     .Border = True
  51.     .Height = Desktop.Height / 2
  52.     .Width = Desktop.Width / 2
  53.     .Padding = iPadding
  54.     .Arrangement = Arrange.Row
  55.     .Center
  56.  
  57.   For iLoop = 0 To 15
  58.     With hPictureBox = New PictureBox(Me) As "PictBoxes"
  59.       .Padding = iPadding
  60.       .Height = Me.Height / 4 - (iPadding * 4)
  61.       .Width = Me.Width / 4 - (iPadding * 4)
  62.       .Mode = PictureBox.Contain
  63.       .Alignment = Align.Center
  64.       .Picture = FMain.pPicture[iOrder[iLoop]]
  65.     End With
  66.   Next
  67.  
  68.  


Hey man!
Thank you so much for replying, what you did really helps me out a lot. I'm really grateful.
Is there a way I could contact you privately? Facebook, IG, WhatsApp or anything?
I have few questions regarding my code.
Again thank you so much.
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
 I am happy to help but would prefer that you post your queries here so that others can assist as well, I don't know it all! Based on the information I have, you are in Mexico and your time zone is GMT-8, mine is GMT, so responses will take a little time.

However, if it helps, you can email me at admin AT gambas DOT one.
Online now: No Back to the top

Post

Posted
Rating:
#7
Trainee

cogier said

I am happy to help but would prefer that you post your queries here so that others can assist as well, I don't know it all! Based on the information I have, you are in Mexico and your time zone is GMT-8, mine is GMT, so responses will take a little time.

However, if it helps, you can email me at admin AT gambas DOT one.
Awesome, I will post here then.
This is my main struggle right now. Each piece of the puzzle has an assigned place on the chart. Pieces go somewhat like this 16 15 14 13
                                                                                                                                                                                   12 11 10  9
                                                                                                                                                                                     8  7   6  5
                                                                                                                                                                                     4  3   2  1
On the right of the screen I have a set of pictures boxes arranged randomly where the user can grab one and place them into of the above. What I dont know to do this; I.E. Lets say the user wants to put piece #5 of the puzzle into the spot where piece #1 should go, I need the code to only allow the drop if the spot he wants to move matches the place where he wants to put it. There is an attempt in this code in a subroutine called "matching". I would appreciate if yall could get me onto the right track. Mario.  PS. Mario is my name
  

Code

Public Sub Form_Open()
 
  PictureBox2.Drop = True
  PictureBox3.Drop = True
  PictureBox4.Drop = True
  PictureBox5.Drop = True
  PictureBox6.Drop = True
  PictureBox7.Drop = True
  PictureBox8.Drop = True
  PictureBox9.Drop = True
  PictureBox10.Drop = True
  PictureBox11.Drop = True
  PictureBox12.Drop = True
  PictureBox13.Drop = True
  PictureBox14.Drop = True
  PictureBox15.Drop = True
  PictureBox16.Drop = True
  PictureBox17.Drop = False

  PictureBox30.tag = "1.jpg"
  PictureBox30.Picture = Picture["1.jpg"]
 
   PictureBox29.tag = "2.jpg"
  PictureBox29.Picture = Picture["2.jpg"]
 
   PictureBox23.tag = "3.jpg"
  PictureBox23.Picture = Picture["3.jpg"]
 
   PictureBox27.tag = "4.jpg"
  PictureBox27.Picture = Picture["4.jpg"]
 
   PictureBox21.tag = "5.jpg"
  PictureBox21.Picture = Picture["5.jpg"]
 
   PictureBox18.tag = "6.jpg"
  PictureBox18.Picture = Picture["6.jpg"]
 
   PictureBox31.tag = "7.jpg"
  PictureBox31.Picture = Picture["7.jpg"]
 
   PictureBox24.tag = "8.jpg"
  PictureBox24.Picture = Picture["8.jpg"]
 
   PictureBox33.tag = "9.jpg"
  PictureBox33.Picture = Picture["9.jpg"]
 
   PictureBox26.tag = "10.jpg"
  PictureBox26.Picture = Picture["10.jpg"]
 
   PictureBox19.tag = "11.jpg"
  PictureBox19.Picture = Picture["11.jpg"]
 
   PictureBox32.tag = "12.jpg"
  PictureBox32.Picture = Picture["12.jpg"]
 
   PictureBox28.tag = "13.jpg"
  PictureBox28.Picture = Picture["13.jpg"]
 
   PictureBox22.tag = "14.jpg"
  PictureBox22.Picture = Picture["14.jpg"]
 
   PictureBox20.tag = "15.jpg"
  PictureBox20.Picture = Picture["15.jpg"]
 
   PictureBox25.tag = "16.jpg"
  PictureBox25.Picture = Picture["16.jpg"]
End

Public Sub PictureBox30_MouseDrag()
   
    drag.icon = Last.Picture
    PictureBox30.Drag(PictureBox30.Tag)
End

Public Sub PictureBox17_Drop()

     'PictureBox17.Picture = Picture[Drag.data]
     matching()
 
End

Public Sub matching()
  If PictureBox30.Picture = PictureBox17.Picture
    PictureBox17.Drop = True
  Endif
 
End

'
'

Public Sub PictureBox29_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox29.Drag(PictureBox29.Tag)
End

Public Sub PictureBox15_Drop()
 
  PictureBox15.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox23_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox23.Drag(PictureBox23.Tag)
End

Public Sub PictureBox16_Drop()
 
  PictureBox16.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox27_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox27.Drag(PictureBox27.Tag)
End

Public Sub PictureBox14_Drop()
 
  PictureBox14.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox21_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox21.Drag(PictureBox21.Tag)
End

Public Sub PictureBox13_Drop()
 
  PictureBox13.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox18_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox18.Drag(PictureBox18.Tag)
End

Public Sub PictureBox11_Drop()
 
  PictureBox11.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox31_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox31.Drag(PictureBox31.Tag)
End

Public Sub PictureBox12_Drop()
 
  PictureBox12.Picture = Picture[Drag.data]
 
End

'
'

Public Sub PictureBox24_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox24.Drag(PictureBox24.Tag)
End

Public Sub PictureBox10_Drop()
 
  PictureBox10.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox33_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox33.Drag(PictureBox33.Tag)
End

Public Sub PictureBox9_Drop()
 
  PictureBox9.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox26_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox26.Drag(PictureBox26.Tag)
End

Public Sub PictureBox7_Drop()
 
  PictureBox7.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox19_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox19.Drag(PictureBox19.Tag)
End

Public Sub PictureBox8_Drop()
 
  PictureBox8.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox32_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox32.Drag(PictureBox32.Tag)
End

Public Sub PictureBox6_Drop()
 
  PictureBox6.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox28_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox28.Drag(PictureBox28.Tag)
End

Public Sub PictureBox5_Drop()
 
  PictureBox5.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox22_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox22.Drag(PictureBox22.Tag)
End

Public Sub PictureBox3_Drop()
 
  PictureBox3.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox20_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox20.Drag(PictureBox20.Tag)
End

Public Sub PictureBox4_Drop()
 
  PictureBox4.Picture = Picture[Drag.data]
 
End
'
'

Public Sub PictureBox25_MouseDrag()
 
    drag.icon = Last.Picture
    PictureBox25.Drag(PictureBox25.Tag)
End

Public Sub PictureBox2_Drop()
 
  PictureBox2.Picture = Picture[Drag.data]
 
End
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Regular
stevedee is in the usergroup ‘Regular’
I'm sorry, I don't really understand your code. But from your description of how it should work…

MB4055 said

…This is my main struggle right now. Each piece of the puzzle has an assigned place on the chart. Pieces go somewhat like this 16 15 14 13
                                                                                                                                                                                   12 11 10  9
                                                                                                                                                                                     8  7   6  5
                                                                                                                                                                                     4  3   2  1
On the right of the screen I have a set of pictures boxes arranged randomly where the user can grab one and place them into of the above…
Lets say the user wants to put piece #5 of the puzzle into the spot where piece #1 should go, I need the code to only allow the drop if the spot he wants to move matches the place where he wants to put it…

…you seem to have a grid of 'destination' PictureBoxes 1 to 16, plus random 'user selection' PictureBoxes with image clips.

Since each random box has the image name in the Tag property, can't you just put the required image name in the appropriate destination PictureBox Tag, and then do a simple Tag compare each time the user tries to make a move?

I'm sorry if I've misunderstood your problem and I'm way off target.
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Guru
cogier is in the usergroup ‘Guru’
I agree with Steve that using a tag will confirm that the correct image drops into the correct PictureBox. I have updated my code to show this working.

If you are posting code use the gb button, it looks a lot better.

<IMG src="https://www.cogier.com/gambas/gb.png"> </IMG>

Code (gambas)

  1. ' Gambas class file
  2.  
  3. pPicture As New Picture[16]
  4. hPictureBox As PictureBox
  5. PictureBox1 As PictureBox
  6. Splitter1 As Splitter
  7. HPanel1 As Hpanel
  8. HPanel2 As Hpanel
  9. pImage As Image
  10. Timer1 As Timer
  11. iPictDragNo As Integer
  12. iTime As Integer
  13. iCounter As Integer
  14. iAttempts As Integer
  15.  
  16. Public Sub Form_Open()
  17.  
  18.   With Me
  19.     .border = False
  20.     .Maximized = True
  21.     .Show
  22.     .Arrangement = Arrange.Fill
  23.  
  24.   With PictureBox1 = New PictureBox(Me) As "PictureBox1"
  25.     .Alignment = Align.Center
  26.     .Mode = PictureBox.Fill
  27.     .Expand = True
  28.     .Picture = Picture.Load(User.home &/ "Pictures/APOD_Northern Summer Twilight.png") ''CHANGE THIS TO A PICTURE THAT WILL FILL YOUR SCREEN e.g 1920 x 1080
  29.  
  30.   Wait 0.25 'Just to allow the picture to display fully
  31.   BreakupPictureInto16Pieces
  32.  
  33.   Timer1 = New Timer As "Timer1"
  34.   Timer1.Start
  35.  
  36.  
  37. Public Sub BreakupPictureInto16Pieces()
  38.  
  39.   Dim iWidth, iHeight, iCount As Integer
  40.  
  41.   For iHeight = 1 To Desktop.Height Step Desktop.Height / 4
  42.     For iWidth = 1 To Desktop.Width Step Desktop.Width / 4
  43.       pPicture[iCount] = Desktop.Screenshot(iWidth, iHeight, Desktop.Width / 4, Desktop.Height / 4)
  44.       Inc iCount
  45.     Next
  46.   Next
  47.  
  48.   Pieces
  49.  
  50.  
  51. Public Sub Pieces()
  52.  
  53.   Dim iLoop As Integer
  54.   Dim iPadding As Integer = 0
  55.   Dim iOrder As Integer[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  56.  
  57.   iOrder.Shuffle()
  58.   PictureBox1.Delete
  59.  
  60.   With Me
  61.     .Maximized = False
  62.     .Border = True
  63.     .Height = Desktop.Height / 2
  64.     .Width = Desktop.Width / 1.5
  65.     .Padding = iPadding
  66.     .Arrangement = Arrange.Vertical
  67.     .Center
  68.  
  69.   With Splitter1 = New Splitter(Me) As "Splitter1"
  70.     .Expand = True
  71.  
  72.   With HPanel1 = New Hpanel(Splitter1) As "HPanel1"
  73.     .Expand = True
  74.  
  75.   With HPanel2 = New Hpanel(Splitter1) As "HPanel2"
  76.     .Expand = True
  77.  
  78.   FMain.Refresh
  79.   Wait 0.1
  80.   Splitter1.layout = [50, 50]
  81.  
  82.   For iLoop = 0 To 15
  83.     With hPictureBox = New PictureBox(HPanel1) As "PictBoxes"
  84.       .Padding = iPadding
  85.       .Height = HPanel1.Height / 4
  86.       .Width = HPanel1.Width / 4
  87.       .Mode = PictureBox.Fill
  88.       .Alignment = Align.Center
  89.       .Picture = pPicture[iOrder[iLoop]]
  90.       .Tag = iOrder[iLoop]
  91.     End With
  92.   Next
  93.  
  94.   For iLoop = 0 To 15
  95.     With hPictureBox = New PictureBox(HPanel2) As "ResultBoxes"
  96.       .Padding = iPadding
  97.       .Height = HPanel1.Height / 4
  98.       .Width = HPanel1.Width / 4
  99.       .Mode = PictureBox.Fill
  100.       .Alignment = Align.Center
  101.       .Tag = iLoop
  102.       .Drop = True
  103.       .Border = Border.Plain
  104.     End With
  105.   Next
  106.  
  107.  
  108. Public Sub PictBoxes_MouseDrag()
  109.  
  110.   pImage = Last.Picture.Image
  111.   iPictDragNo = Last.tag
  112.   If Mouse.Left Then Last.Drag(pImage)
  113.  
  114.  
  115. Public Sub ResultBoxes_Drop()
  116.  
  117.  
  118.   If iPictDragNo = Last.Tag Then
  119.     Last.Picture = pImage.Picture
  120.     Inc Icounter
  121.     For Each PB In HPanel1.Children
  122.       If pb.Tag = iPictDragNo Then pb.Picture = Null
  123.     Next
  124.   End If
  125.   Inc iAttempts
  126.  
  127.  
  128. Public Sub Timer1_Timer()
  129.  
  130.   Inc iTime
  131.   Me.Title = "Time taken = " & Str(iTime) & " Seconds - Attemps = " & Str(iAttempts)
  132.   If iCounter = 16 Then Timer1.Stop
  133.  
  134.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.