Help with Battleship 10x10 Game

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
xkse

Help with Battleship 10x10 Game

Post by xkse »

I pretty much finished a 10x10 version of battleship but am having trouble having two different hit counters. Currently as soon as I sink one battleship, the game ends. However I need it to end after both are sunk. The program is finished except for that (I think).

My right click is working right now (?) so I can't post the code; I'll try at my house later.

If anyone could tell me how to differentiate between the hit counters it would be much appreciated.

Again, I'll try and post the code later

Thanks[/code]
PQBC...

Post by PQBC... »

Since you are probably running Windows, it will take about a minute to do get your code here. Save your file as ********.TXT instead of ********.BAS. Then, open up notepad ad your file. Select all of your code, and then click copy. Open up a message here on the forums, and in the post box, press CTRL+V. The code sould appear.

But, an easy alternative should be this:
After you sink a ship, the game ends. (Thats what you explained) You could add an IF/THEN statement and a counter for how many ships have been sunk. EX:

Code: Select all

IF shotsonbattleship = 4 THEN shipssunk = shipssunk+1 'Or what ever event makes the ship "sunk"
IF shipssunk = 2 THEN CALL EndGame ' Ends the game if 2 ships are sunk.
Post your code and it will be alot easier to help.
xkse

Post by xkse »

Ok thanks.

Will post code tomorrow
xkse

Post by xkse »

Here is the code

I've set up two different hit counters, but only have set them to 0. I haven't done anything with them, however.

Here's the code:

Code: Select all

start:
CLS
DIM b$(10, 10)


RANDOMIZE TIMER

FOR r = 1 TO 10
FOR c = 1 TO 10
b$(r, c) = "e"
NEXT c
NEXT r


r1 = INT(8 * RND) + 1
c1 = INT(8 * RND) + 1

r2 = INT(8 * RND) + 1
c2 = INT(8 * RND) + 1

p1 = INT(2 * RND) + 1

p2 = INT(2 * RND) + 1


IF p1 = 1 THEN
        FOR i = c1 TO (c1 + 2)
        b$(r1, i) = "b"
        NEXT i
END IF

IF p1 = 2 THEN
        FOR i = r1 TO (r1 + 2)
        b$(i, c1) = "b"
        NEXT i
END IF

IF p2 = 1 THEN
        FOR i = c2 TO (c2 + 2)
        b$(r2, i) = "b"
        NEXT i
END IF

IF p2 = 2 THEN
        FOR i = r2 TO (r2 + 2)
        b$(i, c2) = "b"
        NEXT i
END IF

s = 0
h1 = 0
h2 = 0

        INPUT "What is the maximum number of shots"; maxshots

DO
        s = s + 1
       
        PRINT "Guess a point where the battleship is (max is 10, 10)"; "Shot number"; s;
        INPUT r, c

                IF b$(r, c) = "s" THEN
                        PRINT
                        PRINT "You already shot there"
                        PRINT
               
                ELSEIF b$(r, c) = "h" THEN
                        PRINT
                        PRINT "You already shot there (and it was a hit)"
                        PRINT

                ELSEIF b$(r, c) = "e" THEN
                        PRINT
                        PRINT "You missed, try again"
                        PRINT
                        b$(r, c) = "s"

                ELSE
                        PRINT
                        PRINT "That shot was a hit!"
                        PRINT
                        h = h + 1
                        b$(r, c) = "h"
                                IF h = 3 THEN
                                s = maxshots
                                PRINT "You sunk a battleship!"
                                END IF
                END IF
LOOP UNTIL s = maxshots


IF h1 = 3 AND h2 = 3 THEN PRINT "Great job, you sunk the battleships!"


IF p1 = 1 AND (h1 < 3 OR h2 < 3) THEN
PRINT "Unfortunately, you didn't sink the battleship(s)"
PRINT "The battleship was in spots"; r1; ","; c1; ";", r1; ","; (c1 + 1); ";"; ","; "and"; r1, (c1 + 2)
END IF

IF p1 = 2 AND (h1 < 3 OR h2 < 3) THEN
PRINT "Unfortunately, you didn't sink the battleship(s)"
PRINT "The battleship was in spots"; r1; ","; c1; ";", (r1 + 1); ","; c1; ";"; ","; "and"; (r1 + 2), c1
END IF

IF p2 = 1 AND (h1 < 3 OR h2 < 3) THEN
PRINT "Unfortunately, you didn't sink the battleship(s)"
PRINT "The battleship was in spots"; r2; ","; c2; ";", r2; ","; (c2 + 1); ";"; ","; "and"; r2, (c2 + 2)
END IF

IF p2 = 2 AND (h1 < 3 OR h2 < 3) THEN
PRINT "Unfortunately, you didn't sink the battleship(s)"
PRINT "The battleship was in spots"; r2; ","; c2; ";", (r2 + 1); ","; c2; ";"; ","; "and"; (r2 + 2), c2
END IF




INPUT "Would you like to play again(y, n)"; j$
j$ = LCASE$(j$)

IF j$ = "y" THEN GOTO start

finish:

END


xkse

Post by xkse »

Anyone?
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

xkse:

SEE MY COMMENTS WITHIN THE CODE.
*****Moneo*****

Code: Select all

start:
CLS
DIM b$(10, 10)

RANDOMIZE TIMER

FOR r = 1 TO 10
FOR c = 1 TO 10
b$(r, c) = "e"
NEXT c
NEXT r


r1 = INT(8 * RND) + 1
c1 = INT(8 * RND) + 1

r2 = INT(8 * RND) + 1
c2 = INT(8 * RND) + 1

p1 = INT(2 * RND) + 1

p2 = INT(2 * RND) + 1


IF p1 = 1 THEN
        FOR i = c1 TO (c1 + 2)
        b$(r1, i) = "b"
        NEXT i
END IF

IF p1 = 2 THEN
        FOR i = r1 TO (r1 + 2)
        b$(i, c1) = "b"
        NEXT i
END IF

IF p2 = 1 THEN
        FOR i = c2 TO (c2 + 2)
        b$(r2, i) = "b"
        NEXT i
END IF

IF p2 = 2 THEN
        FOR i = r2 TO (r2 + 2)
        b$(i, c2) = "b"
        NEXT i
END IF

s = 0
h1 = 0
h2 = 0

        INPUT "What is the maximum number of shots"; maxshots

DO
        s = s + 1
       
        PRINT "Guess a point where the battleship is (max is 10, 10)"; "Shot number"; s;
        INPUT r, c

                IF b$(r, c) = "s" THEN
                        PRINT
                        PRINT "You already shot there"
                        PRINT
               
                ELSEIF b$(r, c) = "h" THEN
                        PRINT
                        PRINT "You already shot there (and it was a hit)"
                        PRINT

                ELSEIF b$(r, c) = "e" THEN
                        PRINT
                        PRINT "You missed, try again"
                        PRINT
                        b$(r, c) = "s"

                ELSE
                        PRINT
                        PRINT "That shot was a hit!"
                        PRINT
                        h = h + 1
                        b$(r, c) = "h"
                                IF h = 3 THEN
                                s = maxshots
                                PRINT "You sunk a battleship!"
                                END IF
                END IF
LOOP UNTIL s = maxshots

REM *** SO FAR, YOU NEVER SET ANYTHING INTO H1 AND H2
REM *** WHICH YOU THEN TEST IN THE NEXT INSTRUCTION.

IF h1 = 3 AND h2 = 3 THEN PRINT "Great job, you sunk the battleships!"

REM *** HERE AGAIN, THE FOLLOWING "IF'S" TEST FOR H1 AND H2
REM *** WHICH WILL STILL BE ZERO.

IF p1 = 1 AND (h1 < 3 OR h2 < 3) THEN
PRINT "Unfortunately, you didn't sink the battleship(s)"
PRINT "The battleship was in spots"; r1; ","; c1; ";", r1; ","; (c1 + 1); ";"; ","; "and"; r1, (c1 + 2)
END IF

IF p1 = 2 AND (h1 < 3 OR h2 < 3) THEN
PRINT "Unfortunately, you didn't sink the battleship(s)"
PRINT "The battleship was in spots"; r1; ","; c1; ";", (r1 + 1); ","; c1; ";"; ","; "and"; (r1 + 2), c1
END IF

IF p2 = 1 AND (h1 < 3 OR h2 < 3) THEN
PRINT "Unfortunately, you didn't sink the battleship(s)"
PRINT "The battleship was in spots"; r2; ","; c2; ";", r2; ","; (c2 + 1); ";"; ","; "and"; r2, (c2 + 2)
END IF

IF p2 = 2 AND (h1 < 3 OR h2 < 3) THEN
PRINT "Unfortunately, you didn't sink the battleship(s)"
PRINT "The battleship was in spots"; r2; ","; c2; ";", (r2 + 1); ","; c2; ";"; ","; "and"; (r2 + 2), c2
END IF

INPUT "Would you like to play again(y, n)"; j$
j$ = LCASE$(j$)

IF j$ = "y" THEN GOTO start

finish:

END
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
Mavrik

Your next objective

Post by Mavrik »

I hope you are working on a graphic type battleship game. That would be awesome. I tried your code and it plays very well. Have fun.

Russ ;-) Happy Programming!
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

That's funny, I can't see how the code could "play very well" when the variables H1 and H2 are always zero, as indicated in my previous post.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
Post Reply