#include #include #include #include #include #include #include #include #include #include "yeti.h" #define Linux using namespace std; void endCurses(); void startCurses(); int wait(int); int start(bool, bool, double); void story(); bool smessage(int, string); vector turns(int); const char* convert(int); void menu(bool); void endCurses() { if (!isendwin()) endwin(); } void startCurses() { initscr(); cbreak(); noecho(); intrflush(stdscr, false); keypad(stdscr, true); } int wait(int seconds) { int snum; int initial = time(NULL); do { snum = time(NULL) - initial; } while (snum < seconds); } int start(bool immortal, bool story, double escapeturns) { escapeturns = turns((int)escapeturns)[1]; int rate; if (story) { rate = turns((int)escapeturns)[0]; } else { rate = escapeturns; escapeturns = 0; } int x, y; int sizex, sizey; getmaxyx(stdscr,y,x); sizey = y - 3; sizex = x; char input; int yetinum = 1; x = 1; y = 1; vector yetis; yeti yeti1(sizex / 2, sizey / 2, sizex, sizey); yetis.push_back(yeti1); for (int i = 0; i < sizey; ++i) { for (int j = 0; j < sizex; ++j) { addch('.'); } } move(y - 1, x - 1); addch('@'); for (int i = 0; i < yetinum; ++i) { move(yetis[i].ypos() - 1, yetis[i].xpos() - 1); addch('y'); } move(sizey, 0); refresh(); int count = 0; move(sizey + 1, 0); addstr("count: 0"); while (!story || count < escapeturns) { input = getch(); move(y - 1, x - 1); addch('.'); for (int i = 0; i < yetinum; ++i) { move(yetis[i].ypos() - 1, yetis[i].xpos() - 1); addch('.'); } if (input == '4') { --x; } else if (input == '7') { --x; --y; } else if (input == '8') { --y; } else if (input == '9') { ++x; --y; } else if (input == '6') { ++x; } else if (input == '3') { ++x; ++y; } else if (input == '2') { ++y; } else if (input == '1') { ++y; --x; } ++count; // did you go off the side? if (x < 1) { x = sizex; } if (x > sizex) { x = 1; } if (y < 1) { y = sizey; } if (y > sizey) { y = 1; } // begin yeti Ai for(int i = 0; i < yetinum; ++i) { yetis[i].movetowards(x, y, yetis, i); } // End Yeti AI for (int i = 0; i < yetinum; ++i) { if (x == yetis[i].xpos() && y == yetis[i].ypos() && !immortal) { if (story) { return 1; } for (int i = 0; i < yetinum; ++i) { move(yetis[i].ypos() - 1, yetis[i].xpos() - 1); addch('y'); } move(sizey, 0); addstr("You Died!\n"); char buf[100]; for (int j = 0; j < 100; ++j) { buf[j] = ' '; } sprintf(buf, "You survived %d turns\n", count); for(int j = 0; buf[j] != '\n'; ++j) { addch(buf[j]); } addstr("\nDo you want to play again?[y/n] "); refresh(); char playagain = getch(); while (playagain != 'y' && playagain != 'n') { playagain = getch(); } erase(); if (playagain == 'y') { #ifdef Windows system("cls"); #endif return 1; } else { endCurses(); #ifdef Windows system("cls"); #endif cout << "You survived " << count << " turns" << endl; } return 0; } } for (int i = 0; i < yetinum; ++i) { move(yetis[i].ypos() - 1, yetis[i].xpos() - 1); addch('y'); } move(y - 1, x - 1); addch('@'); move(sizey + 1, 7); addstr(" "); move(sizey + 1, 7); addstr(convert(count)); move(sizey, 0); addstr(" "); move(sizey, 0); refresh(); if (count % rate == 0) { move((sizey / 2) - 1,(sizex / 2) - 1); addch('y'); move(sizey, 0); addstr("A New Yeti Has Appeared!"); refresh(); ++yetinum; yetis.push_back(yeti1); } } addstr("\nYou Escaped!"); do { input = getch(); } while (input != '\n'); return 0; } void story() { if (!smessage(1, "One day, you are ambushed by yetis!\n")) { return; } if (!smessage(2, "You escaped, unfortunately, they attack again.\n")) { return; } if (!smessage(3, "Now, you are attacked a third time!\n")) { return; } if (!smessage(4, "There sure are a lot of yetis, because you are attacked again!\n")) { return; } if (!smessage(5, "One last fight.\n")) { return; } addstr("You won!!!!"); getch(); } bool smessage(int num, string message) { char input; int x, y; getmaxyx(stdscr,y,x); const char* printm = message.c_str(); addstr(printm); addstr("You have to survive "); addstr(convert(turns(num)[1])); addstr(" turns\n"); addstr("Press any key to fight them...\n"); refresh(); getch(); erase(); if (start(true, true, num) == 1) { erase(); addstr("You died!"); do { input = getch(); } while (input != '\n'); return false; } erase(); return true; } vector turns(int num) { vector numbers; int x, y; getmaxyx(stdscr,y,x); int turn = num; turn *= 300; turn = turn / 7810. * x * y; num *= 5; int rate = turn / num; numbers.push_back(rate); numbers.push_back(turn); return numbers; } const char* convert(int a) { stringstream convert; string out; convert << a; convert >> out; const char* caout = out.c_str(); return caout; } void menu(bool immortal) { int a = 100; startCurses(); char input; addstr("Welcome to yetihack!\n"); addstr("\t1. Arcade mode (classic)\n"); addstr("\t2. Story mode\n"); addstr("Select an option to proceed: "); refresh(); echo(); input = getch(); noecho(); int rate = 0; switch(input) { case '1': addstr("\nEnter the yeti spawn rate: "); refresh(); echo(); input = '0'; do { a = a + 1; if (input == 7) { int x, y; getyx(stdscr, y, x); move(y, 0); addstr(" "); move(y, 0); addstr("Enter the yeti spawn rate: "); rate /= 10; if (rate != 0) { addstr(convert(rate)); } refresh(); input = getch(); } else { rate *= 10; rate += input - '0'; input = getch(); } } while (input != '\n'); noecho(); erase(); while(start(immortal, false, rate)); break; case '2': erase(); story(); break; } addstr(convert(a)); } int main(int argc, char* argv[]) { /*startCurses(); while (true) { char input = getch(); int a = input; addstr(convert(a)); }*/ bool immortal = false; if (argc > 1 && strcmp(argv[1],"-c") == 0) { immortal = true; } srand(time(0)); menu(immortal); endCurses(); return 0; }