Help with project! Puzzle in Gambas

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
MB4055
Posts: 4
Joined: Monday 22nd November 2021 6:36pm

Help with project! Puzzle in Gambas

Post by MB4055 »

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.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Help with project! Puzzle in Gambas

Post by stevedee »

MB4055 wrote: Monday 22nd November 2021 6:40pm ...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.
MB4055
Posts: 4
Joined: Monday 22nd November 2021 6:36pm

Re: Help with project! Puzzle in Gambas

Post by MB4055 »

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.
User avatar
cogier
Site Admin
Posts: 1117
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Help with project! Puzzle in Gambas

Post by cogier »

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.

Image

Image
' Gambas class file

Public pPicture As New Picture[16]
hPictureBox As PictureBox
PictureBox1 As PictureBox

Public Sub Form_Open()

  With Me
    .border = False
    .Maximized = True
    .Show
    .Arrangement = Arrange.Fill
  End With

  With PictureBox1 = New PictureBox(Me) As "PictureBox1"
    .Alignment = Align.Center
    .Mode = PictureBox.Fill
    .Expand = True
    .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
  End With

  Wait 2 'Just to allow the picture to display fully
  BreakupPictureInto16Pieces

End

Public Sub BreakupPictureInto16Pieces()

  Dim iWidth, iHeight, iCount As Integer

  For iWidth = 1 To Desktop.Width Step Desktop.Width / 4
    For iHeight = 1 To Desktop.Height Step Desktop.Height / 4
      pPicture[iCount] = Desktop.Screenshot(iWidth, iHeight, Desktop.Width / 4, Desktop.Height / 4)
      Inc iCount
    Next
  Next

  Pieces

End

Public Sub Pieces()

  Dim iLoop As Integer
  Dim iPadding As Integer = 5
  Dim iOrder As Integer[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

  iOrder.Shuffle()
  PictureBox1.Delete

  With Me
    .Maximized = False
    .Border = True
    .Height = Desktop.Height / 2
    .Width = Desktop.Width / 2
    .Padding = iPadding
    .Arrangement = Arrange.Row
    .Center
  End With

  For iLoop = 0 To 15
    With hPictureBox = New PictureBox(Me) As "PictBoxes"
      .Padding = iPadding
      .Height = Me.Height / 4 - (iPadding * 4)
      .Width = Me.Width / 4 - (iPadding * 4)
      .Mode = PictureBox.Contain
      .Alignment = Align.Center
      .Picture = FMain.pPicture[iOrder[iLoop]]
    End With
  Next

End
MB4055
Posts: 4
Joined: Monday 22nd November 2021 6:36pm

Re: Help with project! Puzzle in Gambas

Post by MB4055 »

cogier wrote: Wednesday 24th November 2021 5:47pm 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.

Image

Image
' Gambas class file

Public pPicture As New Picture[16]
hPictureBox As PictureBox
PictureBox1 As PictureBox

Public Sub Form_Open()

  With Me
    .border = False
    .Maximized = True
    .Show
    .Arrangement = Arrange.Fill
  End With

  With PictureBox1 = New PictureBox(Me) As "PictureBox1"
    .Alignment = Align.Center
    .Mode = PictureBox.Fill
    .Expand = True
    .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
  End With

  Wait 2 'Just to allow the picture to display fully
  BreakupPictureInto16Pieces

End

Public Sub BreakupPictureInto16Pieces()

  Dim iWidth, iHeight, iCount As Integer

  For iWidth = 1 To Desktop.Width Step Desktop.Width / 4
    For iHeight = 1 To Desktop.Height Step Desktop.Height / 4
      pPicture[iCount] = Desktop.Screenshot(iWidth, iHeight, Desktop.Width / 4, Desktop.Height / 4)
      Inc iCount
    Next
  Next

  Pieces

End

Public Sub Pieces()

  Dim iLoop As Integer
  Dim iPadding As Integer = 5
  Dim iOrder As Integer[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

  iOrder.Shuffle()
  PictureBox1.Delete

  With Me
    .Maximized = False
    .Border = True
    .Height = Desktop.Height / 2
    .Width = Desktop.Width / 2
    .Padding = iPadding
    .Arrangement = Arrange.Row
    .Center
  End With

  For iLoop = 0 To 15
    With hPictureBox = New PictureBox(Me) As "PictBoxes"
      .Padding = iPadding
      .Height = Me.Height / 4 - (iPadding * 4)
      .Width = Me.Width / 4 - (iPadding * 4)
      .Mode = PictureBox.Contain
      .Alignment = Align.Center
      .Picture = FMain.pPicture[iOrder[iLoop]]
    End With
  Next

End

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.
User avatar
cogier
Site Admin
Posts: 1117
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Help with project! Puzzle in Gambas

Post by cogier »

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.
MB4055
Posts: 4
Joined: Monday 22nd November 2021 6:36pm

Re: Help with project! Puzzle in Gambas

Post by MB4055 »

cogier wrote: Thursday 25th November 2021 12:58pm 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[/code]
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Help with project! Puzzle in Gambas

Post by stevedee »

I'm sorry, I don't really understand your code. But from your description of how it should work...
MB4055 wrote: Saturday 27th November 2021 4:16am ...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.
User avatar
cogier
Site Admin
Posts: 1117
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Help with project! Puzzle in Gambas

Post by cogier »

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.

Image
' Gambas class file

pPicture As New Picture[16]
hPictureBox As PictureBox
PictureBox1 As PictureBox
Splitter1 As Splitter
HPanel1 As Hpanel
HPanel2 As Hpanel
pImage As Image
Timer1 As Timer
iPictDragNo As Integer
iTime As Integer
iCounter As Integer
iAttempts As Integer

Public Sub Form_Open()

  With Me
    .border = False
    .Maximized = True
    .Show
    .Arrangement = Arrange.Fill
  End With

  With PictureBox1 = New PictureBox(Me) As "PictureBox1"
    .Alignment = Align.Center
    .Mode = PictureBox.Fill
    .Expand = True
    .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
  End With

  Wait 0.25 'Just to allow the picture to display fully
  BreakupPictureInto16Pieces
  
  Timer1 = New Timer As "Timer1"
  Timer1.Start

End

Public Sub BreakupPictureInto16Pieces()

  Dim iWidth, iHeight, iCount As Integer

  For iHeight = 1 To Desktop.Height Step Desktop.Height / 4
    For iWidth = 1 To Desktop.Width Step Desktop.Width / 4
      pPicture[iCount] = Desktop.Screenshot(iWidth, iHeight, Desktop.Width / 4, Desktop.Height / 4)
      Inc iCount
    Next
  Next

  Pieces

End

Public Sub Pieces()

  Dim iLoop As Integer
  Dim iPadding As Integer = 0
  Dim iOrder As Integer[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

  iOrder.Shuffle()
  PictureBox1.Delete

  With Me
    .Maximized = False
    .Border = True
    .Height = Desktop.Height / 2
    .Width = Desktop.Width / 1.5
    .Padding = iPadding
    .Arrangement = Arrange.Vertical
    .Center
  End With

  With Splitter1 = New Splitter(Me) As "Splitter1"
    .Expand = True
  End With

  With HPanel1 = New Hpanel(Splitter1) As "HPanel1"
    .Expand = True
  End With

  With HPanel2 = New Hpanel(Splitter1) As "HPanel2"
    .Expand = True
  End With

  FMain.Refresh
  Wait 0.1
  Splitter1.layout = [50, 50]

  For iLoop = 0 To 15
    With hPictureBox = New PictureBox(HPanel1) As "PictBoxes"
      .Padding = iPadding
      .Height = HPanel1.Height / 4
      .Width = HPanel1.Width / 4
      .Mode = PictureBox.Fill
      .Alignment = Align.Center
      .Picture = pPicture[iOrder[iLoop]]
      .Tag = iOrder[iLoop]
    End With
  Next

  For iLoop = 0 To 15
    With hPictureBox = New PictureBox(HPanel2) As "ResultBoxes"
      .Padding = iPadding
      .Height = HPanel1.Height / 4
      .Width = HPanel1.Width / 4
      .Mode = PictureBox.Fill
      .Alignment = Align.Center
      .Tag = iLoop
      .Drop = True
      .Border = Border.Plain
    End With
  Next

End

Public Sub PictBoxes_MouseDrag()

  pImage = Last.Picture.Image
  iPictDragNo = Last.tag
  If Mouse.Left Then Last.Drag(pImage)

End

Public Sub ResultBoxes_Drop()

  Dim PB As PictureBox

  If iPictDragNo = Last.Tag Then
    Last.Picture = pImage.Picture
    Inc Icounter
    For Each PB In HPanel1.Children
      If pb.Tag = iPictDragNo Then pb.Picture = Null
    Next
  End If
  Inc iAttempts

End

Public Sub Timer1_Timer()

  Inc iTime
  Me.Title = "Time taken = " & Str(iTime) & " Seconds - Attemps = " & Str(iAttempts)
  If iCounter = 16 Then Timer1.Stop

End
Post Reply