0% found this document useful (0 votes)
148 views6 pages

Tic Tac Toe Game!

The document contains the code for a tic-tac-toe game. It includes functions to draw the game board, make player and computer moves, and check for a winner or tie after each turn. The main function runs a loop allowing player and computer turns until there is a winner or tie.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views6 pages

Tic Tac Toe Game!

The document contains the code for a tic-tac-toe game. It includes functions to draw the game board, make player and computer moves, and check for a winner or tie after each turn. The main function runs a loop allowing player and computer turns until there is a winner or tie.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <iostream>

#include <ctime>

void drawBoard(char *spaces);

void playerMove(char *spaces, char player);

void computerMove(char *spaces, char computer);

bool checkWinner(char *spaces, char player, char computer);

bool checkTie(char *spaces);

int main()

char spaces[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};

char player = 'X';

char computer = 'O';

bool running = true;

drawBoard(spaces);

while(running){

playerMove(spaces, player);

drawBoard(spaces);
if(checkWinner(spaces, player, computer)){

running = false;

break;

else if(checkTie(spaces)){

running = false;

break;

computerMove(spaces, computer);

drawBoard(spaces);

if(checkWinner(spaces, player, computer)){

running = false;

break;

else if(checkTie(spaces)){

running = false;

break;

std::cout << "Thanks for playing! \n";

return 0;
}

void drawBoard(char *spaces){

std::cout << "\n";

std::cout << " | | " << '\n';

std::cout << " " << spaces[0] << " | " << spaces[1] << " | " << spaces[2] << " " << '\n';

std::cout << "______|______|______" << '\n';

std::cout << " | | " << '\n';

std::cout << " " << spaces[3] << " | " << spaces[4] << " | " << spaces[5] << " " << '\n';

std::cout << "______|______|______" << '\n';

std::cout << " | | " << '\n';

std::cout << " " << spaces[6] << " | " << spaces[7] << " | " << spaces[8] << " " << '\n';

std::cout << " | | " << '\n';

std::cout << "\n";

void playerMove(char *spaces, char player){

int number = 0;

do{

std::cout << "Pick a spot to place a marker (1-9): ";

std::cin >> number;

number--;

if(spaces[number] == ' '){

spaces[number] = player;

break;

}
}while(!number > 0 || !number < 8);

void computerMove(char *spaces, char computer){

int number;

srand(time(NULL));

while(true){

number = rand() % 9;

if (spaces[number] == ' '){

spaces[number] = computer;

break;

bool checkWinner(char *spaces, char player, char computer){

if(spaces[0] != ' ' && spaces[0] == spaces[1] && spaces[1] == spaces[2]){

spaces[0] == player ? std:: cout << "You win! \n" : std::cout << "You lose! \n";

else if(spaces[3] != ' ' && spaces[3] == spaces[4] && spaces[4] == spaces[5]){

spaces[3] == player ? std:: cout << "You win! \n" : std::cout << "You lose! \n";

else if(spaces[6] != ' ' && spaces[6] == spaces[7] && spaces[7] == spaces[8]){
spaces[6] == player ? std:: cout << "You win! \n" : std::cout << "You lose! \n";

else if(spaces[0] != ' ' && spaces[0] == spaces[3] && spaces[3] == spaces[6]){

spaces[0] == player ? std:: cout << "You win! \n" : std::cout << "You lose! \n";

else if(spaces[1] != ' ' && spaces[1] == spaces[4] && spaces[4] == spaces[7]){

spaces[1] == player ? std:: cout << "You win! \n" : std::cout << "You lose! \n";

else if(spaces[2] != ' ' && spaces[2] == spaces[5] && spaces[5] == spaces[8]){

spaces[2] == player ? std:: cout << "You win! \n" : std::cout << "You lose! \n";

else if(spaces[0] != ' ' && spaces[0] == spaces[4] && spaces[4] == spaces[8]){

spaces[0] == player ? std:: cout << "You win! \n" : std::cout << "You lose! \n";

else if(spaces[2] != ' ' && spaces[2] == spaces[4] && spaces[4] == spaces[6]){

spaces[2] == player ? std:: cout << "You win! \n" : std::cout << "You lose! \n";

else{

return false;
}

return true;

bool checkTie(char *spaces){

for(int i = 0; i < 9; i++){

if(spaces[i] == ' '){

return false;

std::cout << "IT'S A TIE! \n";

return true;

You might also like