数週間前に C++ を試し始めたばかりです。C++ を試す前に、Java をかなり理解していました。多くの人が、構文の意味で非常に似ていると私に言いました。
一番下に、戦闘シーンを開始する switch ステートメントがあります。戦うオプションを選択すると、プログラムが終了します。
これが私のコードです:
#include "stdafx.h"
#include <iostream>
#include <cstdlib> // For rand()
#include <string>
#include <sstream>
#include <algorithm> // transform()
#include <cctype> // toupper(), tolower()
#include <functional> // ptr_fun()
#include <time.h>
// PUT S*** BELOW THIS POINT
//____________________________________________________________________________
using namespace std;
int main()
{
/* Expirimental text based adventure game.
* Mainly being used for practice methods.
* Developed by Zack Cook.
Generic title. Bad story. Bad interactions.
Lots and lots of bad, bad code. Be warned.
*/
string charName;
string charChoice;
int charDecision;
int playerHealth = 100;
int randomNumber;
int orcHealth = 100;
srand (time(NULL));
cout << "_____________________________________________" << endl;
cout << "| ##### ######### ### ### ### |" << endl;
cout << "| ### ### ### ### ### ### ### |" << endl;
cout << "| ### ### ### ### ##### |" << endl;
cout << "| ### ### ######### ### |" << endl;
cout << "| ### ### ### ### ### ### |" << endl;
cout << "| ##### ### ### ### ### |" << endl;
cout << "_____________________________________________" << endl;
cout << "" << endl;
cout << "" << endl;
cout << "Welcome player. What is your name?" << endl;
getline(cin, charName);
yesOrNo:
cout << "Hello " << charName << ". Are you ready to begin?" << endl;
getline(cin, charChoice);
transform( charChoice.begin(), charChoice.end(), charChoice.begin(), toupper );
cout << charChoice << endl;
if(charChoice == "YES"){
cout << "Good. Let's begin." << endl;
}
else if(charChoice == "NO"){
system ("exit");
}
else
{
cout << "That is not a good answer." << endl;
goto yesOrNo;
}
cout << "Our story begins with a wanderer named " << charName << " passing through the small town of Hark's Pass." << endl;
cout << "A little cozy village with no more than 30 or so home stayers.\nThe village men work hard on the farms during the day." << endl;
cout << "The women cater to the children, and other house hold chores.\nIn the evening, most of the village turns to The Rusty Trough for a bit of drink." << endl;
cout << "As the sun starts to set, our wanderer, " << charName << ", starts foot towards The Rusty Trough." << endl;
cout << "As " << charName << " enters the tavern, a heavily drunken Orc man stumbles towards the entrance." << endl;
cout << "\"I've never seen you 'round here!\" The orc says to our wanderer. \"I think it's time to teach these adventure types what we really think about 'em\"" << endl;
cout << "" << endl;
cout << "What will you do?" << endl;
cout << "1| Get ready to fight!" << endl;
cout << "2| Call for help!" << endl;
cout << "3| Try to run!" << endl;
cout << "4| Do nothing at all!" << endl;
cout << "5| Try to reason!" << endl;
cin >> charDecision;
switch(charDecision)
{
case '1':
do{
cout << "FIGHT" << endl;
randomNumber = rand() % 100 + 1;
if(randomNumber >= 50){
orcHealth = orcHealth - (randomNumber - (randomNumber / 5));
cout << "You hit the orc! He now has " << orcHealth << " life left!" << endl;
}
else
{
playerHealth = playerHealth - (randomNumber - (randomNumber / 5));
cout << "The orc hit you! You now have " << playerHealth << " life left!" << endl;
}
}while(playerHealth || orcHealth != 0);
break;
default:
break;
}
return 0;
}