Advertisement
Redxone

[C++] Testing system!

Oct 29th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include "computercraft.h"
  4. #include <conio.h>
  5. Term *term = new Term;
  6. Color *colors = new Color;
  7. Paintutils *paintutils = new Paintutils();
  8. using namespace std;
  9.  
  10. int qNum = 1;
  11. bool testing = true;
  12. int numQ = 8;
  13. int score = 0;
  14. int passamm = 80;
  15. int Answers[8] = {3,1,3,1,2,4,3,3};
  16. char qlookup[4] = {'A','B','C','D'};
  17. string Questions[8][7] = {
  18.     {"How does a resistor, resist current?","By tightening the electron flow. ","By carrying an opposing field. ","By lowering the electron energy state. ","By drawing electrons to GND."},
  19.     {"Which direction do electrons flow from? ", "GND", "VCC", "LEFT", "RIGHT"},
  20.     {"How does an LED emit light?", "By heating up super hot. ", "By pulling light from the air", "By dropping electron energy states. ", "By conducting photons."},
  21.     {"How much energy do electrons have at their \n   GND state?","None","7 Joules", "18 Joules","5 volts"},
  22.     {"Which of the fallowing can a Transistor be doped \n   with ","NNP","PNP","PPP","NNN"},
  23.     {"How are operation code's usually processed when \n   making an 8bit CPU? ","0x173F","1101-0000","Its not processed in the CPU. ","Instruction in high nibble, operand in low."},
  24.     {"Which popular IC is often used for a clock","74LS245","74HCS04","NE555","Timer456"},
  25.     {"I turned on my shift register and all \n   the bits are high!, what should i \n   do?","Turn if off then on again. ","Buy a new shift register. ","Tie the inputs to a pulldown resistor. ","Block the inputs from GND. "},
  26. };
  27. int points[8] = {10,10,15,10,10,15,10,5};
  28.  
  29. void tout(string txt)
  30. {
  31.    int px = term->printx;
  32.    int py = term->printy;
  33.    term->setCursorPos(px,py);
  34.    cout << txt << endl;
  35.    term->printy++;
  36. }
  37.  
  38. void drawQuestion(int number)
  39. {
  40.     number--;
  41.     paintutils->drawBoxFilled(1,1,50,20,7,7);
  42.     paintutils->drawBoxFilled(1,1,50,1,colors->blue,colors->blue);
  43.     term->setCursorPos(1,1);
  44.     term->setTextColor(colors->white);
  45.     term->setBackgroundColor(colors->blue);
  46.     cout << "(" << number+1 << ".  " << "Question worth - " << points[number] << "pts. " << endl;
  47.     term->setCursorPos(42,1);
  48.     cout << score << "/" << passamm << "pts";
  49.     term->setTextColor(colors->black);
  50.     term->setBackgroundColor(colors->lightGray);
  51.     term->setCursorPos(3,3);
  52.     cout << Questions[number][0] << endl;
  53.     paintutils->drawBox(0,0,51,21,0);
  54.     term->setTextColor(colors->black);
  55.     term->setBackgroundColor(colors->lightGray);
  56.     term->setCursorPos(4,7);
  57.     tout("(A. " + Questions[number][1] );
  58.     tout("(B. " + Questions[number][2] );
  59.     tout("(C. " + Questions[number][3] );
  60.     tout("(D. " + Questions[number][4] );
  61. }
  62.  
  63. void flashselection(int sel)
  64. {
  65.     for (int i = 0; i < 2; i++)
  66.     {
  67.         term->setTextColor(colors->black);
  68.         term->setBackgroundColor(colors->yellow);
  69.         term->setCursorPos(4,6+sel);
  70.         cout << "(" << qlookup[sel-1] << ". " << Questions[qNum-1][sel] << endl;
  71.         Sleep(200);
  72.         term->setTextColor(colors->white);
  73.         term->setBackgroundColor(colors->lightGray);
  74.         term->setCursorPos(4,6+sel);
  75.         cout << "(" << qlookup[sel-1] << ". " << Questions[qNum-1][sel] << endl;
  76.         Sleep(200);
  77.     }
  78.  
  79. }
  80.  
  81. void getTestResults()
  82. {
  83.     testing=false;
  84.     paintutils->drawBoxFilled(1,1,50,20,7,7);
  85.     paintutils->drawBoxFilled(1,1,50,1,colors->blue,colors->blue);
  86.     term->setCursorPos(1,1);
  87.     term->setTextColor(colors->white);
  88.     term->setBackgroundColor(colors->blue);
  89.     cout << "Test Results. " << endl;
  90.     term->setTextColor(colors->black);
  91.     term->setBackgroundColor(colors->lightGray);
  92.     term->setCursorPos(3,3);
  93.     cout << "End of test results. " << endl;
  94.     paintutils->drawBox(0,0,51,21,0);
  95.     term->setTextColor(colors->black);
  96.     term->setBackgroundColor(colors->lightGray);
  97.     term->setCursorPos(4,7);
  98.     cout << "Score: " << score << "/" << passamm << "pts";
  99.     term->setCursorPos(4,8);
  100.     cout << "Required passing grade: " << passamm;
  101.     term->setCursorPos(4,9);
  102.     tout(" ");
  103.     term->setCursorPos(4,10);
  104.     cout << "Status: ";
  105.     if(score >= passamm)
  106.     {
  107.         term->setTextColor(colors->green);
  108.         cout << "Passed! ";
  109.     }
  110.     else
  111.     {
  112.         term->setTextColor(colors->red);
  113.         cout << "Failed. ";
  114.     }
  115.     term->setCursorPos(18,13);
  116.     cout << "Press any key. " << endl;
  117.     getch();
  118. }
  119.  
  120. void nextQuestion()
  121. {
  122.     term->setBackgroundColor(colors->black);
  123.     term->Clear();
  124.     term->setCursorPos(1,1);
  125.     drawQuestion(qNum);
  126. }
  127.  
  128.  
  129. bool checkAnswer(int q, int ans)
  130. {
  131.     flashselection(ans);
  132.     bool corr = false;
  133.     // Checks the users answer and displays the correct one.
  134.     if(ans == Answers[q-1]) // Minus 1 because of table indexing at 0
  135.     {
  136.         corr = true;
  137.         score += points[q-1];
  138.     }
  139.     if(!corr)
  140.     {
  141.         // Display answer as incorrect.
  142.         term->setTextColor(colors->white);
  143.         term->setBackgroundColor(colors->red);
  144.         term->setCursorPos(4,6+ans);
  145.         cout << "(" << qlookup[ans-1] << ". " << Questions[q-1][ans] << endl;
  146.     }
  147.             // Display answers as correct.
  148.         term->setTextColor(colors->white);
  149.         term->setBackgroundColor(colors->green);
  150.         term->setCursorPos(4,6+Answers[q-1]);
  151.         cout << "(" << qlookup[Answers[q-1]-1] << ". " + Questions[q-1][Answers[q-1]] << endl;
  152.         qNum++;
  153.         if(qNum > numQ)
  154.         {
  155.             getTestResults();
  156.         }
  157.         else
  158.         {
  159.             term->setTextColor(colors->white);
  160.             term->setBackgroundColor(colors->lightGray);
  161.             term->setCursorPos(18,13);
  162.             cout << "Press any key. " << endl;
  163.             getch();
  164.             nextQuestion();
  165.         }
  166.     return corr;
  167. }
  168.  
  169. bool getInput()
  170. {
  171.     // Module will later be used to take key input.
  172.     term->setCursorBlink(false);
  173.  
  174.     char inp = getch();
  175.     inp = toupper(inp);
  176.     if(inp == 'A') return checkAnswer(qNum,1);
  177.     if(inp == 'B') return checkAnswer(qNum,2);
  178.     if(inp == 'C') return checkAnswer(qNum,3);
  179.     if(inp == 'D') return checkAnswer(qNum,4);
  180.     return getInput();
  181.     term->setCursorBlink(true);
  182. }
  183.  
  184.  
  185.  
  186. void exittesting()
  187. {
  188.     term->setTextColor(colors->white);
  189.     term->setBackgroundColor(colors->black);
  190.     term->setCursorPos(1,22);
  191.     cout << "Thank you for using Lewisk Testing, this program will now exit. " << endl;
  192.     cout << "                      Press any key. " << endl;
  193.     term->setTextColor(colors->black);
  194.     term->setBackgroundColor(colors->black);
  195.  
  196. }
  197.  
  198.  
  199. int main()
  200. {
  201.     paintutils->initialize(term);
  202.     term->Clear();
  203.     term->setCursorPos(1,1);
  204.     drawQuestion(qNum);
  205.     while(testing)
  206.     {
  207.         getInput();
  208.     }
  209.     exittesting();
  210.     return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement