Hello! I'm new to the the forum and to programming in general, so after learning how to make variables, if statements and loops (badly) I decided I was tired of so much learning and had to make some sort of program or game :) . Tic-Tac-Toe jumped to mind and I started my perhaps over-ambitious little program.
After a couple of days of wrestling with my poor Mac the program is ready.
It may have bugs and I am sincerely sorry for the lack of legibility of the code, the insufficient use of functions, leading to copy-pasting lots of the code and overall poorness of the code. :(
When I began making the game I didn't understand most of the problems I would be facing so I kinda just went with the flow and solved problems as they appeared always using the probably most brute-force and idiotic way there is.
So, without further delay here it is.
Comments and Critiques are highly welcome!
#include <iostream>
using namespace std;
int main ()
{
int i = 0;
string player1;
string player2;
string a1 = "-";
string a2 = "-";
string a3 = "-";
string b1 = "-";
string b2 = "-";
string b3 = "-";
string c1 = "-";
string c2 = "-";
string c3 = "-";
cout<< "---------------Tic-Tac-Toe---------------\n";
cout<< "\n 1 2 3\na "<< a1 <<"|"<< a2 <<"|"<< a3 <<"\nb "<< b1 <<"|"<< b2 <<"|"<< b3 <<"\nc "<< c1 <<"|" << c2 <<"|" << c3 <<"\n";
while (i < 9){
for (int x = 0; x < 1; x++) {
cout<<"\nPlayer 1 please insert the coordinates of your play. (I.E. b1)\n\n";
cin>>player1;
if (player1 == "a1") {if (a1 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {a1 = "x";}}
if (player1 == "a2") {if (a2 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {a2 = "x";}}
if (player1 == "a3") {if (a3 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {a3 = "x";}}
if (player1 == "b1") {if (b1 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {b1 = "x";}}
if (player1 == "b2") {if (b2 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {b2 = "x";}}
if (player1 == "b3") {if (b3 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {b3 = "x";}}
if (player1 == "c1") {if (c1 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {c1 = "x";}}
if (player1 == "c2") {if (c2 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {c2 = "x";}}
if (player1 == "c3") {if (c3 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {c3 = "x";}}
if (player1 != "a1" && player1 != "a2" && player1 != "a3" && player1 != "b1" && player1 != "b2" && player1 != "b3" && player1 != "c1" && player1 != "c2" && player1 != "c3") {cout<<"You must write the coordinate without spaces or capital letters. Please try again.\n"; x--;}
cout<< "\n 1 2 3\na "<< a1 <<"|"<< a2 <<"|"<< a3 <<"\nb "<< b1 <<"|"<< b2 <<"|"<< b3 <<"\nc "<< c1 <<"|" << c2 <<"|" << c3 <<"\n";
}
i++;
if ((a1 == "x" && a2 == "x" && a3 == "x") || (b1 == "x" && b2 == "x" && b3 == "x") || (c1 == "x" && c2 == "x" && c3 == "x") || (a1 == "x" && b1 == "x" && c1 == "x") || (a2 == "x" && b2 == "x" && c2 == "x") || (a3 == "x" && b3 == "x" && c3 == "x") || (a1 == "x" && b2 == "x" && c3 == "x") || (a3 == "x" && b2 == "x" && c1 == "x")) {i = 9; cout << "\nPlayer 1 Wins!";}
else {
if (i >= 9) {}
else {
for (int x = 0; x < 1; x++) {
cout<<"\nPlayer 2 please insert the coordinates of your play. (I.E. c2)\n\n";
cin>>player2;
if (player2 == "a1") {if (a1 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {a1 = "o";}}
if (player2 == "a2") {if (a2 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {a2 = "o";}}
if (player2 == "a3") {if (a3 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {a3 = "o";}}
if (player2 == "b1") {if (b1 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {b1 = "o";}}
if (player2 == "b2") {if (b2 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {b2 = "o";}}
if (player2 == "b3") {if (b3 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {b3 = "o";}}
if (player2 == "c1") {if (c1 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {c1 = "o";}}
if (player2 == "c2") {if (c2 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {c2 = "o";}}
if (player2 == "c3") {if (c3 != "-") {cout<<"The coordinate must be free. Please try again.\n"; x--;} else {c3 = "o";}}
if (player2 != "a1" && player2 != "a2" && player2 != "a3" && player2 != "b1" && player2 != "b2" && player2 != "b3" && player2 != "c1" && player2 != "c2" && player2 != "c3") {cout<<"You must write the coordinate without spaces or capital letters. Please try again.\n"; x--;}
cout<< "\n 1 2 3\na "<< a1 <<"|"<< a2 <<"|"<< a3 <<"\nb "<< b1 <<"|"<< b2 <<"|"<< b3 <<"\nc "<< c1 <<"|" << c2 <<"|" << c3 <<"\n";
}
if ((a1 == "o" && a2 == "o" && a3 == "o") || (b1 == "o" && b2 == "o" && b3 == "o") || (c1 == "o" && c2 == "o" && c3 == "o") || (a1 == "o" && b1 == "o" && c1 == "o") || (a2 == "o" && b2 == "o" && c2 == "o") || (a3 == "o" && b3 == "o" && c3 == "o") || (a1 == "o" && b2 == "o" && c3 == "o") || (a3 == "o" && b2 == "o" && c1 == "o")) {i = 9; cout << "\nPlayer 2 Wins!";}
i++;
}
}
}
cout<< "\n\n Game Over. \n\n";
cin.get ();
}
I don't think you want to hear my critique. You won't like it.
But if I was...I'd say try to properly format your code and eliminate any redundancy for starters
That's what happens when you jump the gun and dive right into the middle of a program. Its sort of like trying to fly a 747 jet airplain after about 4 hours flying time in a Cessna twin engine prop. You might learn to fly it after crash landing several times.
Critiques are always welcome! :P I'll try to clear most redundancy and I'll see what I can do about the formating.
Ancient Dragon, I think you're spot on, I won't make that mistake again. :)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.