I'm quite new too C++ but finally my experience is good enough to actually create something, that's what I thought... So I triedto make this easy Tic Tac Toe Game and I think i got it all right but obviously not cause when I try to choose somewhere to put my X or O the window shuts down. This is the Code if you know what's wrong then please help me.
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <stdio.h>
#include <sstream>
using namespace std;
int main()
{
char Csquare1('1');
char Csquare2('2');
char Csquare3('3');
char Csquare4('4');
char Csquare5('5');
char Csquare6('6');
char Csquare7('7');
char Csquare8('8');
char Csquare9('9');
int PlayerTurn(1);
bool GameOverWin(true);
bool GameOver(false);
do {
cout << Csquare1 << "|" << Csquare2 << "|" << Csquare3 << "|" << endl;
cout << "-+-+-" << endl;
cout << Csquare4 << "|" << Csquare5 << "|" << Csquare6 << "|" << endl;
cout << "-+-+-" << endl;
cout << Csquare7 << "|" << Csquare8 << "|" << Csquare9 << "|" << endl;
cout << "-+-+-" << endl;
char cPlayerMark;
if(PlayerTurn == 1) {
cPlayerMark = 'X';
} else {
cPlayerMark = 'O';
}
cout << "Player" << PlayerTurn << "'s move" << endl;
bool bValidMove;
do{
char NextMove;
cin >> NextMove;
bValidMove = true;
if(NextMove == '1' && Csquare1 == '1') {
Csquare1 = cPlayerMark;
} else if (NextMove == '2' && Csquare2 == '2') {
Csquare2 = cPlayerMark;
} else if (NextMove == '3' && Csquare3 == '3') {
Csquare3 = cPlayerMark;
} else if (NextMove == '4' && Csquare4 == '4') {
Csquare4 = cPlayerMark;
} else if (NextMove == '5' && Csquare5 == '5') {
Csquare5 = cPlayerMark;
} else if (NextMove == '6' && Csquare6 == '6') {
Csquare6 = cPlayerMark;
} else if (NextMove == '7' && Csquare7 == '7') {
Csquare7 = cPlayerMark;
} else if (NextMove == '8' && Csquare8 == '8') {
Csquare8 = cPlayerMark;
} else if (NextMove == '9' && Csquare9 == '9') {
Csquare9 = cPlayerMark;
} else {
cout << "Invalid move, try again" << endl;
bValidMove = false;
}
}
while (!bValidMove);
if(Csquare1 != '1') {
if(Csquare2 == Csquare1 && Csquare3 == Csquare1) {
GameOverWin = true;
} if(Csquare4 == Csquare1 && Csquare7 == Csquare1) {
GameOverWin = true;
} if(Csquare5 == Csquare1 && Csquare9 == Csquare1) {
GameOverWin = true;
}
}
if(Csquare3 != '3') {
if(Csquare6 == Csquare3 && Csquare9 == Csquare3) {
GameOverWin = true;
} if(Csquare5 == Csquare3 && Csquare7 == Csquare3) {
GameOverWin = true;
}
}
if(Csquare5 != '5') {
if(Csquare2 == Csquare5 && Csquare8 == Csquare5) {
GameOverWin = true;
} if(Csquare4 == Csquare5 && Csquare6 == Csquare5) {
GameOverWin = true;
}
}
if(Csquare6 != '9') {
if(Csquare8 == Csquare9 && Csquare7 == Csquare9) {
GameOverWin = true;
}
}
if(Csquare1 != '1' && Csquare2 != '2' && Csquare3 != '3' &&
Csquare4 != '4' && Csquare5 != '5' && Csquare6 != '6' &&
Csquare7 != '7' && Csquare8 != '8' && Csquare9 != '9' && !GameOverWin)
{
GameOver = true;
}
if(GameOverWin) {
cout << "Player" << PlayerTurn << "wins!" << endl;
}
else {
cout << "Game Over\n" << endl;
}
cout << Csquare1 << "|" << Csquare2 << "|" << Csquare3 << "|" << endl;
cout << "-+-+-" << endl;
cout << Csquare4 << "|" << Csquare5 << "|" << Csquare6 << "|" << endl;
cout << "-+-+-" << endl;
cout << Csquare7 << "|" << Csquare8 << "|" << Csquare9 << "|" << endl;
cout << "-+-+-" << endl;
if(PlayerTurn == 1) {
PlayerTurn = 2;
} else {
PlayerTurn = 1;
}
} while(!GameOverWin);
}