Battleship

User projects written in or related to FreeBASIC.
Post Reply
Turd
Posts: 175
Joined: Aug 31, 2007 23:34
Location: Saskatchewan, Canada

Battleship

Post by Turd »

I'm making a battleship game but I don't know if it would be easier to make it a one or two player game.

Image

Code: Select all

ScreenRes 790, 350, 16

Dim x As Integer, y As Integer, buttons As Integer, x1 As Integer, y1 As Integer, i as integer



Dim grida(100) As Integer

Dim gridb(100) As Integer



Dim destroyer As Integer

Dim submarine As Integer

Dim cruiser As Integer

Dim battleship As Integer

Dim carrier As Integer



Dim background As Any Ptr = ImageCreate( 800, 450 )



Dim hdestroyer As Any Ptr = ImageCreate( 60, 30 )

Dim hsubmarine As Any Ptr = ImageCreate( 90, 30 )

Dim hcruiser As Any Ptr = ImageCreate( 90, 30 )

Dim hbattleship As Any Ptr = ImageCreate(120, 30 )

Dim hcarrier As Any Ptr = ImageCreate( 150, 30 )



Dim vdestroyer As Any Ptr = ImageCreate( 30, 60 )

Dim vsubmarine As Any Ptr = ImageCreate( 30, 90 )

Dim vcruiser As Any Ptr = ImageCreate( 30, 90 )

Dim vbattleship As Any Ptr = ImageCreate( 30, 120 )

Dim vcarrier As Any Ptr = ImageCreate( 30, 150 )



Dim white As Any Ptr = ImageCreate( 8, 8 )

Dim red As Any Ptr = ImageCreate( 8, 8 )



Dim hit As Any Ptr = ImageCreate( 25, 12 )

Dim miss As Any Ptr = ImageCreate( 37, 12 )



Bload "background.bmp", background

put (0,0),background



Bload "hdestroyer.bmp", hdestroyer

Bload "hsubmarine.bmp", hsubmarine

Bload "hcruiser.bmp", hcruiser

Bload "hbattleship.bmp", hbattleship

Bload "hcarrier.bmp", hcarrier



Bload "vdestroyer.bmp", vdestroyer

Bload "vsubmarine.bmp", vsubmarine

Bload "vcruiser.bmp", vcruiser

Bload "vbattleship.bmp", vbattleship

Bload "vcarrier.bmp", vcarrier



Bload "white.bmp", white

Bload "red.bmp", red

'        Put (x * 30 + 391, y * 30 + 51), red

'        Put (x * 30 + 51, y * 30 + 51), red



Bload "hit.bmp", hit

'Put (710,270), hit



Bload "miss.bmp", miss

Put (704,270), miss



Do

	'wait for a button press to choose a ship to place on the grid

	Do While buttons = 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    

    'destroyer

    If x > 704 And x < 728 And y > 6 And y < 68 And destroyer = 0 Then

    	destroyerstart:

    	'wait while the mouse button is down

    	Do While buttons > 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	'wait for a button press to place the destroyer on the grid

    	Do While buttons = 0 and x > 1: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	'place a horizontal destroyer

    	If x > 380 And x < 680 And y > 40 And y < 340 And buttons And 1 Then

    		'convert x and y from the mouse to 0 to 9 for the grid

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        'make sure the ship isn't going to be placed of the grid

	        if x1 > 8 then goto destroyerstart

	        'make sure that the destroyer won't be placed onto another ship by looking into the grid B array

	        for i = 0 to 1

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto destroyerstart

	        next i

	        'put the destroyer image onto the grid

	        Put (x1 * 30 + 380, y1 * 30 + 40), hdestroyer

	        'put two 1's in the grid B array to represent the destroyer

	        for i = 0 to 1

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    'place a vertical destroyer

	    elseIf x > 380 And x < 680 And y > 40 And y < 340 And buttons And 2 Then

	    	'convert x and y from the mouse to 0 to 9 for the grid

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        'make sure the ship isn't going to be placed of the grid

	        if y1 > 8 then goto destroyerstart

	        'make sure that the destroyer won't be placed onto another ship by looking into the grid B array

	        for i = 0 to 10 step 10

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto destroyerstart

	        next i

	        'put the destroyer image onto the grid

	        Put (x1 * 30 + 380, y1 * 30 + 40), vdestroyer

	        'put two 1's in the grid B array to represent the destroyer

	        for i = 0 to 10 step 10

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    'go back and try again if the ship was unable to be place in the chosen place

	    else

	    	goto destroyerstart

	    End If

	    'place a 1 in the variable to make sure no more then one destroyer is place on the grid

        destroyer = 1

	

    'submarine

    ElseIf x > 705 And x < 729 And y > 163 And y < 254 And submarine = 0 Then

    	submarinestart:

    	Do While buttons > 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	Do While buttons = 0 and x > 1: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	

    	If x > 380 And x < 680 And y > 40 And y < 340 And buttons And 1 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if x1 > 7 then goto submarinestart

	        for i = 0 to 2

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto submarinestart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), hsubmarine

	        for i = 0 to 2

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    elseIf x > 380 And x < 680 And y > 40 And y < 340 And buttons And 2 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if y1 > 7 then goto submarinestart

	        for i = 0 to 20 step 10

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto submarinestart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), vsubmarine

	        for i = 0 to 20 step 10

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    else

	    	goto submarinestart

	    End If

	    

        submarine = 1

        

    'cruiser

    ElseIf x > 705 And x < 729 And y > 70 And y < 161 And cruiser = 0 Then

    	cruiserstart:

    	Do While buttons > 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	Do While buttons = 0 and x > 1: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	

    	If x > 380 And x < 680 And y > 40 And y < 340 And buttons And 1 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if x1 > 7 then goto cruiserstart

	        for i = 0 to 2

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto cruiserstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), hcruiser

	        for i = 0 to 2

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    elseIf x > 380 And x < 680 And y > 40 And y < 340 And buttons And 2 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if y1 > 7 then goto cruiserstart

	        for i = 0 to 20 step 10

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto cruiserstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), vcruiser

	        for i = 0 to 20 step 10

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    else

	    	goto cruiserstart

	    End If

	    

        cruiser = 1

        

    'battleship

    ElseIf x > 754 And x < 777 And y > 162 And y < 285 And battleship = 0 Then

    	battleshipstart:

    	Do While buttons > 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	Do While buttons = 0 and x > 1: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	

    	If x > 380 And x < 680 And y > 40 And y < 340 And buttons And 1 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if x1 > 6 then goto battleshipstart

	        for i = 0 to 3

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto battleshipstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), hbattleship

	        for i = 0 to 3

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    elseIf x > 380 And x < 680 And y > 40 And y < 340 And buttons And 2 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if y1 > 6 then goto battleshipstart

	        for i = 0 to 30 step 10

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto battleshipstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), vbattleship

	        for i = 0 to 30 step 10

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    else

	    	goto battleshipstart

	    End If

	    

        battleship = 1

    'carrier

    ElseIf x > 752 And x < 778 And y > 12 And y < 162 And carrier = 0 Then

    	carrierstart:

    	Do While buttons > 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	Do While buttons = 0 and x > 1: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	

    	If x > 380 And x < 680 And y > 40 And y < 340 And buttons And 1 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if x1 > 5 then goto carrierstart

	        for i = 0 to 4

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto carrierstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), hcarrier

	        for i = 0 to 4

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    elseIf x > 380 And x < 680 And y > 40 And y < 340 And buttons And 2 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if y1 > 5 then goto carrierstart

	        for i = 0 to 40 step 10

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto carrierstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), vcarrier

	        for i = 0 to 40 step 10

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    else

	    	goto carrierstart

	    End If

	    

        carrier = 1

        

    'start or maybe restart button

    ElseIf x > 695 And x < 782 And y > 293 And y < 315 And buttons And 1 Then

    

    

    'quit button

    ElseIf x > 695 And x < 782 And y > 320 And y < 342 And buttons And 1 Then

    end

    

    End If

    

    'loop while the mouse button is down

    GetMouse (x, y, , buttons)

    Do While x > 1 And buttons And 1: GetMouse (x, y, , buttons): Sleep 10: Loop

    

    Sleep 20

    

    'if all the ships are placed then....

    if destroyer = 1 and submarine = 1 and cruiser = 1 and battleship = 1 and carrier = 1 then

    'Done setting ships

    end if

    

Loop



ImageDestroy( background )



ImageDestroy( hdestroyer )

ImageDestroy( hsubmarine )

ImageDestroy( hcruiser )

ImageDestroy( hbattleship )

ImageDestroy( hcarrier )



ImageDestroy( vdestroyer )

ImageDestroy( vsubmarine )

ImageDestroy( vcruiser )

ImageDestroy( vbattleship )

ImageDestroy( vcarrier )



ImageDestroy( red )

ImageDestroy( white )



ImageDestroy( hit )

ImageDestroy( miss )





'Make and save grid image

'For x = 10 To 340 Step 30

'    Line (x, 10)-(x, 340), &h3366FF

'    Line (x + 340, 10)-(x + 340, 340), &h3366FF

'Next x



'For y = 10 To 340 Step 30

'    Line (10, y)-(340, y), &h3366FF

'    Line (350, y)-(680, y), &H3366FF

'Next y

'Bsave "background.bmp", 0

Images:
http://www.mediafire.com/?mntv5d4ypbd
HalfByte
Posts: 20
Joined: Jul 30, 2005 19:27

Post by HalfByte »

Start with a two player game to get the interface and game play working
correctly then add an option for a one player game. A computer player
would be easier to create with battleship than many other games.
Lachie Dazdarian
Posts: 2338
Joined: May 31, 2005 9:59
Location: Croatia
Contact:

Post by Lachie Dazdarian »

Yeah, I would agree on this. For the human vs CPU mode devise some "smart" randomization when the CPU player is "looking" for a ship on your playfield, and after he hits, it's not hard to tell him to aim at sectors around the hit until a ship is sunk.
Turd
Posts: 175
Joined: Aug 31, 2007 23:34
Location: Saskatchewan, Canada

Post by Turd »

For a two player game, is there an easy way for two computers to talk to each other through the internet in Windows and Linux?

For one player, it shouldn't be to hard to do what you said, Lachie Dazdarian but what if you put all five ships parallel with each other, the computer player might get confused if it hits five in a row and it doesn't return a "sunk".
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Post by Hezad »

the computer player might get confused if it hits five in a row and it doesn't return a "sunk".
I guess a human too ;) imho, it's a pretty realistic approach.
krcko
Posts: 163
Joined: Jul 30, 2006 0:34
Location: Serbia
Contact:

Post by krcko »

For one player, it shouldn't be to hard to do what you said, Lachie Dazdarian but what if you put all five ships parallel with each other, the computer player might get confused if it hits five in a row and it doesn't return a "sunk".
if you allow ships to be right next to each other than keep shooting until you miss and everything will be ok :)

but, when i was in high-school we used to play battleship game on classes :) and one of the rules was that two ships must not "touch", ie there should be at least one field distance between two ships (they must not "touch" each others corners, too)...
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Turd wrote:For a two player game, is there an easy way for two computers to talk to each other through the internet in Windows and Linux?
Yes, pretty easy. I've been planning on releasing some libraries related to chisock to facilitate sending messages between game clients (for instance).

I may have more time pretty soon to take care of this, but don't hold your breath...
egocks
Posts: 20
Joined: Mar 27, 2008 2:02
Location: Philippines

Post by egocks »

...or, allow the end-users (players) to write their own AI scripts...
Turd
Posts: 175
Joined: Aug 31, 2007 23:34
Location: Saskatchewan, Canada

Post by Turd »

I'll start on a random ship placer for the computer player and be waiting for those libraries :-)
Turd
Posts: 175
Joined: Aug 31, 2007 23:34
Location: Saskatchewan, Canada

Post by Turd »

You can play it now but the computer player has no intelligence.
To play it just click on a ship on the side and then click the left button to place a horizontal ship or click the right button to place a vertical ship.
Once all five of your ships are placed you can click on the left grid to make hits on the enemy.

Image

I'm almost to embarrassed to post my code but here it is:

Code: Select all

ScreenRes 790, 350, 16

Dim x As Integer, y As Integer, buttons As Integer, x1 As Integer, y1 As Integer, i as integer, randomnumber as short, fire as string



Dim grida(100) As Integer

Dim gridb(100) As Integer



Dim hhits as integer

Dim chits as integer



Dim destroyer As Integer

Dim submarine As Integer

Dim cruiser As Integer

Dim battleship As Integer

Dim carrier As Integer



Dim destroyera As Integer

Dim submarinea As Integer

Dim cruisera As Integer

Dim battleshipa As Integer

Dim carriera As Integer



Dim background As Any Ptr = ImageCreate( 800, 450 )



Dim hdestroyer As Any Ptr = ImageCreate( 60, 30 )

Dim hsubmarine As Any Ptr = ImageCreate( 90, 30 )

Dim hcruiser As Any Ptr = ImageCreate( 90, 30 )

Dim hbattleship As Any Ptr = ImageCreate(120, 30 )

Dim hcarrier As Any Ptr = ImageCreate( 150, 30 )



Dim vdestroyer As Any Ptr = ImageCreate( 30, 60 )

Dim vsubmarine As Any Ptr = ImageCreate( 30, 90 )

Dim vcruiser As Any Ptr = ImageCreate( 30, 90 )

Dim vbattleship As Any Ptr = ImageCreate( 30, 120 )

Dim vcarrier As Any Ptr = ImageCreate( 30, 150 )



Dim white As Any Ptr = ImageCreate( 8, 8 )

Dim red As Any Ptr = ImageCreate( 8, 8 )



Dim hit As Any Ptr = ImageCreate( 25, 12 )

Dim miss As Any Ptr = ImageCreate( 37, 12 )



Bload "background.bmp", background

put (0,0),background



Bload "hdestroyer.bmp", hdestroyer

Bload "hsubmarine.bmp", hsubmarine

Bload "hcruiser.bmp", hcruiser

Bload "hbattleship.bmp", hbattleship

Bload "hcarrier.bmp", hcarrier



Bload "vdestroyer.bmp", vdestroyer

Bload "vsubmarine.bmp", vsubmarine

Bload "vcruiser.bmp", vcruiser

Bload "vbattleship.bmp", vbattleship

Bload "vcarrier.bmp", vcarrier



Bload "white.bmp", white

Bload "red.bmp", red

'        Put (x * 30 + 391, y * 30 + 51), red

'        Put (x * 30 + 51, y * 30 + 51), red



Bload "hit.bmp", hit

'Put (710,270), hit



Bload "miss.bmp", miss

Put (704,270), miss



'human ship placer

Do

	'wait for a button press to choose a ship to place on the grid

	Do While buttons = 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    

    'destroyer

    If x > 704 And x < 728 And y > 6 And y < 68 And destroyer = 0 Then

    	destroyerstart:

    	'wait while the mouse button is down

    	Do While buttons > 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	'wait for a button press to place the destroyer on the grid

    	Do While buttons = 0 and x > 1: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	'place a horizontal destroyer

    	If x > 380 And x < 680 And y > 40 And y < 340 And buttons And 1 Then

    		'convert x and y from the mouse to 0 to 9 for the grid

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        'locate 9, 9: print i + (x1 + 1) + (10 * y1)

	        

	        'make sure the ship isn't going to be placed of the grid

	        if x1 > 8 then goto destroyerstart

	        'make sure that the destroyer won't be placed onto another ship by looking into the grid B array

	        for i = 0 to 1

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto destroyerstart

	        next i

	        'put the destroyer image onto the grid

	        Put (x1 * 30 + 380, y1 * 30 + 40), hdestroyer

	        'put two 1's in the grid B array to represent the destroyer

	        for i = 0 to 1

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    'place a vertical destroyer

	    elseIf x > 380 And x < 680 And y > 40 And y < 340 And buttons And 2 Then

	    	'convert x and y from the mouse to 0 to 9 for the grid

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        'make sure the ship isn't going to be placed of the grid

	        if y1 > 8 then goto destroyerstart

	        'make sure that the destroyer won't be placed onto another ship by looking into the grid B array

	        for i = 0 to 10 step 10

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto destroyerstart

	        next i

	        'put the destroyer image onto the grid

	        Put (x1 * 30 + 380, y1 * 30 + 40), vdestroyer

	        'put two 1's in the grid B array to represent the destroyer

	        for i = 0 to 10 step 10

	        	gridb(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    'go back and try again if the ship was unable to be place in the chosen place

	    else

	    	goto destroyerstart

	    End If

	    'place a 1 in the variable to make sure no more then one destroyer is place on the grid

        destroyer = 1

	

    'submarine

    ElseIf x > 705 And x < 729 And y > 163 And y < 254 And submarine = 0 Then

    	submarinestart:

    	Do While buttons > 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	Do While buttons = 0 and x > 1: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	

    	If x > 380 And x < 680 And y > 40 And y < 340 And buttons And 1 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if x1 > 7 then goto submarinestart

	        for i = 0 to 2

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto submarinestart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), hsubmarine

	        for i = 0 to 2

	        	gridb(i + (x1 + 1) + (10 * y1)) = 2

	        next i

	    elseIf x > 380 And x < 680 And y > 40 And y < 340 And buttons And 2 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if y1 > 7 then goto submarinestart

	        for i = 0 to 20 step 10

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto submarinestart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), vsubmarine

	        for i = 0 to 20 step 10

	        	gridb(i + (x1 + 1) + (10 * y1)) = 2

	        next i

	    else

	    	goto submarinestart

	    End If

	    

        submarine = 1

        

    'cruiser

    ElseIf x > 705 And x < 729 And y > 70 And y < 161 And cruiser = 0 Then

    	cruiserstart:

    	Do While buttons > 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	Do While buttons = 0 and x > 1: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	

    	If x > 380 And x < 680 And y > 40 And y < 340 And buttons And 1 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if x1 > 7 then goto cruiserstart

	        for i = 0 to 2

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto cruiserstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), hcruiser

	        for i = 0 to 2

	        	gridb(i + (x1 + 1) + (10 * y1)) = 3

	        next i

	    elseIf x > 380 And x < 680 And y > 40 And y < 340 And buttons And 2 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if y1 > 7 then goto cruiserstart

	        for i = 0 to 20 step 10

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto cruiserstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), vcruiser

	        for i = 0 to 20 step 10

	        	gridb(i + (x1 + 1) + (10 * y1)) = 3

	        next i

	    else

	    	goto cruiserstart

	    End If

	    

        cruiser = 1

        

    'battleship

    ElseIf x > 754 And x < 777 And y > 162 And y < 285 And battleship = 0 Then

    	battleshipstart:

    	Do While buttons > 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	Do While buttons = 0 and x > 1: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	

    	If x > 380 And x < 680 And y > 40 And y < 340 And buttons And 1 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if x1 > 6 then goto battleshipstart

	        for i = 0 to 3

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto battleshipstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), hbattleship

	        for i = 0 to 3

	        	gridb(i + (x1 + 1) + (10 * y1)) = 4

	        next i

	    elseIf x > 380 And x < 680 And y > 40 And y < 340 And buttons And 2 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if y1 > 6 then goto battleshipstart

	        for i = 0 to 30 step 10

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto battleshipstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), vbattleship

	        for i = 0 to 30 step 10

	        	gridb(i + (x1 + 1) + (10 * y1)) = 4

	        next i

	    else

	    	goto battleshipstart

	    End If

	    

        battleship = 1

    'carrier

    ElseIf x > 752 And x < 778 And y > 12 And y < 162 And carrier = 0 Then

    	carrierstart:

    	Do While buttons > 0: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	Do While buttons = 0 and x > 1: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

    	

    	If x > 380 And x < 680 And y > 40 And y < 340 And buttons And 1 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if x1 > 5 then goto carrierstart

	        for i = 0 to 4

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto carrierstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), hcarrier

	        for i = 0 to 4

	        	gridb(i + (x1 + 1) + (10 * y1)) = 5

	        next i

	    elseIf x > 380 And x < 680 And y > 40 And y < 340 And buttons And 2 Then

	        x1 = Val(Left(Str((x - 380) / 30), 1))

	        y1 = Val(Left(Str((y - 40) / 30), 1))

	        

	        if y1 > 5 then goto carrierstart

	        for i = 0 to 40 step 10

	        	if gridb(i + (x1 + 1) + (10 * y1)) > 0 then goto carrierstart

	        next i

	        Put (x1 * 30 + 380, y1 * 30 + 40), vcarrier

	        for i = 0 to 40 step 10

	        	gridb(i + (x1 + 1) + (10 * y1)) = 5

	        next i

	    else

	    	goto carrierstart

	    End If

	    

        carrier = 1

        

    'start or maybe restart button

    ElseIf x > 695 And x < 782 And y > 293 And y < 315 And buttons And 1 Then

    'run "/home/turd/Documents/battleship/battleship4"

    end

    

    'quit button

    ElseIf x > 695 And x < 782 And y > 320 And y < 342 And buttons And 1 Then

    end

    

    End If

    

    'loop while the mouse button is down

    GetMouse (x, y, , buttons)

    Do While x > 1 And buttons And 1: GetMouse (x, y, , buttons): Sleep 10: Loop

    

    Sleep 10

    

    if destroyer = 1 and submarine = 1 and cruiser = 1 and battleship = 1 and carrier = 1 then

    	destroyer = 0: submarine = 0: cruiser = 0: battleship = 0: carrier = 0

    'Done setting ships

    exit do

    end if

    

Loop



'computer ship placer

		'destroyer

		randomize

    	destroyerstartb:

    	randomnumber = rnd

    	if randomnumber = 0 then

	        x1 = Val(Left(Str(rnd * 10),1))

	        y1 = Val(Left(Str(rnd * 10),1))

	        if x1 > 8 then goto destroyerstartb

	        for i = 0 to 1

	        	if grida(i + (x1 + 1) + (10 * y1)) > 0 then goto destroyerstartb

	        next i

	        for i = 0 to 1

	        	grida(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    elseIf randomnumber = 1 then

	        x1 = Val(Left(Str(rnd * 10),1))

	        y1 = Val(Left(Str(rnd * 10),1))

	        if y1 > 8 then goto destroyerstartb

	        for i = 0 to 10 step 10

	        	if grida(i + (x1 + 1) + (10 * y1)) > 0 then goto destroyerstartb

	        next i

	        for i = 0 to 10 step 10

	        	grida(i + (x1 + 1) + (10 * y1)) = 1

	        next i

	    else

	    	goto destroyerstartb

	    End If

        destroyer = 1





		'submarine

		randomize

    	submarinestartb:

    	randomnumber = rnd

    	if randomnumber = 0 then

	        x1 = Val(Left(Str(rnd * 10),1))

	        y1 = Val(Left(Str(rnd * 10),1))

	        

	        if x1 > 7 then goto submarinestartb

	        for i = 0 to 2

	        	if grida(i + (x1 + 1) + (10 * y1)) > 0 then goto submarinestartb

	        next i

	        for i = 0 to 2

	        	grida(i + (x1 + 1) + (10 * y1)) = 2

	        next i

	    elseIf randomnumber = 1 then

	        x1 = Val(Left(Str(rnd * 10),1))

	        y1 = Val(Left(Str(rnd * 10),1))

	        

	        if y1 > 7 then goto submarinestartb

	        for i = 0 to 20 step 10

	        	if grida(i + (x1 + 1) + (10 * y1)) > 0 then goto submarinestartb

	        next i

	        for i = 0 to 20 step 10

	        	grida(i + (x1 + 1) + (10 * y1)) = 2

	        next i

	    else

	    	goto submarinestartb

	    End If

	    

        submarine = 1

        

	    'cruiser

	    randomize

    	cruiserstartb:

    	randomnumber = rnd

    	if randomnumber = 0 then

	        x1 = Val(Left(Str(rnd * 10),1))

	        y1 = Val(Left(Str(rnd * 10),1))

	        

	        if x1 > 7 then goto cruiserstartb

	        for i = 0 to 2

	        	if grida(i + (x1 + 1) + (10 * y1)) > 0 then goto cruiserstartb

	        next i

	        for i = 0 to 2

	        	grida(i + (x1 + 1) + (10 * y1)) = 3

	        next i

	    elseIf randomnumber = 1 then

	        x1 = Val(Left(Str(rnd * 10),1))

	        y1 = Val(Left(Str(rnd * 10),1))

	        

	        if y1 > 7 then goto cruiserstartb

	        for i = 0 to 20 step 10

	        	if grida(i + (x1 + 1) + (10 * y1)) > 0 then goto cruiserstartb

	        next i

	        for i = 0 to 20 step 10

	        	grida(i + (x1 + 1) + (10 * y1)) = 3

	        next i

	    else

	    	goto cruiserstartb

	    End If

	    

        cruiser = 1

        

	    'battleship

	    randomize

    	battleshipstartb:

    	randomnumber = rnd

    	if randomnumber = 0 then

	        x1 = Val(Left(Str(rnd * 10),1))

	        y1 = Val(Left(Str(rnd * 10),1))

	        

	        if x1 > 6 then goto battleshipstartb

	        for i = 0 to 3

	        	if grida(i + (x1 + 1) + (10 * y1)) > 0 then goto battleshipstartb

	        next i

	        for i = 0 to 3

	        	grida(i + (x1 + 1) + (10 * y1)) = 4

	        next i

	    elseIf randomnumber = 1 then

	        x1 = Val(Left(Str(rnd * 10),1))

	        y1 = Val(Left(Str(rnd * 10),1))

	        

	        if y1 > 6 then goto battleshipstartb

	        for i = 0 to 30 step 10

	        	if grida(i + (x1 + 1) + (10 * y1)) > 0 then goto battleshipstartb

	        next i

	        for i = 0 to 30 step 10

	        	grida(i + (x1 + 1) + (10 * y1)) = 4

	        next i

	    else

	    	goto battleshipstartb

	    End If

	    

        battleship = 1

        

	    'carrier

	    randomize

    	carrierstartb:

    	randomnumber = rnd

    	if randomnumber = 0 then

	        x1 = Val(Left(Str(rnd * 10),1))

	        y1 = Val(Left(Str(rnd * 10),1))

	        

	        if x1 > 5 then goto carrierstartb

	        for i = 0 to 4

	        	if grida(i + (x1 + 1) + (10 * y1)) > 0 then goto carrierstartb

	        next i

	        for i = 0 to 4

	        	grida(i + (x1 + 1) + (10 * y1)) = 5

	        next i

	    elseIf randomnumber = 1 then

	        x1 = Val(Left(Str(rnd * 10),1))

	        y1 = Val(Left(Str(rnd * 10),1))

	        

	        if y1 > 5 then goto carrierstartb

	        for i = 0 to 40 step 10

	        	if grida(i + (x1 + 1) + (10 * y1)) > 0 then goto carrierstartb

	        next i

	        for i = 0 to 40 step 10

	        	grida(i + (x1 + 1) + (10 * y1)) = 5

	        next i

	    else

	    	goto carrierstartb

	    End If

	    

        carrier = 1

	destroyera = 0: submarinea = 0: cruisera = 0: battleshipa = 0: carriera = 0



'human fire

do

	back:

	

	GetMouse (x, y, , buttons)

	Do While buttons < 1: GetMouse (x, y, , buttons): Locate 1,1: Print Using "### : ### : #          "; x; y; buttons: Sleep 10: Loop

	

	Draw String (22, 22), str(chits), &h0

	Draw String (362, 22), str(hhits), &h0	

    'start or maybe restart button

    If x > 695 And x < 782 And y > 293 And y < 315 And buttons And 1 Then

    	'run "/home/turd/Documents/battleship/battleship4"

    	end

    

    'quit button

    ElseIf x > 695 And x < 782 And y > 320 And y < 342 And buttons And 1 Then

    	end

    

    End If

	

    If x > 40 And x < 340 And y > 40 And y < 340 And buttons And 1 Then

    	x1 = Val(Left(Str((x - 40) / 30), 1))

	    y1 = Val(Left(Str((y - 40) / 30), 1))

	    if grida((x1 + 1) + (10 * y1)) > 0 And grida((x1 + 1) + (10 * y1)) < 6 then

	    	locate 3, 10

	    	if grida((x1 + 1) + (10 * y1)) = 1 then

	    		destroyer += 1

	    		if destroyer > 2 then

	    			destroyer = 10

	    			hhits += 1

	    		end if

	    	elseif grida((x1 + 1) + (10 * y1)) = 2 then

	    		submarine += 1

	    		if submarine > 3 then

	    			submarine = 10

	    			hhits += 1

	    		end if

	    	elseif grida((x1 + 1) + (10 * y1)) = 3 then

	    		cruiser += 1

	    		if cruiser > 3 then

	    			cruiser = 10

	    			hhits += 1

	    		end if

	    	elseif grida((x1 + 1) + (10 * y1)) = 4 then

	    		battleship += 1

	    		if battleship > 4 then

	    			battleship = 10

	    			hhits += 1

	    		end if

	    	elseif grida((x1 + 1) + (10 * y1)) = 5 then

	    		carrier += 1

	    		if carrier > 5 then

	    			carrier = 10

	    			hhits += 1

	    		end if

	    	

			end if

			grida((x1 + 1) + (10 * y1)) = 6

			Put (x1 * 30 + 51, y1 * 30 + 51), red

		

		elseif grida((x1 + 1) + (10 * y1)) = 0 then

			grida((x1 + 1) + (10 * y1)) = 6

		Put (x1 * 30 + 51, y1 * 30 + 51), white

		

		else

		goto back

		

		end if

	else

		goto back

	End If



'computer fire

	locate 1, 30: print "Wait..."

	randomize

	backa:

    	x1 = Val(Left(Str(rnd * 10),1))

	    y1 = Val(Left(Str(rnd * 10),1))

	    if gridb((x1 + 1) + (10 * y1)) > 0  And gridb((x1 + 1) + (10 * y1)) < 6 then

	    	locate 11, 11

	    	if gridb((x1 + 1) + (10 * y1)) = 1 then

	    		destroyera += 1

	    		if destroyera > 1 then

	    			destroyera = 10

	    			chits += 1

	    		end if

	    	elseif gridb((x1 + 1) + (10 * y1)) = 2 then

	    		submarinea += 1

	    		if submarinea > 2 then

	    			submarinea = 10

	    			chits += 1

	    		end if

	    	elseif gridb((x1 + 1) + (10 * y1)) = 3 then

	    		cruisera += 1

	    		if cruisera > 2 then

	    			cruisera = 10

	    			chits += 1

	    		end if

	    	elseif gridb((x1 + 1) + (10 * y1)) = 4 then

	    		battleshipa += 1

	    		if battleshipa > 3 then

	    			battleshipa = 10

	    			chits += 1

	    		end if

	    	elseif gridb((x1 + 1) + (10 * y1)) = 5 then

	    		carriera += 1

	    		if carriera > 4 then

	    			carriera = 10

	    			chits += 1

	    		end if

	    	

			end if

		gridb((x1 + 1) + (10 * y1)) = 6

		Put (x1 * 30 + 391, y1 * 30 + 51), red

		

		elseif gridb((x1 + 1) + (10 * y1)) = 0 then

			gridb((x1 + 1) + (10 * y1)) = 6

		Put (x1 * 30 + 391, y1 * 30 + 51), white

		

		else

		goto backa

		end if



	locate 1, 30: print "       "

	

	Draw String (22, 22), str(chits), &hE51A1A

	Draw String (362, 22), str(hhits), &hE51A1A

    

	if chits > 4 then

		locate 10, 40: print "YOU LOSE!"    

		do

			GetMouse (x, y, , buttons)

			If x > 695 And x < 782 And y > 293 And y < 315 And buttons And 1 Then

    			'run "/home/turd/Documents/battleship/battleship4"

    			end

    

    		'quit button

    		ElseIf x > 695 And x < 782 And y > 320 And y < 342 And buttons And 1 Then

    			end

    			

    		end if

    		sleep 10

		loop

	end if

	

	if hhits > 4 then

		locate 10, 40: print "YOU WIN!"    

		do

			GetMouse (x, y, , buttons)

			If x > 695 And x < 782 And y > 293 And y < 315 And buttons And 1 Then

    			'run "/home/turd/Documents/battleship/battleship4"

    			end

    

    		'quit button

    		ElseIf x > 695 And x < 782 And y > 320 And y < 342 And buttons And 1 Then

    			end

    			

    		end if

    		sleep 10

		loop

	end if

    'start or maybe restart button

    If x > 695 And x < 782 And y > 293 And y < 315 And buttons And 1 Then

		'run "/home/turd/Documents/battleship/battleship4"

    end

    

    'quit button

    ElseIf x > 695 And x < 782 And y > 320 And y < 342 And buttons And 1 Then

    end

    

    End If

		

	GetMouse (x, y, , buttons)

    Do While x > 1 And buttons And 1: GetMouse (x, y, , buttons): Sleep 10: Loop

    sleep 10

loop



ImageDestroy( background )



ImageDestroy( hdestroyer )

ImageDestroy( hsubmarine )

ImageDestroy( hcruiser )

ImageDestroy( hbattleship )

ImageDestroy( hcarrier )



ImageDestroy( vdestroyer )

ImageDestroy( vsubmarine )

ImageDestroy( vcruiser )

ImageDestroy( vbattleship )

ImageDestroy( vcarrier )



ImageDestroy( red )

ImageDestroy( white )



ImageDestroy( hit )

ImageDestroy( miss )





'Make and save grid image

'For x = 10 To 340 Step 30

'    Line (x, 10)-(x, 340), &h3366FF

'    Line (x + 340, 10)-(x + 340, 340), &h3366FF

'Next x



'For y = 10 To 340 Step 30

'    Line (10, y)-(340, y), &h3366FF

'    Line (350, y)-(680, y), &H3366FF

'Next y

'Bsave "background.bmp", 0

Post Reply