Hi guys
I just started writing in c++ yesterday, although I did write in visual basic before.
After watching some basic c++ youtube video's i decided i wanted to make my first game, tic tac toe. So as a warning i did this without using anyone else's code or any tutorials help so it may be a little scary... but i was wondering why my if statment went onto the else so that it would become the "O" turn. Also if anyone has any hints on how i could write better, it would be appreciated.
#include "stdafx.h"
#include <iostream>
using namespace std;
char Square [9];
bool wingame()
{
if(((Square[0] = 'X') && (Square[1] = 'X') && (Square[2] = 'X')) || ((Square[3] = 'X') && (Square[4] = 'X') && (Square[5] = 'X'))
|| ((Square[6] = 'X') && (Square[7] = 'X') && (Square[8] = 'X')) || ((Square[0] = 'X') && (Square[3] = 'X') && (Square[6] = 'X'))
|| ((Square[1] = 'X') && (Square[4] = 'X') && (Square[7] = 'X')) || ((Square[2] = 'X') && (Square[5] = 'X') && (Square[8] = 'X'))
|| ((Square[0] = 'X') && (Square[4] = 'X') && (Square[8] = 'X')) || ((Square[2] = 'X') && (Square[4] = 'X') && (Square[6] = 'X')));
{
return false;
}
if(((Square[0] = 'O') && (Square[1] = 'O') && (Square[2] = 'O')) || ((Square[3] = 'O') && (Square[4] = 'O') && (Square[5] = 'O'))
|| ((Square[6] = 'O') && (Square[7] = 'O') && (Square[8] = 'O')) || ((Square[0] = 'O') && (Square[3] = 'O') && (Square[6] = 'O'))
|| ((Square[1] = 'O') && (Square[4] = 'O') && (Square[7] = 'O')) || ((Square[2] = 'O') && (Square[5] = 'O') && (Square[8] = 'O'))
|| ((Square[0] = 'O') && (Square[4] = 'O') && (Square[8] = 'O')) || ((Square[2] = 'O') && (Square[4] = 'O') && (Square[6] = 'O')));
{
return false;
}
return true;
}
int main()
{
bool PlayerTurn = true;
int move;
Square [0] = '0';
Square [1] = '1';
Square [2] = '2';
Square [3] = '3';
Square [4] = '4';
Square [5] = '5';
Square [6] = '6';
Square [7] = '7';
Square [8] = '8';
cout << Square[0] << Square[1] << Square[2] << endl;
cout << Square[3] << Square[4] << Square[5] << endl;
cout << Square[6] << Square[7] << Square[8] << endl;
while (wingame)
{
if(PlayerTurn = true)
{
PlayerTurn = false;
cout << "X turn, Enter a number";
cin >> move;
switch(move)
{
case (0):
Square[0] = 'X';
break;
case (1):
Square[1] = 'X';
break;
case (2):
Square[2] = 'X';
break;
case (3):
Square[3] = 'X';
break;
case (4):
Square[4] = 'X';
break;
case (5):
Square[5] = 'X';
break;
case (6):
Square[6] = 'X';
break;
case (7):
Square[7] = 'X';
break;
case (8):
Square[8] = 'X';
break;
}
cout << Square[0] << Square[1] << Square[2] << endl;
cout << Square[3] << Square[4] << Square[5] << endl;
cout << Square[6] << Square[7] << Square[8] << endl;
}
else (PlayerTurn = false);
{
PlayerTurn = true;
cout << "O turn, Enter a number";
cin >> move;
switch(move)
{
case (0):
Square[0] = 'O';
break;
case (1):
Square[1] = 'O';
break;
case (2):
Square[2] = 'O';
break;
case (3):
Square[3] = 'O';
break;
case (4):
Square[4] = 'O';
break;
case (5):
Square[5] = 'O';
break;
case (6):
Square[6] = 'O';
break;
case (7):
Square[7] = 'O';
break;
case (8):
Square[8] = 'O';
break;
}
cout << Square[0] << Square[1] << Square[2] << endl;
cout << Square[3] << Square[4] << Square[5] << endl;
cout << Square[6] << Square[7] << Square[8] << endl;
}
}
system("PAUSE");
}