#include <iostream>
#include <string>
#include <windows.h> //For the Sleep command.
using namespace std;
int main()
{
string playerName;
double speed;
//Ask for player name and give them a brief introduction to the game.
cout << "\nWhat is your name?: ";
getline (cin, playerName);
cout << "\nEnter a game speed. 0 is the fastest speed, and 300 is the slowest speed. "; //Sets how fast the game will run. This is tied into the sleep function below.
cin >> speed;
if (playerName == "") //Prevents someone from not using a name.
playerName = "John Doe";
if (speed < 0) //Do not allow a negative game speed.
speed = 0;
if (speed > 300) //Do not allow too slow of a game speed.
speed = 300;
cout << "\nHello, " << playerName << ".";
cout << "\nThe object of this game is to destroy the enemy ship";
cout << "\nthat appears at the top of the screen. This enemy ship";
cout << "\nhas more armor and firepower than you do, so be careful.";
cout << "\n\n";
cout << "\n-Ship Controls-";
cout << "\nLeft and Right arrows: Move your ship left and right.";
cout << "\nUp arrow: Fire a missile.";
cout << "\nDown arrow: Pause the game.";
cout << "\nPlease don't cheat by simply holding down the UP arrow. Be fair to the enemy.\n\n";
system("pause"); //Press any key to continue.
//Set the initial health and X coordinates of the human and AI players.
double aiHealth; //AI's health.
double huHealth; //Human's health.
double aiX; //AI's X coordinate.
double aiXRand; //Used to hold the random value generated to modify the AI's X coordinate.
double huX; //Human's X coordinate.
aiHealth = 1000; //AI gets 1000 health at the start of the game.
aiX = 20; //AI starts at the X coordinate of 20, the far side of the map.
huHealth = 500; //Human gets 500 health at the start of the game.
huX = 1; //Human starts at the X coordinate of 1, the beginning of our map.
while( aiHealth>0 && huHealth>0) //Starts the loop that is the game. Similar to an IF statement, but repeats indefinitely until the condition is false.
{
//Make the game sleep for a bit. This is where the Game Speed question above comes into play.
Sleep(speed);
system("CLS"); //Clear the screen, otherwise the game just spams each frame on a new line.
//Lets make the AI move.
aiXRand = rand() % 3 + 1;//Gets a random number to modify the AI's X coordinate, allowing the AI to move. 3 + 1 just means it can have an outcome of 1 to 3.
//Lets assign a value to these integers. 1=1, 2=0, and 3=-1.
if (aiXRand == 1)
aiXRand = 1; //Adds 1 to the X coordinate, in otherwords the AI could move right.
if (aiXRand == 2)
aiXRand = 0; // AI won't move.
if (aiXRand == 3)
aiXRand = -1; //Takes 1 from the X coordinate, in otherwords the AI could move left.
aiX = aiX + aiXRand;//Now use the random number to modify the AI's current coordinate.
//Lets make sure the AI cannot go out of bounds. I'm setting bounds to 20 characters across.
if (aiX == 0)
aiX = 1;
if (aiX == 21)
aiX = 20;
//Sets the players to not launch a missile unless triggered to do so.
double huLaunch;
double aiLaunch;
huLaunch = 0; //Reset Human missile trigger.
aiLaunch = 0; //Reset AI missile trigger.
//Let the human player move. Tells the computer what to do on the event of a key press.
if (GetAsyncKeyState(VK_LEFT)) //Left arrow has been pressed, remove 1 from the Human x coordinate.
{
huX = huX - 1;
}
if (GetAsyncKeyState(VK_RIGHT)) //Right arrow has been pressed, add 1 from the Human x coordinate.
{
huX = huX + 1;
}
if (GetAsyncKeyState(VK_UP)) //Up arrow has been pressed. Trigger the Human to launch a missile.
{
huLaunch = 1;
}
if (GetAsyncKeyState(VK_DOWN)) //DOWN arrow has been pressed, pause the game.
{
cout << "-Game Paused-\n";
system("pause"); //Press any key to continue.
}
//Triggers the AI player to launch a missile based on a roll-the-dice style chance.
aiXRand = rand() % 6 + 1; //Get a random number. Chance is 1 in 6 that a missile will be triggered.
if (aiXRand == 5) //If outcome of the roll is 5, then triger the AI to launch a missile.
{
aiLaunch = 1;
}
//Detect human missile hit and remove health from the AI.
if (aiX == huX && huLaunch == 1)
{
aiHealth = aiHealth - 50;
}
//Detect AI missile hit and remove health from the Human.
if (aiX == huX && aiLaunch == 1)
{
huHealth = huHealth - 75;
}
//Let's make sure the human cannot go out of bounds either. I'm setting bounds to 20 characters across.
if (huX == 0)
huX = 1;
if (huX == 21)
huX = 20;
//Defining the game's sprites (character, avatar, basically the ingame visuals.)
//Create a ship at the AI's X coordinate.
string aiFiller;
if (aiX == 1) //Basically means if the X coordinate of the AI is equal to 1, then the AI's avatar is "^".
aiFiller = "^";
if (aiX == 2)
aiFiller = " ^"; //Adds whitespace to the avatar to give it the visual impression of movement as the X coordinate rises.
if (aiX == 3)
aiFiller = " ^";
if (aiX == 4)
aiFiller = " ^";
if (aiX == 5)
aiFiller = " ^";
if (aiX == 6)
aiFiller = " ^";
if (aiX == 7)
aiFiller = " ^";
if (aiX == 8)
aiFiller = " ^";
if (aiX == 9)
aiFiller = " ^";
if (aiX == 10)
aiFiller = " ^";
if (aiX == 11)
aiFiller = " ^";
if (aiX == 12)
aiFiller = " ^";
if (aiX == 13)
aiFiller = " ^";
if (aiX == 14)
aiFiller = " ^";
if (aiX == 15)
aiFiller = " ^";
if (aiX == 16)
aiFiller = " ^";
if (aiX == 17)
aiFiller = " ^";
if (aiX == 18)
aiFiller = " ^";
if (aiX == 19)
aiFiller = " ^";
if (aiX == 20)
aiFiller = " ^";
//Create a ship at the Human's X coordinate.
string huFiller;
if (huX == 1)
huFiller = "^";
if (huX == 2)
huFiller = " ^";
if (huX == 3)
huFiller = " ^";
if (huX == 4)
huFiller = " ^";
if (huX == 5)
huFiller = " ^";
if (huX == 6)
huFiller = " ^";
if (huX == 7)
huFiller = " ^";
if (huX == 8)
huFiller = " ^";
if (huX == 9)
huFiller = " ^";
if (huX == 10)
huFiller = " ^";
if (huX == 11)
huFiller = " ^";
if (huX == 12)
huFiller = " ^";
if (huX == 13)
huFiller = " ^";
if (huX == 14)
huFiller = " ^";
if (huX == 15)
huFiller = " ^";
if (huX == 16)
huFiller = " ^";
if (huX == 17)
huFiller = " ^";
if (huX == 18)
huFiller = " ^";
if (huX == 19)
huFiller = " ^";
if (huX == 20)
huFiller = " ^";
//Let's make some fillers for the missiles.
string huMissile;
string aiMissile;
huMissile = ""; //Create no missile.
aiMissile = ""; //Create no missile. Variable Will not be used for now.
if (huX == 1 && huLaunch == 1)
huMissile = "|";
if (huX == 2 && huLaunch == 1)
huMissile = " |";
if (huX == 3 && huLaunch == 1)
huMissile = " |";
if (huX == 4 && huLaunch == 1)
huMissile = " |";
if (huX == 5 && huLaunch == 1)
huMissile = " |";
if (huX == 6 && huLaunch == 1)
huMissile = " |";
if (huX == 7 && huLaunch == 1)
huMissile = " |";
if (huX == 8 && huLaunch == 1)
huMissile = " |";
if (huX == 9 && huLaunch == 1)
huMissile = " |";
if (huX == 10 && huLaunch == 1)
huMissile = " |";
if (huX == 11 && huLaunch == 1)
huMissile = " |";
if (huX == 12 && huLaunch == 1)
huMissile = " |";
if (huX == 13 && huLaunch == 1)
huMissile = " |";
if (huX == 14 && huLaunch == 1)
huMissile = " |";
if (huX == 15 && huLaunch == 1)
huMissile = " |";
if (huX == 16 && huLaunch == 1)
huMissile = " |";
if (huX == 17 && huLaunch == 1)
huMissile = " |";
if (huX == 18 && huLaunch == 1)
huMissile = " |";
if (huX == 19 && huLaunch == 1)
huMissile = " |";
if (huX == 20 && huLaunch == 1)
huMissile = " |";
if (aiX == 1 && aiLaunch == 1)
huMissile = "|"; //Purposely not set to aiMissile.
if (aiX == 2 && aiLaunch == 1)
huMissile = " |";
if (aiX == 3 && aiLaunch == 1)
huMissile = " |";
if (aiX == 4 && aiLaunch == 1)
huMissile = " |";
if (aiX == 5 && aiLaunch == 1)
huMissile = " |";
if (aiX == 6 && aiLaunch == 1)
huMissile = " |";
if (aiX == 7 && aiLaunch == 1)
huMissile = " |";
if (aiX == 8 && aiLaunch == 1)
huMissile = " |";
if (aiX == 9 && aiLaunch == 1)
huMissile = " |";
if (aiX == 10 && aiLaunch == 1)
huMissile = " |";
if (aiX == 11 && aiLaunch == 1)
huMissile = " |";
if (aiX == 12 && aiLaunch == 1)
huMissile = " |";
if (aiX == 13 && aiLaunch == 1)
huMissile = " |";
if (aiX == 14 && aiLaunch == 1)
huMissile = " |";
if (aiX == 15 && aiLaunch == 1)
huMissile = " |";
if (aiX == 16 && aiLaunch == 1)
huMissile = " |";
if (aiX == 17 && aiLaunch == 1)
huMissile = " |";
if (aiX == 18 && aiLaunch == 1)
huMissile = " |";
if (aiX == 19 && aiLaunch == 1)
huMissile = " |";
if (aiX == 20 && aiLaunch == 1)
huMissile = " |";
//Now output the battleground.
cout << "\nFlying Ace: " << aiHealth; //Display the AI's name and health.
cout << "\n--------------------";
cout << "\n" << aiFiller; //Display the AI's ship at his coordinate.
//Missile area. aiMissile intentionally left out. huMissile will pretty much do the job.
cout << "\n" << huMissile; //If both the human and AI fire at once, one missile won't be shown. The AI missile will take priority.
cout << "\n" << huMissile; //It's because I haven't thought of a way to show both at once without screwing up the spacings.
cout << "\n" << huMissile; //Your missile will still work even if its not displayed.
cout << "\n" << huMissile;
cout << "\n" << huMissile;
cout << "\n" << huMissile;
cout << "\n" << huMissile;
cout << "\n" << huMissile;
cout << "\n" << huFiller; //Display the Human's ship at his coordinate.
cout << "\n--------------------";
cout << "\n" << playerName << ": " << huHealth; //Display the Human's name and health.
} // End of the While statement. The While exits if a player dies (<=0 health remaining), otherwise the While statement starts over again.
//The game is now over. Find out who won and display the appropriate win/lose message.
if (huHealth > 0)
cout << "\n\nYou have won the game. Good job.\n\n";
if (aiHealth > 0)
cout << "\n\nYou have died. You suck at this game.\n\n";
system("pause"); //Press any key to continue.
return 0;
}