Do you like Battleship?
Yes
 83%  [ 10 ]
No
 16%  [ 2 ]
Total Votes : 12

Here's the final coding:

Code:
1->A:2->B:3->C:4->D:5->E:6->F:7->G:8->H:9->I:10->J:6->M:5->N:4->O:4->P:3->Q:-2->Z
ClrDraw
0->Xmin
320->Xmax
-240->Ymin
0->Ymax
ClrDraw
AxesOff
For(X,0,10
Line(0,-13X,120,-13X
Line(12X,0,12X,-130
End
Text(90,0,"A"):Text(90,13,"B"):Text(90,23,"C"):Text(90,33,"D"):Text(90,43,"E"):Text(90,53,"F"):Text(90,63,"G"):Text(90,73,"H"):Text(90,83,"I"):Text(90,93,"J"):Text(110,100,"THE BATTELSHIP BOARD")
Pause
StorePic 1
Menu("BATTLESHIP","CREATE BOARD",4,"QUIT",3)
Lbl 4
While M!=1
Disp "CARRIER COORDINATES:"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
M-1->M
If M=1:Goto B
End
Lbl B
While N!=1
Disp "BATTLESHIP COORDINATES"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
N-1->N
If N=1:Goto C
End
Lbl C
While O!=1
Disp "CRUISER COORDINATES"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
O-1->O
If O=1:Goto D
End
Lbl D
While P!=1
Disp "SUBMARINE COORDINATES"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
P-1->P
If P=1:Goto E
End
Lbl E
While Q!=1
Disp "SPEEDBOAT COORDINATES"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
Q-1->Q
Z+1->Z
If Q=1:Goto F
End
Lbl F
If Z>=2:Then:Goto G
Else:StorePic 2:Goto 4
End
Lbl G
Repeat 0
StorePic 3
ClrDraw
RecallPic 2
Disp "PLAYER 1"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
10+pxl-Test(V,R)->K
For(X,0,3
For(Z,0,3
Pxl-On(V+X,R+Z,K)
End:End
StorePic 2
ClrDraw
RecallPic 3
Disp "PLAYER 2"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
10+pxl-Test(V,R)->K
For(X,0,3
For(Z,0,3
Pxl-On(V+X,R+Z,K)
End:End:Goto G
End

Do you think that this will work? If not, please say why.
caleb1997 wrote:
Do you think that this will work? If not, please say why.
We don't answer questions like that, in general, because they don't show that the author put any thought into trying to test or debug things himself or herself. Did you test and run your code? Did you find any inexplicable bugs in the code above that you weren't able to track down, and if so, what steps did you already try? Are there specific techniques you used in your code that you're not very familiar with and you feel you might have misused that you'd like us to double-check, like nested loops or something?

Edit: I'll throw you a bone and ask you to think about how to improve this:

Code:
Text(90,0,"A"):Text(90,13,"B"):Text(90,23,"C"):Text(90,33,"D"):Text(90,43,"E"):Text(90,53,"F"):Text(90,63,"G"):Text(90,73,"H"):Text(90,83,"I"):Text(90,93,"J")
It can be made much, much shorter and less repetitive. How do you think you could do that?
I have tested and run this code. I just wanted to make sure that if I run it many times, it won't crash. And you are far more of an expert than I am, so I ask for help.
And I will think on that.
caleb1997 wrote:
I have tested and run this code. I just wanted to make sure that if I run it many times, it won't crash. And you are far more of an expert than I am, so I ask for help.
That's reasonable. Luckily your program is relatively short, but if it was longer, evaluating it purely by inspection would be a fairly thankless task, so we'd generally prefer more specific questions. Something to keep in mind for the future.
Quote:
And I will think on that.
Throw out the first thing that comes to your mind. Is there a pattern in that code that could be exploited to make it less repetitive? What are some techniques you're already familiar with to avoid having to repeat a similar command over and over?

Here's a problem that will cause problems after your program has run for a while. It's called a memory leak, and it's caused by Goto'ing out of a While/End, For/End, Repeat/End, If/Then/End, or If/Then/Else/End construct. Luckily, it's trivial to solve here. How can you avoid Goto'ing out of this While loop? Hint: When will this While loop end anyway?

Code:
While O!=1
[...]
O-1->O
If O=1:Goto D
End
Lbl D
For the Text( commands, I am trying a For( loop. Goes like this:


Code:
For(X,13,93,10)
Text(90,0,A):Text(90,X,"B","C","D","E","F","G","H","I","J")
End


This is not working. It does the B-J sequence many times over, then stops. Have any ideas to make it only run once?

As for the While loops, I just set it to run only once. Problem solved there.
Well, you may want to look more into the sub() command for your entire code. I was able to cut about 80% of your code off by using this function. Smile In your above case, the sub() function will work quite nicely. Smile
What is the sub( command? I haven't ever heard of that. And by the way, I am just about done with the code. But how could I get it so that it doesn't go past the edge of the border of the gameboard? I was thinking of an If-Then-Else-End loop, but while I do have the edge of the gameboard all figure out, I can't figure out how to make the pixels that would normally go over the edge of the gameboard place themselves behind of what has already been placed. For example, let's say that I put the carrier at (J,eight). In this case, three of the the five dots(which I figured out how to make them place themselves horozontally or vertically, depending on what you chose in the menu( command that I wrote) placed horzontally would go over the edge. Any ideas on how I could make those three dots put themselves behind the two already placed?
The sub() command could be very useful in this case. Your code could be shortened to this:


Code:
ClrHome
For(X,0,4
For(Z,1,6-X
Disp sub("CARRIER   BATTLESHIPCRUISER   SUBMARINE SPEEDBOAT ",10X+1,10),"COORDINATES:"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
End:End


The way it works is by taking a string and selecting certain parts of it. The 1st argument is the string, the second the offset in the string, and the third the number of characters to select. Smile Hope this helps; it eliminates tons of code... Razz

Considering this is what the original was:


Code:
6->M:5->N:4->O:4->P:3->Q
While M!=1
Disp "CARRIER COORDINATES:"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
M-1->M
If M=1:Goto B
End
Lbl B
While N!=1
Disp "BATTLESHIP COORDINATES"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
N-1->N
If N=1:Goto C
End
Lbl C
While O!=1
Disp "CRUISER COORDINATES"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
O-1->O
If O=1:Goto D
End
Lbl D
While P!=1
Disp "SUBMARINE COORDINATES"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
P-1->P
If P=1:Goto E
End
Lbl E
While Q!=1
Disp "SPEEDBOAT COORDINATES"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
Q-1->Q
Z+1->Z
If Q=1:Goto F
End
Lbl F


As for your above question, give me a bit to work it out. Smile
I have tried the sub( command. But for some reason, it now only loops once instead of ten times (five times for each player), and just haves you place just one ship, instead of five. What's wrong?[/code]
caleb1997 wrote:
I have tried the sub( command. But for some reason, it now only loops once instead of ten times (five times for each player), and just haves you place just one ship, instead of five. What's wrong?[/code]

Hmm.... This code seems to work just fine for me...

Code:
ClrHome
For(X,0,4
For(Z,1,6-X
Disp sub("CARRIER   BATTLESHIPCRUISER   SUBMARINE SPEEDBOAT ",10X+1,10),"COORDINATES:"
Input "COLUMN:",L
Input "ROW:",W
3+10(W-1)->R
3+9(L-1)->V
Pxl-On(V,R)
End:End

Of course, it makes you enter in the coordinates for every point of the ship, which is definitely improvable through bounds checking.... Confused
I solved the problem with the sub( code. I have now supershortened my program. I am now trying to do that with this bit..

Code:
Lbl G
StorePic 3
Repeat 0
ClrDraw
RecallPic 3
ClrHome
Disp "PLAYER 1"
Input "COLUMN:",W
Input "ROW:",L
3+10(W-1)->R
3+9(L-1)->V
10+pxl-Test(V,R)->K
For(X,0,3
For(Z,0,3
Pxl-On(V+X,R+Z,K
End:End
Pause
StorePic 3
ClrDraw
RecallPic 2
Disp "PLAYER 2"
Input "COLUMN:",W
Input "ROW:",L
3+10(W-1)->R
3+9(L-1)->V
10+pxl-Test(V,R)->K
For(X,0,3
For(Z,0,3
Pxl-On(V+X,R+Z,K)
End:End
Pause
StorePic 2
End

I have a way to shorten this.
Like this:

Code:
If P=1:StorePic 2:P+1→P:End
If P=2:StorePic 3:P-1→P:End

I think this would work, but I don't have any time to test it now. Would anyone be willing to test it for me? What about you, Mateo?
That could work; nice work on coming up with that on your own! Very Happy An alternative way might be to use 0 and 1 in P, and just make it seem like they are players 1 and 2. That way checking is a lot easier, and switching players can also be easier as well, because you just could not(P). Here's one way that could work:

To clear things up a little, P=0 is player 1, and P=1 is player 2, if that makes sense. Razz

Hope this helps! Smile

Code:
0->P:StorePic 3
Repeat 0
ClrDraw
If P:Then
RecallPic 2:Else:RecallPic 3:End
ClrHome
Disp "PLAYER "+sub("12",P+1,1
Input "COLUMN:",W
Input "ROW:",L
3+10(W-1->R
3+9(L-1->V
10+pxl-Test(V,R->K
For(X,0,3
For(Z,0,3
Pxl-On(V+X,R+Z,K
End:End
Pause 
If P:Then
StorePic 2:Else:StorePic 3:End
not(P->P
End
What does the not( command do on my Graphing Calculator ?
Never mind- I figured what the not( command does on my calc. But there is a problem that I have been trying to solve for about a week now. How can I do the score? I have the score display all set up, but how can I get it so that when M(for player 1) or N(player 2) equals 17, they win. How do I do that?

Also, I have lately been thinking about the ability to save games to continue at a later period. What do you guys think of this idea?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 4 of 4
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement