123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- #include "game.h"
- Game::Game()
- {
-
- lastColumnIndex = CHECKBOXES - 1;
-
- for(int i = 0; i<white1;i++)
- {
- lockOut.push_back(false);
- }
-
- state = NOT_STARTED;
-
- entropyPool = dice.roll();
- };
- bool Game::addPlayer(std::string name)
- {
- if(NOT_STARTED == state)
- {
-
- ScoreSheet newplayer(name, dice.roll());
- 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()
- {
-
- std::vector<int> pointsGuide = {0,1,3,6,10,15,21,28,36,45,55,66,78};
-
- for(int i = 0;i <players.size();i++)
- {
-
- players[i].score = 0;
-
- players[i].score -= players[i].cumulativeOlok.penaltyCount*PENALTY_VALUE;
-
- for(int j = 0;j <white1;j++)
- {
-
- int lockoutPoint = players[i].cumulativeOlok.getLastIndex(j) == lastColumnIndex;
-
- players[i].score += pointsGuide[players[i].cumulativeOlok.getXCount(j) + lockoutPoint];
- }
- }
-
-
- for(int i = 0;i<players.size();i++)
- {
-
- if(players[i].cumulativeOlok.penaltyCount == MAX_PENALTY){state = FINISHED;}
- }
-
- int lockCount = 0;
- for(int i = 0;i<lockOut.size();i++)
- {
- if(true == lockOut[i]){lockCount++;}
- }
- if(MAX_LOCKOUT <= lockCount)
- {
- state = FINISHED;
- }
- }
- void Game::round()
- {
-
- for(int i = 0; i <players.size();i++)
- {
- players[i].isTurnDone = false;
- }
-
- dice.roll();
-
- while(true)
- {
- bool temp = true;
- for(int i = 0; i <players.size();i++)
- {
- temp = temp && players[i].isTurnDone;
- }
- if(temp){break;}
-
- somethingNeedDoing(1000 , storage);
- }
-
- for(int i = 0; i <players.size();i++)
- {
- players[i].cumulativeOlok.addOlok(players[i].currentTurnOlok);
- }
-
- score();
-
- activePlayer++;
- activePlayer %= players.size();
-
- dice.diceTropy(entropyPool);
- }
- std::string Game::send()
- {
- std::string output;
- if(dice.dice.size()==0){fprintf(stdout, "kyle is cool\n");}
-
- for(int i = 0; i < dice.dice.size(); i++)
- {
- output.push_back(dice.dice[i] + '0');
- }
-
- for(int i = 0; i < players.size(); i++)
- {
- output.append(players[i].cumulativeOlok.toString());
- }
-
- for(int i = 0; i < lockOut.size();i++)
- {
- output.push_back(lockOut[i] ? 'T' : 'F');
- }
-
- output.push_back(state == FINISHED ? 'T' : 'F');
-
- fprintf(stdout, "the output is: %s\n", output.c_str());
- return output;
- }
- struct Turn Game::receive(std::string userInput)
- {
- Turn turnInProgress;
- std::string playerSecret, original = userInput;
- int playerNumber = -1, pos = 0;
-
-
- if("add" == userInput.substr(0,3))
- {
-
- addPlayer(userInput.substr(3));
- ScoreSheet lastPlayer = players.back();
-
- fprintf(stdout, "Player %s has a secret of: %d",lastPlayer.savedName, lastPlayer.secretNumber);
- return turnInProgress;
- }
-
- for(int i = 0;i < userInput.size();i++)
- {
-
- if(',' == userInput[i])
- {
- pos = i; break;
- }
- }
-
- if(0<pos)
- {
- playerSecret = userInput.substr(0,pos-1);
- userInput = userInput.substr(pos);
- int secret = stoi(playerSecret);
-
- for(int i = 0;i < players.size(); i++)
- {
- if(secret == players[i].secretNumber)
- {
- playerNumber = i; break;
- }
- }
- }
- if(-1 == playerNumber)
- {
- fprintf(stdout, "Could not find player: %s", original);
- return turnInProgress;
- }
-
- if(1 == userInput.size())
- {
- turnInProgress.penalty = 1;
- }
-
- else if(2 == userInput.size())
- {
- turnInProgress.moves[0] = translateToMove(userInput);
- turnInProgress.numberOfMoves = 1;
- }
-
- else if(4 == userInput.size())
- {
- turnInProgress.moves[0] = translateToMove(userInput.substr(0,2));
- turnInProgress.moves[1] = translateToMove(userInput.substr(2,2));
- turnInProgress.numberOfMoves = 2;
- }
- else {fprintf(stdout, "Form an opinion: WRONG!");}
-
- if(true == checkTurn(playerNumber, turnInProgress))
- {
-
- players[playerNumber].currentTurnOlok.clear();
-
- for(int i = 0;i < turnInProgress.numberOfMoves;i++)
- {
-
- players[playerNumber].currentTurnOlok.addX(turnInProgress.moves[i]);
- }
-
- players[playerNumber].currentTurnOlok.penaltyCount += turnInProgress.penalty;
- }
-
- entropyPool += time(NULL);
- }
- bool Game::isMoveRepresentedInDie(int player, Turn turn)
- {
-
- if(player != activePlayer)
- {
-
- return turn.moves[0].index == dice.dice[white1] + dice.dice[white2];
- }
-
- else
- {
-
- if(1 == turn.numberOfMoves)
- {
-
- if(turn.moves[0].index != (dice.dice[white1] + dice.dice[white2]))
- {
- int c = turn.moves[1].color;
-
- if(turn.moves[1].index == (dice.dice[white1] + dice.dice[c]) || turn.moves[1].index == (dice.dice[white2] + dice.dice[c]))
- {
- return true;
- }
-
- else {return false;}
- }
-
- else {return true;}
- }
-
- else if(2 == turn.numberOfMoves)
- {
-
- if(turn.moves[0].index != (dice.dice[white1] + dice.dice[white2]))
- {
-
- return false;
- }
- else
- {
- int c = turn.moves[1].color;
-
- if(turn.moves[1].index == (dice.dice[white1] + dice.dice[c]) || turn.moves[1].index == (dice.dice[white2] + dice.dice[c]))
- {
- return true;
- }
- else {return false;}
- }
-
- }
- }
- };
- bool Game::lockOutRule(int player, Turn turn)
- {
-
- for(int i = 0; i<turn.moves.size(); i++)
- {
-
- if(LOCKOUT_QUALIFIER > players[player].cumulativeOlok.getXCount(turn.moves[i].color)
- {
- fprintf(stdout, "Player[%d] %s tried to check off column 12 without minimum X\n", player, players[player].savedName);
- return false;
- }
- }
- return true;
- }
- bool Game::leftToRightRule(int player, Turn turn)
- {
- for(int i = 0; i<turn.moves.size(); i++)
- {
-
- if(players[player].cumulativeOlok.getLastIndex(turn.moves[i].color) > turn.moves[i].index)
- {
- fprintf(stdout,"Player[%d] %s not following left to right rule X\n", player, players[player].savedName);
- return false;
- }
- }
- return true;
- }
- bool Game::checkTurn (int player, Turn turn)
- {
-
- return lockOutRule( player, turn) && leftToRightRule( player, turn) && isMoveRepresentedInDie(player, turn);
- }
- void Game::addX(int player, int color, int index)
- {
- }
- struct Move Game::translateToMove(std::string temp)
- {
- int temp2 = stoi(temp);
- struct Move newMove;
-
- newMove.color = (temp2/colors);
-
- newMove.index = (temp2%(CHECKBOXES-1));
- return newMove;
- }
|