JackBlack Card Game

JackBlack Card Game

 Software Analysis & Design II Assignment 2 – JackBlack

Background Information

Hot off his statistics-based flop School of R, Jack Black decided he needed to cool off. He hit up the casinos for a few games of cards. Failing to realize how little he learned from his movie, he lost game after game, but Jack was anything if not tenacious. He decided to take matters into his own hands. Struck by inspiration, Jack Black created his own card game. He called it: JackBlack. Your task will be to write a program that simulates the game he ultimately created.

Basic Information

In each round of JackBlack, multiple players play against one another. Each player’s goal is to get the value of their collection of cards—called their hand—as close to the value 31 as possible. If a player’s hand value goes over 31, they bust, meaning they immediately lose the round. The player with the highest hand value not exceeding 31 wins. Players holding hands with the same highest value tie the round.
JackBlack does not use a standard deck of 52 playing cards; instead, each deck varies in the number of cards it holds and contains three different kinds of cards. Number cards display a single number in the range 1 to 15. Each number card’s value is as-is; in other words, a “5” card has a value of 5. Word cards display a single lowercase word with only letters; no whitespace, punctuation, or numbers. Each word card’s value is determined by its consonants and vowels—each consonant is worth 1 and each vowel is worth 2. The total value is the sum of these consonants and vowels modulo 16, plus 1. In other words, a word card’s value is at least 1 and at most 16. The word card “apples” has a value of 8 and “bureaucracies” has a value of 5. Note that ‘y’ is a consonant. Symbol cards display a single symbol and have associated minimum and maximum values. Whether the minimum or maximum value is “chosen” depends on the player’s other cards. The value that a symbol card contributes to a hand is calculated to maximize the
player’s points without going bust. For example, assume a player has a 3 number card, a j symbol card with a minimum of 7 and a maximum of 17, a –symbol card with a minimum of 6 and a maximum of 9, and a ¦ symbol card with a minimum of 10 and a maximum of 20. In this case, there are many possible value configurations:
(ALL MAX)
3 + 17 + 9 + 20 = 49.
(TWO MAX & ONE MIN)
3 + 7 + 9 + 20 = 39.
3 + 17 + 6 + 20 = 46.
3 + 17 + 9 + 10 = 39.
(ONE MAX & TWO MIN)
3 + 7 + 6 + 20 = 36.
3 + 17 + 9 + 10 = 39.
3 + 7 + 9 + 10 = 29.
(ALL MIN)
3 + 7 + 6 + 10 = 26.

Notice that the configuration that yields the best hand value without going bust, 29, is the one with theminimum of the j symbol card, the maximum of the –symbol card, and the minimum of the ¦ symbolcard. The 3 number card value remains constant. The program will be responsible for choosing the bestvalue configuration per player hand. Note that if no possible configurationyields a value of 31 or less, thenchoose the minimum value configuration and consider the player busted.
At the beginning of each round, starting with whomever is considered the first player, each player is dealtone card from the top of the deck. Then, starting with the first player, each player decides whether tohit—take another card from the deck—or stand—keep their current hand for the round. Each playerhas a certain level of risk that they are willing to live with. In other words, each player has a maximumhit value, between 1 and 30 inclusive, that defines the maximum value they can have in their hand thatstill allows them to hit again. A player keeps hitting until they bust or until they exceed their maximumhit value. Each player must complete this flow before the next player is reached. The round is over whenall the players have been cycled through once.
The simulation ends in the round when the deck runs out of cards. There will almost definitely be asituation where the deck runs out, but players still need to keep hitting. For this reason, in just this lastround, you will effectively reset the deck to its starting position and play out the remainder of the roundfrom the top of the deck. Then, the simulation ends. At this point, the program shows interestinganalytics about the game’s rounds and the players. For each player, you will show the number of handswon, lost, and tied. Afterwards, you will also show the average number of hits per player per round, theaverage size of a winning hand, and the average size of a losing hand. Where necessary, all decimal valuesshould be rounded to two decimal places.
For 0.25 points of extra credit each, you can also print the following analytics information:
• The average number of number, word, and symbol cards appearing in winning hands.
• A list of the words appearing in winning hands.

Input File Formats

Your program will take in a file as input to the program that represents the game deck. The deck iscomprised of any number of number, word, and symbol cards. Assume that the deck will always haveenough cards to last more than one round. An example input deck file appears below. The first line inthe file contains the total number of cards in the deck file. Each subsequent line starts with the type ofcard (N = number card, W = word card, and S = symbol card) and continues with that card’s necessaryinformation (the number, word, and minimum/maximum values respectively). Note that the top of thefile is considered the bottom of the deck.

17
N 5
N 11
N 8
W school
W shallow
W tenacious
W demolition
S k 6 16
S j 7 17
S h 8 18
S i 9 19
N 15
W fidelity
W destiny
N 2
S ¦ 10 20
S –6 9
Your program will also take in a file as input to the program that represents the players configuration.
Assume that there will be a minimum of two players and a maximum of ten players. An example playerconfiguration file appears below. The first line is the number of players. Each subsequent line representsa player, containing a one word name and a maximum hit value. The dealer is not considered special inJackBlack. The dealer is considered a regular player, and appears first in the configuration.
5
Dealer 20
Simon 30
Mary 26
James 22
Samantha 18

Program Startup

The program on startup takes up to two input files as arguments:
$ ./JackBlack <deck file name><player file name>
For instance:
$ ./JackBlack deck1.data players1.data
Running simulation with players:
Dealer (Max Hit: 20)
JackBlack (Max Hit: 25)
Ann (Max Hit: 21)
Charles (Max Hit: 17)
(…normal program flow)
Two files will be provided by default: defaultDeck.data and defaultPlayers.data. If the program is supplied
no deck file and/or no players file, use these default data files instead.
$ ./JackBlack deck1.data
No players file provided. Using default players file instead.
Running simulation with players:
Dealer (Max Hit: 20)
JackBlack (Max Hit: 25)
(…normal program flow)
$ ./JackBlack
No deck file provided. Using default deck file instead.
No players file provided. Using default players file instead.
Running simulation with players:
Dealer (Max Hit: 25)
JackBlack (Max Hit: 27)
(…normal program flow)
You may not assume that any input files provided as arguments to your application are valid. If a user supplies a file that does not exist, then you must print “File does not exist: <invalid file name>.”
You must not abort the program, but instead you should proceed with the respective default file.
$ ./JackBlack invalidDeck.data invalidPlayers.data
File does not exist: invalidDeck.data.
No deck file provided. Using default deck file instead.
File does not exist: invalidPlayers.data.
No players file provided. Using default players file instead.
Running simulation with players:
Dealer (Max Hit: 25)
JackBlack (Max Hit: 27)
(…normal program flow)

Output Format

Your program should output diagnostic information to the screen, including the introductory setup, the round by round results, and the final analysis. Your output should conform to the following format:
Running simulation with players:
Dealer (Max Hit: 24)
Simon (Max Hit: 25)
Michael (Max Hit: 23)
ROUND 1
Dealer: 5, 15, 4, 3. TOTAL: 27.
Simon: apples (8), k (6/16 –> 16), 8. TOTAL: 32 (BUST).
Michael: 7, 10, 5, 5. TOTAL: 28.
WINNER: Michael.
ROUND 2
Dealer: 5, 15, 4, 3. TOTAL: 27.
Simon: apples (8), k (6/16 –> 16), 6. TOTAL: 30.
Michael: 7, 10, 5, 5. TOTAL: 28.
WINNER: Simon.
ROUND 3
Dealer: 5, 15, 4, 4. TOTAL: 28.
Simon: apples (8), k (6/16 –> 16), 8. TOTAL: 32 (BUST).
Michael: 7, 10, 5, 5. TOTAL: 28.
TIED: Dealer, Michael.
FINAL TALLY
Dealer. WINS: 0. LOSSES: 2. TIES: 1.
Simon. WINS: 1. LOSSES: 2. TIES: 0.
Michael. WINS: 1. LOSSES: 1. TIES: 1.
GAME ANALYSIS
Average number of hits per player per round: 2.56.
Average number of cards in winning hands: 4.33.
Average number of cards in losing hands: 5.17.
Average number of number cards in winning hands: 1.43.
Average number of word cards in winning hands: 2.17.
Average number of symbol cards in winning hands: 3.10.
Words from winning hands: tropic, community, nacho, king.
Assignment Completion Phases

This assignment will be completed in two phases.
Program Design. You are responsible for looking through this specification and creating a design
proposal outlining the classes you expect to have, the precise functionality that these classes provide, thedata members/structures that these classes require, and how your classes interact with one another. Besure to note whether any classes have inheritance relationships. Your design explanation should featurelittle to no code. It should instead focus on the macro-elements of your proposed program. You mayinclude UML diagrams.

Program Implementation. After you submit your design proposal, a specific design will be given to you to implement. You will not be implementingyour proposed design. Keep in mind that the design given to you is perhaps not the “perfect” solution.
Your design proposal may have been equally valid. Nevertheless, you should be able to implement anydesign given to you.

Provided Files

For this assignment, you are not given any starter code. You will be provided some sample deck andplayers input files that can be used to test your program, and the default deck and players input files. Youwill also be given an executable file that is a working solution based on this specification. Because theexecutable’s solution code was compiled on the Linux lab machines, the executable can only be run onthe Linux machines with confidence. Running it on any other system may yield undefined behavior. 

Deck.cpp 

#ifndef __DECK_CPP_

#define __DECK_CPP_

#include “Deck.h”

class Deck {

public:

//contructor

Deck();

// Reads in deck from file and stores it in a stack

//user selects the deck

void deckLoad();

//If the deck is not empty it returns false, the round ends

//if deck is empty, returns true and ends round

bool empty();

//at the end of every round deals new set of cards to each player

//pops cards from the top, only if the player hasn’t reached MAX

bool deal();

//if the player hits, deal a card

bool hit();

};

Deck::Deck(){

cout<<“object created”<<endl;

}

void Deck::deckLoad(){

//stack to store each card

//must be type card to store each symbol

//vector <int> cards;

//will store first number in textfile

//int amount;

//will check if its a number, symbol, word

//string cardType;

ifstream input;

//to store data being read from file

string line;

input.open(“deck.numbers.small.data”);

//if file is successfully opened

//        if (input.is_open())

//        {

//            cout<<“files opened”<<endl;

//            input>> amount;

//            for (int i =0; i<=amount; i++)

//            {

//                while (getline(input, line))

//                {

//                    input>> cardType;

//                    if (cardType == “N”)

//                    {

//                        //calculates for space

//                        input>> cardType;

//                        input>> cardType;

//                        stoi(cardType);

//                        cards.push_back(cardType);

//                    }

//                }

//                //stores input into line

//                //pushes back into vector

//                //card.push_back(line);

//            }

//            return;

//        }

//reads in first line, stores amount of cards to be stored

//loops that many times

//first character is stored as card type

//if its and S store max AND min value

return;

}

bool Deck::empty(){

return true;

}

bool Deck::hit(){

return true;

}

bool Deck::deal(){

return true;

}

int main()

{

//create a deck object

Deck deal;

//    deal.deckLoad();

ifstream input;

//to store data being read from file

int amount;

string line;

input.open(“deck.numbers.small.data”);

//if file is successfully opened

if (input.is_open())

{

cout<<“files opened”<<endl;

//stores amount of cards in deck

input>> amount;

//skip line after card count, its blank

getline(input, line);

//loops until all numbers are stored

for (int i =0 ; i<= amount; i++)

{

//used to iterate through a line

char str[80];

string num = “”;

//reads and stores in a line in str

getline(input, line);

cout<<“line : “<<line<<endl;

for (int a = 0; a> line.length(); a++)

{

if (str[a] == ” “)

{

a++;

}

str= a;

}

//            cout<<“str now = : “<<str[0]<<endl;

//            cout<<“youre in the I LOOP “<<endl;

// && str[i] != ‘\0’

//            for (int j= 0; j<= str[i] && str[i] != ‘\0’; j++)

//            {

//                cout<<“this iz str[i] = “<<str[i]<<endl;

//                if (str[i] == ‘ ‘)

//                {

//                    return 0;

//                }

//                else if(str[i] == ‘N’)

//                {

//                    return 0;

//                }

//                else{

//                    //stores numbers

//                    num = num+ str[i];

//                }

//            }

//            cout<< num<<endl;

}

}

return 0;

}

#endif

Deck.h 

#ifndef __DECK_H_

#define __DECK_H_

#include <iostream>

#include <fstream>

#include <vector>

#include <string>

using namespace std;

//function definitions

/*

(has-a card) cards will be stored in a stack

– this function will check if the deck is empty, (bool) empty(),

– end of each round deal new cards pop()

– if not all players are dealt cards in a round because of shortage, that round is

aborted

*/

//class Deck {

//public:

//    //contructor

//    Deck();

//    // Reads in deck from file and stores it in a stack

//    //user selects the deck

//    void deckLoad();

//    //If the deck is not empty it returns false, the round ends

//    //if deck is empty, returns true and ends round

//    bool empty();

//

//    //at the end of every round deals new set of cards to each player

//    //pops cards from the top, only if the player hasn’t reached MAX

//    bool deal();

//

//    //if the player hits, deal a card

//    bool hit();

//};

#endif

 Solution

  JackBlack.cpp

#include <iostream>

#include <fstream>

#include <vector>

#include <cstring>

#include <stack>

#include <stdlib.h>

#include <cmath>

using namespace std;

class Card

{

public:

int flag;// 0 = number, 1 = word, 2 = symbol

int number;

char* word;

int wordValue;

char symbol;

int minValue;

int maxValue;

//nmf == 0 => min else max

int min_max_flag;

int cardValue;

Card(){};

~Card(){};

Card(int f, int num, char* w, char sym, int nn, int mm, int f1)

{

flag = f;

number = num;

word = w;

wordValue = calWordValue(word);

symbol = sym;

minValue = nn;

maxValue = mm;

min_max_flag = f1;

setCardValue();

}

int calWordValue(char* w)

{

int val = 0;

for(int i = 0; i<strlen(w); i++){

if(w[i] == ‘a’ ||w[i] == ‘e’ ||w[i] == ‘i’ ||w[i] == ‘o’ ||w[i] == ‘u’ ){

val += 2;

}else{

val += 1;

}

}

return val;

}

void setCardValue()

{

if(flag == 0){

cardValue = number;

}else if(flag == 1){

cardValue = wordValue;

}else if(flag == 2){

if(min_max_flag == 0){

cardValue = minValue;

}else{

cardValue = maxValue;

}

}

}

int getCardValue()

{

return cardValue;

}

};

class Player

{

private:

int handValue;

public:

string name;

int maxHitValue;

vector<Card> hand;

int wins;

int loses;

int ties;

Player();

~Player(){}

void setName(string n) { name = n; }

void setMaxHitValue(int m) { maxHitValue = m; }

int getHandValue()

{

calculateHandValue();

return handValue;

}

void calculateHandValue(){

int sum = 0;

for(int i = 0; i < hand.size(); i++)

{

sum += hand.at(i).cardValue;

//printf(“%d\n”, hand.at(i).cardValue);

}

handValue = sum;

}

void addCardToHand(Card card)

{

hand.push_back(card);

}

void setWins()

{

wins = wins + 1;

}

void setLoses()

{

loses = loses + 1;

}

void setTies()

{

ties = ties + 1;

}

};

Player:: Player()

{

handValue = 0;

wins = 0;

loses = 0;

ties = 0;

}

stack<Card> deck;

stack<Card> oldDeck;

vector<Player> players;

int str2int(char*);

void getDeck(char* deckFileName);

void getplayers(char* playerFileName);

void simulation();

void round();

void oneCardPerPlayer();

void addCardsToDeck();

void roundAnalysis();

void initializePlayerHands();

FILE *outFile;

bool isLastRound = false;

float avenum_hitsPerPlayerPerRound = 0.00;

float avenum_cardsInWinningHand = 0.00;

int num_WinningHand = 0;

float avenum_cardsInLosingHand = 0.00;

int num_LosingHand = 0;

float avenum_numberInWinningHand = 0.00;

float avenum_wordInWinningHand = 0.00;

float avenum_symbolInWinningHand = 0.00;

vector<char*> wordsFromWinningHand;

int main(int argc, char* argv[])

{

outFile = fopen(“JackBlackSolutioin”, “w”);

char* deckFileName;

char* playerFileName;

if(argc == 3)

{

deckFileName = argv[1];

playerFileName = argv[2];

}else{

//fprintf(outFile, “No players file provided. Using default players file instead. \n”);

fprintf(outFile, “No deck file provided. Using default deck file instead.\nNo players file provided. Using default players file instead.\n”);

deckFileName = “defaultDeck.data”;

playerFileName = “defaultPlayers.data”;

}

getDeck(deckFileName);

getplayers(playerFileName);

/*

printf(“size %d\n”, deck.size());

int len = deck.size();

for (int i = 0; i < len;i++){

Card card = deck.top();

deck.pop();

if(card.flag == 0){

fprintf(outFile, “%d\n”, card.number);

}else if(card.flag == 1){

fprintf(outFile, “%s    %d\n”, card.word, card.wordValue);

}else{

fprintf(outFile, “%c  %d  %d\n”, card.symbol, card.minValue, card.maxValue);

}

}

*/

for(int i = 0; i < players.size(); i++)

{

Player player = players.at(i);

//printf(“%s  \n”, players.at(0).name);

fprintf(outFile, “%s  %d\n”, player.name.c_str(), player.maxHitValue);

//printf(“%s (Max Hit : %d) \n”, player.name, player.maxHitValue);

}

simulation();

fclose(outFile);

return 0;

}

void simulation()

{

//printf(“%d”, deck.size());

fprintf(outFile, “%s\n”, “Running Simulation with players:”);

for(int i = 0; i < players.size(); i++)

{

Player player = players.at(i);

//printf(“%s  \n”, players.at(j).name);

fprintf(outFile, “%s (Max Hit : %d) \n”, player.name.c_str(), player.maxHitValue);

//printf(“%s (Max Hit : %d) \n”, player.name, player.maxHitValue);

}

fprintf(outFile, “\n\n”);

int roundCount = 1;

while(isLastRound == false)

{

fprintf(outFile, “ROUND %d\n”, roundCount);

initializePlayerHands();

round();

//printf(“test %d”, deck.size());

roundAnalysis();

/*

if(deck.empty()){

isLastRound = true;

}*/

//printf(“TEST”);

roundCount++;

fprintf(outFile, “\n\n”);

}

//Print Final Tally

fprintf(outFile, “%s\n”, “FINAL TALLY”);

for(int j = 0; j < players.size(); j++){

Player player = players.at(j);

//printf(“%s  \n”, players.at(j).name);

fprintf(outFile, “%s. WINS: %d. LOSES: %d. TIES: %d.\n”, player.name.c_str(), player.wins, player.loses, player.ties);

}

//Print average statistic

avenum_hitsPerPlayerPerRound = avenum_hitsPerPlayerPerRound / (roundCount – 1);

avenum_cardsInWinningHand = avenum_cardsInWinningHand / num_WinningHand;

avenum_cardsInLosingHand = avenum_cardsInLosingHand / num_LosingHand;

avenum_numberInWinningHand = avenum_numberInWinningHand / num_WinningHand;

avenum_wordInWinningHand = avenum_wordInWinningHand / num_WinningHand;

avenum_symbolInWinningHand = avenum_symbolInWinningHand / num_WinningHand;

fprintf(outFile, “\n\nGAME ANALYSIS\n”);

fprintf(outFile, “Average number of hits per player per round: %.2f.\n”, avenum_hitsPerPlayerPerRound);

fprintf(outFile, “Average number of cards in winning hands: %.2f\n”, avenum_cardsInWinningHand);

fprintf(outFile, “Average number of cards in losing hands: %.2f\n”, avenum_cardsInLosingHand);

fprintf(outFile, “Average number of number cards in winning hands: %.2f\n”, avenum_numberInWinningHand);

fprintf(outFile, “Average number of word cards in winning hands: %.2f\n”, avenum_wordInWinningHand);

fprintf(outFile, “Average number of symbol cards in winning hands: %.2f\n”, avenum_symbolInWinningHand);

fprintf(outFile, “Words from winning hands: “);

if(wordsFromWinningHand.size() == 1){

fprintf(outFile, “%s”, wordsFromWinningHand.at(0));

}else if(wordsFromWinningHand.size() > 1){

fprintf(outFile, “%s”, wordsFromWinningHand.at(0));

for(int k = 1; k < wordsFromWinningHand.size(); k++)

{

fprintf(outFile, “%s”, wordsFromWinningHand.at(0));

}

}

fprintf(outFile, “\n”);

}

void oneCardPerPlayer()

{

//distribute one card per each player for the round

for(int i = 0; i < players.size() ; i++)

{

if(deck.empty()){

addCardsToDeck();

isLastRound = true;

}

Card card = deck.top();

deck.pop();

oldDeck.push(card);

Player player = players.at(i);

player.addCardToHand(card);

players.at(i) = player;

}

}

void initializePlayerHands()

{

for (int i = 0; i < players.size(); i++)

{

Player player = players.at(i);

player.hand.clear();

players.at(i) = player;

}

}

void addCardsToDeck()

{

for(int i = 0; i < 100; i++){

if(!oldDeck.empty()){

Card card = oldDeck.top();

oldDeck.pop();

deck.push(card);

}

}

}

void round()

{

oneCardPerPlayer();

//play one round

int i = 0;

int hitsPerPlayer = 0;

while(i < players.size() )

{

Player player = players.at(i);

while(player.getHandValue() < player.maxHitValue)

{

if(deck.empty()){

addCardsToDeck();

isLastRound = true;

}

Card card = deck.top();

deck.pop();

oldDeck.push(card);

//printf(“%d %d “, card.flag, card.number);

if(card.flag == 2)

{// if card is symbol

if(player.getHandValue() + card.maxValue > 31)

{// choose minimum value in order not to be busted

card.min_max_flag = 0;

card.setCardValue();

}else{

int f = (int)(rand() * 2);

card.min_max_flag = f;

card.setCardValue();

}

}//end if

player.addCardToHand(card);

players.at(i) = player;

hitsPerPlayer++;

}

//    printf(“\n”);

//next player starts to get cards

i++;

}//end while

avenum_hitsPerPlayerPerRound =avenum_hitsPerPlayerPerRound + hitsPerPlayer / players.size();

}

void roundAnalysis()

{

Player winningPlayer = players.at(0);

vector<Player> tieplayers;

int winningIndex = 0;

vector<int> tiesIndex;

tieplayers.push_back(players.at(0));

tiesIndex.push_back(0);

for(int i = 1; i<players.size();i++)

{

Player player = players.at(i);

if(player.getHandValue() == winningPlayer.getHandValue())

{

tieplayers.push_back(player);

tiesIndex.push_back(i);

}else if(player.getHandValue() > winningPlayer.getHandValue() && player.getHandValue() <= 31){

winningPlayer = player;

winningIndex = i;

tieplayers.clear();

tiesIndex.clear();

tieplayers.push_back(player);

tiesIndex.push_back(i);

}

}

//prints the round result

for(int k = 0 ; k < players.size(); k++)

{

Player player = players.at(k);

fprintf(outFile, “%s:   “, player.name.c_str());

//printf(“%d\t”,player.hand.size());

for(int j = 0; j < player.hand.size() ; j++)

{

Card card = player.hand.at(j);

if(card.flag == 0){

fprintf(outFile, “%d   “, card.number);

}else if(card.flag == 1){

fprintf(outFile, “%s (%d)   “, card.word, card.wordValue);

}else if(card.flag == 2){

if(card.min_max_flag==0){

fprintf(outFile, “%c (%d/%d –> %d)   “, card.symbol, card.minValue, card.maxValue, card.minValue);

}else{

fprintf(outFile, “%c (%d/%d –> %d)   “, card.symbol, card.minValue, card.maxValue, card.maxValue);

}

}

}

fprintf(outFile, “Total: %d \n”, players.at(k).getHandValue());

}

if(tieplayers.size() == 1){

//if there is a winning player

winningPlayer.setWins();

players.at(winningIndex) = winningPlayer;

fprintf(outFile, “WINNER: %s\n”, winningPlayer.name.c_str());

avenum_cardsInWinningHand = avenum_cardsInWinningHand + winningPlayer.hand.size();

num_WinningHand++;

//strcat(wordsFromWinningHand, “sfsd”);

for(int h = 0; h < winningPlayer.hand.size();h++)

{

Card card = winningPlayer.hand.at(h);

if(card.flag == 0){

avenum_numberInWinningHand = avenum_numberInWinningHand + 1;

}else if(card.flag == 1){

avenum_wordInWinningHand = avenum_wordInWinningHand + 1;

wordsFromWinningHand.push_back(card.word);

}else{

avenum_symbolInWinningHand = avenum_symbolInWinningHand + 1;

}

}

//set loses

for(int p = 0; p < players.size(); p++){

if(p == winningIndex) p++;

if(p >= players.size()) break;

Player player = players.at(p);

player.setLoses();

players.at(p) = player;

avenum_cardsInLosingHand = avenum_cardsInLosingHand + player.hand.size();

num_LosingHand++;

}

}else{

fprintf(outFile, “TIES: “);

for(int j = 0; j < tieplayers.size() ; j++){

Player player = tieplayers.at(j);

player.setTies();

players.at(tiesIndex.at(j)) = player;

fprintf(outFile, “%s\n”, tieplayers.at(j).name.c_str());

//set loses

//set loses

for(int p = 0; p < players.size(); p++){

bool flag = false;

for(int t = 0; t < tieplayers.size(); t++){

if(p == t){

flag = true;

}

}

if(!flag){

Player player = players.at(p);

player.setLoses();

players.at(p) = player;

avenum_cardsInLosingHand = avenum_cardsInLosingHand + player.hand.size();

num_LosingHand++;

}

}

}

}

}

int str2int(char* str)

{

int len = strlen(str);

int val = 0;

for(int i = len-1; i >=0; i –)

{

val += (pow(10, i)*(str[len-i-1] – 48));

}

return val;

}

void getDeck(char* deckFileName){

FILE *deckFile;

deckFile = fopen(deckFileName, “r”);

if(deckFile == NULL){

deckFile = fopen(“defaultDeck.data”, “r”);

}

char line[50];

if(deckFile != NULL)

{

fgets(line, 50, deckFile);

//int totalNumber = str2int(line);

//printf(“%d\n”, totalNumber);

while(fgets(line, 50, deckFile) != NULL && line[0] != ‘\0’)

{

int len = strlen(line);

if(line[0] == ‘N’)

{

char num[50];

for(int i = 2; i< len-1;i++){

num[i-2] = line[i];

}

num[len-3] = ‘\0’;

int number = str2int(num);

//printf(“%d\n”, number);

Card card(0, number, “NULL”, ‘s’, 0, 0, 0);

deck.push(card);

}else if(line[0] == ‘W’)

{

char word[20];

for(int i = 2; i< len-1;i++){

word[i-2] = line[i];

}

word[len-3] = ‘\0’;

//printf(“%s\n”, word);

Card card(1, 0, word, ‘s’, 0, 0, 0);

deck.push(card);

}else if(line[0] == ‘S’)

{

char minString[20];

char maxString[20];

int flag = 0;

for(int i = 4; i < len – 1; i++)

{

if(line[i] == ‘ ‘){

flag = i + 1;

minString[i-4] = ‘\0’;

i++;

}

if (flag == 0){

minString[i-4] = line[i];

}else{

maxString[i – flag] = line[i];

}

}

maxString[len-strlen(minString)-6] = ‘\0’;

int minValue = str2int(minString);

int maxValue = str2int(maxString);

//printf(“%d %d\n”, minValue, maxValue);

Card card(2, 0, “”, line[2], minValue, maxValue, 1);

deck.push(card);

}

}

}

fclose(deckFile);

}

void getplayers(char* playerFileName)

{

FILE *playerFile;

playerFile = fopen(playerFileName, “r”);

if(playerFile == NULL){

playerFile = fopen(“defaultplayers.data”, “r”);

}

char line[30];

if(playerFile != NULL)

{

Player *player;

fgets(line, 30, playerFile);

int totalNumber = atoi(line);

while(fgets(line, 30, playerFile) && line[0] != ‘\0’)

{

int i  = 0;

while(i < strlen(line))

{

if(line[i] == ‘ ‘) break;

i++;

}

char name[30];

strncpy(name, line, i);

name[i] = ‘\0’;

char num[10];

for(int j = i+1; j < strlen(line)-1; j++)

{

num[j-i-1] = line[j];

}

num[strlen(line)-i-2] = ‘\0’;

int maxHitNum = str2int(num);

player = new Player;

player->setName(name);

player->setMaxHitValue(maxHitNum);

players.push_back(*player);

}

}

fclose(playerFile);

}