D. Final Code

Shown below is the final code created and copied to the SD card for the “Ride the COTA!” game to function on the Proteus controller.

#include <FEHLCD.h>
#include <FEHIO.h>
#include <FEHUtility.h>
#include <stdlib.h>
#include <time.h>
#include <FEHBuzzer.h>
int FillDeck(int Deck[52][2]);
int FlipCard(int Deck[52][2]);
char CheckSuit(int index);
int CheckValue(int index);
char CheckColor(int index);
char ChangeToFace(int index);
void Statistics(int Scores[20]);
//CLASS
class Display
{
public:
    void Credits();
    void Rules();
};

int main(void)
{
    int Deck[52][2], Scores[20];
    int fullgame=1, cardcount, game=0,card1value,card1,card2value,card3value,color, win,roundstowin,winnumber=0;
    char card1color,guess1;
    float x,y;

    //clear clear
    LCD.Clear();
    // set backgroud color to scarlet
    LCD.SetBackgroundColor(GRAY);
    //set font color
    LCD.SetFontColor( FEHLCD::Black );
    // print title of game to screen
    LCD.WriteRC("Welcome to Ride the Cota!",7,1);
    Buzzer.Tone( FEHBuzzer::Bf5, 300 );
        Buzzer.Tone( FEHBuzzer::A5,  100 );
        Buzzer.Tone( FEHBuzzer::Bf5, 300 );
        Buzzer.Tone( FEHBuzzer::A5,  100 );
        Buzzer.Tone( FEHBuzzer::Bf5, 200 );
        Buzzer.Tone( FEHBuzzer::A5,  200 );
        Buzzer.Tone( FEHBuzzer::G5,  200 );
        Buzzer.Tone( FEHBuzzer::Bf5, 200 );
        Buzzer.Tone( FEHBuzzer::A5,  300 );
        Buzzer.Tone( FEHBuzzer::Gs5, 100 );
        Buzzer.Tone( FEHBuzzer::A5,  300 );
        Buzzer.Tone( FEHBuzzer::Gs5, 100 );
        Buzzer.Tone( FEHBuzzer::A5,  800 );
        Buzzer.Tone( FEHBuzzer::A5,  300 );
        Buzzer.Tone( FEHBuzzer::Gs5, 100 );
        Buzzer.Tone( FEHBuzzer::A5,  300 );
        Buzzer.Tone( FEHBuzzer::Gs5, 100 );
        Buzzer.Tone( FEHBuzzer::A5,  200 );
        Buzzer.Tone( FEHBuzzer::G5,  200 );
        Buzzer.Tone( FEHBuzzer::F5,  200 );
        Buzzer.Tone( FEHBuzzer::A5,  200 );
        Buzzer.Tone( FEHBuzzer::G5,  300 );
        Buzzer.Tone( FEHBuzzer::A5,  100 );
        Buzzer.Tone( FEHBuzzer::A5,  200 );
        Buzzer.Tone( FEHBuzzer::D5,  200 );
        Buzzer.Tone( FEHBuzzer::F5,  200 );
        Buzzer.Tone( FEHBuzzer::F6,  200 );
        Buzzer.Tone( FEHBuzzer::F6,  200 );
        Buzzer.Tone( FEHBuzzer::F6,  200 );
        Buzzer.Tone( FEHBuzzer::C6,  200 );
        Buzzer.Tone( FEHBuzzer::A5,  200 );
        Buzzer.Tone( FEHBuzzer::G5,  200 );
        Buzzer.Tone( FEHBuzzer::F5,  200 );
        Buzzer.Tone( FEHBuzzer::Bf5, 100 );
        Buzzer.Tone( FEHBuzzer::A5,  200 );
        Buzzer.Tone( FEHBuzzer::G5,  100 );
        Buzzer.Tone( FEHBuzzer::F5,  400 );
        Buzzer.Tone( FEHBuzzer::C6,  200 );
        Buzzer.Tone( FEHBuzzer::A5,  200 );
        Buzzer.Tone( FEHBuzzer::G5,  200 );
        Buzzer.Tone( FEHBuzzer::A5,  200 );
        Buzzer.Tone( FEHBuzzer::Bf5, 100 );
        Buzzer.Tone( FEHBuzzer::A5,  200 );
        Buzzer.Tone( FEHBuzzer::Bf5, 100 );
        Buzzer.Tone( FEHBuzzer::C6,  400 );
        Buzzer.Tone( FEHBuzzer::D6,  400 );
        Buzzer.Tone( FEHBuzzer::D6,  300 );
        Buzzer.Tone( FEHBuzzer::C6,  100 );
        Buzzer.Tone( FEHBuzzer::Bf5, 200 );
        Buzzer.Tone( FEHBuzzer::G5,  200 );
        Buzzer.Tone( FEHBuzzer::F5,  300 );
        Buzzer.Tone( FEHBuzzer::Fs5, 100 );
        Buzzer.Tone( FEHBuzzer::G5,  100 );
        Buzzer.Tone( FEHBuzzer::Bf5, 200 );
        Buzzer.Tone( FEHBuzzer::G5,  100 );
        Buzzer.Tone( FEHBuzzer::Bf5, 200 );
        Buzzer.Tone( FEHBuzzer::A5,  200 );
        Buzzer.Tone( FEHBuzzer::Bf5, 500 );
    //print user option to screen for intro menu
    LCD.WriteRC("Tap the Screen for Menu:",9,1);
    while (!LCD.Touch(&x,&y));
    LCD.Clear();
    //DO WHILE LOOP
do
    {
        int menu=1;
        //WHILE LOOP
        while (menu>0)
        {
            //now show start menu
                        LCD.Clear();
                        LCD.SetFontColor(WHITE);
                        LCD.SetBackgroundColor(SCARLET);
                        LCD.DrawRectangle(10,0,150,120);
                        LCD.WriteAt("Rules",20,60);
                        LCD.SetFontColor(WHITE);
                        LCD.DrawRectangle(160,0,150,120);
                        LCD.WriteAt("Statistics",20,180);
                        LCD.SetFontColor(WHITE);
                        LCD.DrawRectangle(10,120,150,110);
                        LCD.WriteAt("Credits",180,60);
                        LCD.SetFontColor(WHITE);
                        LCD.DrawRectangle(160,120,150,110);
                        LCD.WriteAt("Play Game",180,180);
                        //START MENU
           while (!LCD.Touch(&x,&y));
           LCD.WriteLine(x);
           LCD.WriteLine(y);
                Display Functions;
                //RELATIONAL AND LOGICAL OPERATOR
               if (x<160&&y<120)
               {
                   //rules
                   LCD.WriteAt("rules",x,y);
                   Sleep(1000);
                   Functions.Rules();

               }

               else if (x>160&&y<120)
               {
                   //credits
                   LCD.WriteAt("credits",x,y);
                   Sleep(1000);
                   Functions.Credits();
               }
               else if(x<160&&y>120)
               {
                   //statistics

                   LCD.WriteAt("stats",x,y);
                   Sleep(1000);
                   Statistics(Scores);
               }
               else if (x>160&&y>120)
               {
                   //game
                   game=1;
                   menu=-1;
               }
        }
        //game
        game=1;
        while (game>0)
        {
            //fill deck,start over card count
            roundstowin=0;
            cardcount=0;
            win=-1;
            FillDeck(Deck);

            while(52-cardcount>=4)
            {
                roundstowin++;
                //start display
                LCD.Clear();
                LCD.DrawRectangle(10,0,70,100);
                LCD.DrawRectangle(85,0,70,100);
                LCD.DrawRectangle(160,0,70,100);
                LCD.DrawRectangle(235,0,70,100);
                LCD.SetFontColor(BLACK);
                LCD.DrawRectangle(12,120,150,100);
                LCD.WriteAt("BLACK?",10,160);
                LCD.SetFontColor(BLACK);
                LCD.DrawRectangle(160,120,150,100);
                LCD.WriteAt("RED?",165,160);
                while (!LCD.Touch(&x,&y));
                //check guess;red or black
                    if (x<160&&y>120)
                    {
                        guess1='b';
                        color=-1;
                    }
                    else if (x>160&&y>120)
                    {
                        guess1='r';
                        color=-1;
                    }
                //flip card, store color in card1color and value in card1value
                card1=FlipCard(Deck);
                card1value=CheckValue(card1);
                //add one to counter
                cardcount++;
                //check color
                card1color=CheckColor(card1);
                //check for match
                //if match, move on, if not match, CONTINUE
                if (guess1!=card1color)
                {
                    //display wrong message
                     LCD.WriteAt("Wrong",x,y);
                     Sleep(1500);
                    continue;
                }
                if (guess1==card1color)
                {
                    //display correct
                    LCD.WriteAt("Correct",x,y);
                    Sleep(1500);
                }
                //if facecard, change to character

                //set new screen with old card
                LCD.Clear();
                if (card1value==0||card1value>9)
                {
                    char card1face=ChangeToFace(card1value);
                    LCD.WriteAt(card1face,12,50);
                }
                else
                {
                    card1value++;
                    LCD.WriteAt(card1value,12,50);
                    card2value--;
                }

                LCD.DrawRectangle(10,0,70,100);
                char card1suit=CheckSuit(card1);
                LCD.WriteAt(card1suit,25,50);
                LCD.DrawRectangle(85,0,70,100);
                LCD.DrawRectangle(160,0,70,100);
                LCD.DrawRectangle(235,0,70,100);
                LCD.SetFontColor(BLACK);
                LCD.DrawRectangle(10,120,150,100);
                LCD.WriteAt("OVER?",10,160);
                LCD.SetFontColor(BLACK);
                LCD.DrawRectangle(160,120,150,100);
                LCD.WriteAt("UNDER?",165,160);
                while (!LCD.Touch(&x,&y))
                {

                }
                //ask user for over or under
                if (x<160&&y>120)
                {
                    int ou=1;
                }
                if (x>160&&y>120)
                {
                    int ou=-1;
                }


                //flip card, store value in card2value
                int card2=FlipCard(Deck);
                card2value=CheckValue(card2);

                //add one to counter
                cardcount++;
                //check for over/under based on user choice
                int ou;
                switch (ou)
                {
                    case 1:
                    if (card2value-card1value>0)
                    {
                        LCD.WriteAt("Correct!",x,y);
                        Sleep(1500);
                    }
                    else
                    {
                        LCD.WriteAt("Wrong!",x,y);
                        Sleep(1500);
                        continue;
                    }
                    break;
                    case -1:
                    if (card2value-card1value<0)
                    {
                        LCD.WriteAt("Correct!",x,y);
                        Sleep(1500);
                    }
                    else
                    {
                        LCD.WriteAt("Wrong!",x,y);
                        Sleep(1500);
                        continue;
                    }
                    break;
                }
                //display new screen with new card and olds
                LCD.Clear();

                if (card1value==0||card1value>9)
                {
                    char card1face=ChangeToFace(card1value);
                    LCD.WriteAt(card1face,12,50);
                }
                else
                {
                    card1value++;
                    LCD.WriteAt(card1value,12,50);
                    card1value--;
                }
                card2value=CheckValue(card2);
                if (card2value==0||card2value>9)
                {
                    char card2face=ChangeToFace(card1value);
                    LCD.WriteAt(card2face,90,50);
                }
                else
                {
                    card2value++;
                    LCD.WriteAt(card2value,90,50);
                    card2value--;
                }
                char card2suit=CheckSuit(card2);
                LCD.WriteAt(card1suit,25,50);
                LCD.WriteAt(card2suit,103,50);
                LCD.DrawRectangle(10,0,70,100);

                LCD.DrawRectangle(85,0,70,100);
                LCD.DrawRectangle(160,0,70,100);
                LCD.DrawRectangle(235,0,70,100);
                LCD.SetFontColor(BLACK);
                LCD.DrawRectangle(10,120,150,100);
                LCD.WriteAt("OUTSIDE?",10,160);
                LCD.SetFontColor(BLACK);
                LCD.DrawRectangle(160,120,150,100);
                LCD.WriteAt("IN BETWEEN?",165,160);
                //ask user for outside or in between
                while (!LCD.Touch(&x,&y))
                {

                }
                //add one to counter
                cardcount++;
                //flip card, store value in card 3 value
                int card3=FlipCard(Deck);
                card3value=CheckValue(card3);
                //check if user was correct
                if (x<160&&y>120)
                {
                    if (card3value>card2value&&card3value<card1value)
                    {
                        LCD.WriteAt("Correct!",x,y);
                        Sleep(1.5);
                    }
                    else
                    {
                        LCD.WriteAt("Wrong!",x,y);
                        Sleep(1.5);
                        continue;
                    }

                }
                if (x>160&&y>120)
                {
                    if (card3value<card2value&&card3value>card1value)
                    {
                        LCD.WriteAt("Correct!",x,y);
                        Sleep(1.5);
                    }
                    else
                    {
                        LCD.WriteAt("Wrong!",x,y);
                        Sleep(1.5);
                        continue;
                    }

                }
                //if correct, move on, if incorrect CONTINUE

                //reset screen
                LCD.Clear();
                if (card1value==0||card1value>9)
                {
                    char card1face=ChangeToFace(card1value);
                    LCD.WriteAt(card1face,12,50);
                }
                else
                {
                    card1value++;
                    LCD.WriteAt(card1value,12,50);
                    card1value--;
                }
                if (card2value==0||card2value>9)
                {
                    char card2face=ChangeToFace(card1value);
                    LCD.WriteAt(card2face,90,50);
                }
                else
                {
                    card2value++;
                    LCD.WriteAt(card2value,90,50);
                    card2value--;
                }
                if (card3value==0||card3value>9)
                {
                    char card2face=ChangeToFace(card1value);
                    LCD.WriteAt(card2face,170,50);
                }
                else
                {
                    card3value++;
                    LCD.WriteAt(card3value,170,50);
                    card3value--;
                }
                char card3suit=CheckSuit(card3);
                LCD.WriteAt(card1suit,25,50);
                LCD.WriteAt(card2suit,103,50);
                LCD.WriteAt(card3suit,182,50);
                LCD.DrawRectangle(10,0,70,100);
                LCD.WriteAt(card2suit,103,50);
                LCD.DrawRectangle(85,0,70,100);
                LCD.DrawRectangle(160,0,70,100);
                LCD.DrawRectangle(235,0,70,100);
                LCD.SetFontColor(BLACK);
                LCD.DrawRectangle(10,120,70,100);
                LCD.WriteAt("Spade",10,160);
                LCD.DrawRectangle(85,120,70,100);
                LCD.WriteAt("Club",90,160);
                LCD.DrawRectangle(160,120,70,100);
                LCD.WriteAt("Heart",170,160);
                LCD.DrawRectangle(235,120,70,100);
                LCD.WriteAt("Diamond",238,160);
                while (!LCD.Touch(&x,&y));
                //ask user for suit
                char guess4;
                if (x<80&&y<120)
                {
                    guess4='S';
                }
                if (x>80&&x<155&&y<120)
                {
                    guess4='C';
                }
                if (x>160&&x<230&&y<120)
                {
                    guess4='H';
                }
                if (x>230&&y<120)
                {
                    guess4='D';
                }
                //flip card
                int card4=FlipCard(Deck);
                //add to counter
                cardcount++;
                //check suit, store in card4suit
                char card4suit=CheckSuit(card4);
                //if user correct, break loop,; if incorrect, tell user wrong and start again
                if (guess4==card4suit)
                {
                    LCD.WriteAt("Correct!!",x,y);
                    Sleep(1500);
                    win=1;
                    break;
                }
                else
                {
                    LCD.WriteAt("Wrong!!",x,y);
                    Sleep(1500);
                }

            }//end of single game
            int i;

            //if no win, display loss message
            if (win==-1)
            {
                LCD.Clear();
                LCD.SetBackgroundColor(SCARLET);
                for (i=1;i<=10;i++)
                {
                    LCD.WriteRC("YOU LOSE",2*i,1);
                    Sleep(200);
                }
            }
            if (win==1)
            {
                LCD.Clear();
                LCD.SetBackgroundColor(GREEN);
                for (i=1;i<=10;i++)
                {
                    LCD.WriteRC("YOU WIN!!!",2*i,1);
                    Sleep(200);
                    Scores[winnumber]=roundstowin;
                    winnumber++;

                }
            }

            //else, display winner message

            //ask user what they would like to do now(again,menu)
            LCD.Clear();
            LCD.WriteRC("What would you like to do now?",3,1);
            LCD.DrawRectangle(10,80,150,70);
            LCD.WriteAt("Menu",15,115);
            LCD.SetFontColor(BLACK);
            LCD.DrawRectangle(160,80,150,70);
            LCD.WriteAt("Play Again",165,115);
            LCD.DrawRectangle(12,160,300,70);
            LCD.WriteAt("Quit Game",155,195);

            while (!LCD.Touch(&x,&y));
            if (x<160&&y>80&&y<160)
            {
                //menu
                game=-1;
            }
            if (x>160&&y>80&&y<160)
            {
                //play again
            }
            if (y>160)
            {
                //quit
                game=-1;
                fullgame=-1;
            }

        } //end of loop to play multiple games (game)

        //thank you for playing message,make fullgame negative

    }while (fullgame>0); //end of entire loop containing code (fullgame)

    }
//end of int main




//USER DEFINED FUNCTIONS
int FillDeck(int Deck[52][2])
 {
     int i,j, FillDeck;
     //FOR LOOP
     for(i=0;i<52;i++)
     {
         for (j=0;j<2;j++)
         {
             if (j==0)
             {
             Deck[i][j]=1+i;
             }
             else if (j==1)
             {
             Deck[i][j]=0;
             }
         }
     }
     return 0;
 }
int FlipCard(int Deck[52][2])
{
    int x=1,index,FlipCard;
    while(x>0)
    {
        //random number range
        int range=52;
        //seed with time now
        srand(TimeNow());
        //generate
        index =rand()%range;
        //check if it has been used already
        if (Deck[index][1]==0)
        {
            x=-1;
            Deck[index][1]=1;
        }
    }

    return index;
}

char CheckColor(int index)
{
    if (index<26)
        return 'b';
    if (index>=26)
        return 'r';
}

int CheckValue(int index)
{
    if (index<13)
        return index;
    if (index>12 & index<26)
        return index-13;
    if (index>25 & index<39)
        return index-26;
    if (index>38)
        return index-39;
}

char CheckSuit(int index)
{
    if (index<14)
        return 'S';
    if (index>13 & index<27)
        return 'C';
    if (index>26 & index<40)
        return 'H';
    if (index>39)
        return 'D';
}
void Display::Credits()
{
    int i;
    for (i=0;i<=26;i++)
    {
        LCD.Clear();
        LCD.SetFontColor(WHITE);
        LCD.SetBackgroundColor(SCARLET);
        LCD.WriteRC("Ride the Cota",12-i,1);
        LCD.WriteRC("Creators: ",14-i,1);
        LCD.WriteRC("Jacob Veney ",16-i,1);
        LCD.WriteRC("and Roger Roettger",18-i,1);
        LCD.WriteRC("Consultants: ",20-i,1);
        LCD.WriteRC("Brian Kachala, ",22-i,1);
        LCD.WriteRC("Jenna Losensky",24-i,1);
        LCD.WriteRC("Sean Messerly, and Dr. H",26-i,1);
        Sleep(200);
    }
}
void Display::Rules()
{
    LCD.Clear();
    float x,y;
    LCD.SetFontColor(WHITE);
    LCD.SetBackgroundColor(SCARLET);
    LCD.WriteRC("Rules for 'Ride the Cota':",4,1);
    LCD.WriteRC("The user is given..",5,1);
    LCD.WriteRC("four cards.",6,1);
    while (!LCD.Touch(&x,&y));
    LCD.Clear();
    LCD.SetBackgroundColor(SCARLET);
    LCD.WriteRC("First, user guesses the..",4,1);
    LCD.WriteRC("color of the first card.",5,1);
    while (!LCD.Touch(&x,&y));
    LCD.Clear();
    LCD.SetBackgroundColor(SCARLET);
    LCD.WriteRC("Next, user guesses if the..",4,1);
    LCD.WriteRC("second card's value is ",5,1);
    LCD.WriteRC("over or under the first.",6,1);
    while (!LCD.Touch(&x,&y));
    LCD.Clear();
    LCD.SetBackgroundColor(SCARLET);
    LCD.WriteRC("Then, the user guesses if the..",4,1);
    LCD.WriteRC("third card's value is ",5,1);
    LCD.WriteRC("outside or in...",6,1);
    LCD.WriteRC("between the first ",7,1);
    LCD.WriteRC("two cards.",8,1);
    while (!LCD.Touch(&x,&y));
    LCD.Clear();
    LCD.SetBackgroundColor(SCARLET);
    LCD.WriteRC("Last, the user guesses the..",4,1);
    LCD.WriteRC("suit of the last card.",5,1);
    while (!LCD.Touch(&x,&y));
    LCD.Clear();
    LCD.SetBackgroundColor(SCARLET);
    LCD.WriteRC("If the user guesses ",4,1);
    LCD.WriteRC("incorrectly at any point",5,1);
    LCD.WriteRC("they must start over.",6,1);
    LCD.WriteRC("The user must correctly",7,1);
    LCD.WriteRC("guess 4 cards in ",8,1);
    LCD.WriteRC("a row before the deck is..",7,1);
    LCD.WriteRC("empty (Tap to Exit to Menu)",8,1);
    while (!LCD.Touch(&x,&y));
    LCD.Clear();
}
char ChangeToFace(int index)
{
      switch (index)
    {
           case 0:
           return 'A';
           break;

           case 10:
           return 'J';
           break;

           case 11:
           return 'Q';
           break;

           case 12:
           return 'K';
           break;
    }

}

void Statistics(int Scores[20])
{
        LCD.Clear();
        float x,y;
        int i;
        LCD.SetFontColor(WHITE);
        LCD.SetBackgroundColor(SCARLET);
        LCD.WriteRC("Most Recent Scores",1,1);
        for (i=0;i<20;i++)
        {
            LCD.WriteRC(Scores[i],i+2,1);
        }

        while (!LCD.Touch(&x,&y));
}