Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctype.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string>
- #include <sstream>
- #include <time.h>
- //This function initializes a nine-char grid with underscores
- void setGrid ( char grid [] )
- {
- for ( int i = 0; i < 9; i++ )
- {
- grid[i] = '_';
- }
- }
- //This function prints the grid
- void showGrid ( char grid [] )
- {
- std::cout << "\n";
- for ( int i = 0; i < 9; i++ )
- {
- if ( i == 3 || i == 6 )
- {
- std::cout << "\n";
- }
- std::cout << grid[i] << " ";
- }
- std::cout << "\n";
- }
- //This function ensures player's input is valid
- int checkInput ( char input [] )
- {
- int iInput;
- while ( !isdigit(input[0]) )
- {
- std::cout << "Please enter a valid number: ";
- std::cin >> input;
- }
- iInput = std::atoi ( input );
- return iInput;
- }
- //This function generates a random play for the computer, based on player's moves
- int computerCheck ( char grid [] )
- {
- std::srand ( time (NULL) );
- for ( int i = 0; i < 9; i+=3 ) //Note i+=3 iterator to check row by row
- {
- if ( grid[i] == 'x' )
- {
- if ( grid[i+1] == 'x' && grid[i+2] == '_' )
- return i + 3;
- else if ( grid[i+1] == '_' && grid[i+2] == 'x' )
- return i + 2;
- }
- else if ( grid[i] == '_' )
- {
- if ( grid[i+1] == 'x' && grid[i+2] == 'x' )
- return i + 1;
- }
- }
- for ( int j = 0; j < 3; j++ ) //checks columns
- {
- if ( grid[j] == 'x' )
- {
- if ( grid[j+3] == 'x' && grid[j+6] == '_' )
- return j + 7;
- else if ( grid[j+3] == '_' && grid[j+6] == 'x' )
- return j + 4;
- }
- else if ( grid[j] == '_' )
- {
- if ( grid[j+3] == 'x' && grid[j+6] == 'x' )
- return j + 1;
- }
- }
- return rand() % 9 + 1;
- }
- //This function generates the computer's play
- char computerPlay ( char grid [] )
- {
- std::string result;
- char toReturn;
- int compGuess = computerCheck ( grid );
- std::ostringstream convert;
- convert << compGuess;
- result = convert.str();
- toReturn = result[0];
- return toReturn;
- }
- //This function plays any square, whether user or computer
- void playSquare ( char grid [], char player, char input [] )
- {
- int iInput = checkInput ( input );
- while ( iInput < 1 || iInput > 9 )
- {
- std::cout << "That number doesn't work. Try again: ";
- if ( player == 'x' )
- std::cin >> input;
- else
- {
- input[0] = computerPlay ( grid );
- std::cout << input;
- }
- iInput = checkInput ( input );
- }
- while ( grid[iInput-1] != '_' )
- {
- std::cout << "That spot's been taken. Try again: ";
- if ( player == 'x' )
- std::cin >> input;
- else
- {
- input[0] = computerPlay ( grid );
- std::cout << input;
- }
- iInput = checkInput ( input );
- }
- grid[iInput-1] = player;
- }
- //This function checks the grid for a winner
- bool checkGrid ( char grid [], char player )
- {
- if ( grid[0] == player )
- {
- if ( grid[1] == player && grid[2] == player )
- return true;
- else if ( grid[3] == player && grid[6] == player )
- return true;
- else if ( grid[4] == player && grid[8] == player )
- return true;
- }
- if ( grid[1] == player )
- {
- if ( grid[4] == player && grid[7] == player )
- return true;
- }
- if ( grid[2] == player )
- {
- if ( grid[4] == player && grid[6] == player )
- return true;
- else if ( grid[5] == player && grid[8] == player )
- return true;
- }
- if ( grid[3] == player )
- {
- if ( grid[4] == player && grid[5] == player )
- return true;
- }
- if ( grid[6] == player )
- {
- if ( grid[7] == player && grid[8] == player )
- return true;
- }
- return false;
- }
- //main function
- int main() {
- //variable declarations
- char p1x = 'x';
- char p2o = 'o';
- bool isOver = false;
- int counter = 0;
- char userInput [100];
- char gameGrid [9];
- std::string winner = "It's a draw!";
- //initialization
- std::cout << "Time to play some Tic Tac Toe! Enter the number for where you want to place your mark (1 for upper-left, 5 for middle, 9 for lower-right, etc.)\n";
- setGrid ( gameGrid );
- showGrid ( gameGrid );
- //main algorithm
- while ( !isOver || counter < 9 )
- {
- std::cout << "Player 1, enter your number: ";
- std::cin >> userInput;
- playSquare ( gameGrid, p1x, userInput );
- showGrid ( gameGrid );
- isOver = checkGrid ( gameGrid, p1x );
- counter++;
- if ( isOver )
- {
- winner = "Player 1 wins!";
- break;
- }
- if ( counter == 9 ) break;
- std::cout << "Computer's pick: ";
- userInput[0] = computerPlay ( gameGrid );
- std::cout << userInput;
- playSquare ( gameGrid, p2o, userInput );
- showGrid ( gameGrid );
- isOver = checkGrid ( gameGrid, p2o );
- if ( isOver )
- {
- winner = "Player 2 wins!";
- break;
- }
- counter++;
- };
- std::cout << "Game over. " << winner;
- std::cin >> counter;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement