Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Tic-Tac-Toe [C++]
- Group[3/3] - BaSs_HaXoR & others
- Had to use 3 groups code combined to work in one project... collaborative (pair-like) programming.
- With that being said, this was an introductory course to C++, and will be messy and inefficient code.
- */
- #include<iostream>
- using namespace std;
- bool playerTurn = true;
- const char valPlayer1 = 'X';
- const char valPlayer2 = 'O';
- char locArray[3][3] {{'1','2','3'}, {'4','5','6'}, {'7','8','9'}};
- void clearArray()
- {
- locArray[0][0] = '1';
- locArray[0][1] = '2';
- locArray[0][2] = '3';
- locArray[1][0] = '4';
- locArray[1][1] = '5';
- locArray[1][2] = '6';
- locArray[2][0] = '7';
- locArray[2][1] = '8';
- locArray[2][2] = '9';
- }
- void setPlayerBool(bool tf)
- {
- playerTurn = tf;
- }
- void boardDisplay()
- {
- //Below was going to be a more efficient way to display the board.
- /* cout<<" | "<<locArray[0,0]<<" | ";
- for(int x = 0; x<3; x++)
- {
- cout<<" | "<<locArray[0][x];
- }
- for(int y = 0; y <3; x++)
- {
- cout<<" | "<<locArray[x][x];
- }*/
- cout << " | | " << endl;
- cout << " " << locArray[0][0] << " | " << locArray[0][1] << " | " << locArray[0][2] << endl;
- cout << " | | " << endl;
- cout << " " << locArray[1][0] << " | " << locArray[1][1] << " | " << locArray[1][2] << endl;
- cout << " | | " << endl;
- cout << " " << locArray[2][0] << " | " << locArray[2][1] << " | " << locArray[2][2] << endl;
- cout << " | | " << endl;
- cout<<endl;
- }
- string currentPlayer()
- {
- if(playerTurn == true)
- {
- return "Player 1";
- }
- else
- {
- return "Player 2";
- }
- }
- int winState(char moves[][3])
- {
- int returnVal = 0;
- const int playerOne = 237;
- const int playerTwo = 264;
- int moveOne = moves[0][0] + moves[0][1] + moves[0][2];
- int moveTwo = moves[1][0] + moves[1][1] + moves[1][2];
- int moveThree = moves[2][0] + moves[2][1] + moves[2][2];
- int moveFour = moves[0][0] + moves[1][0] + moves[2][0];
- int moveFive = moves[0][1] + moves[1][1] + moves[2][1];
- int moveSix = moves[0][2] + moves[1][2] + moves[2][2];
- int moveSeven = moves[0][0] + moves[1][1] + moves[2][2];
- int moveEight = moves[0][2] + moves[1][1] + moves[2][0];
- int boardTotal = moveOne + moveTwo + moveThree;
- if(moveOne == playerOne || moveOne == playerTwo) {
- switch(moveOne)
- {
- case playerOne:
- returnVal = 1;
- break;
- case playerTwo:
- returnVal = 2;
- break;
- }
- }else if(moveTwo == playerOne || moveTwo == playerTwo) {
- switch(moveTwo)
- {
- case playerOne:
- returnVal = 1;
- break;
- case playerTwo:
- returnVal = 2;
- break;
- }
- }else if(moveThree == playerOne || moveThree == playerTwo) {
- switch(moveThree)
- {
- case playerOne:
- returnVal = 1;
- break;
- case playerTwo:
- returnVal = 2;
- break;
- }
- }else if(moveFour == playerOne || moveFour == playerTwo) {
- switch(moveFour)
- {
- case playerOne:
- returnVal = 1;
- break;
- case playerTwo:
- returnVal = 2;
- break;
- }
- }else if(moveFive == playerOne || moveFive == playerTwo) {
- switch(moveFive)
- {
- case playerOne:
- returnVal = 1;
- break;
- case playerTwo:
- returnVal = 2;
- break;
- }
- }else if(moveSix == playerOne || moveSix == playerTwo) {
- switch(moveSix)
- {
- case playerOne:
- returnVal = 1;
- break;
- case playerTwo:
- returnVal = 2;
- break;
- }
- }else if(moveSeven == playerOne || moveSeven == playerTwo) {
- switch(moveSeven)
- {
- case playerOne:
- returnVal = 1;
- break;
- case playerTwo:
- returnVal = 2;
- break;
- }
- }else if(moveEight == playerOne || moveEight == playerTwo) {
- switch(moveEight)
- {
- case playerOne:
- returnVal = 1;
- break;
- case playerTwo:
- returnVal = 2;
- break;
- }
- } else if(boardTotal == 756 || boardTotal == 747) {
- returnVal = 3;
- }
- return returnVal;
- }
- void showWinner()
- {
- if(winState(locArray) == 1)
- {
- system("color 0b");
- cout<<"player 2 wins."<<endl;
- cin.get();
- clearArray();
- playerTurn = true;
- system("color 07");
- }
- if(winState(locArray) == 2)
- {
- system("color 0a");
- cout<<"player 1 wins."<<endl;
- cin.get();
- clearArray();
- playerTurn = true;
- system("color 07");
- }
- if(winState(locArray) == 3)
- {
- system("color 0c");
- cout<<"It's a draw!'"<<endl;
- cin.get();
- clearArray();
- playerTurn = true;
- system("color 07");
- }
- else
- {
- //No current winner (return 0)
- }
- }
- void getPlayerInput()
- {
- int playerInput = 0;
- while(true)
- {
- system("cls");
- cout<<"############"<<endl;
- cout<<"Player 1 = X"<<endl;
- cout<<"Player 2 = O"<<endl;
- cout<<"############"<<endl<<endl;
- cout<<currentPlayer()<<" it is your turn."<<endl<<endl;
- boardDisplay();
- if(playerTurn == true)
- {
- cout<<"Please input where you would like to place your "<<valPlayer1<<": ";
- }
- else
- {
- cout<<"Please input where you would like to place your "<<valPlayer2<<": ";
- }
- cin >> playerInput;
- if(playerTurn == true)
- {
- switch(playerInput)
- {
- case 1:
- if(locArray[0][0] != '1')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[0][0] = valPlayer1;
- setPlayerBool(false);
- winState(locArray);
- break;
- case 2:
- if(locArray[0][1] != '2')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[0][1] = valPlayer1;
- setPlayerBool(false);
- winState(locArray);
- break;
- case 3:
- if(locArray[0][2] != '3')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[0][2] = valPlayer1;
- setPlayerBool(false);
- winState(locArray);
- break;
- case 4:
- if(locArray[1][0] != '4')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[1][0] = valPlayer1;
- setPlayerBool(false);
- winState(locArray);
- break;
- case 5:
- if(locArray[1][1] != '5')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[1][1] = valPlayer1;
- setPlayerBool(false);
- winState(locArray);
- break;
- case 6:
- if(locArray[1][2] != '6')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[1][2] = valPlayer1;
- setPlayerBool(false);
- winState(locArray);
- break;
- case 7:
- if(locArray[2][0] != '7')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[2][0] = valPlayer1;
- setPlayerBool(false);
- winState(locArray);
- break;
- case 8:
- if(locArray[2][1] != '8')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[2][1] = valPlayer1;
- setPlayerBool(false);
- winState(locArray);
- break;
- case 9:
- if(locArray[2][2] != '9')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[2][2] = valPlayer1;
- setPlayerBool(false);
- winState(locArray);
- break;
- }
- }
- else //If it's player 2
- {
- switch(playerInput)
- {
- case 1:
- if(locArray[0][0] != '1')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[0][0] = valPlayer2;
- setPlayerBool(true);
- winState(locArray);
- break;
- case 2:
- if(locArray[0][1] != '2')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[0][1] = valPlayer2;
- setPlayerBool(true);
- winState(locArray);
- break;
- case 3:
- if(locArray[0][2] != '3')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[0][2] = valPlayer2;
- setPlayerBool(true);
- winState(locArray);
- break;
- case 4:
- if(locArray[1][0] != '4')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[1][0] = valPlayer2;
- setPlayerBool(true);
- winState(locArray);
- break;
- case 5:
- if(locArray[1][1] != '5')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[1][1] = valPlayer2;
- setPlayerBool(true);
- winState(locArray);
- break;
- case 6:
- if(locArray[1][2] != '6')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- winState(locArray);
- break;
- }
- locArray[1][2] = valPlayer2;
- setPlayerBool(true);
- winState(locArray);
- break;
- case 7:
- if(locArray[2][0] != '7')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- break;
- }
- locArray[2][0] = valPlayer2;
- setPlayerBool(true);
- winState(locArray);
- break;
- case 8:
- if(locArray[2][1] != '8')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- break;
- }
- locArray[2][1] = valPlayer2;
- setPlayerBool(true);
- winState(locArray);
- break;
- case 9:
- if(locArray[2][2] != '9')
- {
- system("cls");
- cin.get();
- cout<<"Please input a valid location.";
- break;
- }
- locArray[2][2] = valPlayer2;
- setPlayerBool(true);
- winState(locArray);
- break;
- }
- }
- cin.get();
- showWinner();
- }
- }
- int main()
- {
- getPlayerInput();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement