/*
15. Tic-Tac-Toe Game
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional c ha rarray with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a
loop that Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number. Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row and column number. Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end. Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.*/
#include “stdafx.h”
#include<iostream>
using namespace std;
bool player1Won();
bool player1WonInARow();
bool player1WonInAColumn();
bool player1WonInDia();
bool player2Won();
bool player2WonInARow();
bool player2WonInAColumn();
bool player2WonInDia();
char tic_tac_toe[3][3]={‘*’,’*’,’*’,’*’,’*’,’*’,’*’,’*’,’*’};
int player1R[5];
int player1C[5];
int player2R[4];
int player2C[4];
int countForP1R=0;
int countForP2R=0;
int main()
{
int i=0;
for(;i<9;i++)
{
int x,y,z,k;
//display the contents of the board
for(int a=0;a<3;a++)
{
for(int b=0;b<3;b++)
{
cout<<tic_tac_toe[a][b]<<“\t”;
}
cout<<endl;
}
//ask player one to select a location
cout<<“\nPLAYER1\nSelect a location by entering two number while”
<<“\nthe first number, in the range of 0 to 2, stands”
<<“\nfor the row of the loation and the second num, ”
<<“\n in the range of 0 to 2, stands for the columns”
<<“\nof the location. Sperate two number by space.”
<<“\nONLY LOCATIONS OCCUPIED BY * ARE AVALIABLE”
<<“\n”;
cin>>x>>y;
tic_tac_toe[x][y]=’X’;
player1R[i]=x;
player1C[i]=y;
//display the contents of the board
for(int a=0;a<3;a++)
{
for(int b=0;b<3;b++)
{
cout<<tic_tac_toe[a][b]<<“\t”;
}
cout<<endl;
}
//determinate if player1 wins
if(player1Won()==true)
{
cout<<“Player 1 wins the game!”;
system(“pause”);
return 0;
}
//ask player two to select a location
cout<<“\nPLAYER2\nSelect a location by entering two number while”
<<“\nthe first number, in the range of 0 to 2, stands”
<<“\nfor the row of the loation and the second num, ”
<<“\n in the range of 0 to 2, stands for the columns”
<<“\nof the location. Sperate two number by space.”
<<“\nONLY LOCATIONS OCCUPIED BY * ARE AVALIABLE”
<<“\n”;
cin>>z>>k;
tic_tac_toe[z][k]=’O’;
player2R[i]=z;
player2C[i]=k;
//determinate if player2 wins
if(player2Won()==true)
{
cout<<“Player 2 wins the game!”;
system(“pause”);
return 0;
}
}
system(“pause”);
return 0;
}
///////////////////////////////////////////////////////
////////////determinate if player1 wins////////////////
///////////////////////////////////////////////////////
bool player1Won()
{
bool player1Won=false;
if(player1WonInARow()==true)
{
cout<<“player1WonInARow”;
player1Won=true;
}
if(player1WonInAColumn()==true)
{
cout<<“player1WonInAColumn”;
player1Won=true;
}
if(player1WonInDia()==true)
{
cout<<“player1WonInDia”;
player1Won=true;
}
return player1Won;
}
bool player1WonInARow()
{
bool player1WonInARow=false;
if(tic_tac_toe[0][0]==’X’&&tic_tac_toe[1][0]==’X’&&tic_tac_toe[2][0]==’X’) player1WonInARow=true;
else if(tic_tac_toe[0][1]==’X’&&tic_tac_toe[1][1]==’X’&&tic_tac_toe[2][1]==’X’) player1WonInARow=true;
else if(tic_tac_toe[0][2]==’X’&&tic_tac_toe[1][2]==’X’&&tic_tac_toe[2][2]==’X’) player1WonInARow=true;
return player1WonInARow;
}
bool player1WonInAColumn()
{
bool player1WonInAColumn=false;
if(tic_tac_toe[0][0]==’X’&&tic_tac_toe[0][1]==’X’&&tic_tac_toe[0][2]==’X’) player1WonInAColumn=true;
else if(tic_tac_toe[1][0]==’X’&&tic_tac_toe[1][1]==’X’&&tic_tac_toe[1][2]==’X’) player1WonInAColumn=true;
else if(tic_tac_toe[2][0]==’X’&&tic_tac_toe[2][1]==’X’&&tic_tac_toe[2][2]==’X’) player1WonInAColumn=true;
return player1WonInAColumn;
}