// compilation command: // g++ fireballs.cpp -o fireballs.o -lncurses -lpthread #include #include #include // move() is here #include // usleep() is here #include // abs() is here #include // strlen() is here #include // pthread stuffs typedef long int LONG; typedef short int SHORT; void LimitScreen(); void ShowInfo(); void ShowLoser(); void* FireThread(void*); void* MainThread(void*); void* BrainThread(void*); WINDOW *window; bool quit = true; bool loser; //true for brain and false for human #define row 30 #define col 70 #define sleep_time 12000 #define fire_time 80000 #define shield_length 9 #define ball_count 8 // this should be even for working better #define aggressive_level 6 //********************************************************************************************** class FireBall { public: SHORT x; SHORT y; SHORT move_count; bool busy; FireBall() { this->x = -1; this->y = -1; busy = false; } }fireball[ball_count]; //********************************************************************************************** class Player { public: int x; int y; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Player(){} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void Initialize(bool is_brain) { if(is_brain) { this->x = 0; this->y = 1; } else { this->x = row - 1; this->y = 1; } } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~Player() { //loser player can be shown here return; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void DrawShield() { move(this->x,this->y); int ceil = this->x + shield_length; for(SHORT i=this->x;iy < col - shield_length) { move(this->x,this->y); printw(" "); this->y++; } } else { if(this->y > 1) { move(this->x,this->y + shield_length - 1); printw(" "); this->y--; } } } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void Fire(bool is_brain) { if(is_brain) { for(SHORT i=ball_count / 2;ix; fireball[i].y = this->y + 4; fireball[i].move_count = row + 1; fireball[i].busy = true; break; } } } else { for(SHORT i=0;ix; fireball[i].y = this->y + 4; fireball[i].move_count = row + 1; fireball[i].busy = true; break; } } } } }human,brain; //*********************************************************************************************************** //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //*********************************************************************************************************** void LimitScreen() { for(SHORT i=0;i 1)) direction = true; } if(shot_count++ == aggressive_level) shot_count = 0; usleep(fire_time); } } //********************************************************************************************** void *FireThread(void* hamintori_alaki) { while(quit) { // brain fireballs for(SHORT i=ball_count / 2;i 0) { move(fireball[i].x - 1,fireball[i].y); printw(" "); move(fireball[i].x,fireball[i].y); } printw("X"); fireball[i].x++; fireball[i].move_count--; if(fireball[i].move_count == 0) fireball[i].busy = false; } } // human fireballs for(SHORT i=0;i 0) { move(fireball[i].x + 1,fireball[i].y); printw(" "); move(fireball[i].x,fireball[i].y); } printw("X"); fireball[i].x--; fireball[i].move_count--; if(fireball[i].move_count == 0) fireball[i].busy = false; } } // check lose conditions for(SHORT i=ball_count / 2;i= human.y && (fireball[i].y < human.y + shield_length)) { quit = false; loser = false; break; } } for(SHORT i=0;i= brain.y && (fireball[i].y < brain.y + shield_length)) { quit = false; loser = true; break; } } move(row,col); refresh(); usleep(fire_time); } } //********************************************************************************************** void* MainThread(void* hamintori_alaki) { brain.Initialize(true); human.Initialize(false); window = initscr(); clear(); LimitScreen(); ShowInfo(); refresh(); nodelay(window,TRUE); noecho(); char ch; while(quit) { brain.DrawShield(); human.DrawShield(); move(row,col); ch = getch(); switch(ch) { case 'd': case 'D': human.Move(true); break; case 'a': case 'A': human.Move(false); break; case 's': case 'S': human.Fire(false); break; case 'q': case 'Q': quit = false; } //move(row,col); //refresh(); usleep(fire_time); } } //********************************************************************************************** int main(int argc, char *argv[]) { pthread_t main_thread,fire_thread,brain_thread; pthread_create(&main_thread,NULL,MainThread,NULL); pthread_create(&fire_thread,NULL,FireThread,NULL); pthread_create(&brain_thread,NULL,BrainThread,NULL); pthread_join(main_thread,NULL); pthread_join(fire_thread,NULL); pthread_join(brain_thread,NULL); ShowLoser(); move(row,col); refresh(); nodelay(window,FALSE); getch(); endwin(); return 0; }