Simlar to the game Simon Says Code: const int x = 100; int note[x]; int learn[x]
ID: 3825454 • Letter: S
Question
Simlar to the game Simon Says
Code:
const int x = 100;
int note[x];
int learn[x];
int largestindex = 0;
int learnindex=0;
void showsequence();
void readsequnce();
const int PLAY = 1;
const int TRYAGIN = 2;
int state=0;
void loop() {
if (state == PLAY) {
showsequence();
readsequnce();
}
else if (state == TRYAGIN) {
incorrect();
}
MIDI.read();
}
void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
learn[learnindex]=pitch;
learnindex++;
}
void songlearn (int tune) {
if (tune == 1) {
//Bar 1
note[0] = 0x40;
note[1] = 0x3e;
note[2] = 0x3c;
note[3] = 0x3e;
//Bar 2
note[4] = 0x40;
note[5] = 0x40;
note[6] = 0x40;
//Bar 3
note[7] = 0x3e;
note[8] = 0x3e;
note[9] = 0x3e;
//Bar 4
note[10] = 0x40;
note[11] = 0x43;
note[12] = 0x43;
//Bar 5
note[13] = 0x40;
note[14] = 0x3e;
note[15] = 0x3c;
note[16] = 0x3e;
//Bar 6
note[17] = 0x40;
note[18] = 0x40;
note[19] = 0x40;
note[20] = 0x40;
//Bar 7
note[21] = 0x3e;
note[22] = 0x3e;
note[23] = 0x40;
note[24] = 0x3e;
//Bar 8
note[25] = 0x3c;
//End song
}
}
boolean array_cmp(int *a, int *b, int len_a, int len_b){
int n;
// if their lengths are different, return false
if (len_a != len_b) return false;
// test each element to be the same. if not, return false
for (n=0;n<len_a;n++) if (a[n]!=b[n]) return false;
//ok, if we have not returned yet, they are equal :)
return true;
}
void (*SongSeq[x])() =
{ //Bar 1
[] { playNote(0x40, 3, 1, 414); } ,//Quarter note: E
[] { playNote(0x3e, 2, 4, 414); } ,//Quarter note: D
[] { playNote(0x3c, 2, 2, 414); } ,//Quarter note: C
[] { playNote(0x3e, 2, 4, 414); } ,//Quarter note: D
//Bar 2
[] {playNote(0x40, 3, 1, 414, 1);} , //Quarter note: E
[] {playNote(0x40, 3, 1, 414, 1); }, //Quarter note: E
[] {playNote(0x40, 3, 1, 828, 1); }, //Half note: E
//Bar 3
[]{playNote(0x3e, 2, 4, 414, 1);}, //Quarter note: D
[]{playNote(0x3e, 2, 4, 414, 1);}, //Quarter note: D
[]{playNote(0x3e, 2, 4, 828, 1);}, //Half note: D
//Bar 4
[]{playNote(0x40, 3, 1, 414);}, //Quarter note: E
[]{playNote(0x43, 3, 4, 414, 1);}, //Quarter note: G
[]{playNote(0x43, 3, 4, 828, 1);}, //Half note: G
//Bar 5
[]{playNote(0x40, 3, 1, 414);}, //Quarter note: E
[]{playNote(0x3e, 2, 4, 414);}, //Quarter note: D
[]{playNote(0x3c, 2, 2, 414);}, //Quarter note: C
[]{playNote(0x3e, 2, 4, 414);}, //Quarter note: D
//Bar 6
[]{playNote(0x40, 3, 1, 414, 1);}, //Quarter note: E
[]{playNote(0x40, 3, 1, 414, 1);}, //Quarter note: E
[]{playNote(0x40, 3, 1, 414, 1);}, //Quarter note: E
[]{playNote(0x40, 3, 1, 414, 1);}, //Quarter note: E
//Bar 7
[]{playNote(0x3e, 2, 4, 414, 1);}, //Quarter note: D
[]{playNote(0x3e, 2, 4, 414, 1);}, //Quarter note: D
[]{playNote(0x40, 3, 1, 414);}, //Quarter note: E
[]{playNote(0x3e, 2, 4, 414);}, //Quarter note: D
//Bar 8
[]{playNote(0x3c, 2, 2, 1656);}, //Whole note: C
//End song
};
void showsequence() {
//Add a new index to the end of the songseq
SongSeq[largestindex];
largestindex++;
//loop through the songseq
for (int index = 0; index < largestindex; index++) {
SongSeq[index]();
}
}
void readsequnce() {
static uint16_t nextkey;
bool mademistake = false;
nextkey=0;
learnindex=0;
while(learn[largestindex]==0){
MIDI.read();
if (learn[largestindex]!=0){
break;
}
if (array_cmp(learn, note, largestindex, largestindex) == true){
// do this if they are equal
lcd.setCursor(16, 2);
lcd.print("True");
}else{
// do this if they are different
lcd.setCursor(16, 2);
lcd.print("False");
}
}
Explanation / Answer
#ifndef CR_SIMON_SAYS_HPP
#define CR_SIMON_SAYS_HPP
#include "SDL.h"
#include <vector>
#include <random>
namespace cr
{
class SimonSaysGame
{
public:
SimonSaysGame() = default;
~SimonSaysGame();
SimonSaysGame(const SimonSaysGame &) = delete;
SimonSaysGame & operator = (const SimonSaysGame &) = delete;
bool initSDL();
void runGameLoop();
private:
enum class ColorIndex
{
Green, Red, Yellow, Blue,
Count, Invalid = Count
};
void checkAppEvents();
void checkFailCondition();
void renderGameBoard();
void newRandomColors();
void drawColoredSquare(ColorIndex colorIdx, bool halfTone);
std::vector<ColorIndex> colorsThisTurn;
std::vector<ColorIndex> userInputColors;
ColorIndex lastUserColor = ColorIndex::Invalid;
unsigned int numColorsNextTurn = 2;
unsigned int numColorsLeftToDisplay = 0;
std::mt19937 randGenerator{ std::random_device()() };
std::uniform_int_distribution<int> randDist{ 0, static_cast<int>(ColorIndex::Count) - 1 };
SDL_Window * window = nullptr;
SDL_Renderer * renderer = nullptr;
bool lostGame = false;
bool isDone = false;
};
struct renderer_delete
{
void operator()(SDL_Renderer* renderer) const
{
if (renderer != nullptr)
{
SDL_DestroyRenderer(renderer);
}
}
};
using renderer_ptr = std::unique_ptr<SDL_Renderer, renderer_delete>;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.