Computer Science Canada

Rudimentary turing gmae

Author:  TipsyCzar [ Thu Nov 10, 2016 12:18 pm ]
Post subject:  Rudimentary turing gmae

This is a game I made for a computer science class. It's supposed to take the base principal of Duck hunt (use mouse and graphics VS just text). Tell me what you think of it and what needs work. If possible, look at the code and see if the indent needs work.

Author:  Insectoid [ Fri Nov 11, 2016 9:52 am ]
Post subject:  RE:Rudimentary turing gmae

It's funny you asked about the indent because it definitely needs work. That's some crazy unnecessary nesting you've got going on there. Fortunately it's an easy fix. Load up Turing, open your file, and press F2. There, fixed. The automatic indenting will give you the proper indentation. There's no need to come up with your own complicated system. If everyone uses the auto-indent, then everyone's indentation is the same, and everyone can easily read each others' code.

Now that we can read this file, take a look at your variable declarations:
code:
var delay1 : int
delay1 := 3000
var delay2 : int
delay2 := 2800
var delay3 : int
delay3 := 2600
var delay4 : int
delay4 := 2400
var delay5 : int
delay5 := 2200
var delay6 : int
delay6 := 2000
var delay7 : int
delay7 := 1800
var delay8 : int
delay8 := 1600
var delay9 : int
delay9 := 1400
var delay10 : int
delay10 := 1200
var delay11 : int
delay11 := 1000
var delay12 : int
delay12 := 800
var delay13 : int
delay13 := 600
var delay14 : int
delay14 := 400


Holy moly, what a mess. Fourteen variables! What the heck do you need so many for? I'm seeing a pattern here- maybe we can come up with a mathematical function that just gives us the value we want. Can you think of a formula f(n) where f(1) = 3000, f(2) = 2800, f(2) = 2600, etc? Doing this will save you ~40 lines of code.

Now let's look at this part:
code:
var x1 : int
var x2 : int
var x3 : int
var x4 : int
var x5 : int
var x6 : int
var x7 : int
var x8 : int
var x9 : int
var x10 : int
var x11 : int
var x12 : int
var x13 : int
var x14 : int
var y1 : int
var y2 : int
var y3 : int
var y4 : int
var y5 : int
var y6 : int
var y7 : int
var y8 : int
var y9 : int
var y10 : int
var y11 : int
var y12 : int
var y13 : int
var y14 : int


Holy smokes, fourteen sets of coordinates! Why? There's only one box on the screen at a time! Why do we need to keep track of fourteen things?

Instead of having 14 boxes that pop up and disappear, why not just have one box that moves around? Instead of 14 sets of coordinates, just have one set of coordinates and change it fourteen times. Doing this will save you ~50 lines of code.

And now look at this:
code:
    drawfillbox (0, 0, maxx, maxy, black)
    %box #1
    drawfillbox (x1, y1, x1 + 50, y1 + 50, red)
    put "Score : " ..
    put score
    delay (delay1)
    mousewhere (x, y, b)
    if x <= x1 + 50 and x >= x1 and y <= y1 + 50 and y >= y1 then
        score := score + 50
        fork correct
    else
        fork incorrect
    end if
    cls


You repeat this code 14 times and the only things that change are x1, y1 and delay1. But you just changed your code so there's only one set of x and y coordinates and only one delay right? And if those are the only things that changed, then we now have 14 identical pieces of code! And if we have 14 identical pieces of code, then we can just delete 13 of them and throw the other one in a loop! This saves us a whopping ~180 lines of code!

You posted ~360 lines of code, not including blank lines. We're knocking out ~270 of them, which leaves us with 90 lines. 360, to 90. That's a 75% reduction in code size, and a 100% increase in readability, just by eliminating repeated code.

Repeated code is generally not a good thing. Makes things messy and hard to edit. Any time you see code that looks the same or nearly the same, see if you can eliminate all the extras and bring it down to just one copy. This will often massively reduce the size of your file and make it much, much easier to edit and debug.


: