well here is how i did a game of tictactoe; first of all, it doesn't support a computer player. Meaning there has to be two humans, or you playing yourself. Second, it's in C++. Third, it's only 155 lines. So it has it's ups and downs. Lastly, it's a console program, and it's pretty user interactive/friendly. So it's very easy to use for people who didn't make the program/played it before.
- Code: Select all
#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;
void ticTacToe()
{
int x = 4;
string p1, p2;
string l1 = " | |\n";
string l2 = " | | \n";
string l3 = " ----|----|----\n";
string l4 = " | | \n";
string l5 = " ----|----|----\n";
string l6 = " | | \n";
string l7 = " | |\n";
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
cout << "player1 = X\nplayer2 = O" << endl;
do{
cin >> p1;
if (p1 == "x1"||p1 == "1x")
{
system("cls");
l2.replace(4, 1, "X");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "x2"||p1=="2x")
{
system("cls");
l2.replace(9, 1, "X");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "x3"||p1 == "3x")
{
system("cls");
l2.replace(14, 1, "X");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "x4"||p1 == "4x")
{
system("cls");
l4.replace(4, 1, "X");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "x5"||p1 == "5x")
{
system("cls");
l4.replace(9, 1, "X");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "x6"||p1 == "6x")
{
system("cls");
l4.replace(14, 1, "X");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "x7"||p1 == "7x")
{
system("cls");
l6.replace(4, 1, "X");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "x8"||p1 == "8x")
{
system("cls");
l6.replace(9, 1, "X");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "x9"||p1 == "9x")
{
system("cls");
l6.replace(14, 1, "X");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
if (p1 == "o1"||p1 == "1o")
{
system("cls");
l2.replace(4, 1, "O");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "o2"||p1=="2o")
{
system("cls");
l2.replace(9, 1, "O");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "o3"||p1 == "3o")
{
system("cls");
l2.replace(14, 1, "O");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "o4"||p1 == "4o")
{
system("cls");
l4.replace(4, 1, "O");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "o5"||p1 == "5o")
{
system("cls");
l4.replace(9, 1, "O");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "o6"||p1 == "6o")
{
system("cls");
l4.replace(14, 1, "O");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "o7"||p1 == "7o")
{
system("cls");
l6.replace(4, 1, "O");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "o8"||p1 == "8o")
{
system("cls");
l6.replace(9, 1, "O");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "o9"||p1 == "9o")
{
system("cls");
l6.replace(14, 1, "O");
cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
}
else if(p1 == "newgame")
{
system("cls");
ticTacToe();
cout << "\n\n\n";
}
} while(x == 4);
}
void newGame()
{
ticTacToe();
}
int main()
{
string call;
cout << "*-------------*" << endl;
cout << "| TIC-TAC-TOE |" << endl;
cout << "*-------------*" << endl;
cin >> call;
if (call == "newgame")
{
newGame();
}
}