chicken scratchclasses.dma.ucla.edu/spring11/152bc/wp-content/... · //blink led 3 times break;...

3
//WORDS //word for round String roundWord; //list of words to choose from String myWords[]= { "baste", "bones", "caged", "farms", "fryer", "grade", "inject", "meats", "range","weigh", "grind", "talon", "organ", "beaks", "liver", "gland", "birds", "waste", "hired","wings", "grows", "scald", "broil", "fecal", "crate", "crowd", "price", "dying", "fines", "noise", "hangs", "labor", "volts", "veins", "necks", "baths", "death", "drain", "tanks", "dunks", "dirty", "filth", "layer", "burns", "probe", "pound", "tough", "yield", "rigor", "wages", "cruel", "viral", "dusty", "urine", "rocky"}; //list of char in alphabet char alphabet[]= { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; //GUESSES //declare boolean for whether letter has already been guessed boolean alreadyGuessed = false; //declare boolean for whether button states have changed boolean stateChanged = false; //declare boolean for waiting after button press boolean waiting = false; //track time since button press int inputTime = 0; //create an empty array with 26 spots int guessedLetters[26]; //declare life count int lifeCount = 0; //declare death count int deathCount = 0; //declare guess count int guessCount = 0; //BUTTONS // the numbers of the pushbutton pins const int buttonPin01 = 2; const int buttonPin01 = 3; const int buttonPin01 = 4; const int buttonPin01 = 5; const int buttonPin01 = 6; // variable for reading the pushbutton status int buttonState00 = 0; int buttonState01 = 0; int buttonState02 = 0; int buttonState03 = 0; int buttonState04 = 0; //LEDS // the numbers of the LED pin //Space Number const int ledPin00 = 8; //Death Count const int ledPin01 = 9; //Guessed/Already Guessed const int ledPin02 = 10; //Win/Lose const int ledPin03 = 11; // LED States int ledState00 = LOW; int ledState01 = LOW; int ledState02 = LOW; int ledState03 = LOW; CHiCKEN SCRATCH A DIGITAL HANGMAN GAME NPK BUTTON 1 BUTTON 2 BUTTON 3 BUTTON 4 BUTTON 5 LED SPACE NUMBER LED DEATH COUNT LED GUESSED/ALREADT GUESSED LED WIN/LOSE BUTTON COMBINATION = LETTER CHART

Upload: others

Post on 24-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CHiCKEN SCRATCHclasses.dma.ucla.edu/Spring11/152BC/wp-content/... · //blink led 3 times break; case 4: //blink led 4 times break; case 5: //blink led 5 times break;} //go to next

//WORDS//word for roundString roundWord;//list of words to choose fromString myWords[]= { "baste", "bones", "caged", "farms", "fryer", "grade", "inject", "meats", "range","weigh", "grind", "talon", "organ", "beaks", "liver", "gland", "birds", "waste", "hired","wings", "grows", "scald", "broil", "fecal", "crate", "crowd", "price", "dying", "fines", "noise", "hangs", "labor", "volts", "veins", "necks", "baths", "death", "drain", "tanks", "dunks", "dirty", "filth", "layer", "burns", "probe", "pound", "tough", "yield", "rigor", "wages", "cruel", "viral", "dusty", "urine", "rocky"};//list of char in alphabetchar alphabet[]= { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

//GUESSES//declare boolean for whether letter has already been guessedboolean alreadyGuessed = false;//declare boolean for whether button states have changedboolean stateChanged = false;//declare boolean for waiting after button pressboolean waiting = false;//track time since button pressint inputTime = 0;//create an empty array with 26 spotsint guessedLetters[26];//declare life countint lifeCount = 0;//declare death countint deathCount = 0;//declare guess countint guessCount = 0;

//BUTTONS// the numbers of the pushbutton pinsconst int buttonPin01 = 2;const int buttonPin01 = 3;const int buttonPin01 = 4;const int buttonPin01 = 5;const int buttonPin01 = 6;// variable for reading the pushbutton statusint buttonState00 = 0; int buttonState01 = 0; int buttonState02 = 0; int buttonState03 = 0; int buttonState04 = 0;

//LEDS// the numbers of the LED pin//Space Numberconst int ledPin00 = 8;//Death Countconst int ledPin01 = 9;//Guessed/Already Guessedconst int ledPin02 = 10;//Win/Loseconst int ledPin03 = 11; // LED Statesint ledState00 = LOW;int ledState01 = LOW; int ledState02 = LOW; int ledState03 = LOW;

CHiCKEN SCRATCHA DIGITAL HANGMAN GAME

NPK

BUTTON 1 BUTTON 2 BUTTON 3 BUTTON 4 BUTTON 5

LEDSPACE NUMBER

LEDDEATH COUNT

LEDGUESSED/ALREADT GUESSED

LEDWIN/LOSE

BUTTON COMBINATION = LETTER CHART

Page 2: CHiCKEN SCRATCHclasses.dma.ucla.edu/Spring11/152BC/wp-content/... · //blink led 3 times break; case 4: //blink led 4 times break; case 5: //blink led 5 times break;} //go to next

void setup() {

//WORD randomSeed(analogRead(A5)); //choose a random word from the myWords array int randIndex = (random(55)); roundWord = myWords[randIndex]; //BUTTONS // initialize the pushbutton pin as an input pinMode(buttonPin00, INPUT); pinMode(buttonPin01, INPUT); pinMode(buttonPin02, INPUT); pinMode(buttonPin03, INPUT); pinMode(buttonPin04, INPUT); //LEDS pinMode(ledPin00, OUTPUT); pinMode(ledPin01, OUTPUT); pinMode(ledPin02, OUTPUT); pinMode(ledPin03, OUTPUT); //SERIAL Serial.begin(9600);}

void loop() { //set up the binary system for //BUTTONS //check if the buttons have been pressed for 1 second buttonState00 = digitalRead(buttonPin00); buttonState01 = digitalRead(buttonPin01); buttonState02 = digitalRead(buttonPin02); buttonState03 = digitalRead(buttonPin03); buttonState04 = digitalRead(buttonPin04);

//if buttons have been held down for more than one second, with no button state changes, the combination of the buttons is converted into the appropriate char //check to see if letter has been guessed (search array) alreadyGuessed = false; for(int i = 0; i<26; i++){ if (letter == guessedLetters[i]){ alreadyGuessed = true; } } if(millis() - inputTime > 2000){ waiting = true; }

//if letter has been guessed (if it is in the guessed array) if (alreadyGuessed == true){ //blink ledPin02 twice delay(1000); //blink ledPin02 once }

//else if letter has not been guessed else { //add the guessed letter to the array of guessed letters guessedLetters[guessCount]=letter;

//increment guess count guessCount++;

//check to see if letter is in word //declare index value of letter int letterSpace = roundWord.indexOf(letter);

//if the letter is in the word if (letterSpace > -1){

//increment life count lifeCount++;

//blink ledPin00 a certain number of times to signify the space number switch (letterSpace) { case 1: //blink led 1 time break;

case 2: //blink led 2 times break;

case 3: //blink led 3 times break;

case 4: //blink led 4 times break; case 5: //blink led 5 times break; } //go to next turn; nextTurn(); } else { //if letter is not in word //increment death count deathCount++; //blink ledPin01 to signify death count switch (deathCount) { case 1: //blink led 1 time break;

case 2: //blink led 2 times break;

case 3: //blink led 3 times break;

case 4: //blink led 4 times break;

case 5: //blink led 5 times break;

case 6: //blink led 6 times break;

case 7: //blink led 7 times break;

case 8: //blink led 8 times break;

case 9: //blink led 9 times break;

case 10: //blink led 10 times break; } //go to next turn nextTurn(); } } } }

Page 3: CHiCKEN SCRATCHclasses.dma.ucla.edu/Spring11/152BC/wp-content/... · //blink led 3 times break; case 4: //blink led 4 times break; case 5: //blink led 5 times break;} //go to next

//Define function for how to proceed after turnvoid nextTurn(){ if (deathCount == 12){ //blink ledPin03 twice to signify that the player has lost the game delay(1500); Serial.println("YOU LOSER!!"); resetGame(); } else if (lifeCount == 5){ //blin ledPin03 once to signify that the player has won the game delay(1500); Serial.println("YOU WIN!!"); resetGame(); } else if (deathCount < 12 && lifeCount < 5){ //blink ledPin02 once to signify that the player should guess a letter }}

//Reset Gamevoid resetGame() { int randIndex = (random(55)); roundWord = myWords[randIndex];

//GUESSES //reset to false alreadyGuessed = false; //empty array for (int i= 0; i<26; i++){ guessedLetters[i] = -1; // -1 is a non-letter value placeholder } //reset to 0 lifeCount = 0; //reset to 0 deathCount = 0; //reset to 0 guessCount = 0; //blink ledPin02 once to signify that the player should guess a letter }