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

Username:   Password: 
 RegisterRegister   
 Battleship - Help?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
NeedBigHelp




PostPosted: Mon Jun 07, 2010 7:01 pm   Post subject: Battleship - Help?

code:

%Global Variables
var name : string
var continue : string
var coordinate1 : int
var coordinate2 : int
var font : int
var num1 : int
var num2 : int
font := Font.New ("Helvetica:14:bold")

procedure titlelogo
    % find the path of the image, draw it into position and have it disappear
    %local variable declaration
    var picture : int := Pic.FileNew ("Battleshiplogo.bmp")
    Pic.Draw (picture, maxx div 10, maxy div 10, 0)
    delay (2000)
    cls
    %Authors and small description
    put "" : 23, "Trouble in the Third Sea"
    put ""
    put "Ahoy mate! So you're the new recruit, eh? It's nice to always have a new member around here. Why don't you tell me your name first?"
end titlelogo

procedure askname
    %ask the user for their name
    get name
    put "Arr, it's a pleasure to meet you, ", name, "!"
    put "What say we bring you into battle for the first time? On this wide sea there are two ships, and they've got lots of supplies there, lots of food,"
    put "like fish, meat, some vegetables, Arr, I'm gettin hungry now...Oops, arr, looks like I got carried away!"
    put ""
    put "Anyways, what I'm sayin' is that there are two enemy ships, and they've got supplies. If we don't destroy 'em fast, then it's gonna give them there"
    put "enemies at an advantage. So what say, let's get started!"
    get continue
    put "I gotcha mate, that's the spirit! Let's go!"
end askname

procedure renderboard
    setscreen ("graphics:900;630")
    drawbox (120, 10, 10, 120, black)
    drawbox (120, 240, 10, 120, black)
    drawbox (120, 360, 10, 120, black)
    drawbox (120, 480, 10, 120, black)
    drawbox (120, 600, 10, 120, black)
    drawbox (240, 10, 10, 120, black)
    drawbox (360, 10, 10, 120, black)
    drawbox (480, 10, 10, 120, black)
    drawbox (600, 10, 10, 120, black)
    drawbox (240, 120, 120, 240, black)
    drawbox (240, 360, 10, 120, black)
    drawbox (240, 480, 10, 120, black)
    drawbox (240, 600, 10, 120, black)
    drawbox (360, 120, 240, 360, black)
    drawbox (360, 240, 240, 360, black)
    drawbox (360, 480, 240, 360, black)
    drawbox (360, 600, 240, 360, black)
    drawbox (480, 120, 360, 480, black)
    drawbox (480, 240, 360, 480, black)
    drawbox (480, 360, 360, 480, black)
    drawbox (480, 480, 360, 480, black)
    drawbox (480, 600, 360, 480, black)
    drawbox (600, 120, 480, 600, black)
    drawbox (600, 240, 480, 600, black)
    drawbox (600, 360, 480, 600, black)
    drawbox (600, 480, 480, 600, black)
    drawbox (600, 480, 480, 600, black)
end renderboard

procedure turn
    put "Select the X coordinate, between 1 and 5: " ..
    get coordinate1
    put "Select the Y coordinate, between 1 and 5: " ..
    get coordinate2
end turn

procedure declaregrid
    % random variables
    randint (num1, 1, 5)
    % multiply any number from 1 to 5 by 120 to match the coordinate
    num1 := num1 * 120
    randint (num2, 1, 5)
    if num2 = 1 then
        num2 := num2 * 10
        % For 2 the coordinates are different, they have to be multiplied by certain numbers
    elsif num2 = 2 then
        num2 := num2 * 60
        % else 3, 4 or 5 it can just multiply by 120
    else
        num2 := num2 * 120
    end if
end declaregrid

procedure randomship
    for  counter4 : num1 * 120 .. num1
end randomship


From the declaregrid procedure, we basically declared all the coordinates, the random integers are always any of the numbers 120, 240, 360, 480, and 600.
There are 2 ships: and those 2 integers are the starts of the ships. For example, if the randomly generated integer was 240, 240, then the ship would continue on in a straight line, for 3 more coordinates.

Would it at all be possible to make it so that the ship always stems 4 blocks in front of the randomly selected integers?
The number also has to be exactly 120, 240, 360, 480, or 600, and can't be anything in between.
We were convinced that we had to make use of a counter, but weren't aware of how to do it. However, the position the ship stems from must also be random, so sometimes the ship must step North, East, West, or South.

Any help is appreciated, thanks.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Jun 07, 2010 8:01 pm   Post subject: RE:Battleship - Help?

I don't really understand the question, but I'm going to guess that it's about randomly placing ships.

Pick a random spot on the board, pick a random direction, move in that direction for the length of the ship. Verify that all of the locations are valid. If so -- place the ship there. If not, try again.

Your board should be sized such that any retries are not too expensive.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: