All pastes #575196 Raw Edit

^DX^

public text v1 · immutable
·published 2007-06-18 22:09 UTC
rendered paste body
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>

#include <stdlib.h>

#include <stdarg.h>
#include <ctype.h>
#include <string.h>

uint8_t switches;
uint8_t randNumber;

uint8_t moves[100];
int level, checkloop, win, gamelength;

static void delay(uint16_t us) 
{
    while(us)
    	us--;
}

void waitForButton(void) {
	
	while(PIND!=0b11111111);
	int waitseed;
	while(PIND==0xFF) {
		waitseed++;
		waitseed=waitseed % 65535;
	}
	srand(waitseed);
}

void playBackGame(uint8_t movearray[], int level) {
	int loop;
	for(loop=0;loop<=level;loop++) {
		PORTB=0b11111111;
		delay(30000);
		PORTB=movearray[loop];
		delay(30000);
		
	}
}
	
void initGame(void) {
	DDRB = 0xff;	//use all pins on PORT B for output
    DDRD = 0x00;    //use all pins on PORT D for input
	PORTB=0b10101010;
	waitForButton();
}


void blinkWin(void) {
	
	for(;;) {
		PORTB=0b10101010;
		delay(30000);
		PORTB=0b01010101;
		delay(30000);
		PORTB=0b10101010;
		delay(30000);
		PORTB=0b01010101;
		delay(30000);
	}

}

void doLose(void) {

	while(PIND!=0b11111100) {
		PORTB=0b11111100;
		delay(30000);
		PORTB=0b11111111;
		delay(30000);
	}

	playGame();
}

int getRand(void) {
	
	randNumber = (uint8_t) rand();    // Get a random number (0 to 255)
	int randMod = randNumber % 8;
	switch (randMod) {

		case 0:
			return 0b11111110;
		case 1:
			return 0b11111101;
		case 2:
			return 0b11111011;
		case 3:
			return 0b11110111;
		case 4:
			return 0b11101111;
		case 5:
			return 0b11011111;
		case 6:
			return 0b10111111;
		case 7:
			return 0b01111111;
		default:
			return 0b01111111;
	}
}

void playGame() {

	initGame();
 	delay(30000);
	level=0;
	for(gamelength=3;gamelength>=0;gamelength--) {
		
		switches=0;
		moves[level]=getRand();
		delay(30000);
		playBackGame(moves, level);

		delay(10000);
		PORTB=0b11111111;

		for(checkloop=0;checkloop<=level;checkloop++) {
			waitForButton();
			switches = PIND;

			delay(5000);
			PORTB=switches;
			delay(5000);
			PORTB=0b11111111;

			while(PIND!=0b11111111);
			if (switches!=moves[checkloop]) doLose();

		}

		level++;
	}
	
	blinkWin();

}

int main(void) {
	
	playGame();
		
	return 0;
}