Main.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ncurses.h>
  4. #include <chrono>
  5. #include <thread>
  6. #include <time.h>
  7. #include <cstring>
  8. #include "Horse.h"
  9. #include "Race.h"
  10. #define WIDTH 1
  11. #define HEIGHT 1
  12. using namespace std;
  13. int main()
  14. {
  15. // initialize variables
  16. Race race = Race();
  17. WINDOW *menu_win;
  18. int c;
  19. startx = 0;
  20. starty = 0;
  21. menu_win = newwin(HEIGHT, WIDTH, starty, startx);
  22. string cppstr = "";
  23. char *cstr = 0;
  24. // ncurses initialization
  25. initscr(); // open ncurses virtual screen
  26. clear(); // clear the virtual screen
  27. noecho(); // disable input echo
  28. cbreak(); // break for user input
  29. keypad(menu_win, TRUE); // keys are special
  30. curs_set(0); // hide the cursor
  31. // TUI initialization
  32. attron(A_STANDOUT); // highlight top row and keyboard shortcuts
  33. mvprintw(0, 1, " Steve Corya's hrtui 1.0 ");
  34. mvprintw(5, 0, "^B");
  35. mvprintw(5, 20, "^D");
  36. mvprintw(5, 40, "^E");
  37. attroff(A_STANDOUT); // don't highlight the rest
  38. mvprintw(5, 3, "begin race");
  39. mvprintw(5, 23, "delete tallys");
  40. mvprintw(5, 43, "exit");
  41. // _____
  42. cppstr = race.raceToString(); // | / this bit prints the
  43. cstr = new char[cppstr.length() + 1]; // | / race track
  44. strcpy(cstr, cppstr.c_str()); // | /
  45. mvprintw(1, 0, cstr); // |/
  46. refresh();
  47. // main TUI loop
  48. while (1)
  49. {
  50. c = wgetch(menu_win); // get user input
  51. switch (c)
  52. {
  53. case 2: // ^B
  54. mvprintw(7, 0, " "); // clear
  55. srand(time(NULL)); // new random seed every time
  56. race = Race();
  57. while(!race.isFinished()){ // run the race
  58. cppstr = race.raceToString();
  59. cstr = new char[cppstr.length() + 1];
  60. strcpy(cstr, cppstr.c_str());
  61. mvprintw(1, 0, cstr); // output the race
  62. race.step(); // all horses advance...
  63. std::this_thread::sleep_for(std::chrono::milliseconds(85));
  64. refresh(); // print to ncurses
  65. }
  66. race.logWinner(); // output logfile
  67. mvprintw(race.getWinner(), 36, "Horse #%d is the winner!", race.getWinner());
  68. refresh();
  69. break;
  70. case 4: // ^D
  71. if(!remove("winners.log")){ // delete the log file
  72. mvprintw(7, 0, "log file successfully deleted");
  73. }
  74. refresh(); // print success
  75. break;
  76. default: // only three controls
  77. break;
  78. }
  79. if(c == 5){ // exit the TUI loop and the program
  80. break;
  81. }
  82. }
  83. // ____
  84. clrtoeol(); // | / ncurses things
  85. refresh(); // | /
  86. endwin(); // |/
  87. return 0; // the deed is done
  88. }