Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <assert.h>
- #include <time.h>
- using namespace std;
- int Welcome();
- int ModeChoice();
- int Putdata(short arr[], short i, short j, char smybol);
- int WritePlayerName(short gamemode, string players[]);
- int Multiplayer(short gamemode, string players[]);
- int GameTable(short arr[]);
- int Game();
- int PuskAtma();
- int ChoiceSymbol(char playersymbol[], short a, short gamemode);
- int CheckTable(short arr[], short playersirasi);
- int main()
- {
- Game();
- }
- int Game()
- {
- string players[2];
- Welcome();
- short gamemode = ModeChoice();
- cout << "Mode: ";
- if (gamemode == 1)
- {
- cout << "Single player\n";
- }
- else if (gamemode == 2)
- {
- cout << "Two players\n";
- Multiplayer(gamemode, players);
- }
- else
- {
- assert("Error!");
- }
- return 0;
- }
- int ChoiceSymbol(char playersymbol[], short a, short gamemode)
- {
- char symbol;
- if (gamemode == 1)
- {
- if (a == 1)
- {
- short random = PuskAtma();
- if (random == 0)
- {
- playersymbol[1] = 'X';
- }
- else
- {
- playersymbol[1] = 'O';
- }
- if (playersymbol[1] || playersymbol[1] == 'x')
- {
- playersymbol[0] = 'O';
- }
- else
- {
- playersymbol[0] = 'X';
- }
- }
- else
- {
- cout << "X or O?\nPlayer1: ";
- do
- {
- cin >> symbol;
- } while (symbol != 'X' && symbol != 'x' && symbol != 'O' && symbol != 'o');
- playersymbol[0] = symbol;
- if (playersymbol[0] == 'X' || playersymbol[0] == 'x')
- {
- playersymbol[1] = 'O';
- }
- else
- {
- playersymbol[1] = 'X';
- }
- }
- }
- else if (gamemode == 2)
- {
- if (a == 1)
- {
- cout << "X or O?\nPlayer2: ";
- do
- {
- cin >> symbol;
- } while (symbol != 'X' && symbol != 'x' && symbol != 'O' && symbol != 'o');
- playersymbol[1] = symbol;
- if (playersymbol[1] == 'X' || playersymbol[1] == 'x')
- {
- playersymbol[0] = 'O';
- }
- else
- {
- playersymbol[0] = 'X';
- }
- }
- else
- {
- cout << "X or O?\nPlayer1: ";
- do
- {
- cin >> symbol;
- } while (symbol != 'X' && symbol != 'x' && symbol != 'O' && symbol != 'o');
- playersymbol[0] = symbol;
- if (symbol == 'X' || symbol == 'x')
- {
- playersymbol[1] = 'O';
- }
- else
- {
- playersymbol[1] = 'X';
- }
- }
- }
- return 0;
- }
- int PuskAtma()
- {
- srand(time(NULL));
- return rand() % 2;
- }
- int Multiplayer(short gamemode, string players[])
- {
- enum symbol
- {
- X = 0, O
- };
- WritePlayerName(gamemode, players);
- char playersymbol[2];
- short oyunabaslayan = PuskAtma();
- ChoiceSymbol(playersymbol, oyunabaslayan, gamemode);
- const short size = 3;
- short table[3][3]{};
- short index = 0;
- short oyunsiras = oyunabaslayan;
- while (true)
- {
- GameTable(table[0]);
- cout << players[oyunsiras] << " >> ";
- cin >> index;
- table[0][index] = oyunsiras + 1;
- short status = CheckTable(table[0], oyunsiras + 1);
- if (status == 0)
- {
- cout << "Winner Player is " << players[0] << endl;
- return 0;
- }
- else if (status == 1)
- {
- cout << "Winner Player is " << players[1] << endl;
- return 0;
- }
- }
- return -1;
- }
- int Welcome()
- {
- cout << "\t\t\tWelcome!\n";
- cout << "\tTic Tac Toe\n";
- cout << endl << endl;
- cout << "Mode:\n";
- cout << "1. Single player\n";
- cout << "2. Two players\n";
- cout << endl;
- return 0;
- }
- int ModeChoice()
- {
- short choice = 0;
- do
- {
- cout << ">> ";
- cin >> choice;
- } while (choice < 1 || choice > 2);
- return choice;
- }
- int Putdata(short arr[], short i, short j, char symbol)
- {
- return 0;
- }
- int WritePlayerName(short gamemode, string players[])
- {
- string player1, player2 = "Computer";
- if (gamemode == 1)
- {
- cout << "Player name: ";
- cin >> player1;
- players[0] = player1;
- players[1] = player2;
- }
- else if (gamemode == 2)
- {
- cout << "1.Player name: ";
- cin >> player1;
- players[0] = player1;
- cout << "2.Player name: ";
- cin >> player2;
- players[1] = player2;
- }
- else
- {
- return -1;
- }
- return 0;
- }
- int GameTable(short arr[])
- {
- const short size = 9;
- for (short i = 0; i < size; i++)
- {
- cout << arr[i] << ' ';
- if ((i + 1) % 3 == 0)
- {
- cout << endl;
- }
- }
- return 0;
- }
- int CheckTable(short arr[], short playersirasi)
- {
- const short size = 9;
- if (arr[0] == playersirasi && arr[1] == playersirasi && arr[2] == playersirasi)
- {
- return 1;
- }
- else if (arr[0] == playersirasi && arr[1] == playersirasi && arr[2] == playersirasi)
- {
- return 1;
- }
- else if (arr[0] == playersirasi && arr[1] == playersirasi && arr[2] == playersirasi)
- {
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement