Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Paul Fynn Kuemmel
- //This plays tic tac toe, between 2 users.
- #include <stdio.h>
- #include <iostream>
- #include <string>
- using namespace std;
- /**
- *@param The game board that the game is played on
- * @return Nothing
- */
- void printBoard(char gameBoard[3][3] ){
- for (int y = 0; y < 3; y++){ //For each value of the row and column, the current value is printed
- for (int x = 0; x < 3; x++){
- cout << gameBoard [y] [x]; //This creates the board
- }
- cout << endl; //Seperates for the next line.
- }
- }
- /**
- * @param The Game board that the game is played on
- * @return The boolean variable of the victory.
- */
- bool winnerDetermination(char gameBoard[3][3]){
- bool victoryRoyale = false; //Will determine if won or not
- string checker = ""; //String to plug characters into.
- int r = 0;
- int c = 0;
- string diagonal1 = ""; //Hardcoding the 2 diagonals.
- string diagonal2 = "";
- diagonal1 += gameBoard[0] [0];
- diagonal1 += gameBoard[1] [1];
- diagonal1 += gameBoard[2] [2];
- diagonal2 += gameBoard[0] [2];
- diagonal2 += gameBoard[1] [1]; //This creates the diagonals with each location being specific for each diagonal
- diagonal2 += gameBoard[2] [0];
- for(r = 0; r < 3; r++){ //For each row of the board this will run
- checker += gameBoard[r] [c];
- checker += gameBoard[r] [c + 1]; //Checks throughout each column.
- checker += gameBoard[r] [c + 2];
- if(checker == "XXX" or checker == "OOO"){ //If there are 3 of a kind, then victory is true
- victoryRoyale = true;
- break;
- }
- checker = "";
- }
- r = 0;
- for(c = 0; c < 3; c++){
- checker += gameBoard[r] [c];
- checker += gameBoard[r + 1] [c]; //For each column of the board, this will run
- checker += gameBoard[r + 2] [c];
- if(checker == "XXX" or checker == "OOO"){ //If there are 3 of a kind, then victory is true
- victoryRoyale = true;
- break;
- }
- checker = "";
- }
- c = 0;
- if(diagonal1 == "XXX" or diagonal1 == "OOO"){
- victoryRoyale = true;
- } //If the diagonals are 3 of a kind, then victory is true.
- if(diagonal2 == "XXX" or diagonal2 == "OOO"){
- victoryRoyale = true;
- }
- return victoryRoyale;
- }
- /***
- * @param The Victory determination
- * @return 0 to end cold heartedly and efficiently.
- */
- int main()
- {
- bool victoryRoyale = false;
- int r = 0; //Creates the rows and columns
- int c = 0;
- int playerTurn = 1; //Starts this off to determine the turn
- char gameBoard [3] [3]; //Creates the board
- bool endGame(false);
- for (int r = 0; r < 3; r++){
- for (int c = 0; c < 3; c++){
- gameBoard [c] [r] = '*'; //Places symbol as placeholders
- }
- }
- while (1){
- printBoard(gameBoard);
- if (playerTurn % 2 != 0){ //If it is odd then this runs
- cout << "Please enter the row and column of the location you want to place your X.";
- cin >> r >> c;
- while(gameBoard [r] [c] != '*'){ //Error validation
- cout << "There is already a character there you cheeky sod. Different row and column please.";
- cin >> r >> c;
- }
- gameBoard [r] [c] = 'X'; //Places character
- playerTurn ++;
- victoryRoyale = winnerDetermination(gameBoard); //Determines if there is a victory
- }
- else{
- cout << "Please enter the row and column of the location you want to place your O.";
- cin >> r >> c;
- while(gameBoard [r] [c] != '*'){ //Error validation
- cout << "There is already a character there you cheeky sod. Different row and column please.";
- cin >> r >> c;
- }
- gameBoard [r] [c] = 'O'; //Places character
- playerTurn ++;
- victoryRoyale = winnerDetermination(gameBoard);
- }
- r = 0; //Refreshes the addresses.
- c = 0;
- if(victoryRoyale){ //If victory is true, then this comes out
- cout << "Congrats! You won the Game!";
- cout << endl;
- printBoard(gameBoard);
- break;
- }
- else if (playerTurn == 10){
- cout << "Well done to both of you for the Tie!";
- cout << endl;
- printBoard(gameBoard);
- break; //If not, then they tied.
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement