Any way to consolidate this code?

jeffnooch

Member
Licensed User
Longtime User
Any way to consolidate the code in ShowPics SUB?
Seems like a lot of redundant code...already put the images in an array....actually 4 of them...any thoughts? thanks in advance

B4X:
Sub globals
   Dim imgP1() As ImageView
   Dim img15P1, img16P1, img17P1, img18P1, img19P1, img20P1, img25P1 As ImageView
   Dim imgP2() As ImageView
   Dim img15P2, img16P2, img17P2, img18P2, img19P2, img20P2, img25P2 As ImageView
   Dim imgP3() As ImageView
   Dim img15P3, img16P3, img17P3, img18P3, img19P3, img20P3, img25P3 As ImageView
   Dim imgP4() As ImageView
   Dim img15P4, img16P4, img17P4, img18P4, img19P4, img20P4, img25P4 As ImageView
.....

Sub Activity_Create(FirstTime As Boolean)
......
   imgP1 = Array As ImageView(img20P1, img19P1, img18P1, img17P1, img16P1, img15P1, img25P1)
   imgP2 = Array As ImageView(img20P2, img19P2, img18P2, img17P2, img16P2, img15P2, img25P2)
   imgP3 = Array As ImageView(img20P3, img19P3, img18P3, img17P3, img16P3, img15P3, img25P3)
   imgP4 = Array As ImageView(img20P4, img19P4, img18P4, img17P4, img16P4, img15P4, img25P4)

   For i = 0 To 6
      imgP1(i).visible = False
      imgP2(i).visible = False
      imgP3(i).visible = False
      imgP4(i).visible = False
   Next
......



Sub ShowPics
   Dim tm, pm As Int
   For tm = 1 To 7
      For pm = 1 To 4
         Select pm
         Case 1
            If arrScores(pm,tm,0)>=3 Then 
               imgP1(tm-1).Visible=True
               imgP1(tm-1).bitmap=LoadBitmap(File.DirAssets,"a.gif")
            Else If arrScores(pm,tm,0)= 2 Then 
               imgP1(tm-1).Visible=True
               imgP1(tm-1).bitmap=LoadBitmap(File.DirAssets,"b.gif")
            Else If arrScores(pm,tm,0)= 1 Then 
               imgP1(tm-1).Visible=True
               imgP1(tm-1).bitmap=LoadBitmap(File.DirAssets,"c.gif")
            Else If arrScores(pm,tm,0)= 0 Then
               imgP1(tm-1).Visible=False
            End If
         Case 2
            If arrScores(pm,tm,0)>=3 Then 
               imgP2(tm-1).Visible=True
               imgP2(tm-1).bitmap=LoadBitmap(File.DirAssets,"a.gif")
            Else If arrScores(pm,tm,0)= 2 Then 
               imgP2(tm-1).Visible=True
               imgP2(tm-1).bitmap=LoadBitmap(File.DirAssets,"b.gif")
            Else If arrScores(pm,tm,0)= 1 Then 
               imgP2(tm-1).Visible=True
               imgP2(tm-1).bitmap=LoadBitmap(File.DirAssets,"c.gif")
            Else If arrScores(pm,tm,0)= 0 Then
               imgP2(tm-1).Visible=False
            End If         
         Case 3
            If arrScores(pm,tm,0)>=3 Then 
               imgP3(tm-1).Visible=True
               imgP3(tm-1).bitmap=LoadBitmap(File.DirAssets,"a.gif")
            Else If arrScores(pm,tm,0)= 2 Then 
               imgP3(tm-1).Visible=True
               imgP3(tm-1).bitmap=LoadBitmap(File.DirAssets,"b.gif")
            Else If arrScores(pm,tm,0)= 1 Then 
               imgP3(tm-1).Visible=True
               imgP3(tm-1).bitmap=LoadBitmap(File.DirAssets,"c.gif")
            Else If arrScores(pm,tm,0)= 0 Then
               imgP3(tm-1).Visible=False
            End If
         Case 4
            If arrScores(pm,tm,0)>=3 Then 
               imgP4(tm-1).Visible=True
               imgP4(tm-1).bitmap=LoadBitmap(File.DirAssets,"a.gif")
            Else If arrScores(pm,tm,0)= 2 Then 
               imgP4(tm-1).Visible=True
               imgP4(tm-1).bitmap=LoadBitmap(File.DirAssets,"b.gif")
            Else If arrScores(pm,tm,0)= 1 Then 
               imgP4(tm-1).Visible=True
               imgP4(tm-1).bitmap=LoadBitmap(File.DirAssets,"c.gif")
            Else If arrScores(pm,tm,0)= 0 Then
               imgP4(tm-1).Visible=False
            End If
         Case Else
            Msgbox("Something went really wrong!!!","Ooooops")
         End Select
      Next
   Next   
End Sub
 

kickaha

Well-Known Member
Licensed User
Longtime User
Had a quick look and came up with
B4X:
Sub ShowPics
    Dim tm, pm As Int
    For tm = 1 To 7
        For pm = 1 To 4
            Select pm

            Case 1
                imgP1(tm-1).Visible=True
                Select arrScores(pm,tm,0)
                    Case 0
                        imgP1(tm-1).Visible=False
                    Case 1
                        imgP1(tm-1).bitmap=LoadBitmap(File.DirAssets,"c.gif")
                    Case 2
                        imgP1(tm-1).bitmap=LoadBitmap(File.DirAssets,"b.gif")
                    Case Else
                        imgP1(tm-1).bitmap=LoadBitmap(File.DirAssets,"a.gif")
                    End Select

            Case 2
                imgP2(tm-1).Visible=True
                Select arrScores(pm,tm,0)
                    Case 0
                        imgP2(tm-1).Visible=False
                    Case 1
                        imgP2(tm-1).bitmap=LoadBitmap(File.DirAssets,"c.gif")
                    Case 2
                        imgP2(tm-1).bitmap=LoadBitmap(File.DirAssets,"b.gif")
                    Case Else
                        imgP2(tm-1).bitmap=LoadBitmap(File.DirAssets,"a.gif")
                    End Select

            Case 3
                imgP3(tm-1).Visible=True
                Select arrScores(pm,tm,0)
                    Case 0
                        imgP3(tm-1).Visible=False
                    Case 1
                        imgP3(tm-1).bitmap=LoadBitmap(File.DirAssets,"c.gif")
                    Case 2
                        imgP3(tm-1).bitmap=LoadBitmap(File.DirAssets,"b.gif")
                    Case Else
                        imgP3(tm-1).bitmap=LoadBitmap(File.DirAssets,"a.gif")
                    End Select

            Case 4
                imgP4(tm-1).Visible=True
                Select arrScores(pm,tm,0)
                    Case 0
                        imgP4(tm-1).Visible=False
                    Case 1
                        imgP4(tm-1).bitmap=LoadBitmap(File.DirAssets,"c.gif")
                    Case 2
                        imgP4(tm-1).bitmap=LoadBitmap(File.DirAssets,"b.gif")
                    Case Else
                        imgP4(tm-1).bitmap=LoadBitmap(File.DirAssets,"a.gif")
                    End Select

            Case Else
                Msgbox("Something went really wrong!!!","Ooooops")
            End Select
        Next
    Next
    End Sub
Any other savings would need a rethink on your part, possibly use a multidimension array for your images?
 
Upvote 0

jeffnooch

Member
Licensed User
Longtime User
ok so i tried to add to a multi dimension array like in Erel's tick tac toe example...
but is there a way to make this or something similar work...it doesn't like the string going into the array because it's looking for an imageview...
anyway to convert this or do something simple like the theory i'm trying here? once i get the array loaded i think i have the ability to consolidate all the code in my showpics sub...

thanks

B4X:
   For pd = 0 To numP-1
      For td = 0 To 6
         arrImages(pd,td) = "img" & arrTargets(0) & "P" & pd
         arrImages(pd,td).Visible=False   
      Next
   Next
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
It's a bit late for me, but if I haven't missed something in your code, you could try to set your images as follows:
B4X:
 Dim tm, pm As Int
    Dim gifs(4)
    gifs(3)="a.gif":gifs(2)="b.gif":gifs(1)="c.gif"
    For tm = 1 To 7
        For pm = 1 To 4
            Dim tr As Int 
            tr=arrscores(pm,tm,0)
            If tr>=3 Then tr=3
            If tr=0 Then
                img(tm-1,pm-1).visible=False
            Else
                img(tm-1,pm-1).visible=True
                img(tm-1,pm-1).bitmap=LoadBitmap(File.DirAssets,gifs(tr))
            End If
        Next
    Next
where for example, img(N,0)=imgP1(N).
 
Upvote 0

jeffnooch

Member
Licensed User
Longtime User
mc73...thanks...that is pretty close to what i came up with (shown below)...now i'm looking for an easier way to load my 28 imageviews into the arrImages array....

B4X:
   Dim tm, pm As Int
   For pm = 0 To numP-1
      For tm = 0 To 6
         If arrScores(pm+1,tm+1,0)>=3 Then 
            arrImages(pm, tm).Visible=True
            arrImages(pm, tm).bitmap=LoadBitmap(File.DirAssets,"c.gif")
         Else If arrScores(pm+1,tm+1,0)= 2 Then 
            arrImages(pm, tm).Visible=True
            arrImages(pm, tm).bitmap=LoadBitmap(File.DirAssets,"b.gif")
         Else If arrScores(pm+1,tm+1,0)= 1 Then 
            arrImages(pm, tm).Visible=True
            arrImages(pm, tm).bitmap=LoadBitmap(File.DirAssets,"a.gif")
         Else If arrScores(pm+1,tm+1,0)= 0 Then
            arrImages(pm, tm).Visible=False
         End If

but i like the couple of things that you did that consolidate it a bit further...

and here is what i did related to post #5 above...i'm still interested if anything better can be done here...thanks..

B4X:
        imgP1 = Array As ImageView(img20P1, img19P1, img18P1, img17P1, img16P1, img15P1, img25P1)
   imgP2 = Array As ImageView(img20P2, img19P2, img18P2, img17P2, img16P2, img15P2, img25P2)
   imgP3 = Array As ImageView(img20P3, img19P3, img18P3, img17P3, img16P3, img15P3, img25P3)
   imgP4 = Array As ImageView(img20P4, img19P4, img18P4, img17P4, img16P4, img15P4, img25P4)
   For pd = 0 To numP-1
      For td = 0 To 6
         Select pd
            Case 0   'Player 1
               arrImages(pd,td) = imgP1(td)
               arrImages(pd,td).Visible=False   
            Case 1   'Player 2
               arrImages(pd,td) = imgP2(td)
               arrImages(pd,td).Visible=False
            Case 2   'Player 3
               arrImages(pd,td) = imgP3(td)
               arrImages(pd,td).Visible=False
            Case 3   'Player 4
               arrImages(pd,td) = imgP4(td)
               arrImages(pd,td).Visible=False
            End Select         
      Next
   Next
 
Upvote 0
Top