/* FLASH CARD STUDY THING PROGRAM BY: ANDY STONE 8-27-03 */ //Include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; //Type struct FC_Card { string sTitle; string sQuestion; string sAnswer; int iFlags; FC_Card() { iFlags = 1; } }; //Vars bool gfQuit; //True when the program should exit. bool gfWin; //True when there are no cards left //... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... /* Read in a flash cards data file. File format: %num of cards% "card title" "Card Q" "Card A" */ void ReadCardsIn(string sFile, vector* plCards) { //Vars int iTotalCards = 0; int iCurrentCard = -1; //Start at -1 so the first card read in is 0 ifstream oFile; //Clear out all old data plCards->resize(0); //Open up the flash card file oFile.open(sFile.c_str()); //Read in the # of cards oFile >> iTotalCards; plCards->resize(iTotalCards); //Keep reading in new data until we reach the end of the file for(int i = 0; i < iTotalCards; i++) { //Make a new card, Read in "Card Title" "Card Q" "Card A" (*plCards)[i].sTitle = ReadString(oFile); (*plCards)[i].sQuestion = ReadString(oFile); (*plCards)[i].sAnswer = ReadString(oFile); } //CLose the file oFile.close(); } //... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... /* Randomly pick a card */ int RandomCard(vector* plCards) { //Vars double dRand; ASFC_LinkedList lCardMap; //Create a map of cards so that one card exists for each card flagged for(int i = 0; i < plCards->size(); i++) { //Create a card for each flag for(int j = 0; j < (*plCards)[i].iFlags; j++) { lCardMap.Push(i); } } //If there are no cards left if(lCardMap.Length() == 0) { gfWin = true; return -1; } //Pick a random card dRand = ((double)rand() / (double)(RAND_MAX+1)); return(lCardMap[dRand * lCardMap.Length()]); } //... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... /* Flags a card */ void FlagCard(vector* plCards, int iCardPicked, ASFC_Console* poCon, ASFC_Screen* poScreen) { //Vars ASFC_Input oInput; //Flag the card once (*plCards)[iCardPicked].iFlags++; //Say the card was flagged (*poCon) << "\n\n\n"; (*poCon) << "Card has been flagged: " << (*plCards)[iCardPicked].iFlags << " times.\n\n"; (*poCon) << "Press space to continue...\n"; poCon->Draw(0xFF); poScreen->Update(); //Pause while(oInput.KeyUp(SDLK_SPACE)) oInput.Update(); while(oInput.KeyDown(SDLK_SPACE)) oInput.Update(); } //... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... /* Flags a card */ void DeFlagCard(vector* plCards, int iCardPicked, ASFC_Console* poCon, ASFC_Screen* poScreen) { //Vars ASFC_Input oInput; //Flag the card once (*plCards)[iCardPicked].iFlags--; //Say the card was flagged (*poCon) << "\n\n\n"; (*poCon) << "Card has been flagged: " << (*plCards)[iCardPicked].iFlags << " times.\n\n"; (*poCon) << "Press space to continue...\n"; poCon->Draw(0xFF); poScreen->Update(); //Pause while(oInput.KeyUp(SDLK_SPACE)) oInput.Update(); while(oInput.KeyDown(SDLK_SPACE)) oInput.Update(); } //... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... /* Randomly pick a card and ask its question. Wait for the user to press space, then reveal the answer. */ void AskQuestion(vector* plCards, ASFC_Console* poCon, ASFC_Screen* poScreen) { //Vars int iCardPicked; double dRand; ASFC_Input oInput; SDLKey iKey; bool fAskCard = true; int iR, iG, iB; //Pick a random card iCardPicked = RandomCard(plCards); //If we've won stop if(gfWin) { (*poCon) << "\n\n\n You Win!!!"; poCon->Draw(0xFF); poScreen->Update(); gfQuit = true; //Random rects while(oInput.KeyUp(SDLK_SPACE)) { dRand = ((double)rand() / (double)(RAND_MAX+1)); iR = dRand * 255; dRand = ((double)rand() / (double)(RAND_MAX+1)); iB = dRand * 255; dRand = ((double)rand() / (double)(RAND_MAX+1)); iG = dRand * 255; poScreen->DrawFillRectangle(0, 0, 640, 480, iR, iG, iB, 0xFF); poScreen->Update(); oInput.Update(); } return; } //Keep looping until we've specifically asked to stop seeing this card while(fAskCard) { //By default don't ask again fAskCard = false; //Clear the screen poCon->Clear(); //Display card stats (*poCon) << "Card: " << iCardPicked << " Flagged: " << Val((*plCards)[iCardPicked].iFlags) << "\n\n"; //Now ask the question (*poCon) << (*plCards)[iCardPicked].sTitle << "\n"; (*poCon) << (*plCards)[iCardPicked].sQuestion << "\n"; (*poCon) << "\n"; poCon->Draw(0xFF); poScreen->DrawFillRectangle(0, 0, 640, 14, COLOR_RED, 55); poScreen->DrawLine(0, 15, 639, 15, COLOR_WHITE, 0xFF); poScreen->Update(); //Pause iKey = SDLKey(-1); while(iKey == -1) { oInput.Update(); if(oInput.KeyDown(SDLK_SPACE)) {iKey = SDLK_SPACE;} if(oInput.KeyDown(SDLK_ESCAPE)) {iKey = SDLK_ESCAPE; gfQuit = true;} } while(oInput.KeyDown(iKey)) oInput.Update(); //Reveal answer (*poCon) << "ANSWER: "; (*poCon) << (*plCards)[iCardPicked].sAnswer << "\n"; poCon->Draw(0xFF); poScreen->DrawFillRectangle(0, 0, 640, 14, COLOR_RED, 55); poScreen->DrawLine(0, 15, 639, 15, COLOR_WHITE, 0xFF); poScreen->Update(); //Pause (if not exitting) if(iKey != SDLK_ESCAPE) { iKey = SDLKey(-1); while(iKey == -1) { oInput.Update(); if(oInput.KeyDown(SDLK_SPACE)) {iKey = SDLK_SPACE;} if(oInput.KeyDown(SDLK_ESCAPE)) {iKey = SDLK_ESCAPE; gfQuit = true;} if(oInput.KeyDown(SDLK_r)) {iKey = SDLK_r; fAskCard = true;} if(oInput.KeyDown(SDLK_f)) {iKey = SDLK_f; FlagCard(plCards, iCardPicked, poCon, poScreen);} if(oInput.KeyDown(SDLK_d)) {iKey = SDLK_d; DeFlagCard(plCards, iCardPicked, poCon, poScreen);} } while(oInput.KeyDown(iKey)) oInput.Update(); } } } //... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... /* Print flashcards instructions into console poCon */ void PrintInsturctions(ASFC_Console* poCon, string sInstructionsFile) { //Vars /* TE_Text oText; TE_TextEditor oTextEditor; //Set up the text editor oTextEditor.SetConsole(poCon); //Now view the text oText.Load(sInstructionsFile); oTextEditor.ViewText(&oText); */ } //... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... /* Lists all cards from card iCardNum */ void ListCards(vector* plCards, ASFC_Console* poCon, ASFC_Screen* poScreen) { //Loop all cards (or as many will fit on the screen) /* for(int i = 0; i < plCards->size() > poCon->Height() ? plCards->Size() : poCon->Height() - 1; i++) { (*poCon) << i << ": " << (*plCards)[i] << endl; } poCon->Draw(0xFF); poScreen->Update(); ASFC_Input oInput; while(oInput.KeyUp(SDLK_SPACE)) oInput.Update(); */ } //... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... int main(int argc, char *argv[]) { //Const #define __INSTRUCTIONS_FILE "help.txt" //Vars vector lCards; time_t oSeconds; string sCards = "##ERR#"; //Set it up so we don't quit gfQuit = false; gfWin = false; //Set up the screen ASFC_Screen screen(640, 480, 16, true); SDL_WM_SetCaption("Flash Cards", "Flash Cards"); //Setup the console ASFC_Font oConFont("normal_console_font.png", 8, 14, COLOR_BLUE); ASFC_FontPalette myoFonts; myoFonts.Push(oConFont); ASFC_Console oCon; oCon.SetFontPalette(&myoFonts); oCon.SetSurface(&screen); oCon.SetConsolePosition(0, 0); oCon.SetViewport(0, 0); oCon.SetViewportSize(80, 29); oCon.SetConsoleSize (80, 29); //Randomize oSeconds = time(NULL); srand(oSeconds); //Figure out what stack to read in while(!FileExist(sCards) && !gfQuit) { oCon.Clear(); oCon << "Welcome to Flashcards\n"; oCon << "By: Andy Stone."; oCon << "Type '?' for a list of instructions or enter a file name.\n\n"; oCon << "What stack would you like to read in?\n"; oCon >> sCards; //If the user wants instructions print them out if(sCards == "?" || sCards == "'?'") PrintInsturctions(&oCon, __INSTRUCTIONS_FILE); if(UpperString(sCards) == "QUIT" || UpperString(sCards) == "EXIT") gfQuit = true; } //Clear the screen screen.DrawFillRectangle(0, 0, screen.GetWidth(), screen.GetHeight(), COLOR_BLACK, 0xFF); //Read in the card data (if we're not quitting) if(!gfQuit) ReadCardsIn(sCards, &lCards); //Loop forever asking questions while(!gfQuit) { AskQuestion(&lCards, &oCon, &screen); } return 0; }