Skip to content

Instantly share code, notes, and snippets.

@mgalgs
Created June 23, 2011 17:23
Show Gist options
  • Save mgalgs/1043047 to your computer and use it in GitHub Desktop.
Save mgalgs/1043047 to your computer and use it in GitHub Desktop.
hand and foot program
// -*- mode: c++; compile-command: "g++ -g -o haf haf.cpp"; -*-
// hand and foot scoring program by Joseph Mickelson
#include <iostream>
#include <stdio.h> //for printf
#include <vector>
#include <numeric> //for accumulate
#ifdef __linux__
#include <stdlib.h>
#endif
using namespace std;
#define DEFAULT_UPPER_LIMIT 10000
#define DEFAULT_DIRTY_WEIGHT 300
#define DEFAULT_CLEAN_WEIGHT 500
/**
* Forward declarations
*/
void clearscrplz();
void pauseplz();
void do_gameplay();
int vector_sum(vector<int>);
void print_scorecard();
void print_hline(char, int);
/**
* Some global variables (these are usually bad practice but since the
* program is so small we're okay, plus we're renegades)
*/
vector<int> boys_scores;
vector<int> girls_scores;
int main()
{
// the meat:
do_gameplay();
if (vector_sum(girls_scores) > vector_sum(boys_scores))
{
clearscrplz();
cout << "GIRLS WIN!" << endl;
}
else if (vector_sum(girls_scores) < vector_sum(boys_scores))
{
clearscrplz();
cout << "BOYS WIN!" << endl;
}
else
{
clearscrplz();
cout << "IT WAS A TIE!!!" << endl;
}
cout << endl;
print_scorecard();
cout << "Press any key to quit" << endl;
pauseplz();
return 0;
}
// "cross-platform" clear
// ps doing system calls is almost always a bad thing, but we're renegades!
void clearscrplz()
{
#ifdef __linux__
system("clear");
#else
system("cls");
#endif
}
// "cross-platform" pause
void pauseplz()
{
#ifdef __linux__
system("read");
#else
system("pause");
#endif
}
void do_gameplay()
{
int selection, dirty, clean, misc;
selection=1;
// we do a "return" when we finally want to break out of this
// "forever" loop to finish gameplay:
for(;;)
{
clearscrplz();
cout << "What would you like to do?" << endl;
cout << "1) Enter Scoring" << endl;
cout << "2) Show scorecard" << endl;
cout << "3) Revert to previous round" << endl;
cout << "4) Cowardly desist" << endl;
cin >> selection;
switch(selection) {
case 1:
clearscrplz();
// update girls scores:
cout << "Girl scoring" << endl;
cout << "How many dirty books do the girls have?" << endl;
cin >> dirty;
cout << "How many clean books do the girls have?" << endl;
cin >> clean;
dirty *= DEFAULT_DIRTY_WEIGHT; // *= is the same as saying dirty = dirty * DEFAULT_DIRTY_WEIGHT
clean *= DEFAULT_CLEAN_WEIGHT;
cout << "What is the girls misc score?" << endl;
cin >> misc;
girls_scores.push_back(dirty + clean + misc);
clearscrplz();
// update boys scores:
cout << "Boys scoring:" << endl;
cout << "How many dirty books do the boys have?" << endl;
cin >> dirty;
cout << "How many clean books do the boys have?" << endl;
cin >> clean;
dirty *= DEFAULT_DIRTY_WEIGHT;
clean *= DEFAULT_CLEAN_WEIGHT;
cout << "What is the boys misc score?" << endl;
cin >> misc;
boys_scores.push_back(dirty + clean + misc);
clearscrplz();
print_scorecard();
pauseplz();
if (vector_sum(girls_scores) >= DEFAULT_UPPER_LIMIT || vector_sum(boys_scores) >= DEFAULT_UPPER_LIMIT)
{
return;
}
break;
case 2:
clearscrplz();
print_scorecard();
pauseplz();
break;
case 3:
// get rid of the last score by deleting the most recent
// element from the vectors:
// http://www.cplusplus.com/reference/stl/vector/pop_back/
if (!boys_scores.empty())
boys_scores.pop_back();
if (!girls_scores.empty())
girls_scores.pop_back();
clearscrplz();
cout << "Reverted. New scorecard:\n";
print_scorecard();
cout << "Press enter...\n";
pauseplz();
break;
case 4:
return;
default:
cout << "Invalid choice, silly!" << endl;
pauseplz();
break;
}
} // eo forever loop
}
/**
* Sum up all the elements in a vector using std::accumulate (from numeric)
* http://www.cplusplus.com/reference/std/numeric/accumulate/
*/
int vector_sum(vector<int> v)
{
return accumulate(v.begin(), v.end(), 0);
}
void print_scorecard()
{
// We'll be using c-preprocessor string concatentation with
// SCORE_SEP, like this: "some string" "another string" Note,
// there is no comma between them so the c-preprocess concatenates
// them.
// http://en.wikipedia.org/wiki/C_syntax#String_literal_concatenation
#define SCORE_SEP " | "
#define HLINE_WIDTH 38
print_hline('=', HLINE_WIDTH);
// okay printf is more of a C thing, but it is actually usually
// easier to use than cout... Here we're doing left-aligned
// strings of width 15
// http://en.wikipedia.org/wiki/Printf#Format_placeholders
printf("%-15s" SCORE_SEP "%-15s\n",
"Girls scores",
"Boys scores");
print_hline('=', HLINE_WIDTH);
int total_scores = girls_scores.size(); // should be the same as boys_scores.size()
for (int i=0; i < total_scores; i++)
{
printf("%-15d" SCORE_SEP "%-15d\n",
girls_scores[i],
boys_scores[i]);
}
print_hline('=', HLINE_WIDTH);
printf("\ntotals:\n");
print_hline('+', HLINE_WIDTH);
printf("%-15d" SCORE_SEP "%-15d\n",
vector_sum(girls_scores),
vector_sum(boys_scores));
print_hline('+', HLINE_WIDTH);
printf("\n");
}
void print_hline(char ch, int width)
{
for (int i=0; i < width; i++) {
printf("%c", ch);
}
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment