Mobile Devices Development Diary 3

Tuesday 31st March – Ship Placement, New Grid, Scores

I spent most of today working out how to get the ship placement more like battleships. I needed a template for the Aircraft carrier like highlighting 3 cells. I initially made it so you select your cell and it places/sets the cell to the left of your choice and to the right so it sets 3 cells in a row. This worked fine if you were near the middle and it would fit into the grid. I wrote some more checks to see if you were three cells away from the left side or the right side of the grid if so then place ship down. I ran into the problem what if you move to the first or last cell 1 or 2. I thought about preventing screen wrapping but ended up with changing it so it adds two cells to the right of the current one this meant just move the current one to the first cell and not move the other two. I did the same for the AI placement. After placing three down it then had to check if it was two away so it could place two down. The AI I wrote for placing the ships down wouldn’t work right as if the cell was empty it should place but it never checked one to the left or the right I changed this to check the other cells.

I then put in rotation in the ship placement. I made it so pressing 0 would rotate the ship either horizontally or vertically. Adding this would throw off my boundary checks as it only checked in the X axis. The code was similar just checking fewer cells as its 8×6. The big problem came from the corners if you started horizontal in the top corner and you rotate to make it vertical it would go off the grid. It is a similar problem with checking wrapping. I made it check if y is 0 then you are at the top of the grid so rotate down. I got myself confused with all the checks and spent a while getting it to work as sometimes if you were horizontal and switched to vertical it would move them diagonal. I found I had made a mistake in one the checks.

As I was still working off one grid Minesweeper style I decided it needs to look like battleships and so I implemented an AI grid this was a copy of the player grid but just held my initial ship placement in. A lot of changes would need to be made to incorporate the new grid mainly AI and player grid checks. I made it after you have placed your initial ships on the AI grid it would then move the AI grid up and bring down the player grid which is off-screen; it would come down every frame until it reached the bottom of the screen. This looked really good as it was animating and gave it a more professional ready to play feel. Once it came down to the bottom of the screen the target had to be changed to move down to the player grid. This involved making a spacing variable which changed when the new board had stopped moving. This made the target snap to the new player grid. I had a few problems with it snapping, the problem being I was setting the targets x and y to equal the start of the grid so the top left corner however I was setting the gridXposition or gridYposition back to 0. Changes were made to check the player’s grid for enemy ships. The enemy ship initial placement was now changed to the new player’s grid. And the AI would now check its AI grid where it had my initial ships.

To get the game up and working I needed scores. I wrote it so if your ship has been hit it removes 1 from your life. A check to see if your life is at 0 if so you lose, same applies to the AI. While this way works fine it didn’t have the battleships feel. I needed life for each ship this would require the ships to have the own unique number to identify them. The problems being all the ships use the same number as they index into the same ship graphic. To make them each have life’s they would need their own graphic so it can give the grid cell a different index. I added 3 more graphics to the image sheet. 1 for the carrier, 1 for the battleship and 1 for the cruiser. This had a knock on effect for everything as grid was looking for the right index value. I looked up using enums in j2me but found there was no support for them. I wrote my own static ints and used them as enums. It took a while to go through the code and place names instead of numbers but it was a good pay off as I could easily alter the image index. I was also able to write life for the ships. The only thing that was missing was the text to screen.


Leave a Reply