Battleship ~ Code problem
Author |
Message |
TheSoldierDude
|
Posted: Fri May 31, 2013 2:34 pm Post subject: Battleship ~ Code problem |
|
|
What is it you are trying to achieve?
<Replace all the <> with your answers/code and remove the <>>
I'm trying to get this program to work.
What is the problem you are having?
When you run this program, everything runs smoothly although then you notice that if you try to place a ship on any of the rows + column 6, the ship would never actually show up there.. if you place a ship on column 5 it would put an extra ship on column 5. Same goes with when the computer randomizes and picks the spots for it's ships. If the computer picks a spot on column 5 it would add an extra ship on column 6 and so on. (I purposely let the computer's grid be visible just for debugging..)
Describe what you have tried to solve this problem
I've tried nothing because I don't have any idea what what's wrong with the code. Although I have looked it over more then a couple of times.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
import GUI
setscreen ("graphics:1280;720")
var playerGrid : array 'A' .. 'F', 1 .. 6 of string
var compGrid : array 'A' .. 'F', 1 .. 6 of int
for row : 'A' .. 'F'
for col : 1 .. 6
playerGrid (row, col ) := "_"
end for
end for
procedure userGrid
colour (green)
put "Your Grid"
put " 1 2 3 4 5 6"
put "A ", playerGrid ('A', 1), " ", playerGrid ('A', 2), " ", playerGrid ('A', 3), " ", playerGrid ('A', 4), " ", playerGrid ('A', 5), " ", playerGrid ('A', 5)
put "B ", playerGrid ('B', 1), " ", playerGrid ('B', 2), " ", playerGrid ('B', 3), " ", playerGrid ('B', 4), " ", playerGrid ('B', 5), " ", playerGrid ('B', 5)
put "C ", playerGrid ('C', 1), " ", playerGrid ('C', 2), " ", playerGrid ('C', 3), " ", playerGrid ('C', 4), " ", playerGrid ('C', 5), " ", playerGrid ('C', 5)
put "D ", playerGrid ('D', 1), " ", playerGrid ('D', 2), " ", playerGrid ('D', 3), " ", playerGrid ('D', 4), " ", playerGrid ('D', 5), " ", playerGrid ('D', 5)
put "E ", playerGrid ('E', 1), " ", playerGrid ('E', 2), " ", playerGrid ('E', 3), " ", playerGrid ('E', 4), " ", playerGrid ('E', 5), " ", playerGrid ('E', 5)
put "F ", playerGrid ('F', 1), " ", playerGrid ('F', 2), " ", playerGrid ('F', 3), " ", playerGrid ('F', 4), " ", playerGrid ('F', 5), " ", playerGrid ('F', 5)
end userGrid
procedure oppGrid
colour (red)
put "The Computer's Grid"
put " 1 2 3 4 5 6"
put "A ", compGrid ('A', 1), " ", compGrid ('A', 2), " ", compGrid ('A', 3), " ", compGrid ('A', 4), " ", compGrid ('A', 5), " ", compGrid ('A', 5)
put "B ", compGrid ('B', 1), " ", compGrid ('B', 2), " ", compGrid ('B', 3), " ", compGrid ('B', 4), " ", compGrid ('B', 5), " ", compGrid ('B', 5)
put "C ", compGrid ('C', 1), " ", compGrid ('C', 2), " ", compGrid ('C', 3), " ", compGrid ('C', 4), " ", compGrid ('C', 5), " ", compGrid ('C', 5)
put "D ", compGrid ('D', 1), " ", compGrid ('D', 2), " ", compGrid ('D', 3), " ", compGrid ('D', 4), " ", compGrid ('D', 5), " ", compGrid ('D', 5)
put "E ", compGrid ('E', 1), " ", compGrid ('E', 2), " ", compGrid ('E', 3), " ", compGrid ('E', 4), " ", compGrid ('E', 5), " ", compGrid ('E', 5)
put "F ", compGrid ('F', 1), " ", compGrid ('F', 2), " ", compGrid ('F', 3), " ", compGrid ('F', 4), " ", compGrid ('F', 5), " ", compGrid ('F', 5)
end oppGrid
loop
userGrid
var choice : string
var first : char
var second : int
var changer : string
var adder : int := 0
var adder2 : int := 0
var random : int
var checker : int := 0
loop
exit when adder = 8
put ""
put "Please Enter where you wish to place your ships, (Example: A5)..." ..
get choice
if length (choice ) = 2 then
if choice (1) > 'F' or choice (1) < 'A' then
put "Wrong input, Try again!"
elsif choice (2) < "1" or choice (2) > "6" then
put "Wrong input, Try again!"
else
adder := adder + 1
for a : 1 .. 1
first := choice (a )
end for
for b : 2 .. 2
changer := choice (b )
second := strint (changer )
end for
playerGrid (first, second ) := "S"
end if
else
put "Wrong input, Try again!"
end if
end loop
cls
put "You have placed your ships on the positions marked by an \"S\""
put ""
userGrid
put ""
put "The Computer is choosing it's Fleet, please stand by"
for row : 'A' .. 'F'
for col : 1 .. 6
adder2 := adder2 + 1
compGrid (row, col ) := adder2
end for
end for
loop
exit when checker = 8
randint (random, 1, 36)
checker := checker + 1
for row : 'A' .. 'F'
for col : 1 .. 6
if compGrid (row, col ) = random then
compGrid (row, col ) := 0
end if
end for
end for
end loop
for row : 'A' .. 'F'
for col : 1 .. 6
if compGrid (row, col ) not= 0 then
compGrid (row, col ) := 1
end if
end for
end for
put ""
oppGrid
put ""
put ""
loop
var input : string
var putter : int := 0
var first2 : char
var second2 : int
var mover : string
var compHits : int
var compShipCounter : int := 0
var userShipCounter : int := 0
var compHitsFind : array 1 .. 36 of string := init ("A1", "A2", "A3", "A4", "A5", "A6", "B1", "B2", "B3", "B4", "B5", "B6", "C1", "C2", "C3", "C4", "C5", "C6", "D1", "D2", "D3", "D4", "D5",
"D6", "E1", "E2", "E3", "E4", "E5", "E6", "F1", "F2", "F3", "F4", "F5", "F5")
var compHitsStore : string
var first3 : char
var second3 : int
var changer2 : string
loop
exit when compShipCounter = 8
exit when userShipCounter = 8
put "Pick a position on the opponent's grid to attack (Example: A1).." ..
get input
if length (choice ) = 2 then
if choice (1) > 'F' or choice (1) < 'A' then
put "Wrong input, Try again!"
elsif choice (2) < "1" or choice (2) > "6" then
put "Wrong input, Try again!"
else
putter := putter + 1
for b : 1 .. 1
first2 := input (b )
end for
for v : 2 .. 2
mover := input (v )
second2 := strint (mover )
end for
if compGrid (first2, second2 ) = 0 then
put ""
put "You sunk a ship!"
compShipCounter := compShipCounter + 1
compGrid (first2, second2 ) := 9
else
put ""
put "You missed!"
compGrid (first2, second2 ) := 0
end if
put ""
put "Your opponent's turn!"
randint (compHits, 1, 36)
compHitsStore := compHitsFind (compHits )
for q : 1 .. 1
first3 := compHitsStore (q )
end for
for w : 2 .. 2
changer2 := compHitsStore (w )
second3 := strint (changer2 )
end for
if playerGrid (first3, second3 ) = "S" then
playerGrid (first3, second3 ) := "D"
put ""
put "Your opponent sunk one of your ships!"
put ""
userGrid
put ""
put ""
put ""
oppGrid
userShipCounter := userShipCounter + 1
elsif playerGrid (first3, second3 ) = "_" then
playerGrid (first3, second3 ) := "H"
put ""
put "Your opponent missed!"
put ""
userGrid
put ""
put ""
put ""
oppGrid
end if
end if
end if
end loop
if userShipCounter = 8 then
put "Your opponent has defeated you! All your ships are gone.. GAME OVER!"
exit
elsif compShipCounter = 8 then
put "You have defeated your enemy! YOU WON!"
exit
end if
end loop
end loop
|
Please specify what version of Turing you are using
Version 4.1 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Fri May 31, 2013 2:52 pm Post subject: RE:Battleship ~ Code problem |
|
|
Take a very close look at the last thing you put in userGrid and oppGrid. What's wrong with it? |
|
|
|
|
![](images/spacer.gif) |
TheSoldierDude
|
Posted: Fri May 31, 2013 3:01 pm Post subject: Re: RE:Battleship ~ Code problem |
|
|
DemonWasp @ Fri May 31, 2013 2:52 pm wrote: Take a very close look at the last thing you put in userGrid and oppGrid. What's wrong with it?
Oh my god I am so dumb, Thank you so much.. I would have had never noticed that. |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Fri May 31, 2013 8:45 pm Post subject: RE:Battleship ~ Code problem |
|
|
Behold the glory that is nested looping. I see you used it already in your program, but I'm guessing you've never used it like this.
Adding .. onto the end of a put statement will cause the next put statement to continue from the same line. In other words, it doesn't add the newline.
Turing: |
for y : 'A' .. 'F'
put y : 8 ..
for x : 1 .. 6
put y, x, " " ..
% Eg: put playerGrid(y,x) ..
end for
put ""
end for
|
code: |
A A1 A2 A3 A4 A5 A6
B B1 B2 B3 B4 B5 B6
C C1 C2 C3 C4 C5 C6
D D1 D2 D3 D4 D5 D6
E E1 E2 E3 E4 E5 E6
F F1 F2 F3 F4 F5 F6
|
I'm sure you could find a couple of ways to apply that into your program. |
|
|
|
|
![](images/spacer.gif) |
evildaddy911
|
Posted: Sat Jun 01, 2013 12:05 pm Post subject: RE:Battleship ~ Code problem |
|
|
cool, ive never seen a for loop using char, only loops... is it possible to use real #s to? |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Sat Jun 01, 2013 3:10 pm Post subject: RE:Battleship ~ Code problem |
|
|
Not in Turing:
Turing wrote: 'for' range bounds must be both integers, chars, or elements of the same enumerated type
Most other languages support real/floating-point numbers or whatever else you want. The generalized for loop used by many other languages is as follows:
code: |
for ( start-code; continue-condition; continue-code ) {
body-code;
}
|
The start-code can be anything. The most common one is probably int i = 0;, but you could just as easily say float f = 0.5f; or similar.
The continue-condition can be anything that evaluates as a boolean (or integer in C/C++). The most common condition is i < length; but again it could be f < 55.7;.
The continue-code is executed before evaluating the continue-condition every time the loop repeats. The most common is ++i but f += 0.32 is also allowable.
You can write character loops in a lot of languages:
code: |
// This probably works in a lot of languages...
for ( char c = 'A'; c <= 'F'; ++c ) {
// insert language-specific equivalent of "put c"
}
|
However, this relies on the underlying representation of a character as an integer corresponding to a specific character set. In Turing, you get to use ASCII and nothing else ever (here: http://www.asciitable.com/ ). A lot of other languages allow you to use different character sets, which may change how iteration works. Most, but not all, character sets have the lowercase English alphabet together and the uppercase together. Notably, EBCDIC didn't: http://en.wikipedia.org/wiki/EBCDIC
In C++, it has been very common to do something like:
c++: |
for ( std::vector<type>::iterator iter = my_vector.begin(); iter != my_vector.end(); ++iter ) {
// work with *iter here...
}
|
The new preference is for the upgraded for-loop syntax:
c++: |
for ( auto & element : vector ) {
// work with element here, possibly including modifying element's contents
}
// or...
for ( auto const & element : vector ) {
// work with element here, without modifying element's contents
}
|
Other languages don't have the same complexity as C++:
Java: |
for ( Type element : collection ) {
// work on element
}
|
Python: |
for element in collection:
# work on element
|
|
|
|
|
|
![](images/spacer.gif) |
|
|