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

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




PostPosted: Sat Jun 07, 2008 5:07 pm   Post subject: Help with project

I am in the homestretch of my project that I have for school but I ran into a snag.

My Program is based on the battleship game. It has ships set using arrays (see code: 01.t) and I need to know if there is a way to see if the mouse x and y co-ordinates can be compared to the ships location.

code:
% 01.t
% This program defines where ships are set

% Declaration Statement
var carrier_x : array 1 .. 5, 1 .. 2 of int % Aircraft Carrier
var carrier_y : array 1 .. 5, 1 .. 2 of int
var btlship_x : array 1 .. 4, 1 .. 2 of int % Battleship
var btlship_y : array 1 .. 4, 1 .. 2 of int
var destroy_x : array 1 .. 3, 1 .. 2 of int % Destroyer
var destroy_y : array 1 .. 3, 1 .. 2 of int
var submarn_x : array 1 .. 3, 1 .. 2 of int % Submarine
var submarn_y : array 1 .. 3, 1 .. 2 of int
var patrolb_x : array 1 .. 2, 1 .. 2 of int % Patrol Boat
var patrolb_y : array 1 .. 2, 1 .. 2 of int


Let me explain the array..

var patrolb_y : array 1 .. 2, 1 .. 2 of int

Length of ship in units
(1) is starting x (or y) co-ordinate; (2) is ending x (or y) co-ordinate

code:
procedure drawBoard
    % Player's Board
    for x : 2 .. 227 by 25
        for y : 2 .. 202 by 25
            drawbox (x, y, x + 25, y + 25, 7)
        end for
    end for

    % Opponent's Board
    for x : 256 .. 483 by 25
        for y : 2 .. 202 by 25
            drawbox (x, y, x + 25, y + 25, 7)
        end for
    end for
end drawBoard


The screen resolution is 508 by 350.
Sponsor
Sponsor
Sponsor
sponsor
Zampano




PostPosted: Sat Jun 07, 2008 10:31 pm   Post subject: Re: Help with project

It's not beyond the means of some humble ingenious thought. In your drawboard procedure, you drew a line at a certain coordinate according to the array indice that the for loop counter suggests. Try and work that same logic backwards so that, given a coordinate, you could derive the array indice.
maxudaskin




PostPosted: Sat Jun 07, 2008 11:03 pm   Post subject: Re: Help with project

The reason I was asking, is because I want to try not to do many if statements... I am not too proficient with Turing.
DemonWasp




PostPosted: Thu Jun 12, 2008 8:39 pm   Post subject: RE:Help with project

1. The best way to get proficient is by writing some code. If you want to get proficient, trial and error is honestly far more enlightening than you'd think.

2. As Zampano says, you already have the right idea. Your code for drawing the boards is the hint.

Hint: You know the coordinates for each square in each board, because you've already written a set of nested for loops to handle each.

Hint: For example, take a mouse click at (120, 90). This is outside the boundaries of the opponents array, so we'll completely ignore that array. However, it's inside the boundaries of your player's grid, so you have to check against that.

Hint: The example mouse click at (120, 90) is offset from the upper-left corner of your board by (118, 88) - take each coordinate and subtract the array starting positions given in your for loops.

Hint: Now each grid box takes up 25 pixels. I'm sure you and division can work out that the click at offset (118, 88) works out to (4.72, 3.52). Take the floor of each of those and you notice we have array coordinates (4, 3). Those are the coordinates in your grid (though Turing's arrays start at 1, so you may have to add one to each value).
jeffgreco13




PostPosted: Fri Jun 13, 2008 8:11 am   Post subject: Re: Help with project

Trial and error is like the basis of programming in my eyes. There's no concrete answer for anything in programming, so you've got to try and try and try until you get what you want. And along the way you'll learn so many new things, which will in the future make programming easier and more rewarding.
Insectoid




PostPosted: Fri Jun 13, 2008 9:00 am   Post subject: RE:Help with project

If statements are the most commonly used function (or whatever they are) in almost every game. If you don't want to use many of them, don't do a game; do an animation.
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  [ 6 Posts ]
Jump to:   


Style:  
Search: