Skip to content

Instantly share code, notes, and snippets.

@Zhomart
Created March 21, 2014 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zhomart/9695644 to your computer and use it in GitHub Desktop.
Save Zhomart/9695644 to your computer and use it in GitHub Desktop.
/*
The Wheel Game
--------------
- There are three contestants: A, B, C. A plays first, then B, then C.
- Each contestant's score is determined by the sum of three spins of a wheel.
- Each spin is a number from 5 to 100, intervals of 5, ie. 5, 10, 15, 20, ... , 95, 100
- If a players score is at 100 or more after one or two spins, no more spins can be attempted.
- The winner is the player closest to 100 but not over.
- In the case of a tie, the winner is the first player to achieve the winning score.
The Assignment
--------------
Write a C++ program plays the wheel game using file I/O.
This program shell contains the basics to get started.
The program reads results from a file "wheelgame.txt" in which each
game is coded in three lines of the form:
Name spin1 spin2 spin3
If no spin is attempted for spin2 or spin3, a 0 is entered in its place.
An example wheelgame_example.txt is included
The result of the program should be the creation of a file "wheelresults.txt"
There will be one output line per game, either with the name and score of the winner,
or the text "Invalid Game."
The only invalid game cases you should deal with are non-zero spins when no spin was allowed.
A corresponding example wheelresults_example.txt is included.
*/
#include <iostream>
#include <fstream>
using namespace std;
// return a players score in total, or zero if they went over.
// valid should indicate with the spins were valid or not.
// implement the following function exactly if you want partial credit
void score(int l[],bool& valid, int& total){
valid = true;
total = 0;
if (l[0] >= 100 && l[1] + l[2] > 0){
valid = false;
return;
}
else if (l[0] + l[1] >= 100 && l[2] > 0){
valid = false;
return;
}
total = l[0] + l[1] + l[2];
if (total > 100) total = 0;
}
int main() {
ifstream infile("wheelgame.txt");
ofstream outfile;
outfile.open("wheelresults.txt");
/* your code here */
while (!infile.eof()){
char s[3][100];
int a[3][3];
bool finish = false;
for (int i=0;i<3;++i){
if (infile.eof()) { finish = true; break; }
infile >> s[i] >> a[i][0] >> a[i][1] >> a[i][2];
}
if (finish) break;
bool valid = true;
int i_max;
int total_max = -1;
for (int i=0;i<3;++i){
bool i_valid;
int i_total;
score(a[i], i_valid, i_total);
valid = valid && i_valid;
if (total_max < i_total){
total_max = i_total;
i_max = i;
}
}
if (valid){
outfile << s[i_max] << " " << total_max<<endl;
}
else {
outfile << "Invalid Game\n";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment