//http://www.cplusplus.com/reference/cctype/
//http://en.cppreference.com/w/cpp/string/basic_string/to_string
//#include <climits>// limits of named constants, see page 1284
//#include <cmath> //math library
//#include <cctype> // library for strings and other useful validations
#include <iomanip> // library for manipulating output
//#include <fstream> // library to direct output to files and get input from
files
//#include <cstdlib> // library for the rand and srand functions
//#include <ctime> // library to work with time
//#include <string> //string library
//#include <cstring> //to be able to use strlen
//#define NDEBUG
//#include <assert.h>
//#include <array>
//#include <typeinfo>// ----typeid(varName).name()
#include <iostream>
using namespace std;
//Declare Functions
void displayMessage();
void refresh();
void move(bool who);
int checkWin();
int checkUse(int n);
int checkTie();
int checkTie2();
void playAgain();
void reset();
//Declare public Vars
char pos[9] = { '0', '1', '2', '3', '4', '5', '6', '7', '8' }; //Grid Positions
char res[9] = { '0', '1', '2', '3', '4', '5', '6', '7', '8' }; //For Reseting
int mov;//Grid Choice
bool player = true;//True = 'X' False = 'O'
//Main Function
void main()
{
refresh();
move(player);
}
//Display Title at start
void displayMessage()
{
cout << "+----------------------------+" << endl;
cout << "|Tic Tac Toe |" << endl;
cout << "| Your Name |" << endl;
cout << "|____________________________|" << endl;
cout << endl;
}
//Display Grid on screen
void refresh()
{
system("cls");
displayMessage();
cout << endl;
cout << "" << pos[0] << "|" << pos[1] << "|" << pos[2] << endl;
cout << "-----" << endl;
cout << "" << pos[3] << "|" << pos[4] << "|" << pos[5] << endl;
cout << "-----" << endl;
cout << "" << pos[6] << "|" << pos[7] << "|" << pos[8] << endl;
}
//Prompt users move/Test for win,tie,pos in-use
void move(bool who)
{
if(who == true)
{
cout << endl;
cout << "Make your move Player 1: ";
while(1)
{
cin >> mov; //Enter Grid Position
if(checkUse(mov))
{
cout << endl;
cout << "Sorry, that position is in use" << endl;
cout << " Try again: " << endl;
}
else
break;
}
pos[mov] = 'X'; //Set selected position to 'X'
refresh();
if(checkTie())
{
cout << endl;
cout << " Tie Game!" << endl;
playAgain();
}
//Check for win
if(checkWin())
{
cout << endl;
cout << "Player 1 Wins" << endl;
playAgain();
}
else
{
player = false;//Switch Player
move(player); //Player 2's turn
}
}
else
{
cout << endl;
cout << "Make your move Player 2: ";
while(1)
{
cin >> mov; //Ask for position
if(checkUse(mov))
{
cout << endl;
cout << "Sorry that position is in use" << endl;
cout << " Try again: " << endl;
}
else
break;
}
pos[mov] = 'O';
refresh();
if(checkTie())
{
cout << endl;
cout << " Tie Game!" << endl;
playAgain();
}
if(checkWin())
{
cout << endl;
cout << "Player 2 Wins" << endl;
playAgain();
}
else
{
player = true; //Switch Player
move(player);//Player 1's turn
}
}
}
//Check for used position
int checkUse(int n)
{
if(pos[n] == 'X' || pos[n] == 'O')
return true;
else
return false;
}
//Check for Win
int checkWin()
{
if(pos[0] == pos[1] && pos[0] == pos[2])
return true;
if(pos[0] == pos[3] && pos[0] == pos[6])
return true;
if(pos[0] == pos[4] && pos[0] == pos[8])
return true;
if(pos[1] == pos[4] && pos[1] == pos[7])
return true;
if(pos[2] == pos[4] && pos[2] == pos[6])
return true;
if(pos[2] == pos[5] && pos[2] == pos[8])
return true;
if(pos[3] == pos[4] && pos[3] == pos[5])
return true;
if(pos[6] == pos[7] && pos[6] == pos[8])
return true;
else
return false;
}
//Check for Tie
int checkTie()
{
if(pos[0] != '0' && pos[1] != '1' && pos[2] != '2' && pos[3] != '3' &&
pos[4] != '4'
&& pos[5] != '5' && pos[6] != '6' && pos[7] != '7' && pos[8] != '8')
return true;
else
return false;
}
//Play Again Prompt
void playAgain()
{
char choice;
cout << endl;
cout << "Do you wish to play again: Y? N?";
//Loop question in case of error
while(1)
{
cin >> choice;
if(choice == 'y'|| choice == 'y')
{
player = true;//Restart with Player 1 starting
reset(); //Reset position array
refresh();//Restart Grid
move(player);//Player start
}
else if(choice == 'n' || choice == 'N')
{
cout << endl;
cout << "Thanks for playing!" << endl;
break; //Quit Game
}
else //Reloop
{
cout << endl;
cout << " Please enter either 'n' or 'y'";
}
}
}
//Reseting Grid After Restart
void reset()
{
for(int i = 0; i <= 9; i++)
pos[i] = res[i];
}

Tic tac toe

  • 1.
    //http://www.cplusplus.com/reference/cctype/ //http://en.cppreference.com/w/cpp/string/basic_string/to_string //#include <climits>// limitsof named constants, see page 1284 //#include <cmath> //math library //#include <cctype> // library for strings and other useful validations #include <iomanip> // library for manipulating output //#include <fstream> // library to direct output to files and get input from files //#include <cstdlib> // library for the rand and srand functions //#include <ctime> // library to work with time //#include <string> //string library //#include <cstring> //to be able to use strlen //#define NDEBUG //#include <assert.h> //#include <array> //#include <typeinfo>// ----typeid(varName).name() #include <iostream> using namespace std; //Declare Functions void displayMessage(); void refresh(); void move(bool who); int checkWin(); int checkUse(int n); int checkTie(); int checkTie2(); void playAgain(); void reset(); //Declare public Vars char pos[9] = { '0', '1', '2', '3', '4', '5', '6', '7', '8' }; //Grid Positions char res[9] = { '0', '1', '2', '3', '4', '5', '6', '7', '8' }; //For Reseting int mov;//Grid Choice bool player = true;//True = 'X' False = 'O' //Main Function void main() { refresh(); move(player); } //Display Title at start void displayMessage() { cout << "+----------------------------+" << endl; cout << "|Tic Tac Toe |" << endl; cout << "| Your Name |" << endl; cout << "|____________________________|" << endl; cout << endl; } //Display Grid on screen void refresh() { system("cls"); displayMessage(); cout << endl; cout << "" << pos[0] << "|" << pos[1] << "|" << pos[2] << endl; cout << "-----" << endl; cout << "" << pos[3] << "|" << pos[4] << "|" << pos[5] << endl; cout << "-----" << endl; cout << "" << pos[6] << "|" << pos[7] << "|" << pos[8] << endl; } //Prompt users move/Test for win,tie,pos in-use
  • 2.
    void move(bool who) { if(who== true) { cout << endl; cout << "Make your move Player 1: "; while(1) { cin >> mov; //Enter Grid Position if(checkUse(mov)) { cout << endl; cout << "Sorry, that position is in use" << endl; cout << " Try again: " << endl; } else break; } pos[mov] = 'X'; //Set selected position to 'X' refresh(); if(checkTie()) { cout << endl; cout << " Tie Game!" << endl; playAgain(); } //Check for win if(checkWin()) { cout << endl; cout << "Player 1 Wins" << endl; playAgain(); } else { player = false;//Switch Player move(player); //Player 2's turn } } else { cout << endl; cout << "Make your move Player 2: "; while(1) { cin >> mov; //Ask for position if(checkUse(mov)) { cout << endl; cout << "Sorry that position is in use" << endl; cout << " Try again: " << endl; } else break; }
  • 3.
    pos[mov] = 'O'; refresh(); if(checkTie()) { cout<< endl; cout << " Tie Game!" << endl; playAgain(); } if(checkWin()) { cout << endl; cout << "Player 2 Wins" << endl; playAgain(); } else { player = true; //Switch Player move(player);//Player 1's turn } } } //Check for used position int checkUse(int n) { if(pos[n] == 'X' || pos[n] == 'O') return true; else return false; } //Check for Win int checkWin() { if(pos[0] == pos[1] && pos[0] == pos[2]) return true; if(pos[0] == pos[3] && pos[0] == pos[6]) return true; if(pos[0] == pos[4] && pos[0] == pos[8]) return true; if(pos[1] == pos[4] && pos[1] == pos[7]) return true; if(pos[2] == pos[4] && pos[2] == pos[6]) return true; if(pos[2] == pos[5] && pos[2] == pos[8]) return true; if(pos[3] == pos[4] && pos[3] == pos[5]) return true; if(pos[6] == pos[7] && pos[6] == pos[8]) return true; else return false; } //Check for Tie int checkTie() { if(pos[0] != '0' && pos[1] != '1' && pos[2] != '2' && pos[3] != '3' && pos[4] != '4' && pos[5] != '5' && pos[6] != '6' && pos[7] != '7' && pos[8] != '8') return true; else
  • 4.
    return false; } //Play AgainPrompt void playAgain() { char choice; cout << endl; cout << "Do you wish to play again: Y? N?"; //Loop question in case of error while(1) { cin >> choice; if(choice == 'y'|| choice == 'y') { player = true;//Restart with Player 1 starting reset(); //Reset position array refresh();//Restart Grid move(player);//Player start } else if(choice == 'n' || choice == 'N') { cout << endl; cout << "Thanks for playing!" << endl; break; //Quit Game } else //Reloop { cout << endl; cout << " Please enter either 'n' or 'y'"; } } } //Reseting Grid After Restart void reset() { for(int i = 0; i <= 9; i++) pos[i] = res[i]; }