help wid few things:
-exit function
-check if one wins
-cannot enter in an occupied place...

i would be thankful!

#include <iostream>
#include <string>
using namespace std;
int main()
{
string a = "1"; 
string b = "2";
string c = "3";
string d = "4";
string e = "5";
string f = "6";
string g = "7";
string h = "8"; 
string i = "9";
int a1=1;
int b1=2;
int c1=3;
int a2=4;
int b2=5;
int c2=6;
int a3=7;
int b3=8;
int c3=9;
int playerX;
int playerO;
int sum=0;
string x ="X";
string o = "O";
cout << "\t\tWelcome to tic-tac-toe!"<<endl;
cout << "\t\tThis is a two player game" << endl;
cout << endl;
cout << "For X to go first enter 1" <<endl;
cout << "For O to go first enter 2" <<endl;
while (sum <20)
{
 int n;
 cin >> n;
 switch (n)
 {
case 1 :
cout << "\n\t\t " << a << " | " << b << " | " << c << endl;
cout << "\t\t---+---+---"<< endl;
cout << "\t\t " << d << " | " << e << " | " << f << endl;
cout << "\t\t---+---+---"<< endl;
cout << "\t\t " << g << " | " << h << " | " << i << endl;
cout << "\nFor X: ";
cin >> playerX; 
if (playerX == a1)
a = x;
else if (playerX == b1)
b = x;
else if (playerX == c1)
c = x;
else if (playerX == a2)
d = x;
else if (playerX == b2)
e = x;
else if (playerX == c2)
f = x;
else if (playerX == a3)
g = x;
else if (playerX == b3)
h = x;
else if (playerX == c3)
i = x;
 cout << "\n\t\t " << a << " | " << b << " | " << c << endl;
 cout << "\t\t---+---+---"<< endl;
 cout << "\t\t " << d << " | " << e << " | " << f << endl;
 cout << "\t\t---+---+---"<< endl;
 cout << "\t\t " << g << " | " << h << " | " << i << endl;
cout << "For O: ";
cin >> playerO;

if (playerO == a1)
a = o;
else if (playerO == b1)
b = o;
else if (playerO == c1)
c = o;
else if (playerO == a2)
d = o;
else if (playerO == b2)
e = o;
else if (playerO == c2)
f = o;
else if (playerO == a3)
g = o;
else if (playerO == b3)
h = o;
else if (playerO == c3)
i = o;
cout << "\n\t\t " << a << " | " << b << " | " << c << endl;
cout << "\t\t---+---+---"<< endl;
cout << "\t\t " << d << " | " << e << " | " << f << endl;
cout << "\t\t---+---+---"<< endl;
cout << "\t\t " << g << " | " << h << " | " << i << endl;
break;
case 2:
cout << "\n\t\t " << a << " | " << b << " | " << c << endl;
cout << "\t\t---+---+---"<< endl;
cout << "\t\t " << d << " | " << e << " | " << f << endl;
cout << "\t\t---+---+---"<< endl;
cout << "\t\t " << g << " | " << h << " | " << i << endl;

cout << "\nFor O: ";
cin >> playerO;

if (playerO == a1)
a = o;
else if (playerO == b1)
b = o;
else if (playerO == c1)
c = o;
else if (playerO == a2)
d = o;
else if (playerO == b2)
e = o;
else if (playerO == c2)
f = o;
else if (playerO == a3)
g = o;
else if (playerO == b3)
h = o;
else if (playerO == c3)
i = o;
cout << "\n\t\t " << a << " | " << b << " | " << c << endl;
cout << "\t\t---+---+---"<< endl;
cout << "\t\t " << d << " | " << e << " | " << f << endl;
cout << "\t\t---+---+---"<< endl;
cout << "\t\t " << g << " | " << h << " | " << i << endl;
cout << "\nFor X: ";
cin >> playerX; 
if (playerX == a1)
a = x;
else if (playerX == b1)
b = x;
else if (playerX == c1)
c = x;
else if (playerX == a2)
d = x;
else if (playerX == b2)
e = x;
else if (playerX == c2)
f = x;
else if (playerX == a3)
g = x;
else if (playerX == b3)
h = x;
else if (playerX == c3)
i = x;
 cout << "\n\t\t " << a << " | " << b << " | " << c << endl;
 cout << "\t\t---+---+---"<< endl;
 cout << "\t\t " << d << " | " << e << " | " << f << endl;
 cout << "\t\t---+---+---"<< endl;
 cout << "\t\t " << g << " | " << h << " | " << i << endl;
 break;
};
};
 return 0;
}

Recommended Answers

All 2 Replies

format your code

Welcome. I'm not sure how you managed to enclose your code in a pretty grey box but I hope it was that process and not your coding style that left justified every line. It is much easier to read code that is indented consistently.

I would also suggest you start commenting your code if you're going to be asking others to evaluate it.

The way you have it set up a win could be determined if any of the following are true
//horizontal win
a == b and a == c or
d == e and d == f or
g == h and g == i or
//vertical win
a == d and a == g or
b == e and b == h or
c == f and c == i or
//diagonal win
a == e and a == i or
g == e and g == c

The program could exit at the decision of either player. If they enter a value of 99, for example, instead of 1-9, then that could act as a trigger to exit the program.

To determine if a player's move was already chosen you will need to look at the value of variable chosen. If that value is X or O then the player should make a different choice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.