Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Writing a 2 player Tic-Tac-Toe game in eVB for your Pocket PC

Written by Pete Vickers  [author's bio]  [read 60104 times]
Edited by Derek

Download the code

Page 1  Page 2  Page 3  Page 4 

As any sharp-eyed reader may have noticed, I have a rather sad interest in TCP/IP and its use on the Pocket PC. When working with eVB, it is rather like being interested in constantly banging your head on the wall, but after a while, you forget about the pain, and just get on with it. I got a bee in my bonnet about writing a noughts and crosses (tic-tac-toe) game playable between 2 Pocket PC's using IRDA or ethernet and eVB. During the creation of the game, I used several of the articles on devBuzz, so I thought it may make an interesting project, drawing these areas together in a 'practical' project. A game such as this cries out for a control array, but as we know, these don't exist in eVB. This is where the article Simulating Control Arrays in eVB: http://www.devbuzz.com/content/zinc_evb_control_arrays_pg1.asp came in useful.

Let's have a look at the screen design. We have 2 frames, 1 for sorting out the connections and 1 for the game itself. We will take care of the positioning in code. (See Building a GUI Engine with Frames : http://www.devbuzz.com/content/zinc_evb_gui_frames_pg1.asp)

The 'connections' frame is pretty straightforward, so we will look at the 'game frame' in more detail. The rather fetching pink squares are in fact 9 picture box controls, named xdisp0 to xdisp8. This is where we will put our noughts and crosses. The names will be explained in more detail shortly. We also add a listbox, menubar, timer and of course a Winsock control. The listbox will tell us what is happening, and the timer will allow us to reset the game when we have a winner.

We will only show the more interesting bits of the code, as the project is downloadable.

In our declarations we have:-

Private xdisp(8) As PictureBox

and in Form_Load

Set xdisp(0) = xdisp0
Set xdisp(1) = xdisp1
Set xdisp(2) = xdisp2
Set xdisp(3) = xdisp3
Set xdisp(4) = xdisp4
Set xdisp(5) = xdisp5
Set xdisp(6) = xdisp6
Set xdisp(7) = xdisp7
Set xdisp(8) = xdisp8

This allows us to simulate a control array, as demonstrated in Robert Levy's article.

The rest of the Form_Load procedure is:-

MyScore = 0
opponentScore = 0
opponentName = "Opponent"
If IRDA Then
txtServer.Text = WinSock1.LocalHostName
Else
txtServer.Text = WinSock1.LocalIP
End If
Make_Menu
Frame1.Top = 0
Frame1.Left = 0
Frame1.Width = Me.Width
Frame1.Height = Me.Height
Frame1.ZOrder
IRDA = True
Simpsons = True
Choose_Picture
SoundOn = True

I decided it would be a good idea to use sound in the game, and allow a choice of symbols. My children are addicted to the Simpsons, so the decision was taken out of my hands. To enable the game to be played silently across the table in the board room or a meeting, the sound is configurable, as is the ability to use IRDA or Ethernet. This is where the menubar control comes in, and it is set up in the Make_Menu Procedure. We set Sound, IRDA and the Simpsons options here, so they are checked when the game starts.

Private Sub Make_Menu()
Dim MenuItems
Set MenuItems = _&
MenuBar.Controls.AddMenu("Options")
MenuItems.Items.Add , 1, "Connect Screen"
MenuItems.Items.Add , 2, "Disconnect"
MenuItems.Items.Add , 3, "IRDA"
MenuItems.Items.Item(3).Checked = True
MenuItems.Items.Add , 4, "Sound"
MenuItems.Items.Item(4).Checked = True
MenuItems.Items.Add , 5, "Simpsons"
MenuItems.Items.Item(5).Checked = True
MenuItems.Items.Add , 6, "Exit"
End Sub

We start the game by showing the 'connections' frame. Here we decide whether to be a server or to be a client. If we are a server we will listen for a connection. If we are a client, we must connect to a server. For ethernet, we can use the IP address or name of the server, but for IRDA, we MUST use the name. My 2 Pocket PC's are rather imaginatively called Ipaq and Jornada. If we say that Ipaq is the server, we would 'listen' on Ipaq by clickin on 'Be a Server'. On Jornada, we would enter Ipaq in the textbox, and click on 'Connect To'. This is where I found out that names in IRDA are case-sensitive. 'Connect To' ipaq would not find the server, where 'Connect To' Ipaq works.

The code for the 'listener' is handled by the Winsock Connectionrequest event:-

Private Sub WinSock1_
ConnectionRequest()
On Error GoTo 0
opponentSymbol = "o"
MySymbol = "x"
myName = txtName.Text

WinSock1.Accept

bMyGo = True
WinSock1.SendData _
"OXNAME" & myName & "|"

lstStatus.AddItem _
"Connected to " & WinSock1.RemoteHostIP, 0
bReady = True
lstStatus.AddItem _
"Game Started. You are " & UCase(MySymbol) _
& "'s", 0
lstStatus.AddItem "Your Go", 0
End Sub

Next Page