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

Username:   Password: 
 RegisterRegister   
 Help with a inefficiant Checkers code
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Adalias




PostPosted: Thu May 18, 2006 5:15 pm   Post subject: Help with a inefficiant Checkers code

Hi I'm working on programming Checkers but have a few problems:

First, to draw and colour my board I use Draw.FillBox 32 times to make the board. I know that I can use a for statment and 2D arrays to draw it but I couldn't get it to work when I tried.

Second, I use Draw.FillOval to make the pieces, I would like to use pictures instead but couldn't make any sense of the F10 turning help on the subject.

Finaly, to move the pieces I use a series of ifs that check to see if the mouse is in the right spot, if the mouse button is pressed, if that spot is taken, if the piece is yours, and if it is your turn then it highlights the piece and the possible place(s) you can go. The only problem is that I would have to repeat this 30 someodd lines of code 64 times just so you can click on every piece, then there will be 64 more if the piece is a king, and another long set for jumping there has got to be a better way

I know it would be nice if I could post the code I have but it's on the school computers and I need to get it from there, I can post the code tommorow

FYI I am comfortable with arrays 2D arrays, mouse functions, procedures, processes, and am a very fast learner for other things
Sponsor
Sponsor
Sponsor
sponsor
TheOneTrueGod




PostPosted: Thu May 18, 2006 5:31 pm   Post subject: (No subject)

Well, the most important thing is that you know math.

code:
for i : 0..7
   for j : 0..7
        %Pseudocode
        draw the box
   end for
end for


allright, now you need to pick one of the letters to represent your x's, and one to represent your y's. Because I generally do, I'm going to pick j for x, and i for y.

So, lets use a constant for the size of the boxes:
code:

    const size := 20


Allright, now that we have the basics established, all that is lacking is the math skills your grade 7 teacher told you that you would need later in life, and you told him to shut up because you'd never need it. Very Happy I'm sure he's laughing at you now.

Anyways, lets start with a simplified view of it. In order to get the bottom left co-ordinates of each box, we just need to multiply the variables by that constant size.
The co-ordinate (j*size,i*size) will net you the bottom left co-ordinate. Why is this? Well, the board is 8 spaces large, correct? We are iterating through j 8 times. If we just used j's value, we'd have co-ordinates of (0,0), (1,0), (2,0) etc. So, if we multiply each of those values by size, we would get:
(0*size,0), (1*size,0), (2*size,0) etc... This works out to values of 0, 20, and 40.

Now that we have the bottom left of the box, these are the first two parameters. Now for the next two. Doesn't it make sense that if we just take the bottom left corner, and add the size to that, it will net us the top right of the box?

(0*size+size,0+size), (1*size+size,0+size) etc...

Using factoring, we can simplify these into:
((0+1)*size,(0+1)*size), ((1+1)*size,(0+1)*size) etc...

If we bring everything that i've told you together, we can determine the corners of the box in order to draw it:

code:
drawbox(j*size,i*size,(j+1)*size,(i+1)*size,colour)


Allright, now to determine the colour. (I hope you havn't given up on reading this yet.)

For the colour, we want it alternating every square. So, lets start with the basics:
code:
colour := j mod 2

This will make it so that every other square is the same colour. However, this will only work in one dimension. We havn't handled the second dimension (i) yet.

I'm going to let you attempt this part, but it involves modifying the last equation I gave you. Good luck, and please post any successes you have, or if you need any help. Very Happy

Sorry if I used incorrect terms. If I can't think of the right word, I just kinda pick a word from my vocabulary and hope its the right one Razz
XeroX




PostPosted: Thu May 18, 2006 5:37 pm   Post subject: (No subject)

And as for help with pictures, view the tutoral at http://www.compsci.ca/v2/viewtopic.php?t=191
Adalias




PostPosted: Fri May 19, 2006 3:22 pm   Post subject: (No subject)

how is colour := j mod 2 supposed to colour every other square?
Adalias




PostPosted: Fri May 19, 2006 3:26 pm   Post subject: (No subject)

Oh and btw heres my coding

code:
setscreen ("graphics:532;532;nobuttonbar,nocursor")
var x, y, b : int
var turn : int := 1
var king : array 1 .. 32 of int := init (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var spot : array 1 .. 32 of int := init (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
var move : array 1 .. 32 of int := init (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var movej : array 1 .. 32 of int := init (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var spotred : array 1 .. 32 of int := init (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var spotblack : array 1 .. 32 of int := init (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
%Draws the Checks
Draw.FillBox (4, 4, 68, 68, red)
Draw.FillBox (4, 136, 68, 200, red)
Draw.FillBox (4, 268, 68, 332, red)
Draw.FillBox (4, 400, 68, 465, red)
Draw.FillBox (68, 68, 136, 136, red)
Draw.FillBox (68, 200, 136, 268, red)
Draw.FillBox (68, 332, 136, 400, red)
Draw.FillBox (68, 465, 136, 528, red)
Draw.FillBox (136, 4, 200, 68, red)
Draw.FillBox (136, 136, 200, 200, red)
Draw.FillBox (136, 268, 200, 332, red)
Draw.FillBox (136, 400, 200, 465, red)
Draw.FillBox (200, 68, 268, 136, red)
Draw.FillBox (200, 200, 268, 268, red)
Draw.FillBox (200, 332, 268, 400, red)
Draw.FillBox (200, 465, 268, 528, red)
Draw.FillBox (268, 4, 332, 68, red)
Draw.FillBox (268, 136, 332, 200, red)
Draw.FillBox (268, 268, 332, 332, red)
Draw.FillBox (268, 400, 332, 465, red)
Draw.FillBox (332, 68, 400, 136, red)
Draw.FillBox (332, 200, 400, 268, red)
Draw.FillBox (332, 332, 400, 400, red)
Draw.FillBox (332, 465, 400, 528, red)
Draw.FillBox (400, 4, 465, 68, red)
Draw.FillBox (400, 136, 465, 200, red)
Draw.FillBox (400, 268, 465, 332, red)
Draw.FillBox (400, 400, 465, 465, red)
Draw.FillBox (465, 68, 528, 136, red)
Draw.FillBox (465, 200, 528, 268, red)
Draw.FillBox (465, 332, 528, 400, red)
Draw.FillBox (465, 465, 528, 528, red)
% Draws the black border
Draw.Box (1, 1, maxx - 1, maxy - 1, black)
Draw.Box (2, 2, maxx - 2, maxy - 2, black)
Draw.Box (3, 3, maxx - 3, maxy - 3, black)
Draw.Box (0, 0, maxx, maxy, black)
% Draws the Pieces and tags the spots they are in
Draw.FillOval (35, 35, 30, 30, brightred)
Draw.FillOval (169, 35, 30, 30, brightred)
Draw.FillOval (300, 35, 30, 30, brightred)
Draw.FillOval (433, 35, 30, 30, brightred)
Draw.FillOval (103, 103, 30, 30, brightred)
Draw.FillOval (235, 103, 30, 30, brightred)
Draw.FillOval (366, 103, 30, 30, brightred)
Draw.FillOval (498, 103, 30, 30, brightred)
Draw.FillOval (35, 168, 30, 30, brightred)
Draw.FillOval (169, 168, 30, 30, brightred)
Draw.FillOval (300, 168, 30, 30, brightred)
Draw.FillOval (433, 168, 30, 30, brightred)
Draw.FillOval (103, 366, 30, 30, black)
Draw.FillOval (235, 366, 30, 30, black)
Draw.FillOval (366, 366, 30, 30, black)
Draw.FillOval (498, 366, 30, 30, black)
Draw.FillOval (35, 433, 30, 30, black)
Draw.FillOval (169, 433, 30, 30, black)
Draw.FillOval (300, 433, 30, 30, black)
Draw.FillOval (433, 433, 30, 30, black)
Draw.FillOval (103, 498, 30, 30, black)
Draw.FillOval (235, 498, 30, 30, black)
Draw.FillOval (366, 498, 30, 30, black)
Draw.FillOval (498, 498, 30, 30, black)
% Alows the pieces to be moved
loop
    Mouse.Where (x, y, b)
    if b = 1 and x < 68 and y < 200 and x > 4 and y > 4 and turn = 1 and spot (9) = 1 then
        Draw.FillOval (35, 168, 30, 30, yellow)
        if spot (13) = 0 then
            Draw.Oval (103, 235, 30, 30, yellow)
            move (13) := 1
        end if
        if spot (13) = 1 and spotblack (13) = 1 then
            Draw.Oval (169, 300, 30, 30, yellow)
            movej (13) := 1
        end if
    end if
    if b = 1 and x < 136 and y < 268 and x > 68 and y > 200 and turn = 1 and spot (13) = 0 and move (13) = 1 then
        Draw.FillOval (35, 168, 30, 30, red)
        Draw.FillOval (103, 235, 30, 30, brightred)
        move (13) := 0
        spot (9) := 0
        spot (13) := 1
    end if
    if x > 136 and y > 268 and x < 200 and y < 322 and b = 1 and movej (13) = 1 then
        Draw.FillOval (35, 168, 30, 30, red)
        Draw.FillOval (103, 235, 30, 30, red)
        Draw.FillOval (169, 300, 30, 30, brightred)
        movej (13) := 0
        spot (13) := 0
        spot (9) := 0
        spotblack (13) := 0
        spot (18) := 1
    end if
    if b = 1 and x < 465 and y < 200 and x > 400 and y > 136 and turn = 1 and spot (12) = 1 then
        Draw.FillOval (433, 168, 30, 30, yellow)
        if spot (16) = 0 then
            Draw.Oval (498, 235, 30, 30, yellow)
            move (16) := 1
        end if
        if spot (15) = 0 then
            Draw.Oval (366, 235, 30, 30, yellow)
            move (15) := 1
        end if
    end if
    if x > 332 and y > 200 and x < 400 and y < 268 and b = 1 and move (15) = 1 then
        Draw.FillOval (433, 168, 30, 30, red)
        Draw.FillOval (366, 235, 30, 30, brightred)
        Draw.Oval (498, 235, 30, 30, red)
        spot (15) := 1
        spot (12) := 0
        move (15) := 0
        move (16) := 0
    end if
    if x > 465 and y > 200 and x < 528 and y < 268 and b = 1 and move (16) = 1 then
        Draw.Oval (366, 235, 30, 30, red)
        Draw.FillOval (433, 168, 30, 30, red)
        Draw.FillOval (498, 235, 30, 30, brightred)
        spot (16) := 1
        spot (12) := 0
        move (16) := 0
        move (15) := 0
    end if
end loop




Like i said it takes alot of lines to do very little
Adalias




PostPosted: Wed May 24, 2006 7:25 am   Post subject: (No subject)

OK after some twinking i've figured out most of it but still need a little help
Could somone explain how a 2D array can help me locate what square the mosue is over.
Is it a set of ifs? if so then i already know how to do it
Clayton




PostPosted: Wed May 24, 2006 7:45 am   Post subject: (No subject)

ok heres the thing, look through the Turing Walkthrough, in it is an exhaustive walkthrough on just about any topic you can cover in turing, take a look through it, recommended topics for you :
arrays
flexible arrays
records
Very Happy enjoy Very Happy
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  [ 7 Posts ]
Jump to:   


Style:  
Search: