Computer Science Canada

Battleship Gameboard

Author:  penny_k1994 [ Wed May 25, 2011 4:02 pm ]
Post subject:  Battleship Gameboard

I am in grade 11 making a battleship board game for my Turing class.


The problem that I am having is that I wan't to make a game board to place the ships onto using arrays but I dont know how to. I want the user to be playing with the computer.


What I have so far is:
var ship
var cannon
array A1..A10
array B1..B10
array C1..C10
array D1..D10
array E1..E10
array F1..F10
array G1..G10
array H1..H10
put "Place your 5 ships on the gameboard while the opponent is placing theirs"
put""
put "Where are you going to shoot your cannon?"
loop
get ship
if ship="A1" then
put "You hit the ship go again Smile"
elsif ship="A2" then
put "Sorry you missed Sad"
elsif ship="A3" then
put "Sorry you missed Sad"
end if
end loop

Author:  Tony [ Wed May 25, 2011 4:41 pm ]
Post subject:  RE:Battleship Gameboard

you could read more about using arrays in Turing's documentation -- array and tutorials we have linked to from The Turing Walkthrough

Author:  RandomLetters [ Wed May 25, 2011 6:32 pm ]
Post subject:  RE:Battleship Gameboard

protip: Letters are really numbers in disguise.

Author:  penny_k1994 [ Wed May 25, 2011 7:12 pm ]
Post subject:  Re: Battleship Gameboard

why isnt it running?

var ship
var cannon
var shot: array A1..A10 of int
var shot: array B1..B10 of int
var shot: array C1..C10 of int
var shot: array D1..D10 of int
var shot: array E1..E10 of int
var shot: array F1..F10 of int
var shot: array G1..G10 of int
var shot: array H1..H10 of int
put "Place your 5 ships on the gameboard while the opponent is placing theirs"
put""
put "Where are you going to shoot your cannon?"
loop
get ship
if ship="A1" then
put "You hit the ship go again Smile"
elsif ship="A2" then
put "Sorry you missed Sad"
elsif ship="A3" then
put "Sorry you missed Sad"
end if
end loop

Author:  Raknarg [ Wed May 25, 2011 7:15 pm ]
Post subject:  RE:Battleship Gameboard

Arrays don't quite work like that. We could explain them here, but it would probably be most beneficial to you if you read the tutorial Tony's supplied you with.

Author:  Tony [ Wed May 25, 2011 7:23 pm ]
Post subject:  Re: Battleship Gameboard

penny_k1994 @ Wed May 25, 2011 7:12 pm wrote:
why isnt it running?

Because it highlights a line and says something along the lines of "syntax error". This is where you consult the documentation for the valid uses of that particular statement.

Also
Quote:

if ship="A1" then
put "You hit the ship go again "
elsif ship="A2" then
...

10x10 grid is 100 if statements, right? That does not seem like the right path to follow...

Author:  penny_k1994 [ Wed May 25, 2011 7:31 pm ]
Post subject:  Re: Battleship Gameboard

im sorry im an amateur how do i fix it?

Author:  Tony [ Wed May 25, 2011 7:50 pm ]
Post subject:  RE:Battleship Gameboard

fix what?

Author:  penny_k1994 [ Wed May 25, 2011 8:01 pm ]
Post subject:  RE:Battleship Gameboard

fix the arrays im an amateur at this and im really bad at it so can you keep it as simple as possible?

Author:  Tony [ Wed May 25, 2011 9:20 pm ]
Post subject:  RE:Battleship Gameboard

start by reading tutorials and documentation. That's how any of this ever makes sense.

Author:  apython1992 [ Wed May 25, 2011 9:51 pm ]
Post subject:  Re: RE:Battleship Gameboard

penny_k1994 @ Wed May 25, 2011 8:01 pm wrote:
fix the arrays im an amateur at this and im really bad at it so can you keep it as simple as possible?

Being an amateur is totally OK, everyone had to be at some point. Don't get discouraged by the fact that you aren't getting answers right away; one of the biggest challenges in learning how to program is developing good problem solving skills - figuring out what went wrong, and then why. Do as much as you can to look into the problem yourself (like why there might be a syntax error with your array declarations), and if you get it, that's great, but if not, then we're here to help.

Whenever you come across difficulties in getting programs to run, don't be afraid of the error message - it is actually telling you a whole lot! It's cryptic as all heck before you get used to it, but you'll soon become really good at debugging by following a read error message -> trace back to code -> read documentation/proofread erroneous code -> try again cycle. Of course, you'll also come across logic errors (much more frequently after you become familiar with the syntax of the language), but you'll get into a system for dealing with these as well.

Author:  penny_k1994 [ Wed May 25, 2011 10:00 pm ]
Post subject:  RE:Battleship Gameboard

Thank you Smile ill try again and if i get stuck ill ask you

Author:  penny_k1994 [ Fri May 27, 2011 8:27 am ]
Post subject:  RE:Battleship Gameboard

i fixed up my program to run now i just need help on the arrays please

var ship:string
var score:int:=0
var A:array 1..10 of int
var B:array 1..10 of int
var C:array 1..10 of int
var D:array 1..10 of int
var E:array 1..10 of int
var F:array 1..10 of int
var G:array 1..10 of int
var H:array 1..10 of int
var column:int
var row:int
put "Place your 5 ships on the board while the opponent is placing theirs"
put "A is the same as A(1)"
loop
put "Where are you going to shoot your cannon? Choose a row and column betwen A1 and H10"
put ""
if ship="A1" then
put "Is Correct!!! Very Happy Great Job"
score:=score+0
elsif ship="A2" then
put "Is Incorrect Sad Sorry"
score:=score+0
elsif ship="A3" then
put "Is Incorrect Sad Sorry"
score:=score+1
end if
end loop

Author:  apython1992 [ Fri May 27, 2011 8:34 am ]
Post subject:  RE:Battleship Gameboard

Could you be a little more specific? What exactly about the arrays is causing you trouble?

Author:  penny_k1994 [ Fri May 27, 2011 8:53 am ]
Post subject:  RE:Battleship Gameboard

i dont know what the problem is im assuming its the arrays i guess its not i need help with being able to place the ships

Author:  apython1992 [ Fri May 27, 2011 8:58 am ]
Post subject:  RE:Battleship Gameboard

That's better. Right now it has nothing to do with arrays, and everything to do with allowing the user to enter in a set of co-ordinates for ship placement. One of the first things you need is to ask for user input. Do you know how to do this?

Author:  penny_k1994 [ Fri May 27, 2011 6:04 pm ]
Post subject:  RE:Battleship Gameboard

no i dont can you please help me with that?

Author:  apython1992 [ Fri May 27, 2011 9:53 pm ]
Post subject:  RE:Battleship Gameboard

http://www.beens.org/turing/Turing_018___Get.htm

Author:  penny_k1994 [ Fri May 27, 2011 11:29 pm ]
Post subject:  RE:Battleship Gameboard

ok thank you but when i run it it says "string literal ends at end of line" how do i fix that?

var ship:string
var coordinates:string
var score:int:=0
var A:array 1..10 of int
var B:array 1..10 of int
var C:array 1..10 of int
var D:array 1..10 of int
var E:array 1..10 of int
var F:array 1..10 of int
var G:array 1..10 of int
var H:array 1..10 of int
var column:int
var row:int
put "Place your 5 ships on the board while the opponent is placing theirs"
get coordinates
get coordinates
get coordinates
get coordinates
get coordinates
put ""
put "the locations of your ships are",coordinates"
put "A is the same as A(1)"
loop
put "Where are you going to shoot your cannon? Choose a row and column betwen A1 and H10"
put ""
if ship="A1" then
put "Is Correct!!! Great Job"
score:=score+0
elsif ship="A2" then
put "Is Incorrect Sorry"
score:=score+0
elsif ship="A3" then
put "Is Incorrect Sorry"
score:=score+1
end if
end loop

Author:  Tony [ Sat May 28, 2011 12:04 am ]
Post subject:  RE:Battleship Gameboard

Quote:

put "the locations of your ships are",coordinates"

Author:  penny_k1994 [ Sat May 28, 2011 10:34 am ]
Post subject:  RE:Battleship Gameboard

that's what i did before and it still says "string literal ends at end of line" how do i fix that?

Author:  apython1992 [ Sat May 28, 2011 11:54 am ]
Post subject:  RE:Battleship Gameboard

He's quoting your code, where there is a mistake. All strings should be surrounded by quotation marks, right? Variable names are not strings, and so they don't need quotation marks. How many quotation marks do you have?

Author:  8749236 [ Sat May 28, 2011 7:04 pm ]
Post subject:  Re: Battleship Gameboard

Tony @ Wed May 25, 2011 7:23 pm wrote:
penny_k1994 @ Wed May 25, 2011 7:12 pm wrote:
why isnt it running?

Because it highlights a line and says something along the lines of "syntax error". This is where you consult the documentation for the valid uses of that particular statement.

Also
Quote:

if ship="A1" then
put "You hit the ship go again "
elsif ship="A2" then
...

10x10 grid is 100 if statements, right? That does not seem like the right path to follow...


How about using "not ="
if it is not equal to the ship position then tell the player you didn't hit, and if is equal and tell the player you hit and you got next round of fire..
I think thats more better than using 100 if statement, that makes people crazy..

Author:  tyuo9980 [ Sat May 28, 2011 11:06 pm ]
Post subject:  Re: Battleship Gameboard

read up on 2d arrays.

Author:  penny_k1994 [ Tue May 31, 2011 5:09 pm ]
Post subject:  RE:Battleship Gameboard

yes thats a good idea thank you this is what I have so far can you please help me get started on the board i read up on 2d arrays i just need help getting started:

var ship:string
var coordinates:string
var hit:string
var shipplacement:array 1..10,1..10 of int
var score:int:=0
var A:array 1..10 of int
var B:array 1..10 of int
var C:array 1..10 of int
var D:array 1..10 of int
var E:array 1..10 of int
var F:array 1..10 of int
var G:array 1..10 of int
var H:array 1..10 of int
var column:int
var row:int
put "Place your 5 ships on the board while the opponent is placing theirs"
get coordinates
put "Pick a direction that you want to place your ship North, South, East and West"
get coordinates
put "Pick a direction that you want to place your ship North, South, East and West"
get coordinates
put "Pick a direction that you want to place your ship North, South, East and West"
get coordinates
put "Pick a direction that you want to place your ship North, South, East and West"
get coordinates
put ""
put "The locations of your ships are,coordinates"
put "A is the same as A(1)"
loop
put "Where are you going to shoot your cannon? Choose a row and column betwen A1 and H10"
put ""
if ship= hit then
put "You hit the opponents ship, fire again Smile"
elsif ship not= hit then
put "You mised the opponents ship Sad"
end if
end loop

Author:  apython1992 [ Tue May 31, 2011 6:28 pm ]
Post subject:  RE:Battleship Gameboard

You are trying to store all of your information within a single variable (coordinates). Every time you have the line "get coordinates", you are overwriting what was previously stored there.

Author:  Raknarg [ Tue May 31, 2011 8:46 pm ]
Post subject:  RE:Battleship Gameboard

As tyuo9980 said before, I would strongly recommend using a 2D array for this. It will save you a lot of pain in the future.
Turing:

var tile : array 1 .. 8, 1 .. 10 of int


: