Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <map>
- #include <algorithm>
- #include <vector>
- using namespace std;
- map<string, char> board;
- char symbols[2] = {'X', 'O'};
- int movesMade = 0;
- void PrintBoard()
- {
- printf("\n 1 | 2 | 3 \n");
- printf("A %c | %c | %c \n", board["a1"], board["a2"], board["a3"]);
- printf(" -----------\n");
- printf("B %c | %c | %c \n", board["b1"], board["b2"], board["b3"]);
- printf(" -----------\n");
- printf("C %c | %c | %c \n\n ", board["c1"], board["c2"], board["c3"]);
- }
- bool validMove(string move)
- {
- if (board.find(move) == board.end())
- {
- cout << "INVALID MOVE" << endl;
- return false;
- }
- if (board[move] != ' ')
- {
- cout << "MOVE ALREADY MADE" << endl;
- return false;
- }
- return true;
- }
- void MakeMove(int player)
- {
- string move;
- do
- {
- PrintBoard();
- cout << "Player " << player + 1 << " make your move: ";
- cin >> move;
- transform(move.begin(), move.end(), move.begin(), ::tolower);
- }
- while(!validMove(move));
- board[move] = symbols[player];
- }
- bool CheckVictory(char symbol)
- {
- if (board["a1"] == symbol && board["a2"] == symbol && board["a3"] == symbol) return true;
- if (board["b1"] == symbol && board["b2"] == symbol && board["b3"] == symbol) return true;
- if (board["c1"] == symbol && board["c2"] == symbol && board["c3"] == symbol) return true;
- if (board["a1"] == symbol && board["b1"] == symbol && board["c1"] == symbol) return true;
- if (board["a2"] == symbol && board["b2"] == symbol && board["c2"] == symbol) return true;
- if (board["a3"] == symbol && board["b3"] == symbol && board["c3"] == symbol) return true;
- if (board["a1"] == symbol && board["b2"] == symbol && board["c3"] == symbol) return true;
- if (board["a3"] == symbol && board["b2"] == symbol && board["c1"] == symbol) return true;
- return false;
- }
- bool EndGame(int player)
- {
- if (CheckVictory(symbols[player]))
- {
- PrintBoard();
- cout << "Player " << player + 1 << " WINS" << endl;
- return true;
- }
- if (movesMade == 9)
- {
- PrintBoard();
- cout << "DRAW" << endl;
- return true;
- }
- return false;
- }
- int main()
- {
- board["a1"] = ' ';
- board["a2"] = ' ';
- board["a3"] = ' ';
- board["b1"] = ' ';
- board["b2"] = ' ';
- board["b3"] = ' ';
- board["c1"] = ' ';
- board["c2"] = ' ';
- board["c3"] = ' ';
- while(true)
- {
- MakeMove(movesMade % 2);
- if (EndGame(movesMade % 2))
- break;
- movesMade++;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment