Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // what is it
- // Michael Freeland 4/25/17
- #include <iostream>
- #include <string>
- using namespace std;
- // This allows you to input and output data and us string data class.
- const int PLAYER_ROWS = 3,
- PLAYER_COLS = 3,
- DISPLAY_ROWS = 9,
- DISPLAY_COLS = 11;
- // this makes a constant intager that will be used in the program.
- void displayBoard(char[][11]);
- void makeMove(char[][11], char[][PLAYER_COLS], bool);
- bool boardFull(char[][PLAYER_COLS]);
- bool isaWinner(char[][PLAYER_COLS], char);
- // It sets up the board and the bolian statments.
- int main()
- {
- bool xTurn = true;
- bool xWins = false,
- oWins = false;
- // This is setting up what will be determand as true of false.
- char gameBoard[DISPLAY_ROWS][DISPLAY_COLS] =
- { { ' ', ' ', ' ', '|', ' ', ' ', ' ', '|', ' ', ' ', ' ' },
- { ' ', '*', ' ', '|', ' ', '*', ' ', '|', ' ', '*', ' ' },
- { '_', '_', '_', '|', '_', '_', '_', '|', '_', '_', '_' },
- { ' ', ' ', ' ', '|', ' ', ' ', ' ', '|', ' ', ' ', ' ' },
- { ' ', '*', ' ', '|', ' ', '*', ' ', '|', ' ', '*', ' ' },
- { '_', '_', '_', '|', '_', '_', '_', '|', '_', '_', '_' },
- { ' ', ' ', ' ', '|', ' ', ' ', ' ', '|', ' ', ' ', ' ' },
- { ' ', '*', ' ', '|', ' ', '*', ' ', '|', ' ', '*', ' ' },
- { ' ', ' ', ' ', '|', ' ', ' ', ' ', '|', ' ', ' ', ' ' } };
- // This is making the gameboard.
- char moves[PLAYER_ROWS][PLAYER_COLS] = { '*', '*', '*', '*', '*', '*', '*', '*', '*', };
- // this makes the moves.
- while (!(xWins || oWins || boardFull(moves)))
- {
- displayBoard(gameBoard);
- makeMove(gameBoard, moves, xTurn);
- if (xTurn && isaWinner(moves, 'X'))
- xWins = true;
- else if (!xTurn && isaWinner(moves, 'O'))
- oWins = true;
- xTurn = !xTurn;
- }
- // This is setting up the gameboard and makeing the moves.
- if (xWins)
- cout << "\n**** GAME OVER -- X WINS! ****\n";
- else if (oWins)
- cout << "\n**** GAME OVER -- O WINS! ****\n";
- else
- cout << "\n**** GAME OVER -- It's a TIE. ****\n";
- displayBoard(gameBoard);
- return 0;
- }
- // This show who wins and also is the end of the game.
- /***************************************************
- * displayBoard *
- * *
- ***************************************************/
- void displayBoard(char gameBoard[][DISPLAY_COLS])
- {
- string spaces = " ";
- for (int row = 0; row < DISPLAY_ROWS; row++)
- {
- cout << endl << spaces;
- for (int col = 0; col < 11; col++)
- cout << gameBoard[row][col];
- }
- cout << endl << endl;
- }
- // This is how the the board gets displayed.
- /*********************************************
- * boardFull *
- * *
- *********************************************/
- bool boardFull(char moves[][PLAYER_COLS])
- {
- for (int row = 0; row < PLAYER_ROWS; row++)
- {
- for (int col = 0; col < PLAYER_COLS; col++)
- {
- if (moves[row][col] == '*')
- return false;
- }
- }
- return true;
- }
- // It test whether the board is full or not.
- /**************************************
- * makeMove *
- * *
- **************************************/
- void makeMOve(char gameBoard[][DISPLAY_COLS], char moves[][PLAYER_COLS], bool xTurn)
- {
- int row,
- col,
- moveRow,
- moveCol;
- char sign;
- if (xTurn)
- {
- cout << "\nX's turn -Enter your move\n";
- sign = 'X';
- }
- else
- {
- cout << "\nO,s turn - Enter your move \n";
- sign = 'O';
- }
- cout << "Row (1-3) and colum (1-3) seperated by a space: ";
- cin >> row >> col;
- while (row <1 || col < 1 || row > PLAYER_ROWS || col > PLAYER_COLS || moves[row - 1][col - 1] != '*')
- {
- cout << "\nRow and column must be a \"free\" space between 1 and 3.\n";
- cout << "re-enter row (1-3) and column (1-3) seperted by a space: ";
- cin >> row >> col;
- }
- moveRow = row - 1;
- moveCol = col - 1;
- moves[moveRow][moveCol] = sign;
- moveRow = moveRow * 3 + 1;
- moveCol = moveCol * 4 + 1;
- gameBoard[moveRow][moveCol] = sign;
- }
- // This sets up how moving will work on the board and how the turns will go.
- /************************************
- * isaWinner *
- * *
- ************************************/
- bool isaWinner(char moves[][PLAYER_COLS], char sign)
- {
- for (int row = 0; row < PLAYER_ROWS; row++)
- {
- if (moves[row][0] == sign && moves[row][1] == sign && moves[row][2] == sign)
- return true;
- }
- for (int col = 0; col < PLAYER_COLS; col++)
- {
- if (moves[0][col] == sign && moves[1][col] == sign && moves[2][col] == sign)
- return true;
- }
- if (moves[0][0] == sign && moves[1][1] == sign && moves[2][2] == sign)
- return true;
- if (moves[0][2] == sign && moves[1][1] == sign && moves[2][2] == sign)
- return true;
- return false;
- }
- // This show how depeding on the placment of the moves if you win or lose.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement