//Dylan Lawrence IT312 Final Farkle
#include <iostream>
#include <string>
#include <fstream>
#include <ctime> // ctime combines asctime and localtime functions for tracking purposes
using namespace std;
class Dice //rolls 6 Dice per player
{
private:
int valueOfDice[6];
bool valueOfReroll[6];
int countPerDie;
int rerollCount;
public:
Dice() //constructor https://e...content-available-to-author-only...e.com/w/cpp/language/initializer_list
{
for (int i = 0; i < 6; i++) {
valueOfDice[i] = 0; //aggregate initialization
valueOfReroll[i] = false; //giving warnings on debugger.
}
countPerDie = 0;
rerollCount = 0;
}
void diceRoll() { //first roll of all six dice
for (int i = 0; i < 6; i++) {
valueOfDice[i] = (rand() % 6 + 1);
if (valueOfDice[i] != 1 && valueOfDice[i] != 5) {
valueOfReroll[i] = true;
}
}
}
void anotherDiceRoll() { //rolls dice again or multiple times when not worth points
for (int i = 0; i < 6; i++) {
if (valueOfReroll[i] == true)
valueOfDice[i] = (rand() % 6 + 1);
}
}
void valueOfReroll() { // set false to valueOfReroll
for (int i = 0; i < 6; i++) {
valueOfReroll[i] = false;
}
}
int pointsPerRound() { //acquires point value for the dice
int numOne = 0, numTwo = 0, numThree = 0, numFour = 0, numFive = 0, numSix = 0;
int points = 0;
countPerDie = 0;
for (int i = 0; i < 6; i++) {
switch (valueOfDice[i]) {
case 1:
points += 100;
numOne++;
break;
case 2:
numTwo++;
countPerDie++;
break;
case 3:
numThree++;
countPerDie++;
break;
case 4:
numFour++;
countPerDie++;
break;
case 5:
points += 50;
numFive++;
break;
case 6:
numSix++;
countPerDie++;
break;
}
}
if (countPerDie == 6) {
cout << "Well That's FARKLE!!!";
points = 0;
}
else {
if (numOne >= 3) { // paste to 5
points = (points - 300) + 1000;
}
else if (numTwo >= 3) { //copy-pasta'd ahead
points += 200;
for (int i = 0; i < 6; i++) {
if (valueOfDice[i] == 2) {
valueOfReroll[i] = false;
}
}
countPerDie -= 3;
}
else if (numThree >= 3) {
points += 300;
for (int i = 0; i < 6; i++) {
if (valueOfDice[i] == 3) {
valueOfReroll[i] = false;
}
}
countPerDie -= 3;
}
else if (numFour >= 3) {
points += 400;
for (int i = 0; i < 6; i++) {
if (valueOfDice[i] == 4) {
valueOfReroll[i] = false;
}
}
countPerDie -= 3;
}
else if (numFive >= 3) {
points = (points - 150) + 500;
}
else if (numSix >= 3) {
points += 600;
for (int i = 0; i < 6; i++) {
if (valueOfDice[i] == 6){
valueOfReroll[i] = false;
}
}
countPerDie -= 3;
}
}
return points;
}
//not gonna lie, My tutor helped me out with this section
void showDice() {//Shows the values of all six die
for (int i = 0; i < 6; i++) {
cout << valueOfDice[i] << " ";
}
}
void setNumRoll(int value) { //think set and get in Blueprints via UE4
countPerDie = value;
}
int getNumRoll() {
return countPerDie;
}
};
class Player { //holds total points and accumulated points
private:
string name;
int pointsAccrued;
int totalPoints;
public:
Player() {
name = "XXXXXX";
pointsAccrued = 0;
totalPoints = 0;
}
Player(string name) {
this->name = name;
pointsAccrued = 0;
totalPoints = 0;
}
void setName(string name) {
this->name = name;
}
string getName() {
return this->name;
}
void addPoints(int pointsperround) {
pointsAccrued = pointsperround;
totalPoints += pointsperround;
}
void setPoints(int points)
{
this->pointsAccrued = points;
}
int getTotalPoints()
{
return totalPoints;
}
int getPointsAccrued()
{
return pointsAccrued;
}
};
class WellThatsFarkle { //should hold name, round, winner initilization
private:
Player* players;
int playerCount;
Dice dice;
public:
WellThatsFarkle(int playerAmt) {//Parameterized
players = new Player[playerAmt];
playerCount = playerAmt;
}
void readPlayerNames() {
string name;
for (int i = 0; i < playerCount; i++){
cout << "Please name player " << (i + 1) << ": ";
cin >> name;
players[i].setName(name);
}
}
void playersToEnterGame() { //should roll dice per player >= thousand
int points = 0;
for (int i = 0; i < playerCount; i++) {
points = 0;
while (points < 1000) {
dice.diceRoll();
dice.showDice();
points = dice.pointsPerRound();
cout << "\t Score: " << points << endl;
}
players[i].addPoints(points);
cout << players[i].getName() << " want's to throw down some dice!!!" << endl << endl;
}
dice.setNumRoll(0);
cout << "Everyone here? Then good luck ;)" << endl << endl;
}
bool hasWonFarkle(int* index) { //once player reached >= ten thousand, acquire winner index
for (int i = 0; i < playerCount; i++) {
if (players[i].getTotalPoints() >= 10000) {
*index = i;
return true;
}
}
return false;
}
void gamePerPlayer(int i) {//rounds per player
char wantsReroll;
int points = 0;
cout << players[i].getName() << " Throws down: " << endl;
dice.diceRoll();
dice.showDice();
points = dice.pointsPerRound();
cout << "\t Accumulated points: " << points << endl;
players[i].setPoints(points);
if (dice.getNumRoll() == 0) {
cout << "You got some points! If you're feeling lucky, play another round." << endl;
players[i].addPoints(points);
dice.anotherDiceRoll();
dice.showDice();
points += dice.pointsPerRound();
cout << "\t Accumulated points: " << points << endl << endl;
players[i].setPoints(points);
}
else if (dice.getNumRoll() != 6) {
cout << endl << "Would you like to reroll the " << dice.getNumRoll()
<< " non-point dice? (Y/N): ";
cin >> wantsReroll;
cout << endl;
if (wantsReroll == 'y' || wantsReroll == 'Y') {
dice.anotherDiceRoll();
dice.showDice();
points = dice.pointsPerRound();
cout << "\t Accumulated points: " << points << endl << endl;
if (players[i].getPointsAccrued() > points)
points = players[i].getPointsAccrued();
players[i].setPoints(points);
}
else if (wantsReroll == 'n' || wantsReroll == 'N') {
cout << players[i].getName() << " got no throws left!" << endl << endl;
dice.setNumRoll(0);
}
}
dice.valueOfReroll(); //Sometimes acts up via debugger?
cout << players[i].getName() << " points from round: " << players[i].getPointsAccrued() << endl;
players[i].addPoints(points);
cout << "Total points after round: " << players[i].getTotalPoints() << endl << endl;
cout << endl << "--------------------------------------------" << endl << endl;
}
void playEachRound(int index) {
for (int i = index; i < playerCount; i++) {
gamePerPlayer(i);
}
}
void playTheGame() { //Winner winner chicken chinner
int index = 0;
while (!hasWonFarkle(&index)) {
playEachRound(0);
}
int i = 0;
int count = 0;
for (i = index + 1; i < playerCount; i++) {
gamePerPlayer(i);
count++;
}
if (count != playerCount - 1) {
for (i = 0; i < index; i++)
gamePerPlayer(i);
}
int max = 0;
for (int i = 0; i < playerCount; i++) {
if (players[i].getTotalPoints() > max)
{
max = players[i].getTotalPoints();
index = i;
}
}
cout << endl << players[index].getName() << " wins the game!" << endl;
}
};
void showRules() //gathering and importing the read .txtfiles are still giving issues
//needed help from online sources to cross reference and take your notes into consideration
{
string line_;
ifstream file_;
file_.open("FarklinRules.txt"); //located in same directories as 6-3 Final BB4
while (getline(file_, line_)) { // I assume I have it in the correct place because,
cout << line_ << '\n'; //feedback received: "This one was a challenge and I understand there were issues."
} //... was the only response I got for my questions/concerns
file_.close();
}
int main(string* players[]) {
srand(time(0));
int playerAmt;
showRules();
cout << endl << "__________________________________________________" << endl << endl;
cout << endl << "\t\t---------- OK Game on!!! ----------" << endl << endl;
//get numPlayers from user
cout << endl << endl << "How many Farkin people wanna play? ";
cin >> playerAmt;
while (playerAmt < 2) {
cin.clear();
cin.ignore();
cout << "Enter an integer" << endl;
cin >> playerAmt;
}
WellThatsFarkle fg(playerAmt); //calls methods
fg.readPlayerNames();
fg.playersToEnterGame();
fg.playTheGame();
system("Hold up!");
return 0;
}