We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi.
So we all know the good old game "battleships", which i am trying to make as my exam project. However, i am WAY far away from being just a decent programmer.
All i want to ask is hints about how to make the battlefields in "battleships". Like, am i gonna need to use arrays in arrays? Or is there anything from cp5 that i can use to make the battlefields?
Thanks for any answer.
Answers
2d arrays?
Did you google it here in the forum?
Hey, that's a good one! Haven't had someone ask for this before.
As with all software development projects, let's start simple and work our way up to complex. The first step is a simple layout / blank sketch.
So far so good. We now know where we'll draw the different displays the game needs.
Now let's just make that a little bit more pretty.
Alright. Now we can see the sea, and we've got grid lines drawn and taken care of. We've also verified that the system message text will show up in the right position and is aligned properly.
@Chrisir Your question makes me feel like i dont even understand what 2d arrays are, haha, yea that's how bad of a programmer i am. But no i didnt google anything in the forum
Now we need to make sure that we can draw different symbols in the grids based on the values stored in some data structures. So we create two arrays to store what is being displayed in each grid. We then call a function to draw these symbols instead of the plain rectangles we were drawing before. This function also has a secret parameter which determines if ships can be seen.
Well, this takes care of all the visuals we'll need. Now we need to start programming some game logic. The next step is to add states to this sketch. We'll start in state #0, which is where you can click to place ships. Then we'll need a state where we're waiting for the user to click on the computer's grid. We might also want a YOU WIN state and a YOU LOSE state that show where the hidden computer controlled ships were, with an appropriate message displayed.
@TfGuy44 Wow, it looks pretty nice. Surprises me more than it should :).
Well, apparently there was something else i wanted to ask too in my question, but i forgot because im so nervous about this exam project because im not a good programmer.
Anyway, what i also wanted to know is when it comes to the shooting part in the game(where you click on the fields), what codes are good to use to make the clickable fields, if you know what i mean. Is there something from cp5 that can be used?
Sorry if my question sounds dumb, because i am kinda dumb when it comes to programming.
Thanks in advance.
@TfGuy44 Man, i wish i at least was as good as you at programming so simple things like this. Those codes you have in your comments in this thread - aren't they very easy for you to figure out? Because it isn't for me. Well i can only say thank you. Im gonna take my time to read and try to understand your code! :)
And so instead of adding states... I made some other changes.
Whoa, what even changed here? Well, now I have a Sea class, which represents a single sea square. Why bother with this step? Well, even though it LOOKS the same, this step allows me to add logic to every square without worrying too much about the details...
And just like that, the grid suddenly recognizes where it's clicked.
And here's a version with a basic state system added. The sketch starts is a ship_place_state, and this lets you place your ship parts. This state ends when space is pressed, and then you can click on the enemy ocean to try to find where it randomly placed some ship parts. After each time you click, the computer will have a turn too. It's not a very smart AI yet, obviously, but at least now you have some code to work with, and all the basics are there.
Make sure you understand how this code works before you try improving it. You'll need to learn quite a bit, but them's the brakes!
@TfGuy44
I didnt ask for this much, but i guess it can't hurt. Thank you so much for your appreciable help, like really!! I will try to read and understand your wonderful code!
I think you've overstepped the mark here about supplying solutions to obvious homework questions. Poster even asked for hints and got pages of code written for him. Hopefully he'll do the right thing.
Yes, I agree.
I mean, best write your own code and ask when / where you are stuck and post your own code.
Otherwise you won't learn - and fail in any oral exam
That's why i asked for hints, not solutions.
yes, i appreciate that. my dismay wasn't aimed at you 8)
And also this wasn't homework.
No, I critized tfguy44 not you
Also exam>>homework
But just come back when you have questions please
Hey agian, guys. I have a silly question. Can someone explain what the pushMatrix and popMatrix do exactly?
It saves and reactivates the matrix configuration:
https://processing.org/reference/printMatrix_.html
That includes things like translate(), rotate(), etc btW.
P.S.: Matrix is automatically reset on each draw():
https://Processing.org/reference/resetMatrix_.html
It means when you rotate or translate different stuff, pushMatrix and popMatrix makes it be isolated from each other
So you can rotate one cube clockwise and the other counterclockwise