Computer Science Canada Help with a inefficiant Checkers code |
Author: | Adalias [ 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 |
Author: | TheOneTrueGod [ Thu May 18, 2006 5:31 pm ] | ||||||||
Post subject: | |||||||||
Well, the most important thing is that you know math.
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:
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. 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:
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:
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. 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 |
Author: | XeroX [ Thu May 18, 2006 5:37 pm ] |
Post subject: | |
And as for help with pictures, view the tutoral at http://www.compsci.ca/v2/viewtopic.php?t=191 |
Author: | Adalias [ Fri May 19, 2006 3:22 pm ] |
Post subject: | |
how is colour := j mod 2 supposed to colour every other square? |
Author: | Adalias [ Fri May 19, 2006 3:26 pm ] | ||
Post subject: | |||
Oh and btw heres my coding
Like i said it takes alot of lines to do very little |
Author: | Adalias [ Wed May 24, 2006 7:25 am ] |
Post 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 |
Author: | Clayton [ Wed May 24, 2006 7:45 am ] |
Post 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 enjoy |