| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #include <iostream>
- #include <cstdlib>
- #include <ncurses.h>
- #include <chrono>
- #include <thread>
- #include <time.h>
- #include <cstring>
- #include "Horse.h"
- #include "Race.h"
- #define WIDTH 1
- #define HEIGHT 1
- using namespace std;
- int main()
- {
- // initialize variables
- Race race = Race();
- WINDOW *menu_win;
- int c;
- startx = 0;
- starty = 0;
- menu_win = newwin(HEIGHT, WIDTH, starty, startx);
- string cppstr = "";
- char *cstr = 0;
- // ncurses initialization
- initscr(); // open ncurses virtual screen
- clear(); // clear the virtual screen
- noecho(); // disable input echo
- cbreak(); // break for user input
- keypad(menu_win, TRUE); // keys are special
- curs_set(0); // hide the cursor
- // TUI initialization
- attron(A_STANDOUT); // highlight top row and keyboard shortcuts
- mvprintw(0, 1, " Steve Corya's hrtui 1.0 ");
- mvprintw(5, 0, "^B");
- mvprintw(5, 20, "^D");
- mvprintw(5, 40, "^E");
- attroff(A_STANDOUT); // don't highlight the rest
- mvprintw(5, 3, "begin race");
- mvprintw(5, 23, "delete tallys");
- mvprintw(5, 43, "exit");
- // _____
- cppstr = race.raceToString(); // | / this bit prints the
- cstr = new char[cppstr.length() + 1]; // | / race track
- strcpy(cstr, cppstr.c_str()); // | /
- mvprintw(1, 0, cstr); // |/
- refresh();
- // main TUI loop
- while (1)
- {
- c = wgetch(menu_win); // get user input
- switch (c)
- {
- case 2: // ^B
- mvprintw(7, 0, " "); // clear
- srand(time(NULL)); // new random seed every time
- race = Race();
- while(!race.isFinished()){ // run the race
- cppstr = race.raceToString();
- cstr = new char[cppstr.length() + 1];
- strcpy(cstr, cppstr.c_str());
- mvprintw(1, 0, cstr); // output the race
- race.step(); // all horses advance...
- std::this_thread::sleep_for(std::chrono::milliseconds(85));
- refresh(); // print to ncurses
- }
- race.logWinner(); // output logfile
- mvprintw(race.getWinner(), 36, "Horse #%d is the winner!", race.getWinner());
- refresh();
- break;
- case 4: // ^D
- if(!remove("winners.log")){ // delete the log file
- mvprintw(7, 0, "log file successfully deleted");
- }
- refresh(); // print success
- break;
- default: // only three controls
- break;
- }
- if(c == 5){ // exit the TUI loop and the program
- break;
- }
- }
- // ____
- clrtoeol(); // | / ncurses things
- refresh(); // | /
- endwin(); // |/
- return 0; // the deed is done
- }
|