You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.8 KiB

  1. #include "Game.h"
  2. #include <algorithm>
  3. #include <limits.h>
  4. using namespace std;
  5. SnakeGame::SnakeGame()
  6. :gameName{"~Snake Game by VissaM~"},highScore{0}, bestPlayer{"None"}
  7. {}
  8. SnakeGame::~SnakeGame(){}
  9. void SnakeGame::addPlayer(string playerName){
  10. players.emplace_back( Player{playerName} );
  11. }
  12. unsigned int SnakeGame::getHighScore(void){
  13. return highScore;
  14. }
  15. string SnakeGame::getBestPlayer(void){
  16. return bestPlayer;
  17. }
  18. void SnakeGame::play(string playerName){
  19. pair<string, unsigned int> curBest{playerName, 0};
  20. for(auto player : players){
  21. if(player.getName() == playerName){
  22. initializeGraphics((char *)gameName.c_str());
  23. curBest = player.play();
  24. endGraphics();
  25. break;
  26. }
  27. }
  28. if(curBest.second > highScore){
  29. highScore = curBest.second;
  30. bestPlayer = curBest.first;
  31. }
  32. cout << "Highscore: " << highScore << " by " << bestPlayer << "\n" <<endl;
  33. }
  34. void SnakeGame::play(void){
  35. while(1){
  36. string playerName;
  37. cout << "Who's playing: ";
  38. cin >> playerName;
  39. cout << endl;
  40. list<Player>::iterator p;
  41. for(p=players.begin(); p!=players.end(); p++){
  42. if(p->getName() == playerName)
  43. break;
  44. }
  45. if(p == players.end()){
  46. //if the player isn't in the list, add him/her
  47. addPlayer(playerName);
  48. }
  49. play(playerName); //get the player to play the game
  50. cin.clear();
  51. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  52. cout << "Do you want to play again? (yes or no): ";
  53. string ans;
  54. cin >> ans;
  55. if(ans != "yes"){
  56. cout << "Exiting ..." << endl;
  57. break;
  58. }
  59. cout << "Perfect...\n" << endl;
  60. }
  61. }