Computer Science Canada Battleship ~ Code problem |
Author: | TheSoldierDude [ 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)
Please specify what version of Turing you are using Version 4.1 |
Author: | DemonWasp [ 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? |
Author: | TheSoldierDude [ 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 ![]() |
Author: | Zren [ 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.
I'm sure you could find a couple of ways to apply that into your program. |
Author: | evildaddy911 [ 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? |
Author: | DemonWasp [ 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:
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:
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:
The new preference is for the upgraded for-loop syntax:
Other languages don't have the same complexity as C++:
|