battleship
Author |
Message |
Jenkinz
|
Posted: Tue May 16, 2006 8:46 am Post subject: battleship |
|
|
im trying to make my battleship program so that i would be able to put in if five ships were hit by the user then put "you win" but i tried with counters and it didnt seem to work any help would be appreciated
code: |
var BattleShip : array 1 .. 10, 1 .. 10 of int
var name : string
var z : int
var x : int
var y : int
for i : 1 .. 10
for j : 1 .. 10
randint (x, 0, 1)
BattleShip (i, j) := x
% put BattleShip (i, j)
end for
end for
put " enter longitude "
get y
put " enter latitude "
get z
if BattleShip (y, z) = 1 then
put "hit"
else
put "miss"
end if
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Jenkinz
|
Posted: Tue May 16, 2006 8:48 am Post subject: (No subject) |
|
|
i also believe i need a loop as well because the program stops after you type in the first longitude and lagitude? |
|
|
|
|
|
Clayton
|
Posted: Tue May 16, 2006 3:27 pm Post subject: (No subject) |
|
|
you are correct, you would need to put in a loop because you are doing the same thing over and over again (unless your five ships are in exactly the same spot and only have one hit each lol) to check and see if all of your ships were hit, you could have a record with an array of booleans to keep track of what ships are hit as well as how many, and if youve won or not check the Turing Walkthrough for the tutorial on records good luck |
|
|
|
|
|
Jenkinz
|
Posted: Wed May 17, 2006 8:57 am Post subject: (No subject) |
|
|
i went through the walkthrough and it helped but im trying a new concept of the battleship game that allows you to input the positions of your ship into a 2d array, and another player player to play unti they either hit the ship in which case it tells you you what type of ship you have hit
Ive got everything down except that im not sure how to make it so that whenever i hit and sink a ship it will show something or another ship other than Submarine, so whenever i keep hitting a ship it keeps saying Submarine is hti and sunk, take a look i need some help fixing this, thanks
code: | var ships : array 1 .. 5 of string := init ("Submarine", "U-Boat", "Destroyer", "Carrier", "Recon")
var myship : array 1 .. 10, 1 .. 10 of int
var shipnumber : array 1 .. 5 of int
var ship : string
var y2 : int
var x2 : int
var battleship : array 1 .. 10, 1 .. 10 of int
var name : string
var z : int
var x : int
var y : int
var hit : string
var counter : int
counter := 0
for c : 1 .. 10
for v : 1 .. 10
randint (x2, 0, 1)
myship (c, v) := x2
end for
end for
loop
for b : 1 .. 1
put "what ship is this"
get shipnumber (b)
put "please enter your ships first co-ordinate"
get x2
put "please enter your ships second co-ordinate"
get y2
end for
for i : 1 .. 10
for j : 1 .. 10
randint (x, 0, 1)
battleship (i, j) := x
end for
counter := counter + 1
end for
put "enter length"
get y
put "enter width"
get z
if battleship (y, z) = myship (x2, y2)
then
hit := "hit"
put "you sunk their ship"
else
hit := "miss"
put "miss"
end if
for u : 1 .. 1
if hit = "hit"
then
put ships (u)
end if
exit when hit = "hit"
end for
end loop |
|
|
|
|
|
|
TheOneTrueGod
|
Posted: Wed May 17, 2006 2:06 pm Post subject: (No subject) |
|
|
code: |
for u : 1 .. 1
if hit = "hit"
then
put ships (u)
end if
exit when hit = "hit"
end for
|
that would be your problem... Think about it, what is the ONLY value that "u" will ever be?
now, what did you declare ships (1) as?
! code: | var ships : array 1 .. 5 of string := init ("Submarine", "U-Boat", "Destroyer", "Carrier", "Recon") |
Submarine |
|
|
|
|
|
Jenkinz
|
Posted: Wed May 17, 2006 5:54 pm Post subject: (No subject) |
|
|
i changed U to 1..5, then when i ran the program it didnt state the ship hit at all |
|
|
|
|
|
MysticVegeta
|
Posted: Wed May 17, 2006 6:02 pm Post subject: (No subject) |
|
|
Why are you making it so complicated? Why not use records?? Also, you are taking width and height as an input too, you shouldnt do that because in battleship I think you are supposed to guess only 1 point on the cartesian plane and thats it, you "sunk" a ship if all parts of the ship are destroyed.. |
|
|
|
|
|
|
|