DuckShoot [Uni Yr1]

created:17 Aug 2008 @ 02:02 am
edited:07 Jul 2009 @ 11:13 am
Mark: 97%

Programming in C (UFEEHE-30-1) - Part 1

Computer Systems Engineering - Year 1

The Task:

For this assignment I was handed a sheet containing the following information:

DuckShoot

Your assignment is to implement the DuckShoot program as specified. You should start the program with alternate LEDs lit (1010 1010) on the MARCO RACK, and rotate the bit pattern, inverting LEDs with a trigger up, then down, then up sequence on the bottom switch. Not "machine gunning" implies that the starting with the switch up, down to fire, return to up again sequence has to be repeated in full to fire again. Penalty for leaving the switch down means that unless the switch is returned to up in a given time, the duck reappears to penalize the player...

Deliverables:

The marks shown are the maximum avaliable in each category:

A diagrammatic design of your program using any method with which you are familiar............................... (10)
Source code
Well commented (-5% for C comments)........................................................................................ (10)
Program banner.......................................................................................................................... (5)
Program uses functions............................................................................................................... (5)
Function banners......................................................................................................................... (5)
Functions use arguments.............................................................................................................. (15)
No global variables...................................................................................................................... (10)
Program is laid out in either classic C or Pascal style...................................................................... (10)

Together with the following requirements signed off by my tutor:

Continuously rotating bit pattern on a suitable time base, not using a software delay loop... (5)
Player penalty for leaving switch down (duck reappears)................................................................ (5)
Not machine-gunning............................................................................................................................. (10)
Direction variable by switch within game............................................................................................ (5)
Using 2 switches to implement 4 speeds or levels of difficulty within the game........................ (5)

My Thought Process:

I made this list from the information on the sheet (above):
  1. Starts with alternate LEDs on (1010 1010 - 0xAA)
  2. The bit pattern rotates - use shift and logic tests to test wrap around bits (DUCKS & 0x80) and (DUCKS & 0x01)
  3. Switch down, up, down kills or revives a duck
  4. No machine gunning - need to remember if they have "re-cocked" the gun
  5. All LEDs are lit = Lost, all LEDs out = Won
Then I went into more detail:
  1. Starts with alternate LEDs on
    • The start pattern in binary is 1010 1010 or 0101 0101
    • Therefore in hex it is 0xAA or 0x55
  2. The bit pattern rotates
    • You can use logic functions to test (mask) and set bits
      • To test if a bit is 1 or 0, use the AND function:
        1010 1010 0xAA
        AND 1000 0000 0x80
        = 1000 0000 0x80
        NOTE: Remember, in C, 0 is false ANYTHING else is true!
      • To set a bit to 1, use the OR function:
        0101 0100 0x54
        OR 0000 0001 0x01
        = 0101 0101 0x55
      • To set a bit to 0, use the AND function:
        0101 0101 0x55
        AND 1111 1110 0x01
        = 0101 0100 0x54
    • For shifting left:
      • Test the MSB with DUCKS & 0x80 (logic AND), if it's true remember for later
      • Shift the bits left one space
      • If the test was true, then set the LSB to 1 with DUCKS | 0x01 (logic OR)
    • For shifting right:
      • Test the LSB with DUCKS & 0x01, if it's true remember for later
      • Shift the bits right one space
      • If the test was true, then set the MSB to 1 with DUCKS | 0x80
  3. Switch up, down, up kills or revives a duck
    • To very easily kill or revive a duck, you could just invert the bit!
    • To invert a bit, use the XOR function:
      0101 0101 0x55
      XOR 0000 0001 0x01
      = 0101 0100 0x54
  4. No machine gunning - need to remember if they have "re-cocked" the gun
    • Need a counter that is incremented every time the ducks rotate.
      This will let us determine if we are still on the same go, or have actually moved on.
    • Also we need a variable to let us remember if the trigger was pulled or released last time we looked
    • And a variable to remember if we have already penalised the player

Flowchart:

The next stage is to figure out what happens when. To represent this, I made the following flowchart:

Flowchart

NOTE: For the loop, changing the value of X rather than the delay time will prevent the switches being less responsive for a slower delay.

The Coding Process:

  1. Get COMEDI working
    • Reading & Writing
  2. Make a function to display the ducks on the LEDs
  3. Make a function to rotate the ducks 1 space left
    • Make it to go right as well
    • Make a switch control left / right
  4. Setup the main game loop
  5. Make a switch kill / revive a duck
  6. Make it quit when there are no ducks left
    • Make it quit when all ducks are alive
  7. Make it handle machine gunning
    • Make it only kill one duck for every up down up
    • Modify it so that it will revive 1 duck if the switch is held down for too long
  8. Make 2 switches control the speed giving 4 possible levels of difficulty
  9. Make a switch quit the program
  10. Make it present you with a score when all of the ducks are dead or alive
  11. Figure out 4 suitable playing speeds
  12. Play with curses and make the on screen display

The Code:

Unfortunately I will not provide you with the source code, but feel free to download the compiled binary here