Mobile Devices Development Diary 4

Wednesday 1st April – Scores, Refactoring and Bluetooth

The basic game was nearly done now it just needed scores. I researched text to screen and found it very easy to implement. I had it displaying “You sank the enemy’s Battleship” when the life of the AIs battleship equalled 0. I had a few issues with the text being too big for the screen. I used the width of the device and divided it by 2 to find the middle of the screen to draw to. Initially it would go from the left of the text but I saw there were different parameters for where to draw the text from such as Baseline meaning draw from the centre of the string. The next problem was the text didn’t wrap or move onto a new line. I tried putting \n for new line but got nowhere. I looked on the internet and found there is no new line method for j2me. I made two draw strings and just moved the y down slightly so it looked like a new line. I then got the text to come from the top of the screen down to the bottom each frame moving down. It looked really nice and made it feel more alive and animated instead of boring static text flashing up. I had a single player battleship game all working.

By now my code had become a mess the biggest thing was I wasn’t taking advantage of classes mainly because being so new to java and j2me I didn’t want try using them. I began to sort out what needed to be classed, the two main areas where the AI and the Player. I put these in a separate class but found I couldn’t access the variables in the main class. I checked the variables and they were private so I changed them to public the next problem was I had been using mostly global variables in my main class this meant I couldn’t pass them into the new classes as there was too many. Instead I passed in the main class as an object to the new classes default constructor. This allowed me full access to the main class if the variables and methods were public. From within the AI class I needed access to the player class to do this I could go through the main class and go into the Player class. A lot of modifications where needed to the other classes as they relied on main and the variables there. After the classes worked I decided to neaten up my code this involved lots of refactoring to make variables have nicer easy to read names.

The next thing I looked at was getting a main menu working. I looked at a sample main menu from a book but I had problems integrating it with mine as it something to do with extending canvas and not extending midlet. I then looked at a travel list sample which shows how to use lists. I took this and modified it work with my game. The only down side is I can’t get the canvas functionality as it must extend midlet. I could write a midlet that launches the menu to the launch my game midlet but it works fine the way is at the moment.

So far I’ve got my menu with my single player battleships game working now I needed to make a Bluetooth mode. For this I began looking at a counter example in which the phone acts as both the client and server, I read a chapter from a book on Bluetooth and planned out what would be needed for a Bluetooth mode. I initially wanted to have a one mobile be a client and the other be a server as I have used this method before but with visual basic. I wanted one phone to be sending out the information while the other listens. I decided to base my game off the counter example as it was all there ready to be used the core functionality was set up I just needed to make a new client and server class for my game. I thought about duplicating my classes and keeping them separate from single player mode, so if I made a mistake single player would still work but then realised that if I can get it sending and receiving data, I can just pass that on to the AI class and trick it into thinking its running AI. It also saves having to make the same changes to single and multiplayer when adding new features later.

This is my 2nd and 3rd milestone reached which was to get scores and menus working.

From the midlet class selecting Bluetooth would change a Boolean to true in my main app. The player class would now become the server so it would send out data and the AI class would receive the data. To start with I just sent out the current grids x and y position just to test that Bluetooth was working. I then displayed them on screen to see it working. I planned out just to send the AI grid and for the client to receive this. This was because when you are firing on the player grid it is checking the AIs grid to see if you have hit so by receiving the other players AI grid you are actually playing against them. This involved two for loops with i and j to index into the 2d AI grid array. This worked great with no slow down or lag even though it was sending 48 ints I expected there to be some delay in updating the grid. The only problem with this method was it wasn’t turn based you also couldn’t see the player attacking your ships or their moves. I put the send and receive in the players go and the AI go method and this made the mobile freeze until the both players have chosen this worked well as was turn based. The only downside is the first place you put your initial ship doesn’t count as putting your ship why I have no idea.


Leave a Reply