I finally made tic tac toe as a beginner(took a month and a bit) and I want to know if I made things more complicated than they should. It seems like a lot of code for a simple game but I'm not sure. Still learning and I would like some advice if there's something really bad.
PS:I dont know a clear command other than system("cls") so even though I saw people online saying no to it I dont know an alternative.
Code:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <conio.h>
#include <time.h>
using namespace std;
int aivall, aivaln;
char p1 = ' ', p2 = ' ', p3 = ' ', p4 = ' ', p5 = ' ', p6 = ' ', p7 = ' ', p8 =' ', p9 = ' ', l;
int n, endg = 0, again;
int ai(int a, int b) {
if (a == 1) {
switch (b) {
case 1: {if (p1 == 88 || p1 == 79) {again = 1;} else {p1 = 79;}} break;
case 2: {if (p2 == 88 || p2 == 79) {again = 1;} else {p2 = 79;}} break;
case 3: {if (p3 == 88 || p3 == 79) {again = 1;} else {p3 = 79;}} break;
}}
if (a == 2) {
switch (b) {
case 1: {if (p4 == 88 || p4 == 79) {again = 1;} else {p4 = 79;}} break;
case 2: {if (p5 == 88 || p5 == 79) {again = 1;} else {p5 = 79;}} break;
case 3: {if (p6 == 88 || p6 == 79) {again = 1;} else {p6 = 79;}} break;
}}
if (a == 3) {
switch (b) {
case 1: {if (p7 == 88 || p7 == 79) {again = 1;} else {p7 = 79;}} break;
case 2: {if (p8 == 88 || p8 == 79) {again = 1;} else {p8 = 79;}} break;
case 3: {if (p9 == 88 || p9 == 79) {again = 1;} else {p9 = 79;}} break;
}}
}
char xno(char p1, char p2, char p3, char p4, char p5, char p6, char p7, char p8, char p9) {
cout << " 1 2 3 " << endl;
cout << endl;
cout << " A. " << p1 << " " << p2 << " " << p3 << endl;
cout << endl;
cout << " B. " << p4 << " " << p5 << " " << p6 << endl;
cout << endl;
cout << " C. " << p7 << " " << p8 << " " << p9 << endl;
cout << endl;}
int triplecheckx() {
//vertical
if (p1 == 88 && p4 == 88 && p7 == 88) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "Player Won!!!";
}
if (p2 == 88 && p5 == 88 && p8 == 88) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "Player Won!!!";
}
if (p3 == 88 && p6 == 88 && p9 == 88) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "Player Won!!!";
}
//Horizontal
if (p1 == 88 && p2 == 88 && p3 == 88) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "Player Won!!!";
}
if (p4 == 88 && p5 == 88 && p6 == 88) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "Player Won!!!";
}
if (p7 == 88 && p8 == 88 && p9 == 88) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "Player Won!!!";
}
//Cross
if (p1 == 88 && p5 == 88 && p9 == 88) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "Player Won!!!";
}
if (p3 == 88 && p5 == 88 && p7 == 88) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "Player Won!!!";
}
}
int triplechecko() {
//vertical
if (p1 == 79 && p4 == 79 && p7 == 79) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "AI Won!!!";
}
if (p2 == 79 && p5 == 79 && p8 == 79) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "AI Won!!!";
}
if (p3 == 79 && p6 == 79 && p9 == 79) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "AI Won!!!";
}
//Horizontal
if (p1 == 79 && p2 == 79 && p3 == 79) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "AI Won!!!";
}
if (p4 == 79 && p5 == 79 && p6 == 79) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "AI Won!!!";
}
if (p7 == 79 && p8 == 79 && p9 == 79) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "AI Won!!!";
}
//Cross
if (p1 == 79 && p5 == 79 && p9 == 79) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "AI Won!!!";
}
if (p3 == 79 && p5 == 79 && p7 == 79) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "AI Won!!!";
}
}
int checkalltie(char p1,char p2,char p3,char p4,char p5,char p6,char p7,char p8,char p9){
if(p1 != 32 && p2 != 32 && p3 != 32 && p4 != 32 && p5 != 32 && p6 != 32 && p7 != 32 && p8 != 32 && p9 != 32 ) {
endg = 1;
system("cls");
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout<<"TIE!!!";}
}
int numberpick() {
if (l == 'A') {
switch (n) {
case 1:
p1 = 88;
break;
case 2:
p2 = 88;
break;
case 3:
p3 = 88;
break;
}
}
if (l == 'B') {
switch (n) {
case 1:
p4 = 88;
break;
case 2:
p5 = 88;
break;
case 3:
p6 = 88;
break;
}
}
if (l == 'C') {
switch (n) {
case 1:
p7 = 88;
break;
case 2:
p8 = 88;
break;
case 3:
p9 = 88;
}
}
}
int main() {
ifendg1:
checkalltie(p1,p2,p3,p4,p5,p6,p7,p8,p9);
while (endg == 0) {
srand(time(0));
xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
cout << "Enter the position: ";
cin >> l >> n;
switch (l) {
case 'A':
numberpick();
case 'B':
numberpick();
case 'C':
numberpick();
}
checkalltie(p1,p2,p3,p4,p5,p6,p7,p8,p9);
triplecheckx();
if(endg==1) goto ifendg1;
repeat:
again = 0;
aivall = (rand() % 3) + 1;
aivaln = (rand() % 3) + 1;
ai(aivall, aivaln);
triplechecko();
if(endg==1) goto ifendg1;
if (again == 1) {
goto repeat;
}
checkalltie(p1,p2,p3,p4,p5,p6,p7,p8,p9);
system("cls");
}
}
'X'
and'O'
instead of writing88
and79
-- similar to how you initialized your values as' '
and not32
for clarity e.g.if (p1 == 'X' && p4 == 'X' && p7 == 'X')
. Also, many of your variables are declared in global scope, so when you callcheckalltie(p1,p2,p3,p4,p5,p6,p7,p8,p9)
, you are passing variables to the function that are already in the scope of the function (because they are global). It is good practice to not declare global variables. For example, you could use an arraychar p[9]
and then pass the pointerp
into the functions that need it e.g.checkalltie(p)
. \$\endgroup\$foo(int& a) { a = 3; }
will allow you to update the value that is passed into foo. You can also achieve this by providing the address of the variable as a pointer, and then assigning the value by dereferencing it i.e.foo (int* a) { *a = 3; }
. Arrays are just pointers to the first element in the array, so you can just use the array name and it will behave like the pointer method. \$\endgroup\$