Battleship project

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)

@Dave SYou'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 ?

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

. 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.

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 horizontalDirection 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 :

Create the board

Fill the board with default values

Create the type of ships

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.

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.

@Dave S 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

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.