Hi All,
There don't seem to be many IRIX simple example C++ programs, if you know where they are hiding please share that. Aside from other simple examples, I have this text based lunar lander game that I would like as vanilla IRIX-friendly, C++ that I can compile and experiment with. My machine is an O2 running IRIX 6.5.30 and has the Base Compiler Dev Environment 7.2.1. If you need further info in order to do this for me please ask.
Please add any IRIX specific comments where necessary, that will help me see the difference. I can PayPal $50, I hope that's fair. Thank you.
KB
// ----------------------------------------------------------------------------
//
// LUNAR LANDER
//
// Based on Lunar lander - Eleven source code by Joe Morrison
// From
http://eleven.sourceforge.net/demo/lander.html
//
// See instructions() for details of the game
// Ported to C++ by JL Popyack, January 2012
//
// ----------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <cmath>
// ----------------------------------------------------------------------------
//
// -------------------------------- Prototypes --------------------------------
//
// ----------------------------------------------------------------------------
string lowerCase(string s) ;
bool getYesNoResponse(string question) ;
void instructions(void) ;
void updateStatus(double &velocity, double &burnAmount,
double &fuelRemaining, double &height) ;
void touchdown(double &velocity, double &burnAmount,
double &fuelRemaining, double &height,
double & delta) ;
void finalAnalysis(int elapsedTime, double delta,
double velocity, double fuelRemaining) ;
// ----------------------------------------------------------------------------
//
// ------------------------------- Main Program -------------------------------
//
// ----------------------------------------------------------------------------
int main(void)
{
// ----------------------------------------------------------------------------
// Initial position and velocity of the lander
// ----------------------------------------------------------------------------
int elapsedTime = 0;
double height = 1000; // distance above the lunar surface, in feet
double velocity = 50; // speed of descent, in feet/sec
double fuelRemaining = 150; // each unit of fuel slows descent by 1 ft/sec
double burnAmount = 0;
cout << "LUNAR LANDER" << endl ;
cout << "By Dave Ahl (translation from BASIC to ELEVEN by Joe Morrison)" << endl ;
cout << "Adapted to C++ by JL Popyack" << endl << endl ;
instructions() ;
cout << endl << "LUNAR LANDER" << endl ;
cout << "Beginning landing procedure.........." << endl ;
cout << "DIGBY wishes you good luck !!!!!!!" << endl << endl ;
// ------------------------------------------------------------------------
// Each second, the user is asked how many units of fuel to burn.
// The fuel is burned, the lander descends, the velocity is adjusted
// and the user is again asked how many units to burn, until landing.
// ------------------------------------------------------------------------
while (height > 0)
{
cout << endl ;
cout << "Status of your APOLLO spacecraft: " << endl ;
cout << "Time : " << elapsedTime << " seconds" << endl ;
cout << "Height: " << height << " feet" << endl ;
cout << "Speed : " << velocity << " feet/second" << endl ;
cout << "Fuel : " << fuelRemaining << endl ;
// --------------------------------------------------------------------
// The user can burn up to 30 units of fuel.
// --------------------------------------------------------------------
if (fuelRemaining > 0)
{
cout << "Enter fuel burn amount: " ;
cin >> burnAmount ;
if( burnAmount < 0 )
burnAmount = 0;
if( burnAmount > 30 )
burnAmount = 30;
if( burnAmount > fuelRemaining )
burnAmount = fuelRemaining;
}
else
{
cout << "**** OUT OF FUEL ****" << endl ;
burnAmount = 0;
}
// --------------------------------------------------------------------
// The new position and velocity are determined after burning the fuel.
// --------------------------------------------------------------------
updateStatus(velocity,burnAmount,fuelRemaining,height) ;
elapsedTime++ ;
}
// ------------------------------------------------------------------------
// Touchdown. Calculate landing parameters.
// ------------------------------------------------------------------------
double delta ;
touchdown(velocity,burnAmount,fuelRemaining,height,delta) ;
elapsedTime-- ;
// ------------------------------------------------------------------------
// Print results and analysis.
// ------------------------------------------------------------------------
finalAnalysis(elapsedTime,delta,velocity,fuelRemaining) ;
return 0 ;
}
// ----------------------------------------------------------------------------
//
// -------------------------- Subprogram Definitions --------------------------
//
// ----------------------------------------------------------------------------
string lowerCase(string s)
{
// ------------------------------------------------------------------------
// Returns string with alpha characters in s converted to lower case
// ------------------------------------------------------------------------
for(unsigned int i=0; i<s.size(); i++)
if( s[i]>='A' && s[i]<='Z' )
s[i] += 'a'-'A' ;
return s ;
}
// ----------------------------------------------------------------------------
//
bool getYesNoResponse(string question)
{
// ------------------------------------------------------------------------
// Prompts user with question.
// Accepts "y" or "yes" or "n" or "no" in upper or lower case or mixed
// and returns true or false as appropriate
// ------------------------------------------------------------------------
bool done ;
string response ;
cout << question << " " ;
do
{
getline(cin,response) ;
response = lowerCase(response) ;
done = response == "y" || response == "yes" ||
response == "n" || response == "no" ;
if( !done )
cout << "Please answer 'y' or 'n': " ;
} while(!done) ;
return ( response == "y" || response == "yes" ) ;
}
// ----------------------------------------------------------------------------
//
void instructions(void)
{
// ------------------------------------------------------------------------
// Asks if the user wants instructions and prints them if so
// ------------------------------------------------------------------------
bool wantInstructions = getYesNoResponse("Do you want instructions (y/n)? ") ;
if (!wantInstructions)
return ;
cout << endl ;
cout << "LUNAR LANDER INSTRUCTIONS" << endl << endl;
cout << "You are landing on the moon and have taken over manual" << endl ;
cout << "control 1000 feet above a good landing spot. You have a" << endl ;
cout << "downward velocity of 50 feet/sec. 150 units of fuel remain." << endl ;
cout << endl ;
cout << "Here are the rules that govern your APOLLO space-craft:" << endl ;
cout << "(1) After each second, the height, velocity, and remaining" << endl ;
cout << " fuel will be reported via DIGBY, your on-board computer." << endl ;
cout << "(2) After each report, enter the number of units of fuel you" << endl ;
cout << " wish to burn during the next second. Each unit of fuel" << endl ;
cout << " will slow your descent by 1 foot/sec." << endl ;
cout << "(3) The maximum thrust of your engine is 30 feet/sec/sec or" << endl ;
cout << " 0 units of fuel per second." << endl ;
cout << "(4) When you contact the lunar surface, your descent engine" << endl ;
cout << " will automatically shut down and you will be given a " << endl ;
cout << " report of your landing speed and remaining fuel." << endl ;
cout << "(5) If you run out of fuel, you will no longer be prompted" << endl ;
cout << " to enter the number of units to burn each second. Your" << endl ;
cout << " second by second reports will continue until you contact" << endl ;
cout << " the lunar surface." << endl ;
cout << endl ;
}
// ----------------------------------------------------------------------------
//
void updateStatus(double &velocity, double &burnAmount,
double &fuelRemaining, double &height)
{
// ------------------------------------------------------------------------
// Given current velocity, height and fuel remaining, calculates the
// new values if burnAmount amount of fuel is burned.
// ------------------------------------------------------------------------
double newVelocity = velocity - burnAmount + 5;
fuelRemaining = fuelRemaining - burnAmount;
height = height - (velocity + newVelocity) * 0.5;
velocity = newVelocity;
}
// ----------------------------------------------------------------------------
//
void touchdown(double &velocity, double &burnAmount,
double &fuelRemaining, double &height,
double & delta)
{
// ------------------------------------------------------------------------
// Touchdown occurs when the height reaches 0.
// Final values are computed.
// ------------------------------------------------------------------------
double newVelocity = velocity;
velocity = velocity - 5 + burnAmount;
height = height + (velocity + newVelocity) * 0.5;
if (burnAmount == 5)
delta = height/velocity;
else
delta = ( sqrt(velocity*velocity + height*(10 - burnAmount*2) ) - velocity) / (5 - burnAmount);
newVelocity = velocity + (5 - burnAmount)*delta;
velocity = newVelocity ;
}
// ----------------------------------------------------------------------------
//
void finalAnalysis(int elapsedTime, double delta,
double velocity, double fuelRemaining)
{
// ------------------------------------------------------------------------
// Prints final statistics and analysis.
// ------------------------------------------------------------------------
cout << endl ;
cout << "***** CONTACT *****" << endl ;
cout << "Touchdown at " << elapsedTime + delta << " seconds." << endl ;
cout << "Landing velocity = " << velocity << " feet/second" << endl ;
cout << fuelRemaining << " units of fuel remaining." << endl << endl ;
if (velocity <= 0)
{
cout << "Congratulations! A perfect landing!!" << endl ;
cout << "Your license will be renewed.............later." << endl ;
}
else if (velocity < 2)
{
cout << "A little bumpy." << endl ;
}
else if (velocity < 5)
{
cout << "You blew it!!!!!!" << endl ;
cout << "Your family will be notified..............by post." << endl ;
}
else if (velocity < 10)
{
cout << "Your ship is a heap of junk !!!!!" << endl ;
cout << "Your family will be notified..............by post." << endl ;
}
else if (velocity < 30)
{
cout << "You blasted a huge crater !!!!!" << endl ;
cout << "Your family will be notified..............by post." << endl ;
}
else if (velocity < 50)
{
cout << "Your ship is a wreck !!!!!" << endl ;
cout << "Your family will be notified..............by post." << endl ;
}
else
{
cout << "You totaled an entire mountain !!!!!" << endl ;
cout << "Your family will be notified..............by post." << endl ;
}
}