Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- #pragma region
- void print(string text);
- void print(string text, int num);
- void print(string text, int num, string text2);
- void printBoard(int arr[][3], int sizeX, int sizeY);
- void drawBoard(char arr[][3]);
- void start(char ans);
- bool logic(int _input, int logicBoard[][3], int _pl, int _round);
- void boardManager(int type);
- void visualMng(int _input, char _board[][3], char _pl, int _round);
- #pragma endregion
- int main() {
- char answer;
- print("Do you wish to start the game ? y/n ");
- cin >> answer;
- system("cls");
- start(answer);
- cin.get();
- cin.get();
- }
- #pragma region
- void print(string text)
- {
- cout << text << endl;
- }
- void print(string text, int num)
- {
- cout << text << " " << num << endl;
- }
- void print(string text, int num, string text2)
- {
- cout << text << " " << num << " " << text2 << endl;
- }
- void printBoard(int arr[][3], int sizeX, int sizeY)
- {
- for (int y = 0; y < sizeY; y++)
- {
- for (int x = 0; x < sizeX; x++)
- {
- cout << arr[x][y] << " ";
- }
- cout << endl;
- }
- }
- void drawBoard(char arr[][3]) {
- print(" | | ");
- cout << " " << arr[0][0] << " | " << arr[1][0] << " | " << arr[2][0] << endl;
- print("_____|_____|_____");
- print(" | | ");
- cout << " " << arr[0][1] << " | " << arr[1][1] << " | " << arr[2][1] << endl;
- print("_____|_____|_____");
- print(" | | ");
- cout << " " << arr[0][2] << " | " << arr[1][2] << " | " << arr[2][2] << endl;
- print(" | | ");
- }
- #pragma endregion
- void start(char ans) {
- char answers;
- switch (ans)
- {
- case'y':
- print("Choose which side you want to play on : X / O");
- cin >> answers;
- if (answers == 'x' || answers == 'X')
- boardManager(1);
- else if (answers == 'o' || answers == 'O')
- boardManager(2);
- else
- print("Incorrect answer");
- break;
- case'n':
- print("Okay bb");
- break;
- default:
- print("Incorrect answer");
- break;
- }
- }
- bool logic(int _input, int logicBoard[][3], int _pl, int _round)
- {
- bool gameOn = true;
- int xmatcher;
- int ymatcher;
- int zmatcher;
- switch (_input)
- {
- case 1:
- if (_round % 2 == 0)
- {
- if (logicBoard[0][2] != 1 || logicBoard[0][2] != 2)
- logicBoard[0][2] = 1;
- break;
- }
- else {
- if (logicBoard[0][2] != 1 || logicBoard[0][2] != 2)
- logicBoard[0][2] = 2;
- break;
- }
- case 2:
- if (_round % 2 == 0)
- {
- if (logicBoard[1][2] != 1 || logicBoard[1][2] != 2)
- logicBoard[1][2] = 1;
- break;
- }
- else {
- if (logicBoard[1][2] != 1 || logicBoard[1][2] != 2)
- logicBoard[1][2] = 2;
- break;
- }
- case 3:
- if (_round % 2 == 0)
- {
- if (logicBoard[2][2] != 1 || logicBoard[2][2] != 2)
- logicBoard[2][2] = 1;
- break;
- }
- else {
- if (logicBoard[2][2] != 1 || logicBoard[2][2] != 2)
- logicBoard[2][2] = 2;
- break;
- }
- case 4:
- if (_round % 2 == 0)
- {
- if (logicBoard[0][1] != 1 || logicBoard[0][1] != 2)
- logicBoard[0][1] = 1;
- break;
- }
- else {
- if (logicBoard[0][1] != 1 || logicBoard[0][1] != 2)
- logicBoard[0][1] = 2;
- break;
- }
- case 5:
- if (_round % 2 == 0)
- {
- if (logicBoard[1][1] != 1 || logicBoard[1][1] != 2)
- logicBoard[1][1] = 1;
- break;
- }
- else {
- if (logicBoard[1][1] != 1 || logicBoard[1][1] != 2)
- logicBoard[1][1] = 2;
- break;
- }
- case 6:
- if (_round % 2 == 0)
- {
- if (logicBoard[2][1] != 1 || logicBoard[2][1] != 2)
- logicBoard[2][1] = 1;
- break;
- }
- else {
- if (logicBoard[2][1] != 1 || logicBoard[2][1] != 2)
- logicBoard[2][1] = 2;
- break;
- }
- case 7:
- if (_round % 2 == 0)
- {
- if (logicBoard[0][0] != 1 || logicBoard[0][0] != 2)
- logicBoard[0][0] = 1;
- break;
- }
- else {
- if (logicBoard[0][0] != 1 || logicBoard[0][0] != 2)
- logicBoard[0][0] = 2;
- break;
- }
- case 8:
- if (_round % 2 == 0)
- {
- if (logicBoard[1][0] != 1 || logicBoard[1][0] != 2)
- logicBoard[1][0] = 1;
- break;
- }
- else {
- if (logicBoard[1][0] != 1 || logicBoard[1][0] != 2)
- logicBoard[1][0] = 2;
- break;
- }
- case 9:
- if (_round % 2 == 0)
- {
- if (logicBoard[2][0] != 1 || logicBoard[2][0] != 2)
- logicBoard[2][0] = 1;
- break;
- }
- else {
- if (logicBoard[2][0] != 1 || logicBoard[2][0] != 2)
- logicBoard[2][0] = 2;
- break;
- }
- default:
- break;
- }
- if (_round > 4)
- {
- if (_round % 2 == 0) {
- zmatcher = logicBoard[0][0] + logicBoard[1][1] + logicBoard[2][2];
- if (zmatcher < 3)
- zmatcher = 0;
- zmatcher = logicBoard[0][2] + logicBoard[1][1] + logicBoard[2][0];
- if (zmatcher < 3)
- zmatcher = 0;
- for (int y = 0; y < 3; y++)
- {
- ymatcher = logicBoard[0][y] + logicBoard[1][y] + logicBoard[2][y];
- }
- if (ymatcher < 3)
- ymatcher = 0;
- for (int x = 0; x < 3; x++)
- {
- xmatcher = logicBoard[x][0] + logicBoard[x][1] + logicBoard[x][2];
- }
- if (xmatcher < 3)
- xmatcher = 0;
- if (xmatcher == 3 || ymatcher == 3 || zmatcher == 3) {
- print("Game Over, Player2 won the game.");
- gameOn = false;
- }
- }
- else {
- zmatcher = logicBoard[0][0] + logicBoard[1][1] + logicBoard[2][2];
- if (zmatcher < 6)
- zmatcher = 0;
- zmatcher = logicBoard[0][2] + logicBoard[1][1] + logicBoard[2][0];
- if (zmatcher < 6)
- zmatcher = 0;
- for (int y = 0; y < 3; y++)
- {
- ymatcher = logicBoard[0][y] + logicBoard[1][y] + logicBoard[2][y];
- }
- if (ymatcher < 6)
- ymatcher = 0;
- for (int x = 0; x < 3; x++)
- {
- xmatcher = logicBoard[x][0] + logicBoard[x][1] + logicBoard[x][2];
- }
- if (xmatcher < 6)
- xmatcher = 0;
- if (xmatcher == 6 || ymatcher == 6 || zmatcher == 6) {
- print("Game Over, Player1 won the game.");
- gameOn = false;
- }
- }
- }
- else if (_round == 9) {
- print("Game over, play again?");
- }
- return gameOn;
- }
- void visualMng(int _input, char _board[][3],char _pl, int _round)
- {
- int endRound = 1;
- bool nextRound = true;
- while (nextRound) {
- switch (_input)
- {
- case 1:
- if (_round > 1) {
- if (_board[0][2] != ' ')
- {
- print("Position already taken :) Enter different position: ");
- nextRound = false;
- }
- else {
- _board[0][2] = _pl;
- system("cls");
- endRound++;
- }
- }
- else {
- _board[0][2] = _pl;
- system("cls");
- endRound++;
- }
- break;
- case 2:
- if (_round > 1) {
- if (_board[1][2] != ' ')
- {
- print("Position already taken :) Enter different position: ");
- nextRound = false;
- }
- else {
- _board[1][2] = _pl;
- system("cls");
- endRound++;
- }
- }
- else {
- _board[1][2] = _pl;
- system("cls");
- endRound++;
- }
- break;
- case 3:
- if (_round > 1) {
- if (_board[2][2] != ' ')
- {
- print("Position already taken :) Enter different position: ");
- nextRound = false;
- }
- else {
- _board[2][2] = _pl;
- system("cls");
- endRound++;
- }
- }
- else {
- _board[2][2] = _pl;
- system("cls");
- endRound++;
- }
- break;
- case 4:
- if (_round > 1) {
- if (_board[0][1] != ' ')
- {
- print("Position already taken :) Enter different position: ");
- nextRound = false;
- }
- else {
- _board[0][1] = _pl;
- system("cls");
- endRound++;
- }
- }
- else {
- _board[0][1] = _pl;
- system("cls");
- endRound++;
- }
- break;
- case 5:
- if (_round > 1) {
- if (_board[1][1] != ' ')
- {
- print("Position already taken :) Enter different position: ");
- nextRound = false;
- }
- else {
- _board[1][1] = _pl;
- system("cls");
- endRound++;
- }
- }
- else {
- _board[1][1] = _pl;
- system("cls");
- endRound++;
- }
- break;
- case 6:
- if (_round > 1) {
- if (_board[2][1] != ' ')
- {
- print("Position already taken :) Enter different position: ");
- nextRound = false;
- }
- else {
- _board[2][1] = _pl;
- system("cls");
- endRound++;
- }
- }
- else {
- _board[2][1] = _pl;
- system("cls");
- endRound++;
- }
- break;
- case 7:
- if (_round > 1) {
- if (_board[0][0] != ' ')
- {
- print("Position already taken :) Enter different position: ");
- nextRound = false;
- }
- else {
- _board[0][0] = _pl;
- system("cls");
- endRound++;
- }
- }
- else {
- _board[0][0] = _pl;
- system("cls");
- endRound++;
- }
- break;
- case 8:
- if (_round > 1) {
- if (_board[1][0] != ' ')
- {
- print("Position already taken :) Enter different position: ");
- nextRound = false;
- }
- else {
- _board[1][0] = _pl;
- system("cls");
- endRound++;
- }
- }
- else {
- _board[1][0] = _pl;
- system("cls");
- endRound++;
- }
- break;
- case 9:
- if (_round > 1) {
- if (_board[2][0] != ' ')
- {
- print("Position already taken :) Enter different position: ");
- nextRound = false;
- }
- else {
- _board[2][0] = _pl;
- system("cls");
- endRound++;
- }
- }
- else {
- _board[2][0] = _pl;
- system("cls");
- endRound++;
- }
- break;
- default:
- break;
- }
- if (nextRound = false)
- {
- cin >> _input;
- }
- }
- }
- void boardManager(int type) {
- char visualBoard[3][3];
- int logicBoard[3][3];
- char p1;
- char p2;
- int logicalP1;
- int logicalP2;
- int input;
- int round = 0;
- bool gameState = true;
- //creates visual board
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- visualBoard[x][y] = ' ';
- }
- }
- //creates logical board
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- logicBoard[x][y] = 0;
- }
- }
- //chooses player side
- switch (type)
- {
- case 1:
- print("Player 1 is X you may begin to play now.");
- p1 = 'X';
- p2 = 'O';
- logicalP1 = 1;
- logicalP2 = 2;
- break;
- case 2:
- print("Player 1 is O you may begin to play now.");
- p1 = 'O';
- p2 = 'X';
- logicalP1 = 2;
- logicalP2 = 1;
- break;
- default:
- break;
- }
- system("cls");
- drawBoard(visualBoard);
- print("Use numpad on your keyboard to play the game, when you choose your destination press enter.");
- while (gameState)
- {
- cin >> input;
- if (round % 2 == 0) {
- print("Player 1's turn:");
- visualMng(input, visualBoard, p1, round);
- logic(input, logicBoard, logicalP1 , round);
- }
- else {
- print("Player 2's turn");
- visualMng(input, visualBoard, p2, round);
- logic(input, logicBoard, logicalP2, round);
- }
- drawBoard(visualBoard);
- print("Use numpad on your keyboard to play the game, when you choose your destination press enter.");
- round++;
- gameState = logic(input, logicBoard, p1, round);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement