123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- #include "game.h"
- Game::Game()
- {
- // initialize the number of columns that each "color" will contain
- lastColumnIndex = CHECKBOXES - 1;
- // set up lockOut
- for(int i = 0; i<white1;i++)
- {
- lockOut.push_back(false);
- }
- // set the state of game [how to move from state to state ("game logic?")]
- state = NOT_STARTED;
- // dice have been initialized with random seed
- };
- bool Game::addPlayer(std::string name)
- {
- if(NOT_STARTED == state)
- {
- ScoreSheet newplayer(name);
- players.push_back(newplayer);
- fprintf (stdout, "added: %s to the game\n", name.c_str());
- return true;
- }
- else
- {
- fprintf(stdout, "player %s was not added to the game\n", name.c_str());
- return false;
- }
- };
- void Game::score()
- {
- // **CALCULATE EACH PLAYERS' SCORE**
- std::vector<int> pointsGuide = {0,1,3,6,10,15,21,28,36,45,55,66,78};
- // check players' card for marked boxes
- for(int i = 0;i <players.size();i++)
- {
- // clear each players' previous score
- players[i].score = 0;
- // calculate penalty amount
- players[i].score -= players[i].cumulativeOlok.penaltyCount*PENALTY_VALUE;
- // count the Xs of each "color" and give me a score
- for(int j = 0;j <white1;j++)
- {
- // determine if player qualifies for the lockout point (this is a rule)
- int lockoutPoint = players[i].cumulativeOlok.getLastIndex(j) == lastColumnIndex;
- // use pointsGuide to get the score for this user's "color" row
- players[i].score += pointsGuide[players[i].cumulativeOlok.getXCount(j) + lockoutPoint];
- }
- }
- // **CHECK IF GAME IS DONE**
- // check each player's card for penalties
- for(int i = 0;i<players.size();i++)
- {
- // check for maximum penalty to see if game is over
- if(players[i].cumulativeOlok.penaltyCount == MAX_PENALTY){state = FINISHED;}
- }
- // check for lockout to see if game is over
- int lockCount = 0;
- for(int i = 0;i<lockOut.size();i++)
- {
- if(true == lockOut[i]){lockCount++;}
- }
- if(MAX_LOCKOUT <= lockCount)
- {
- state = FINISHED;
- }
- }
- void Game::turn(ScoreSheet activePlayer)
- {
- // start all players' turns
- for(int i = 0; i <players.size();i++)
- {
- players[i].isTurnDone = false;
- }
- // roll the dice
- dice.roll();
- // check to see if all players are done with turn
- while(true)
- {
- bool temp = true;
- for(int i = 0; i <players.size();i++)
- {
- temp = temp && players[i].isTurnDone;
- }
- if(temp){break;}
- Sleep(1);
- }
- // save data from currentTurnOlok into cumulativeOlok
- for(int i = 0; i <players.size();i++)
- {
- players[i].cumulativeOlok.addOlok(players[i].currentTurnOlok);
- }
- // score the cards
- score();
- // go to next player's turn
- activePlayer++;
- activePlayer %= players.size();
- }
- // generate text: per player(48boxes, penalty count), locked off rows, dice roll, game end(when it occurs) send it
- std::string Game::send()
- {
- std::string output;
- if(dice.dice.size()==0){fprintf(stdout, "kyle is cool\n");}
- // add dice values to output
- for(int i = 0; i < dice.dice.size(); i++)
- {
- output.push_back(dice.dice[i] + '0');
- }
- // look at each scoresheet
- for(int i = 0; i < players.size(); i++)
- {
- output.append(players[i].cumulativeOlok.toString());
- }
- // add locked off row to output
- for(int i = 0; i < lockOut.size();i++)
- {
- output.push_back(lockOut[i] ? 'T' : 'F');
- }
- // add game end to output
- output.push_back(state == FINISHED ? 'T' : 'F');
- // print everything needed to send current game state to players
- fprintf(stdout, "the output is: %s\n", output.c_str());
- return output;
- }
- // kyle's intention is to check off red 12 and blue 5
- // " 1143"
- // emma's intention is to take a penalty
- // "1"
- void Game::receive(std::string userInput)
- {
- players[0].currentTurnOlok.clear();
- // penalties are marked to card if user string = 1
- // did the user send a number?
- // ## Range Check to go into Receive ##
- #ifdef WORKING_ON_RANGE_CHECK
- // if index is too high or too low, throw an error
- if(CHECKBOXES > index || 0 < index){fprintf(stdout,"Player[%d] %s index is too high or too low X\n", player, players[player].savedName);}
- // if color is not valid throw an error
- players[0].cumulativeOlok.isColor(red);
- if(color){fprintf(stdout,"Player[%d] %s invalid color selected X\n", player, players[player].savedName);}
- #endif
- // if a user did not send a number no changes take place
- // take the userinput (string) and parse it into parts
- // what is the length of the userInput? length of 2, 4? (boxes 0-9 are represented as 00,01,02...)
- // convert userInput into number
- // user number to change the boolean on corresponding spot on ScoreSheet
- }
- /* add a checkbox to a specified players scoresheet user specifies player's: XXXX, the color, and index of the checkbox on the colored row
- * dont know what XXXX is yet - but it will be used to find correct player
- *
- *
- *
- * the white die and color combo must match the corresponding RowColor and index
- *
- */
- /*
- * all players can do white die combination, active player can do a colored + white die combination.
- *
- * ## Things we need to know ##
- * sum of both white die
- * sums of 8 dice
- * who is active player
- *
- * ## How to do ##
- * on a given turn, sum the white die and sum each of the white die and all of the colored die
- * check to see if each players' requested move is valid
- * - does their input match the sum of the white die
- * check to see if active player requested move is valid
- * - for the active player check white die sum if one input. if it fails then check each white die + color die sums add item to scoresheet
- * - for the active player if two inputs are passsed, check white die sum first if it passes, add to scoresheet. then check the second input with white die + colored die
- * if it is valid, add it to player card
- * if it is not valid, return an error to player
- *
- * ## players' move
- * // this function has access to diceRoll and player input
- * // is the players' input a valid move <- what does valid mean? (is it represented in die, does it follow left to right, does it follow lockout, does it fit within the confines of a move)
- * // the confines of a move for non-actie player is (0 or one white die move)
- * // the confines of a move for active palyer is (penalty or (one or two die moves))
- *
- * //
- */
- bool Game::isMoveRepresentedInDie(int player, CollectionOfMoves collectionOfMoves)
- {
- // is this the non-active player
- if(player != activePlayer)
- {
- // if the move equals the sum of the two white die return true, if not return false
- return collectionOfMoves.white.index == dice.dice[white1] + dice.dice[white2];
- }
- // otherwise this is the active player
- else
- {
- // if one move
- if(1 == collectionOfMoves.numberOfMoves)
- {
- // check sum of white die
- if(collectionOfMoves.white.index != (dice.dice[white1] + dice.dice[white2]))
- {
- int c = collectionOfMoves.color.color;
- // if any white die plus the colored die equals the move
- if(collectionOfMoves.color.index == (dice.dice[white1] + dice.dice[c]) || collectionOfMoves.color.index == (dice.dice[white2] + dice.dice[c]))
- {
- return true;
- }
- // colored die plus either white die was not a move
- else {return false;}
- }
- // otherwise the white sum die was a move
- else {return true;}
- }
- // if two move
- else if(2 == collectionOfMoves.numberOfMoves)
- {
- // check first input with white die sum
- if(collectionOfMoves.white.index != (dice.dice[white1] + dice.dice[white2]))
- {
- // white die sum was not a move
- return false;
- }
- else
- {
- int c = collectionOfMoves.color.color;
- // check to see if color die + either white die sum equals a move
- if(collectionOfMoves.color.index == (dice.dice[white1] + dice.dice[c]) || collectionOfMoves.color.index == (dice.dice[white2] + dice.dice[c]))
- {
- return true;
- }
- else {return false;}
- }
-
- }
- }
- };
- void Game::lockOutRule(int player, int color, int index)
- {
- // player can add an x to olok in last column if there are 5 x
- if(lastColumnIndex == index)
- {
- if(LOCKOUT_QUALIFIER >= players[player].cumulativeOlok.getXCount(color))
- {
- players[player].currentTurnOlok.addX(color, index);
- }
- else{fprintf(stdout, "Player[%d] %s tried to check off column 12 without minimum X\n", player, players[player].savedName);}
- }
- }
- void Game::leftToRightRule(int player, int color, int index)
- {
- // player must add an x to olok from left to right
- if(players[player].cumulativeOlok.getLastIndex(color) > index)
- {
- fprintf(stdout,"Player[%d] %s not following left to right rule X\n", player, players[player].savedName);
- }
- else{players[player].currentTurnOlok.addX(color, index);}
- }
- void Game::addX(int player, int color, int index)
- {
- }
|