Computer Science Canada

Battleship Game

Author:  TheXploder [ Tue Jan 20, 2004 9:40 pm ]
Post subject:  Battleship Game

Here is the soure code to the Battleship Game I made.
Notes:
* do not drag when holding the left-mouse-button, and don't verlap the ships. Sad

I'll add a check later... Smile

** the AI could be better. Sad

*** Write only capital letters.

How to play?

mouse left-click - places ship.
mouse right-click - increases size of ship placed.
key arrow right, or left - spins the ship.

But anyway here it is, enjoy Razz :

Author:  poly [ Tue Jan 20, 2004 10:38 pm ]
Post subject: 

bravo!!!! That is very well done, for not using any graphics (like drawn graphics using paint etc). If thats your final project you will get a pretty damn good mark!

Author:  TheXploder [ Thu Jan 22, 2004 10:10 am ]
Post subject: 

thanx, any more comments or suggestions?
Smile

Author:  we64 [ Tue Jan 27, 2004 6:36 pm ]
Post subject: 

great program, well done...
I would like to see it with graphics, that will be so cool...

Author:  shorthair [ Thu Jan 29, 2004 12:07 pm ]
Post subject: 

Awsome job , get the graphics and i throw in some BITS , keep up hte good code , reminds me of my battleship game , my was alot messier than yours Shocked

Author:  sport [ Thu Jan 29, 2004 12:23 pm ]
Post subject: 

Great Program.

Author:  Paul [ Thu Jan 29, 2004 12:26 pm ]
Post subject: 

And to think... I was a part of it...
*gets glares from everyone*
I WAS part of the testing!!!

Author:  TheXploder [ Thu Jan 29, 2004 12:36 pm ]
Post subject: 

But don't you think the AI is well how do you put it in the right words, STUPID, Laughing, Gotta fix that I guess.

so lets say the 2 place sip is located here as a '1':
|0|0|0|0|
|0|1|1|0|
|0|0|0|0|

and then the computer hits it and then the '1' is replaced by an 'x':

|0|0|0|0|
|0|x|1|0|
|0|0|0|0|

*so now the next random procces should only randomly choose to go up, left, right, or down...

when it misses the next turn and chooses wrong, the old cordinate should be retreated and randomly choose again...

Thats my problem right now... And imagine 3 place ships..
Graphics are easy game... (Just need to draw them)

Author:  Paul [ Thu Jan 29, 2004 1:02 pm ]
Post subject: 

So once it hits something, it has to store that co-ordinate and hit next to it?

Author:  TheXploder [ Thu Jan 29, 2004 1:28 pm ]
Post subject: 

yes something like that, ohh, let's make a quick program to simulate the placement.. here..:
code:

var lengthOfArray : int := 10

var map : array 1 .. lengthOfArray, 1 .. lengthOfArray of int

for row : 1 .. lengthOfArray
    for col : 1 .. lengthOfArray
        map (row, col) := 0
    end for
end for

var x : int := Rand.Int (1, 5)
var y : int := Rand.Int (1, 5)
var sizeOfShip : int := Rand.Int (1, 4)
var direction : int := Rand.Int (0, 1)

proc ship (x, y, size, dir : int)
    if dir = 1 and size = 1 then
        map (x, y) := 1
        elsif dir = 0 and size = 1 then
    end if

    if dir = 1 and size = 2 then
        map (x, y) := 1
        map (x, y + 1) := 1
    elsif dir = 0 and size = 2 then
        map (x, y) := 1
        map (x + 1, y) := 1
    end if

    if dir = 1 and size = 3 then
        map (x, y) := 1
        map (x, y + 1) := 1
        map (x, y + 2) := 1
    elsif dir = 0 and size = 3 then
        map (x, y) := 1
        map (x + 1, y) := 1
        map (x + 2, y) := 1
    end if

    if dir = 1 and size = 4 then
        map (x, y) := 1
        map (x, y + 1) := 1
        map (x, y + 2) := 1
        map (x, y + 3) := 1
    elsif dir = 0 and size = 4 then
        map (x, y) := 1
        map (x + 1, y) := 1
        map (x + 2, y) := 1
        map (x + 3, y) := 1
    end if
end ship

ship (x, y, sizeOfShip, direction)

for row : 1 .. lengthOfArray
    for col : 1 .. lengthOfArray
        put map (row, col)..
    end for
    put ""
end for

Author:  TheXploder [ Thu Jan 29, 2004 6:37 pm ]
Post subject: 

ohh, I updated it...:
code:

%Variables/Declearation of Variables

View.Set ("offscreenonly")

var lengthOfArray : int := 10

var map : array 1 .. lengthOfArray, 1 .. lengthOfArray of string

for row : 1 .. lengthOfArray
    for col : 1 .. lengthOfArray
        map (row, col) := "0"
    end for
end for

% Computer AI attacking Variables..
var xattack, yattack : int

var AIrandXArea, AIrandYArea : int := 4
var AIShootDir : int

var computerHits : boolean
var saveCoordinate : string := ""

computerHits := false

var sizeOfShip : int := Rand.Int (1, 4)
var direction : int := Rand.Int (0, 1)

proc ship (x, y, size, dir : int)
    if dir = 1 and size = 1 then
        map (x, y) := "1"
    elsif dir = 0 and size = 1 then
    end if

    if dir = 1 and size = 2 then
        map (x, y) := "1"
        map (x, y + 1) := "1"
    elsif dir = 0 and size = 2 then
        map (x, y) := "1"
        map (x + 1, y) := "1"
    end if

    if dir = 1 and size = 3 then
        map (x, y) := "1"
        map (x, y + 1) := "1"
        map (x, y + 2) := "1"
    elsif dir = 0 and size = 3 then
        map (x, y) := "1"
        map (x + 1, y) := "1"
        map (x + 2, y) := "1"
    end if

    if dir = 1 and size = 4 then
        map (x, y) := "1"
        map (x, y + 1) := "1"
        map (x, y + 2) := "1"
        map (x, y + 3) := "1"
    elsif dir = 0 and size = 4 then
        map (x, y) := "1"
        map (x + 1, y) := "1"
        map (x + 2, y) := "1"
        map (x + 3, y) := "1"
    end if
end ship

proc Shoot
    if computerHits = true then

        randint (AIShootDir, 1, 4)
    else
        randint (xattack, 1, AIrandYArea)
        randint (yattack, 1, AIrandYArea)
    end if
end Shoot

Shoot
ship (1, 1, sizeOfShip, direction)

loop
    for row : 1 .. lengthOfArray
        for col : 1 .. lengthOfArray
            map (row, col) := "0"
        end for
    end for

    ship (1, 1, sizeOfShip, direction)

    put intstr (xattack) + "," + intstr (yattack)

    put computerHits

    map (xattack, yattack) := "x"

    Shoot
   
    if map (xattack, yattack) = "1" then
        put "Computer Hits"
        computerHits := true
        saveCoordinate := intstr (xattack) + "," + intstr (yattack)
        put "Saved Coordinate: ", saveCoordinate
        put ""
    else
        put "Computer Misses"
        put ""
        put ""
        computerHits := false
    end if

    computerHits := false

    for row : 1 .. lengthOfArray
        for col : 1 .. lengthOfArray
            put map (row, col) ..
        end for
        put ""
    end for

    delay (1000)

    View.Update
    cls
end loop

Author:  white_dragon [ Sat Feb 07, 2004 5:54 pm ]
Post subject: 

i encountered some troubles and problems in the program. but it looks and sounds really good!

Author:  Cervantes [ Sat Feb 07, 2004 6:19 pm ]
Post subject: 

good work, I like it. fix a few bugs and it'll be super!
the annoying bug is the text display messing up Confused you should make it so that it clears the field after you entered the coordinate. or clear it as soona s its your turn to choose again.

Author:  Andy [ Sat Feb 07, 2004 6:24 pm ]
Post subject: 

u should do the starwarz version, its harder cuz the blocks r hexagons

Author:  TheXploder [ Sat Feb 07, 2004 6:26 pm ]
Post subject: 

never heard of a starwarz version.. can you xplain

Author:  Andy [ Sat Feb 07, 2004 6:27 pm ]
Post subject: 

its battle ship but with starwars ships and stuff

Author:  GUI.GetScrollBarWidth : i [ Fri Feb 13, 2004 6:23 pm ]
Post subject: 

this game is to addictive, but u should fix it so that u can't make infinate ships

Author:  Jodo Yodo [ Thu Mar 04, 2004 10:21 pm ]
Post subject: 

Wow, that's SuperJuicy

Author:  the_short1 [ Sat Mar 06, 2004 10:26 pm ]
Post subject: 

that is WICKED>>>> omg.... but only thing is ....
you should probaly make it clear location after entering coordinates
locate (14,1)
put coordinates
locatete (14,13)
put " "
so it erases old coordinate.... otherewise... OMG... that is just one WICKED PROGRAM.... keep up the good work

Author:  programer007 [ Sun Mar 07, 2004 4:49 pm ]
Post subject: 

that is too good... i cant belive stuff that good can be made in turing.... wow... i will be playing that for HOURS

Author:  Hackster [ Sun Mar 07, 2004 7:42 pm ]
Post subject: 

tHATS QUITE AMAZING!! i kinda got bored with always typing the coords, but still very technical

Author:  kit-kat-kid [ Sat Jul 10, 2004 4:33 pm ]
Post subject: 

whay to go i really enjoyed playing keep p the good work


: