Created
October 3, 2012 19:14
-
-
Save daniel-j/3829127 to your computer and use it in GitHub Desktop.
Yatzy by djazz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <ctime> | |
#include <cstdlib> | |
#include <fstream> | |
using namespace std; | |
const int DICE_COUNT = 5; | |
const int ROUND_COUNT = 6; | |
const int PLAYER_COUNT = 2; | |
bool didCin = false; | |
void safeGetLine (string &val) { | |
if (didCin) { | |
cin.ignore(); | |
didCin = false; | |
} | |
getline(cin, val); | |
} | |
void safeCin(int &val) { | |
cin >> val; | |
didCin = true; | |
} | |
void getPlayerNames(string list[], int n) { | |
for (int i = 0; i < n; i++) { | |
cout << "Enter the name for player " << i+1 << ": "; | |
safeGetLine(list[i]); | |
} | |
} | |
int throwDice() { | |
return (rand() % 6)+1; | |
} | |
string dashTabs (int n) { | |
string output = ""; | |
while (n--) { | |
output.append("--------"); | |
} | |
return output; | |
} | |
int main () { | |
char ch; // Used to pause input | |
srand(time(0)); | |
int diceValue[DICE_COUNT] = {0}; | |
int protocol[ROUND_COUNT+1][PLAYER_COUNT] = {{0}}; | |
string playerName[PLAYER_COUNT]; | |
int bestScore = 0; | |
cout << endl << " ==== Let's play Yatzy ==== "; | |
cout << endl << " ===== Daniel Jönsson ===== " << endl << endl; | |
getPlayerNames(playerName, PLAYER_COUNT); | |
cout << endl; | |
for (int round = 0; round < ROUND_COUNT; round++) { | |
cout << "Try to get as many dices with " << round+1 << " as possible" << endl; | |
cin.get(ch); didCin = true; | |
// Each player | |
for (int p = 0; p < PLAYER_COUNT; p++) { | |
cout << playerName[p] << "'s turn" << endl; | |
cin.get(ch); didCin = true; | |
int score = 0; | |
// Three throws | |
for (int t = 0; t < 3 && score < DICE_COUNT; t++) { | |
int throwScore = 0; | |
cout << "Throw " << t+1 << " of 3" << endl; | |
for (int i = 0; i < DICE_COUNT; i++) { | |
if (t == 0) { | |
// Reset the diceValues array | |
diceValue[i] = 0; | |
} | |
if (diceValue[i] != round+1) { | |
diceValue[i] = throwDice(); | |
cout << " " << diceValue[i]; | |
if (diceValue[i] == round+1) { | |
score++; | |
throwScore++; | |
} | |
} else { | |
//cout << " "; | |
} | |
} | |
if (throwScore > 0) { | |
cout << " (+" << throwScore << ")"; | |
} | |
cout << endl; | |
cin.get(ch); didCin = true; | |
} | |
protocol[round][p] = score; | |
protocol[ROUND_COUNT][p] += score; // Score sum | |
if (bestScore < protocol[ROUND_COUNT][p]) { | |
bestScore = protocol[ROUND_COUNT][p]; | |
} | |
cout << playerName[p] << " scored " << score << " in this round" << endl; | |
cin.get(ch); didCin = true; | |
} | |
cout << " == Scoreboard ==" << endl; | |
cout << "Round\t"; | |
for (int r = -1; r <= round; r++) { | |
if (r > -1) { | |
cout << r+1 << "\t"; | |
} | |
for (int p = 0; p < PLAYER_COUNT; p++) { | |
// Write name as headers | |
if (r == -1) { | |
cout << playerName[p] << "\t"; | |
} else { | |
cout << protocol[r][p] << "\t"; | |
} | |
} | |
cout << endl; | |
} | |
cout << dashTabs(PLAYER_COUNT+1); | |
cout << endl; | |
cout << "Total:\t"; | |
for (int p = 0; p < PLAYER_COUNT; p++) { | |
cout << protocol[ROUND_COUNT][p] << "\t"; | |
} | |
cout << endl; | |
if (ROUND_COUNT - round - 1 > 0) { | |
cout << endl << ROUND_COUNT - round - 1 << " round(s) remaining" << endl; | |
} | |
cin.get(ch); didCin = true; | |
} | |
cout << "Winner(s) with the score " << bestScore << ":" << endl << endl; | |
for (int p = 0; p < PLAYER_COUNT; p++) { | |
if (protocol[ROUND_COUNT][p] == bestScore) { | |
cout << playerName[p] << endl; | |
} | |
} | |
cout << endl << "Congratulations!" << endl << "Press enter to save the scoreboard and end the program."; | |
cin.get(ch); didCin = true; | |
ofstream out; | |
out.open("scoreboard.txt"); | |
// I should reuse the code above... somehow... | |
out << " == Scoreboard ==" << endl; | |
out << "Round\t"; | |
for (int r = -1; r < ROUND_COUNT; r++) { | |
if (r > -1) { | |
out << r+1 << "\t"; | |
} | |
for (int p = 0; p < PLAYER_COUNT; p++) { | |
// Write name as headers | |
if (r == -1) { | |
out << playerName[p] << "\t"; | |
} else { | |
out << protocol[r][p] << "\t"; | |
} | |
} | |
out << endl; | |
} | |
out << dashTabs(PLAYER_COUNT+1); | |
out << endl; | |
out << "Total:\t"; | |
for (int p = 0; p < PLAYER_COUNT; p++) { | |
out << protocol[ROUND_COUNT][p] << "\t"; | |
} | |
out << endl; | |
out.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment