#include<iostream.h>
#include<stdlib.h>
//ANG LIBRARY NA STDLIB.H EH PINAPAGANA UNG SYSTEM("CLS")
// ANG SYSTEM("CLS") EH PANGCLEAR SCREEN
//TAWAG DITO MGA FUNCTION HEADER. NOT SURE. LOLZ
void gamePlay();
void resetGame();
void playerOne();
void playerTwo();
void ticTac();
void ticTacEx();
void ticTacExOne();
void tryAgain();
void howtoPlay();
void contGame();
void contGameEx();
void displayScreen();
int gameWin(int& check);
char array[]={'1','2','3','4','5','6','7','8','9'};
void main() //MAIN FUNTION NATIN TOH.
{
displayScreen(); //TINATAWAG NI MAIN SI DISPLAYSCREEN
}
void gamePlay() //PAPASOK SI DISPLAYSCREEN DITO PAG '1' ANG ININPUT NG USER
{
int check, plyrA=0, plyrB=0, numGames, sum=0, again=0;
cout<<"\t\t\t Number Of Games You Want: ";
//MAY MALI DITO D KO MAGAWAN NG PARAAN.
cin>>numGames;
system("cls");
if (numGames<=0)
{
cout<<"\t\t\t\t Invalid Input"<<endl;
gamePlay();
}
system("cls");
ticTac(); //TATAWAGIN NI GAMEPLAY SI TICTAC
//BABALIK DITO SI TICTAC PAGKATAPOS MAPRINT YUNG BOX
while(sum!=numGames)
{
int ctr=0;
int tie=0;
//BAKIT YUNG TIE EH 9? KASI LAST MOVE YUN
//KASI DIBA PAG LAST MOVE NA SA TICTACTOE AUTOMATIC TIE NA SILA
while(ctr!=1 && tie!=9)
{
playerOne();//TATAWAGIN NI GAMEPLAY SI PLAYERONE
ctr=gameWin(check); //PUPUNTA SIYA KAY GAMEWIN(CHECK) AT MALALAGAY KAY CTR
//KAPAG YUNG CONDITION KAY GAMEWIN EH MAY TUMAMA,
//PAPASOK SIYA DITO SA IF STATEMENT NA UNA
if (ctr==1)
{
system("cls");
cout<<endl;
cout<<"\t\t\t\tPlayer One Wins The Game!"<<endl<<endl;
plyrA++;
//ETO UNG CONDITION PAG NANALO YUNG PLAYER
//PARANG NUMBRE OF WINS TOH
resetGame();
//ITONG RESETGAME EH PAG MAY NANALO LANG
//BALE MAALIS YUNG LETTER X OR LEETER O
//BABALIK ULIT SA MGA CHOICES NA NUMBERS
ticTac();
continue;
}
tie++;
//KAPAG UMABOT SILA NG HANGGANG 9 MOVES, EDI TIE SILA. LOLZ
if(tie==9)
{
system("cls");
cout<<endl;
cout<<"\t\t\t\t\tA Tie!"<<endl;
resetGame();
ticTac();
continue;
}
playerTwo(); //PAREHO LANG KAY PLAYER ONE
ctr=gameWin(check);
if (ctr==1)
{
system("cls");
cout<<endl;
cout<<" Player Two Wins The Game!"<<endl<<endl;
plyrB++; //PAREHO LANG KAY PLAYERONE
resetGame(); //PAREHO LANG KAY PLAYERONE
ticTac();
continue;
}
tie++;
//PAREHO LANG DIN KAY PLAYER ONE BASAHIN NIYO NA LANG
if(tie==9)
{
system("cls");
cout<<endl;
cout<<"\t\t\t\t\tA Tie!"<<endl;
resetGame();
ticTac();
continue;
}
}
//YUNG SUM++ NA TOH PARA DUN SA CONDITION STATEMENT
//DIBA SUM=0, TAPOS PAG ANG ININPUT NG USER EH 2GAMES
//MAUULIT ITONG PROGRAM NG DALAWANG BESES
//ANG PURPOSE NG SUM++ PARA D MAGTULOY-TULOY ANG LARO
sum++;
}
//PAG TAPOS NA YUNG GAME, ITO NA ANG MADIDISPLAY
cout<<endl;
cout<<"\t\t ========================================="<<endl;
cout<<"\t\t TOTAL SCORE"<<endl;
cout<<"\t\t WINS"<<endl;
cout<<"\t\t Player One: "<<plyrA<<endl;
cout<<"\t\t Player Two: "<<plyrB<<endl;
cout<<endl;
tryAgain(); //TATAWAGIN NI GAMEPLAY SI TRYAGAIN KUNG GUSTO PA O HINDI NA
}
void resetGame() //DITO PAPASOK SI GAMEPLAY PAG MAY NANALO
{
int b='1';
for(int a=0; a<9;a++)
{
array[a]=b;
b++;
}
}
{
if(array[0] == array[1] && array[2] == array[0])
{
check=1;
return check;
}
else if(array[3] == array[4] && array[5] == array[3])
{
check=1;
return check;
}
else if(array[6] == array[7] && array[8] == array[6])
{
check=1;
return check;
}
else if(array[0] == array[3] && array[6] == array[0])
{
check=1;
return check;
}
else if(array[1] == array[4] && array[7] == array[1])
{
check=1;
return check;
}
else if(array[2] == array[5] && array[8] == array[2])
{
check=1;
return check;
}
else if(array[0] == array[4] && array[8] == array[0])
{
check=1;
return check;
}
else if(array[2] == array[4] && array[6] == array[2])
{
check=1;
return check;
}
else
{
check=0;
return check;
}
}
void displayScreen()
{
char choose;
cout<<endl;
cout<<" [1] Start The Game"<<endl;
cout<<endl;
cout<<" [2] How To Play The Game"<<endl;
cout<<endl;
cout<<" [3] Exit"<<endl;
cout<<endl;
cout<<endl;
cout<<"\t\t\tEnter A Number In The Menu That You Want: ";
cin>>choose;
system("cls");
switch(choose)
{
case '1':
gamePlay();
break;
case '2':
howtoPlay();
break;
case '3':
cout<<"\t\t\t Thank You For Using Our Program"<<endl<<endl;
break;
default:
cout<<"\t\t\t\tInvalid Input"<<endl;
tryAgain();
break;
}
}
void howtoPlay() //DITO MAPUPUNTA SI DISPLAYSCREEN PAG 2 ANG PINILI NG USER
{
cout<<"\t\tThis Is A Simple Program Made In C++ Called TicTacToe"<<endl;
cout<<"\t\t\tThis Is A Sample Preview Of The Program"<<endl<<endl;
resetGame(); //NILAGAY KO TOH KASI BAKA MAGLARO MUNA SILA BAGO TIGNAN ANG HOWTOPLAY
ticTac(); //TAPOS IPRIPRINT NIYA SI TICTAC
contGameEx(); //PUPUNTA SI HOWTOPLAY KAY CONTGAMEEX
cout<<"\t\tThe Player One Will Be The One To Choose A Number In The Box"<<endl;
cout<<"\t\tThe Player One's Legend is 'X' and Player Two's Legend Is 'O'"<<endl;
cout<<"\t\tThis Is A Sample Output If Player One Choose Box 1 and"<<endl;
cout<<"\t\t\t Player Two Choose Box 5"<<endl<<endl;
resetGame(); //PAREHO LANG SA UNA
ticTacEx(); //IDIDISPLAY NIYA YUNG SAMPLE FIGURE NG BOX
contGameEx(); //PAREHO LANG SA UNA
cout<<"\t\tA Player Wins If He/She Get A Three Legend In Straight Line"<<endl;
cout<<"\t\tor Diagonal Line"<<endl<<endl;
cout<<"\t\tThis Is A Sample Of A Player's Move Who Wins The Game"<<endl<<endl;
ticTacExOne();
}
void contGame()
{
char cont;
cout<<endl;
cout<<"\t\t\tDo You Want To Proceed To The Game [y/n]: ";
cin>>cont;
//KAPAG Y, PUNTA NA SIYA AGAD KAY GAMEPLAY
if(cont=='y' || cont=='Y')
{
system("cls");
gamePlay();
}
//PAG N NAMAN, BABALIK SIYA KAY MAIN
else if(cont=='n' || cont=='N')
{
system("cls");
main();
}
//PAG WALA NAMAN, INVALID ANG INPUT
else
{
cout<<"\t\t\t\t Invalid Input"<<endl;
contGame(); //BABALIK LANG SIYA KAY CONTGAME
}
}
void contGameEx()
{
char cont;
cout<<endl;
cout<<"\t\t\t Do You Want Read More? [y/n]: ";
cin>>cont;
if(cont=='y' || cont=='Y')
{
system("cls");
}
else if(cont=='n' || cont=='N')
{
system("cls");
main();
}
else
{
cout<<"\t\t\t\t Invalid Input"<<endl;
contGameEx();
}
}
void tryAgain()
{
char tryA;
cout<<"\t\tInput letter m if you want to go back to main menu"<<endl;
cout<<"\t\t\t Do You Want Try Again?[y/n]: ";
cin>>tryA;
if(tryA=='y' || tryA=='Y')
{
system("cls");
resetGame();
gamePlay();
}
else if(tryA=='n' || tryA=='N')
{
system("cls");
cout<<"\t\t That Was A Great Game! Thanks For Playing"<<endl<<endl;
}
else if (tryA=='m' || tryA=='M')
{
system("cls");
main();
}
else
{
cout<<"\t\t\t\t Invalid Input"<<endl;
tryAgain();
}
}
void ticTac()
{
cout<<endl<<endl;
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t| "<<array[0]<<" | "<<array[1]<<" | "<<array[2]<<" |"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t| "<<array[3]<<" | "<<array[4]<<" | "<<array[5]<<" |"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t| "<<array[6]<<" | "<<array[7]<<" | "<<array[8]<<" |"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<endl;
}
void ticTacEx()
{
//IDIDISPLAY NIYA YUNG SAMPLE OUTPUT MAY NAINPUT
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t| "<<"X"<<" | "<<array[1]<<" | "<<array[2]<<" |"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t| "<<array[3]<<" | "<<"O"<<" | "<<array[5]<<" |"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t| "<<array[6]<<" | "<<array[7]<<" | "<<array[8]<<" |"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<endl;
}
void ticTacExOne()
{
//SAMPLE OUTPUT NAMAN NG PANALO TAPOS BABALIK ULIT KAY HOWTOPLAY
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t| "<<"X"<<" | "<<array[1]<<" | "<<array[2]<<" |"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t| "<<array[3]<<" | "<<"X"<<" | "<<array[5]<<" |"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t| "<<array[6]<<" | "<<array[7]<<" | "<<"X"<<" |"<<endl;
cout<<"\t\t\t| | | |"<<endl;
cout<<"\t\t\t|===========|===========|===========|"<<endl;
cout<<endl;
}
void playerOne()
{
char boxOne;
int ctr=0;
while(ctr!=1)
{
cout<<"\t\t\t\t Player One: "<<endl;
cout<<"\t\t\t Choose A Number In The Box: ";
cin>>boxOne;
switch(boxOne)
{
case '1':
if (array[0] == 'X' || array[0] =='O')
cout<<endl<<"\t\tThe Box [1] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[0]='X';
ctr++;
}
break;
case '2':
if (array[1] == 'X' || array[1] =='O')
cout<<endl<<"\t\tThe Box [2] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[1]='X';
ctr++;
}
break;
case '3':
if (array[2] == 'X' || array[2] == 'O')
cout<<endl<<"\t\tThe Box [3] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[2]='X';
ctr++;
}
break;
case '4':
if (array[3] == 'X' || array[3] == 'O')
cout<<endl<<"\t\tThe Box [4] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[3]='X';
ctr++;
}
break;
case '5':
if (array[4] == 'X' || array[4] == 'O')
cout<<endl<<"\t\tThe Box [5] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[4]='X';
ctr++;
}
break;
case '6':
if (array[5] == 'X' || array[5] == 'O')
cout<<endl<<"\t\tThe Box [6] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[5]='X';
ctr++;
}
break;
case '7':
if (array[6] == 'X' || array[6] == 'O')
cout<<endl<<"\t\tThe Box [7] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[6]='X';
ctr++;
}
break;
case '8':
if (array[7] == 'X' || array[7] =='O')
cout<<endl<<"\t\tThe Box [8] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[7]='X';
ctr++;
}
break;
case '9':
if (array[8] == 'X' || array[8] =='O')
cout<<endl<<"\t\tThe Box [9] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[8]='X';
ctr++;
}
break;
default:
cout<<endl<<"\t\t\tInvalid Input. Please Enter A Valid Number"<<endl<<endl;
break;
}
}
system("cls");
}
void playerTwo()
{
char boxTwo;
int ctr=0;
while(ctr!=1)
{
cout<<"\t\t\t\t Player Two: "<<endl;
cout<<"\t\t\t Choose A Number In The Box: ";
cin>>boxTwo;
//PAREHO LANG TOH KAY PLAYERONE PERO LETTER O NAMAN KAY PLAYERTWO
//KAY PLAYER ONE KASI LETTER X
switch(boxTwo)
{
case '1':
if (array[0] == 'X' || array[0] =='O')
cout<<endl<<"\t\tThe Box [1] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[0]='O';
ctr++;
}
break;
case '2':
if (array[1] == 'X' || array[1] =='O')
cout<<endl<<"\t\tThe Box [2] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[1]='O';
ctr++;
}
break;
case '3':
if (array[2] == 'X' || array[2] == 'O')
cout<<endl<<"\t\tThe Box [3] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[2]='O';
ctr++;
}
break;
case '4':
if (array[3] == 'X' || array[3] == 'O')
cout<<endl<<"\t\tThe Box [4] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[3]='O';
ctr++;
}
break;
case '5':
if (array[4] == 'X' || array[4] == 'O')
cout<<endl<<"\t\tThe Box [5] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[4]='O';
ctr++;
}
break;
case '6':
if (array[5] == 'X' || array[5] == 'O')
cout<<endl<<"\t\tThe Box [6] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[5]='O';
ctr++;
}
break;
case '7':
if (array[6] == 'X' || array[6] == 'O')
cout<<endl<<"\t\tThe Box [7] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[6]='O';
ctr++;
}
break;
case '8':
if (array[7] == 'X' || array[7] =='O')
cout<<endl<<"\t\tThe Box [8] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[7]='O';
ctr++;
}
break;
case '9':
if (array[8] == 'X' || array[8] =='O')
cout<<endl<<"\t\tThe Box [9] Has Been Chosen. Choose Valid Number In The Box"<<endl<<endl;
else
{
array[8]='O';
ctr++;
}
break;
default:
cout<<endl<<"\t\t\tInvalid Input. Please Enter A Valid Number"<<endl<<endl;
break;
}
}
system("cls");
ticTac();
}