/* * Worm (as excerpted from Tequila) * by Shiv, completed 6/9/98. * Mr. Hill, slot 3 Computer Science. */ #include #include #include #include #include Intro(); Header(); Engine(int &direction, int &horizontal, int &vertical); WhiteWorm(int &opponentX, int &apponentY, int &horizontal, int &vertical, int &kills, int &opponentMove, int &enemyX, int &enemyY, int &difficulty); GreenWorm(int &enemyX, int &enemyY, int &horizontal, int &vertical, int &killed, int &difficulty); Score(int &difficulty, int &kills, int &killed, int &dead); Obstacle(int &horizontal, int &vertical, int &dead, int &opponentX, int &opponentY, int &enemyX, int &enemyY); LengthAdjust(int &length, int &reset); struct finalData { int victories; int losses; int barrierHits; int totalLosses; }; main() { char keypress; int horizontal; int vertical; int direction; int difficulty; int opponentX, opponentY, opponentMove; int enemyX, enemyY; int kills, killed, dead; int length; int reset; struct finalData finals; int endVar; Intro(); do { clrscr(); randomize(); difficulty = Header(); //calls the program's header LoopReset: //program backdoor (see line 170) horizontal = 39; //initialize variables, vertical = 12; //perform prelims dead = 0; reset = 7; length = 1; direction = 0; opponentX = 1; opponentY = 1; opponentMove = 0; enemyX = 1; enemyY = 1; kills = 0; killed = 0; clrscr(); opponentX = random(79 + 1); enemyX = random(79 + 1); opponentY = random(24 + 1); enemyY = random(24 + 1); while (keypress != 120) { //begin inner loop Scenery(); //run function to create background //run function to create the white worm WhiteWorm(opponentX, opponentY, horizontal, vertical, kills, opponentMove, enemyX, enemyY, difficulty); //run function to create the green worm GreenWorm(enemyX, enemyY, horizontal, vertical, killed, difficulty); //run function to create the main program engine keypress = Engine(direction, horizontal, vertical); //ensure location on screen gotoxy(horizontal, vertical); //calls obstacle function Obstacle(horizontal, vertical, dead, opponentX, opponentY, enemyX, enemyY); //call function to reset worm sizes LengthAdjust(length, reset); //outputs score, if user asks for it if (keypress == 115) Score(difficulty, kills, killed, dead); finals.victories = kills; finals.losses = killed; finals.barrierHits = dead; if (keypress == 122) //program back door, in the event { //of an unforseen glitch keypress = 'Ü'; goto LoopReset; } } //end inner loop clrscr(); finals.totalLosses = (dead += killed); textbackground(BLACK); textcolor(LIGHTGRAY); gotoxy(1,1); //outputs number of kills cprintf("You got %d kills.",finals.victories); gotoxy(1,2); //outputs number of times killed cprintf("The enemy got you %d times.",finals.losses); dead += killed; //calculates total gotoxy(1,3); cprintf("You died %d times.",finals.totalLosses); //outputs total //outputs win/loss message if (finals.totalLosses > finals.victories) { gotoxy(34,13); cprintf("YOU LOSE!"); } else if (finals.totalLosses == finals.victories) { gotoxy(34,13); cprintf("IT'S A TIE!"); } else { gotoxy(34,13); cprintf("YOU WIN!"); } keypress = 'c'; gotoxy(1,24); cprintf("Do you want to quit? 1 for yes, 0 for no. "); gotoxy(44,24); cin >> endVar; } while (endVar != 1); return 0; //end program } //*************************************************************** //Worm intro function Intro() { char phrase[5] = "Worm\0"; int x1, x2; int LCV = 0; clrscr(); textcolor(LIGHTGRAY); textbackground(BLACK); x1 = 38; x2 = 38; do //scrolls 'Worm' left and { //left across the screen while ((x1 > 1) && (x2 < 76)) { gotoxy(x1,10); cout << phrase; delay(30); delline(); gotoxy(x2,10); cout << phrase; delay(30); delline(); x1--; x2++; } while ((x1 < 76) && (x2 > 1)) //bounces 'Worm' back the other way { gotoxy(x1,10); cout << phrase; delay(30); delline(); gotoxy(x2,10); cout << phrase; delay(30); delline(); x1++; x2--; if (x1 == x2) //causes 'Worm' to shoot upwards { gotoxy(38,25); cout << "Worm"; } } LCV++; } while (LCV < 1); return 0; } //*************************************************************** //Worm Header function Header() { int difficulty; clrscr(); textcolor(LIGHTGRAY); textbackground(BLUE); //shows how control scheme works cout << "\nThe following is how the worm control works:\n\n"; cout << " (7) (8) (9) \n"; cout << " \\\\ || // \n"; cout << " (4) === === (6) \n"; cout << " // || \\\\ \n"; cout << " (1) (2) (3) \n\n"; cout << "Your goal is to catch the white worm, while avoiding the "; cout << "green worm and the \nbarriers. Your results are shown after "; cout << "you quit (you win if you kill more \nwhite worms than green "; cout << "ones).\n"; cout << "To quit, hit a lowercase 'x'. If you want to show your score, "; cout << "hit 's'. \n\n"; cout << "What do you want the difficulty to be?\n"; cout << "[1] for easy, [2] for medium, [3] for hard. "; cin >> difficulty; cout << "\nPress enter to continue. "; getch(); return(difficulty); } //*************************************************************** //Main Program Engine function Engine(int &direction, int &horizontal, int &vertical) { int keypress; textcolor(BLACK); //reset colors textbackground(BLUE); gotoxy(horizontal, vertical); if (direction == 0) //output worm character putch('Ü'); else if (direction == 1) putch('Û'); else putch('Ü'); gotoxy(horizontal, vertical); //required to get rid of cursor keypress = getch(); //reads users keypress gotoxy(horizontal, vertical); switch (keypress) //determines direction { //cardinal directions case (50) : { vertical++; //up (8) direction = 1; break; } case (52) : { horizontal--; //left (4) direction = 0; break; } case (54) : { horizontal++; //right (6) direction = 0; break; } case (56) : { vertical--; //down (2) direction = 1; break; } /* * It was impossible to represent the keypad numbers using * anything other than their ASCII values. This brought * about several additional bugs, and with the slanted * directions, certain directional manipulations responded * to the incorrect ASCII values. This is why the following * code may seem mixed up, or incorrect. */ //slanted directions case (49) : { horizontal--; //down and left (1) vertical++; direction = 2; break; } case (51) : { horizontal++; //down and right (3) vertical++; direction = 2; break; } case (55) : { horizontal--; //up and left (7) vertical--; direction = 2; break; } case (57) : { horizontal++; //up and right (9) vertical--; direction = 2; break; } } //end switch statement if (horizontal < 1) //corrects if user attempts to horizontal++; //go outside screen if (horizontal > 80) horizontal--; if (vertical < 1) vertical++; if (vertical > 25) vertical--; return(keypress); } //*************************************************************** //WhiteWorm function WhiteWorm(int &opponentX, int &opponentY, int &horizontal, int &vertical, int &kills, int &opponentMove, int &enemyX, int &enemyY, int &difficulty) { int kill = 0; textcolor(WHITE); textbackground(BLUE); if ((horizontal == opponentX) && (vertical == opponentY)) { kill = 1; //executes if the white kills++; //worm was killed sound(800); delay(100); nosound(); enemyX = random(79 + 1); enemyY = random(24 + 1); clrscr(); Scenery(); textbackground(BLUE); } if (kill == 1) { opponentX = random(79 + 1); //resets white worm values opponentY = random(24 + 1); //if killed clrscr(); Scenery(); //recalls the scenery function kill = 0; } if (kill == 0) //executes if white worm { //wasn't killed if (difficulty == 1) //difficulty level 1 { opponentMove = random(3); switch (opponentMove) { case (0) : { opponentX++; //decides randomly which break; } //direction the worm will go case (1) : { opponentX--; break; } case (2) : { opponentY++; break; } case (3) : { opponentY--; break; } } } else if (difficulty == 2) //difficulty level 2 { opponentMove = random(3); switch (opponentMove) { case (0) : { opponentX++; //also decides randomly which break; } //direction the worm will go case (1) : { opponentX--; break; } case (2) : { opponentY++; break; } case (3) : { opponentY--; break; } } } else //difficulty level 3 { if (opponentX < horizontal) opponentX--; if (opponentX > horizontal) opponentX++; if (opponentY < vertical) opponentY--; if (opponentY > vertical) opponentY++; } if (opponentX < 1) //corrects if white worm attempts to opponentX++; //go outside screen if (opponentX > 80) opponentX--; if (opponentY < 1) opponentY++; if (opponentY > 25) opponentY--; gotoxy(opponentX, opponentY); textcolor(WHITE); textbackground(BLUE); putch('*'); gotoxy(horizontal, vertical); } return 0; } //*************************************************************** //GreenWorm function GreenWorm(int &enemyX, int &enemyY, int &horizontal, int &vertical, int &killed, int &difficulty) { int death = 0; int movement = 0; int LCV; textcolor(GREEN); textbackground(BLUE); if (death == 1) //executes if green worm { //caught the player clrscr(); Scenery(); death = 0; } if (death == 0) { if (difficulty == 1) //difficulty level 1 { movement = random(3); switch (movement) { case (0) : { enemyX++; //decides randomly which break; } //direction the worm will go case (1) : { enemyX--; break; } case (2) : { enemyY++; break; } case (3) : { enemyY--; break; } } } else if (difficulty == 2) //difficulty level 2 { movement = random(9 + 1); //decides green worm's movement if (movement < 8) { if (enemyX < horizontal) //green worm has a 4/5 chance enemyX++; //of following the user directly if (enemyX > horizontal) enemyX--; if (enemyY < vertical) enemyY++; if (enemyY > vertical) enemyY--; } else if (movement >= 8) { movement = random(3); //following statements cause the if (movement == 0); //green worm to move randomly enemyX++; if (movement == 1) enemyX--; if (movement == 2) enemyY++; if (movement == 3) enemyY--; } } else //difficulty level 3 { if (enemyX > horizontal) enemyX--; if (enemyX < horizontal) enemyX++; if (enemyY > vertical) enemyY--; if (enemyY < vertical) enemyY++; } if ((enemyX == horizontal) && (enemyY == vertical)) { killed++; //executes if the green worm death = 1; //has caught the player sound(1000); delay(200); nosound(); horizontal = 39; vertical = 12; enemyX = random(79 + 1); enemyY = random(24 + 1); clrscr(); Scenery(); textbackground(BLUE); } if (enemyX < 1) //corrects if enemy attempts to enemyX++; //go outside screen if (enemyX > 80) enemyX--; if (enemyY < 1) enemyY++; if (enemyY > 25) enemyY--; gotoxy(enemyX, enemyY); textcolor(GREEN); textbackground(BLUE); putch('*'); gotoxy(horizontal, vertical); } return 0; } //*************************************************************** //Score function Score(int &difficulty, int &kills, int &killed, int &dead) { gotoxy(1,1); //outputs score textcolor(WHITE); textbackground(BLACK); if (difficulty == 1) cprintf("You're on low difficulty."); else if (difficulty == 2) cprintf("You're on medium difficulty."); else cprintf("You're on high difficulty."); gotoxy(1,2); cprintf("You have %d kills.",kills); gotoxy(1,3); cprintf("You've died %d times.",killed); gotoxy(1,4); cprintf("Your total losses are %d.",dead += killed); gotoxy(1,5); if (dead > kills) cprintf("You're losing."); else if (dead == kills) cprintf("You're tied."); else cprintf("You're winning."); dead -= killed; gotoxy(1,6); cprintf("Press any key to continue."); getch(); textcolor(BLACK); textbackground(BLUE); clrscr(); return 0; } //*************************************************************** //Obstacle function Obstacle(int &horizontal, int &vertical, int &dead, int &opponentX, int &opponentY, int &enemyX, int &enemyY) { /* Okay, this is gonna be a pretty big bunch of IF statements. */ //top left 'L' watcher if (((horizontal == 5) && (vertical == 3)) || ((horizontal == 6) && (vertical == 3)) || ((horizontal == 7) && (vertical == 3)) || ((horizontal == 8) && (vertical == 3)) || ((horizontal == 9) && (vertical == 3)) || ((horizontal == 10) && (vertical == 3)) || ((horizontal == 11) && (vertical == 3)) || ((horizontal == 12) && (vertical == 3)) || ((horizontal == 13) && (vertical == 3)) || ((horizontal == 14) && (vertical == 3)) || ((horizontal == 15) && (vertical == 3)) || ((horizontal == 16) && (vertical == 3)) || ((horizontal == 5) && (vertical == 4)) || ((horizontal == 5) && (vertical == 5))) { clrscr(); horizontal = 39; vertical = 12; gotoxy(horizontal, vertical); sound(500); delay(80); nosound(); dead++; opponentX = random(79 + 1); opponentY = random(24 + 1); enemyX = random(79 + 1); enemyY = random(24 + 1); } //top right 'L' watcher if (((horizontal == 65) && (vertical == 3)) || ((horizontal == 66) && (vertical == 3)) || ((horizontal == 67) && (vertical == 3)) || ((horizontal == 68) && (vertical == 3)) || ((horizontal == 69) && (vertical == 3)) || ((horizontal == 70) && (vertical == 3)) || ((horizontal == 71) && (vertical == 3)) || ((horizontal == 72) && (vertical == 3)) || ((horizontal == 73) && (vertical == 3)) || ((horizontal == 74) && (vertical == 3)) || ((horizontal == 74) && (vertical == 4)) || ((horizontal == 74) && (vertical == 5))) { clrscr(); horizontal = 39; vertical = 12; gotoxy(horizontal, vertical); sound(500); delay(80); nosound(); dead++; opponentX = random(79 + 1); opponentY = random(24 + 1); enemyX = random(79 + 1); enemyY = random(24 + 1); } //bottom left 'L' watcher if (((horizontal == 5) && (vertical == 22)) || ((horizontal == 6) && (vertical == 22)) || ((horizontal == 7) && (vertical == 22)) || ((horizontal == 8) && (vertical == 22)) || ((horizontal == 9) && (vertical == 22)) || ((horizontal == 10) && (vertical == 22)) || ((horizontal == 11) && (vertical == 22)) || ((horizontal == 12) && (vertical == 22)) || ((horizontal == 13) && (vertical == 22)) || ((horizontal == 14) && (vertical == 22)) || ((horizontal == 15) && (vertical == 22)) || ((horizontal == 5) && (vertical == 21)) || ((horizontal == 5) && (vertical == 20))) { clrscr(); horizontal = 39; vertical = 12; gotoxy(horizontal, vertical); sound(500); delay(80); nosound(); dead++; opponentX = random(79 + 1); opponentY = random(24 + 1); enemyX = random(79 + 1); enemyY = random(24 + 1); } //bottom right 'L' watcher if (((horizontal == 65) && (vertical == 22)) || ((horizontal == 66) && (vertical == 22)) || ((horizontal == 67) && (vertical == 22)) || ((horizontal == 68) && (vertical == 22)) || ((horizontal == 69) && (vertical == 22)) || ((horizontal == 70) && (vertical == 22)) || ((horizontal == 71) && (vertical == 22)) || ((horizontal == 72) && (vertical == 22)) || ((horizontal == 73) && (vertical == 22)) || ((horizontal == 74) && (vertical == 22)) || ((horizontal == 75) && (vertical == 22)) || ((horizontal == 75) && (vertical == 21)) || ((horizontal == 75) && (vertical == 20))) { clrscr(); horizontal = 39; vertical = 12; gotoxy(horizontal, vertical); sound(500); delay(80); nosound(); dead++; opponentX = random(79 + 1); opponentY = random(24 + 1); enemyX = random(79 + 1); enemyY = random(24 + 1); } //central box watcher if (((horizontal == 34) && (vertical == 10)) || ((horizontal == 35) && (vertical == 10)) || ((horizontal == 36) && (vertical == 10)) || ((horizontal == 37) && (vertical == 10)) || ((horizontal == 34) && (vertical == 11)) || ((horizontal == 41) && (vertical == 10)) || ((horizontal == 42) && (vertical == 10)) || ((horizontal == 43) && (vertical == 10)) || ((horizontal == 44) && (vertical == 10)) || ((horizontal == 44) && (vertical == 11)) || ((horizontal == 34) && (vertical == 14)) || ((horizontal == 35) && (vertical == 14)) || ((horizontal == 36) && (vertical == 14)) || ((horizontal == 37) && (vertical == 14)) || ((horizontal == 34) && (vertical == 13)) || ((horizontal == 41) && (vertical == 14)) || ((horizontal == 42) && (vertical == 14)) || ((horizontal == 43) && (vertical == 14)) || ((horizontal == 44) && (vertical == 14)) || ((horizontal == 44) && (vertical == 13))) { clrscr(); horizontal = 39; vertical = 12; gotoxy(horizontal, vertical); sound(500); delay(80); nosound(); dead++; opponentX = random(79 + 1); opponentY = random(24 + 1); enemyX = random(79 + 1); enemyY = random(24 + 1); } return 0; } //*************************************************************** //Length Adjust function LengthAdjust(int &length, int &reset) { length++; //adjusts the length of the worm if (length >= reset) { length = 0; clrscr(); } return 0; }