# Battleship project

Hey guys,

I might be asking for a lot, but I have to make a battleship game in 2 weeks. I started it but I feel like it’s getting nowehere.
So I’ve got a couple of questions to make this project go a bit faster. (I actually think I’ll have to restart from zero but this is what I’ve made until now.)

This it the interface I’ve got so far, but each square is an individual canvas. My friend made a chess board using only one canvas, but he won’t tell me how he did it. Can you please tell me how it’s done ? Also, how will I interact with it ?

Since I want two players to play, I now have a way to make each window visible at once (while window1 is visible, window2 is invisible and vice versa). I was wondering if there was a way to save the data of each slot in an array that I could access for each player’s turn without them interacting with each other so I don’t have to use 2 windows.

I’m using a single dimension array right for all the canvas from window1 and all of the canvas from window2. I heard 2D arrays exist but I can’t find anything in REALbasic about them. If you could explain it simply on both how to use it and how to interact with it, it would be awesome!

Last problem but not least, I have no idea how to position the boats and “select” them from the interface even though I feel like they would only be images drawn on the canvas. Please help me in that too.

For me to get this highest grade I can get, I need to add an AI too. I guess it will be easier when everything else will be done.

first… this sounds like a homework assignment… so I will only give you hints.

yes, use a SINGLE canvas… then in the paint event draw the “board”… a little math will take a mouse click and return the “square” selected (Note : tell you friend he isn’t helping you learn by lording his knowledge over yours)

you dim a single dimension array like this

``Dim A(14) as integer // or whatever datatype you need``

a 2D array is

``Dim A(14,14) as integer // or whatever datatype you need``

@Dave S beat me to the finish.

+1 on what he says.

I would also suggest that you start by reading this.
You will find very interesting information on arrays here

Once you made some progress on your own, don’t hesitate to ask if you have questions. Just make sure that you show what you tried already to solve the issue. You will get more precise help.

Have fun!

@Dave S
You’re right, this is an assignement. But that doesn’t change the fact that I have no idea where to start. You guys gave me a nice hint but that doesn’t change the fact that I have no idea how to use the 2D array to make a “board”. The Array page didn’t help me understand what I can use them for. Please explain in more details if possible ?

@Louis Desjardins
Making progress on my own… be prepared because I may ask a lot of things. (Tell me if it’s a problem)

``````//draw a board
dim i,j as byte
for i=1 to 10
for j=1 to 10
g.ForeColor=&c0000ff
g.penwidth=2
g.fillrect(1*i,1*j,25,25)
j=j+1
next j
i=i+1
next i``````

Could you tell me what’s wrong with this code ?

1. Enter for i
2. Enter for j
3. Draw a 25x25 pixels rectangle at position 1,1 in the canvas
5. Do it again with new variables.
But instead it just makes a square with a huge border.

Louis, please remember a few things, (and I don’t mean to be harsh)

• its a homework assignment. so either your instructor has given you something beyond the current level of the class, or you haven’t been studying as required, or (least likely) you have a terrible instructor
• this is a community based volunteer forum, and we are here to help you, not to do it for you, so as the other Louis said [quote=387923:@Louis Desjardins]Once you made some progress on your own, don’t hesitate to ask [/quote]. But it is not our place (nor should it be) to replace your instructor, replace you own need to research. What we will do is guide you, one you show some due dilegence. “I have this code and I don’t understand why it doesn’t work” , but “Not design my software for me”

Note : this project will be near impossible without knowing what/how a 2D array is… (see bullet #1 above)

P.S. you should also step through your code, and at least twice through each loop, to make sure they do what you think they do. If you would do so youd have noticed immediately that your code is not doing what you thought it should.

Damn. Accidentally deleted my earlier post.

You are drawing lots of overlapping rectangles, so of course you end up with one big rectangle.

Walk through your code - and at least twice through each loop - and check how the values of your variables change. Doing so would have quickly revealed that the code is not doing what you think it is doing.

The first thing you need to do - before writing any code - is to write down the step by step instructions on HOW to solve the problem. Those can be in plain English. Then each step just needs to be converted into code. Identical steps become methods.

The way I start a new project is to write the step by step instructions as comments, adding methods (with their own comments) as required. That way you end up with a perfect blueprint for your code AND you have already well documented it.

An 2 D(imensional) array has two directions :
Direction X for horizontal
Direction Y for vertical.

An array is always the same type, string, integer, double but never can be mixed types. Also note that array indexes start from “0”. Individual positions are called “elements”.

So if you have an array lets say “intBoard(15,15)” there are 16 elements horizontally (X) and 16 elements vertically (Y).

As an excercise, please workout the following code without running it (cheating). Make sure you predict those values correctly by double checking your results. Here it is :

``````// Dimensioning (creating) an 2D array
// containing 16 elements horizontally (X) and
// 16 elements vertically (Y)
Dim intBoard(15,15) As Integer
Dim intLoop_X_Direction As Integer
Dim intLoop_Y_Direction As Integer
Dim intValue As Integer

intValue = 0
For intLoop_Y_Direction = 0 To 15
// Outer For loop moving vertically
For intLoop_X_Direction = 0 To 15
// Inner For loop moving horizontally
intBoard(intLoop_X_Direction, intLoop_Y_Direction) = intValue
intValue = intValue + 1
Next intLoop_X_Direction
Next intLoop_Y_Direction

// Without running the code and see the results,
// take a piece of paper and write down what the
// value of the following positions are :
// intBoard(15,15) :
// intBoard(15,14) :
// intBoard(14,15) :

// Hint :
// When an array or any datastructure is not clear,
// draw it out in a drawing program or on paper.
// You will see it is easier to understand.

// Now considering the next code :
intValue = 0
For intLoop_Y_Direction = 0 To 15
// Outer For loop moving vertically
intValue = intValue + 1
For intLoop_X_Direction = 0 To 15
// Inner For loop moving horizontally
intBoard(intLoop_X_Direction, intLoop_Y_Direction) = intValue
Next intLoop_X_Direction
Next intLoop_Y_Direction

// Now workout what happens with the positions :
// intBoard(15,15) :
// intBoard(15,14) :
// intBoard(14,15) :

// Don't test it but work it out without running the above code.
// When you can predict what happens correctly, you do
// understand 2 dimensional arrays.``````

However, for your board I would use a “Class array”. This has the advantage that you can store more data for a particular field in one element of the class array. For example the horizontal, vertical board position, also what type of “ship” is occupying that element, amount of hits and so on.

For applying Artificial Intelligence (AI), Scirra has some very good tutorials on this subject. As an excercise, look at those tutorials and apply that information with Xojo.

This all can look very difficult and complex and sometimes it is. However when developing any kind of computer program, break things in smaller parts. As an example :

1. Create the board
2. Fill the board with default values
3. Create the type of ships
4. Put those ships on the board

Now you have a general structure or idea what in the example above the beginning of the game has to do.

Now you workout those individual points. This method can make a problem which you fail to overview, nicely visible. Don’t just start coding as you go. Plan, structure and then code.

You see, when you organize and structure your idea and creations, you will finish your battleship project nicely.

Hope this helps.

Chris

This is a good algorithm for (sort of) AI: http://datagenetics.com/blog/december32011/index.html

With this I almost won the Battleship contest at XDC 2013 in Orlando. Defeated by Bob Delaney having an algorithm made to spot THIS one.

Very nice indeed, and a perfect example on how to start programming

Thanks for your big help! Now I need to see tomorrow if it works or not… I’ll work all weekend on it for sure.

Ok, so I’ve made a bit of progress. I got my “boards” working, so now I’m working on positionning the boats on it by the players as the game starts. I was wondering how to create a rectangle object from the code without using classes (the same one that would be created from the drag and drop interface). I know there’s a way but I’ve got no idea how to do it and I didn’t see it in the xojo help. By the way big thanks to Chris Verberne for making me understand how to use 2D arrays properly.

Rectangle
Class (inherits from RectControl)
Draws a rectangle.

Create a matching array of RECTs holding the corner positions of the rectangles

When the screen paints, use g.drawrect x,y,width, height to draw them if required.?

[quote=389185:@Jeff Tullin]Create a matching array of RECTs holding the corner positions of the rectangles

When the screen paints, use g.drawrect x,y,width, height to draw them if required.?[/quote]
How would I access those ? I’ll be using those rectangles as the boats.

I’ll have to use the answer Dave game me and research on how to use classes.

Being a homework (as it seems) this would be a very instructive research.

So is the new approach to teaching programming the “sink or swim method”… I would have thought the cirriculum would have talked about alll the “parts” (2D arrays, classes, graphics etc), and the “homework” would be to put together what had been learned into a cohesive project, but based on what I am seeing here, that isn’t the case.

I asked my classmates and he actually didn’t teach us everything I need for this project. He never mentionned anything about graphics nor the 2D arrays. Some other guys in my class even use folders… Which I got no idea how to use, and won’t use anyway.

So, you never even went to class… “Folders” for this kind of project? Dude, if you PAID for this class, get your money back.