Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Battleship question~
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xpurepikex




PostPosted: Wed May 24, 2006 10:17 am   Post subject: Battleship question~

i just have a little problem at the beginning i just started programming so i just need someone to get me started....how do u make the program so that when the game starts the computer randomly picks the spot, after picking the spot, it checks around so that it checks if the boat can actually fit into the other box (i used label) if u can help thx alot
Sponsor
Sponsor
Sponsor
sponsor
xpurepikex




PostPosted: Wed May 24, 2006 10:19 am   Post subject: Re: Battleship question~

i'm really stuck~ lets say the boat is 3boxes long...it checks around until it finds the spot where it can fit....i'm a newb so i just made a 5 by 5 small board
Brightguy




PostPosted: Wed May 24, 2006 3:23 pm   Post subject: Re: Battleship question~

If you've just started this might be a little complex...

You're going to need to use a matrix (2D array). Each cell will have different attributes, one of which will be if the cell is occupied by a boat. You get the computer to pick a random cell and orientation until you've found a place that isn't occupied.
xpurepikex




PostPosted: Fri May 26, 2006 10:10 am   Post subject: Re: Battleship question~

i'm really stuck could u take a look at it


Battleship.frm
 Description:

Download
 Filename:  Battleship.frm
 Filesize:  47.13 KB
 Downloaded:  145 Time(s)

cool dude




PostPosted: Fri May 26, 2006 5:34 pm   Post subject: (No subject)

wow your code is brutal. here's suggestions

1)indent code!!! i don't care that your new or you want to just get it to work. if u don't indent your code not only is it harder for you to read and debug but it is hard for ppl that are helping u. we will help more if u indent which is really easy to do!

2)why when i look at your instructions you call 2 message boxes? you can just say + vbNewLine to make another line. absolutely pointless to have 2 messages boxes!

3)please comment your code because there are some stuff in your code that i don't really understand the reason of u putting it there Confused

as for randomizing the ship you can do it this way:

code:

Dim hiddenship As Integer
Randomize
hiddenship = Int(63 * Rnd) + 1
lblSquare(hiddenship).Caption = "H"


Note: put this code in your form! this is just an example for u to see that wherever "H" is thats where the ship is
xpurepikex




PostPosted: Mon May 29, 2006 10:09 am   Post subject: ^^

heres my new one that i worked on for a day... it's improved but forgot to comment:P


Battleship.frm
 Description:

Download
 Filename:  Battleship.frm
 Filesize:  75.71 KB
 Downloaded:  119 Time(s)

wtd




PostPosted: Mon May 29, 2006 1:31 pm   Post subject: (No subject)

You indent some parts, but not others (subroutines and the like). Why?

code:
    If pick Mod 4 = 1 Then
        b = 4
    ElseIf pick Mod 4 = 2 Then
        b = 3
    ElseIf pick Mod 4 = 3 Then
        b = 2
    Else
        b = 1: lblSquare(Index).BackColor = vbGreen: MsgBox "Let Play!"
        For a = 0 To 63
            lblSquare(a).Enabled = False
        Next a
        Exit Sub
    End If


You're always checking for equality to one value. Use a select statement to save yourself some work. Smile

There's lots of this:

code:
ElseIf Index = 17 Or Index = 22 Or Index = 20 Or Index = 19 Then


What do these numbers mean? Why are they significant?

Again, you're only checking for equality with "index". Use a "select case" statement instead of a complex "if" statement.

code:
Select Case Index
   Case 17 Or 22 Or 20 Or 19
      ' do something
   ' other cases
End Select
xpurepikex




PostPosted: Thu Jun 01, 2006 10:14 am   Post subject: um

how do u make sure the first boat thats entered doesn't overlap the second one...
Sponsor
Sponsor
Sponsor
sponsor
cool dude




PostPosted: Thu Jun 01, 2006 4:45 pm   Post subject: (No subject)

well if your randomizing the ships using the way i showed you then you could make an if statement saying:

code:

if newhiddenship = oldhiddenship then
      newhiddenship += 1
end if


this will check that if the numbers are equal then it will increase the other number by 1 to make it not overlap.
xpurepikex




PostPosted: Fri Jun 02, 2006 9:42 am   Post subject: um

instead of putting letters i changed it but i can't make it so that it overlaps check the new one that i put up
xpurepikex




PostPosted: Fri Jun 02, 2006 10:11 am   Post subject: Re: um

heres my new one i really need help;;;;;i can't get the collision working...the code is confusing and bretty brutal...


Battleship.frm
 Description:

Download
 Filename:  Battleship.frm
 Filesize:  90.58 KB
 Downloaded:  117 Time(s)

cool dude




PostPosted: Fri Jun 02, 2006 7:48 pm   Post subject: (No subject)

firstly 1000+ lines of code Shocked thats insane for wat your program does. to be honest with u i could make a good battleship game probably under half the lines of code u have! u have so much pointless code and some of it i just can't even understand why? u should start from scratch learn control arrays better and basically look through a lot of vb tutorials. asside from that note i don't see u randomizing for a ship for one thing, even though i showed u how. wat do u mean by u can't get collision working? wat collision? be more specific. you should look at some previous battle ship game programs made and look how they made it maybe you'll get an idea. also comment your code it will help u and especially me to understand it better. good luck with this game!
wtd




PostPosted: Sat Jun 03, 2006 8:50 am   Post subject: Re: um

xpurepikex wrote:
heres my new one i really need help;;;;;i can't get the collision working...the code is confusing and bretty brutal...


For the love of Dog man! Indent the contents of subroutines!
cool dude




PostPosted: Sat Jun 03, 2006 5:21 pm   Post subject: Re: um

wtd wrote:
xpurepikex wrote:
heres my new one i really need help;;;;;i can't get the collision working...the code is confusing and bretty brutal...


For the love of Dog man! Indent the contents of subroutines!


lol i was actually going to say the exact same thing!
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: