Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Tick-Tack-Toe: working with arrays of views

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-2011, 02:36 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Tick-Tack-Toe: working with arrays of views

This is an example of a Tick-Tack-Toe game.
This game demonstrates two concepts. The first is an example of working with multiple views without duplicating code. In this case it is 9 buttons, however similar code can easily handle any number of views.
The second concept is the handling of Activity_Pause and Activity_Resume events to keep the game state during the activity life cycle.

The test it you just need to rotate the device. During the orientation change the activity is paused, created again and eventually resumed.
The game should continue from the previous state.



The following code creates the 9 buttons, puts each button in the correct place and stores a reference to this button in the Buttons array (two dimensions array):
Code:
    For x = 0 To 2
    
For y = 0 To 2
        
Dim b As Button
        b.Initialize(
"button"'All buttons share the same event sub
        b.TextSize = 30
        Activity.AddView(b,offsetX + x * (width + 
10dip), offsetY + y * (width + 10dip), width, width)
        Buttons(x, y) = b 
'store a reference to this view
    Next
Next
Later in Sub Button2_Click we find the button that raised the event with the help of Sender keyword:
Code:
Sub Button_Click
    
'Using Sender we find the button that raised this event
    Dim b As Button
    b = 
Sender
    
If b.Text <> "" Then Return 'Already used button
    ...
Saving the game state is done by using a process global variable. Unlike regular global variables, these variables are kept even when the activity is recreated.
Code:
Sub Activity_Resume
    
'restore the previous state when the activity resumes
    For x = 0 To 2
        
For y = 0 To 2
            Buttons(x, y).Text = ButtonsText(x, y)
        
Next
    
Next
End Sub
Sub Activity_Pause (UserClosed As Boolean)
    
If UserClosed Then
        
'If the user pressed on the back key we cancel the current game
        NewGame
    
End If
    
'save the current state when the activity is paused.
    For x = 0 To 2
        
For y = 0 To 2
            ButtonsText(x, y) = Buttons(x, y).Text
        
Next
    
Next
End Sub
Note that the Buttons array cannot be declared as a process global variable. Only non-UI objects can be declared in Sub Process_Globals.

The code is available here: http://www.basic4ppc.com/android/fil...ickTackToe.zip
Reply With Quote
  #2 (permalink)  
Old 04-08-2011, 02:47 PM
Knows the basics
 
Join Date: Mar 2011
Location: Denver, CO USA
Posts: 91
Smile Who was pushed from an array stand point ?

Who was pushed from an array stand point ? I love
the generic approach using "b = Sender", but how
do you translate that info to which item in the array
was pushed ? Could you use "b.Tag" to store array
index info and how ?
Thanks, Jerry
Reply With Quote
  #3 (permalink)  
Old 04-08-2011, 03:23 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You have two options. You can iterate over the array and check for equality.
Or you can use the Tag property to store the array index.
In this case there are two values so you need to declare your own type:
Code:
Sub Globals
 
Type indices(x as int, y as int)
End  Sub
...
For y = 0 To 2
            
Dim b As Button
            b.Initialize(
"button"'All buttons share the same event sub
            b.TextSize = 30
            Activity.AddView(b,offsetX + x * (width + 
10dip), offsetY + y * (width + 10dip), width, width)
            Buttons(x, y) = b 
'store a reference to this view
            Dim i as indices
            i.initialize 
'Not really necessary in this case
            i.x = x
            i.y = y
            b.Tag = i
        
Next
...
Sub Button_Click
    
'Using Sender we find the button that raised this event
    Dim b As Button
    b = 
Sender
   
Dim i as indices
    i = b.Tag
Reply With Quote
  #4 (permalink)  
Old 04-08-2011, 04:23 PM
Knows the basics
 
Join Date: Mar 2011
Location: Denver, CO USA
Posts: 91
Smile With all the FAST help, my project done in half time

Thanks Erel !
Reply With Quote
  #5 (permalink)  
Old 09-16-2011, 02:18 PM
nfordbscndrd's Avatar
Basic4ppc Expert
 
Join Date: Jan 2011
Location: Hot Springs Village, AR, USA
Posts: 792
Default

Quote:
Originally Posted by Erel View Post
...
Code:
Sub Activity_Resume
    
'restore the previous state when the activity resumes
    For x = 0 To 2
        
For y = 0 To 2
            Buttons(x, y).Text = ButtonsText(x, y)
        
Next
    
Next
End Sub
I just came across this thread while searching for something else.
I believe that the above code should be in Activity_Create, not Activity_Resume.

As you noted in this post:

Quote:
Loading the state from a file should be done in Activity_Create.
If FirstTime = False you can optimize your application and load the state
from a process global variable instead of a file.
And that Activity_Resume should [only] be used for...

Quote:
resuming elements like timers, animations, GPS, camera and so on
[which presumably were turned off in Activity_Pause]...
...since code in Activity_Resume is run every time the app is started, even if the
device was just turned on, and every time you leave the app and come back
even if Android didn't kill the app and its data/states are still in memory.
Reply With Quote
  #6 (permalink)  
Old 09-16-2011, 02:27 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You are correct. Putting the code in Activity_Resume is correct but less optimized.
Reply With Quote
  #7 (permalink)  
Old 05-11-2012, 06:08 PM
Peter Simpson's Avatar
Junior Member
 
Join Date: Apr 2012
Location: Birmingham, UK.
Posts: 14
Thumbs up Thank you for this code

Hello Erel,
Thank you for putting this code in the forum.

I'm already writing an app but I've just started writing another app that uses MySQL DB with dynamically generated buttons.

This code hit the sweet spot :-)
__________________
Wow, what a buzz
www.simplyinvoice.co.uk

Simplicity is the key.
Probably sent from my android tablet ...
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I tick a check box through code? mistermentality Basic4android Updates and Questions 3 03-14-2011 09:23 AM
Index out of range on tick plus speed prob. enonod Questions (Windows Mobile) 2 07-11-2009 05:49 PM
Tick Tack To Obelix Share Your Creations 0 04-20-2009 10:43 AM
simple tick wont fire in a module Cableguy Questions (Windows Mobile) 3 10-16-2008 08:49 AM
Wrong Notation of Tick value Zenerdiode Questions (Windows Mobile) 4 05-28-2008 04:22 PM


All times are GMT. The time now is 10:41 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0