123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #include "game.h"
- Game::Game()
- {
-
- lastColumnIndex = CHECKBOXES - 1;
-
- for(int i = 0; i<white1;i++)
- {
- lockOut.push_back(false);
- }
-
- state = NOT_STARTED;
-
- };
- 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()
- {
-
- 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::turn(ScoreSheet activePlayer)
- {
-
- 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;}
- Sleep(1);
- }
-
- for(int i = 0; i <players.size();i++)
- {
- players[i].cumulativeOlok.addOlok(players[i].currentTurnOlok);
- }
-
- score();
-
- activePlayer++;
- activePlayer %= players.size();
- }
- 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;
- }
- void Game::receive(std::string userInput)
- {
- players[0].currentTurnOlok.clear();
-
-
-
- #ifdef WORKING_ON_RANGE_CHECK
-
- if(CHECKBOXES > index || 0 < index){fprintf(stdout,"Player[%d] %s index is too high or too low X\n", player, players[player].savedName);}
-
- players[0].cumulativeOlok.isColor(red);
- if(color){fprintf(stdout,"Player[%d] %s invalid color selected X\n", player, players[player].savedName);}
- #endif
-
-
-
-
-
- }
- void Game::addX(int player, int color, int index)
- {
-
-
-
- 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);}
- }
-
- 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);}
- }
|