Guest User

Untitled

a guest
Mar 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. map<string, char> board;
  10. char symbols[2] = {'X', 'O'};
  11. int movesMade = 0;
  12.  
  13. void PrintBoard()
  14. {
  15. printf("\n 1 | 2 | 3 \n");
  16. printf("A %c | %c | %c \n", board["a1"], board["a2"], board["a3"]);
  17. printf(" -----------\n");
  18. printf("B %c | %c | %c \n", board["b1"], board["b2"], board["b3"]);
  19. printf(" -----------\n");
  20. printf("C %c | %c | %c \n\n ", board["c1"], board["c2"], board["c3"]);
  21. }
  22.  
  23. bool validMove(string move)
  24. {
  25. if (board.find(move) == board.end())
  26. {
  27. cout << "INVALID MOVE" << endl;
  28. return false;
  29. }
  30. if (board[move] != ' ')
  31. {
  32. cout << "MOVE ALREADY MADE" << endl;
  33. return false;
  34. }
  35. return true;
  36. }
  37.  
  38. void MakeMove(int player)
  39. {
  40. string move;
  41. do
  42. {
  43. PrintBoard();
  44. cout << "Player " << player + 1 << " make your move: ";
  45. cin >> move;
  46. transform(move.begin(), move.end(), move.begin(), ::tolower);
  47. }
  48. while(!validMove(move));
  49. board[move] = symbols[player];
  50. }
  51.  
  52. bool CheckVictory(char symbol)
  53. {
  54. if (board["a1"] == symbol && board["a2"] == symbol && board["a3"] == symbol) return true;
  55. if (board["b1"] == symbol && board["b2"] == symbol && board["b3"] == symbol) return true;
  56. if (board["c1"] == symbol && board["c2"] == symbol && board["c3"] == symbol) return true;
  57. if (board["a1"] == symbol && board["b1"] == symbol && board["c1"] == symbol) return true;
  58. if (board["a2"] == symbol && board["b2"] == symbol && board["c2"] == symbol) return true;
  59. if (board["a3"] == symbol && board["b3"] == symbol && board["c3"] == symbol) return true;
  60. if (board["a1"] == symbol && board["b2"] == symbol && board["c3"] == symbol) return true;
  61. if (board["a3"] == symbol && board["b2"] == symbol && board["c1"] == symbol) return true;
  62. return false;
  63. }
  64.  
  65. bool EndGame(int player)
  66. {
  67. if (CheckVictory(symbols[player]))
  68. {
  69. PrintBoard();
  70. cout << "Player " << player + 1 << " WINS" << endl;
  71. return true;
  72. }
  73. if (movesMade == 9)
  74. {
  75. PrintBoard();
  76. cout << "DRAW" << endl;
  77. return true;
  78. }
  79. return false;
  80. }
  81.  
  82. int main()
  83. {
  84. board["a1"] = ' ';
  85. board["a2"] = ' ';
  86. board["a3"] = ' ';
  87. board["b1"] = ' ';
  88. board["b2"] = ' ';
  89. board["b3"] = ' ';
  90. board["c1"] = ' ';
  91. board["c2"] = ' ';
  92. board["c3"] = ' ';
  93.  
  94. while(true)
  95. {
  96. MakeMove(movesMade % 2);
  97. if (EndGame(movesMade % 2))
  98. break;
  99. movesMade++;
  100. }
  101.  
  102. return 0;
  103. }
Add Comment
Please, Sign In to add comment