I'm kind of teaching myself C++ from the ground up. I have previous programming experience in PHP, VB, Javascript, .net and i've done some other small stuff here and there but i've been putting off C++ for way too long and feel like I really need to learn it in-depth.

This is some code that I whipped up for a basic tic-tac-toe game. I'm just looking for some pointers and some constructive criticism of my code. Keep in mind i'm new to C++ and this is really only coded with a days knowledge of the language plus some previous coding experience.

Thanks

#include <iostream>
#include <string>
using namespace std;

void main () {

/***VARIABLES*******/
string squares[] = {"0","1","2","3","4","5","6","7","8","9"};
string squareArray[] = {"0","1","2","3","4","5","6","7","8","9"};
string playAgain = "N";
string strMove;

short iTurn(1);
short turns(0);
short gameWon(0);
short i;

bool validMove(false);
bool gameQuit(false);
/*******************/

cout << "     *************************************************************\n";
cout << "     *                       TIC-TAC-TOE                         *\n";
cout << "     *                         Author: Pat                          *\n";
cout << "     *                   Testing coding stuff                    *\n";
cout << "     *   Player 1: X                              Player 2: O    *\n";
cout << "     *************************************************************\n";
cout << "\n\n\n\n";

do{

	do{
cout << "\n" << squareArray[1] << "|" << squareArray[2] << "|" << squareArray[3] << endl;
cout << "-+-+-" << endl;
cout << squareArray[4] << "|" << squareArray[5] << "|" << squareArray[6] << endl;
cout << " -+-+-" << endl;
cout << squareArray[7] << "|" << squareArray[8] << "|" << squareArray[9] << endl;

while(validMove == false){
	cout << "\nPlayer " << iTurn << " pick a square: ";
	cin >> strMove;
	strMove = strMove.substr(0,1);	// only grabs first character

for(i = 1;i < 10; i++){
	if(squares[i] == strMove){// If true number entered (strMove) is 1-9
		if(squareArray[i] == strMove){// If true square != X or O
		validMove = true;
		break;
			}
		}
	}
}

validMove = false;
if( iTurn == 1) { squareArray[i] = "X";} else { squareArray[i] = "O"; } // chooses sybmol

	// checks through possible winning combinations
if(squareArray[1] != "1"){ // checks wins through square 1
	if(squareArray[1] == squareArray[2] && squareArray[1] == squareArray[3]){ gameWon = iTurn; }
	if(squareArray[1] == squareArray[4] && squareArray[1] == squareArray[7]){ gameWon = iTurn; }
			}

if(squareArray[5] != "1"){ // checks wins through square 5 (middle)
	if(squareArray[5] == squareArray[1] && squareArray[5] == squareArray[9]){ gameWon = iTurn; }
	if(squareArray[5] == squareArray[3] && squareArray[5] == squareArray[7]){ gameWon = iTurn; }
	if(squareArray[5] == squareArray[4] && squareArray[5] == squareArray[6]){ gameWon = iTurn; }
	if(squareArray[5] == squareArray[2] && squareArray[5] == squareArray[8]){ gameWon = iTurn; }
			}

if(squareArray[9] != "1"){ // checks wins through square 9
	if(squareArray[9] == squareArray[6] && squareArray[9] == squareArray[3]){ gameWon = iTurn; }
	if(squareArray[9] == squareArray[7] && squareArray[9] == squareArray[8]){ gameWon = iTurn; }
			}

if( iTurn == 1) { iTurn = 2; } else { iTurn = 1; } //changes turns
turns++; // adds one to total turns

	} while(turns < 9 && gameWon == 0);

cout << "\n" << squareArray[1] << "|" << squareArray[2] << "|" << squareArray[3] << endl;
cout << "-+-+-" << endl;
cout << squareArray[4] << "|" << squareArray[5] << "|" << squareArray[6] << endl;
cout << "-+-+-" << endl;
cout << squareArray[7] << "|" << squareArray[8] << "|" << squareArray[9] << endl;

if(turns >= 9){ cout << "     *       No Moves Left - Game Over             *\n"; }
		
if(gameWon != 0){ cout<<"     **********PLAYER " << gameWon << " WINS**********\n"; }

		cout << "\nPlay Again (Y/N): ";
		cin >> playAgain;

		if(playAgain == "Y" || playAgain == "y"){
			gameWon = 0;
			turns = 0;
			squares[1] = "1";
			squares[2] = "2";
			squares[3] = "3";
			squares[4] = "4";
			squares[5] = "5";
			squares[6] = "6";
			squares[7] = "7";
			squares[8] = "8";
			squares[9] = "9";

			squareArray[1] = "1";
			squareArray[2] = "2";
			squareArray[3] = "3";
			squareArray[4] = "4";
			squareArray[5] = "5";
			squareArray[6] = "6";
			squareArray[7] = "7";
			squareArray[8] = "8";
			squareArray[9] = "9";
			gameQuit = false;

		} else { gameQuit = true; }
}while(gameQuit == false);

cout << "\n\nThanks for playing\n";
system("PAUSE");
	}

Recommended Answers

All 3 Replies

>>and some constructive criticism
ok, here goes

>> void main()
never use it. Read this:

lines 13-19: that an onconventional way to initialize variables. It will work, but that form is rarly ever used. more commen is int iTurn = 1; "\n" is usually preferred to endl because endl also performs unnecessary flush.

And format your code. It's too hard to follow with inconsistent spacing.

First things first, I second to WaltP, really format your code to make it readable. Now it's close to unreadable.
Arrays are zero-indexed .. hence both of your string arrays now have an unused index 0, in other words, you should do with smaller (by one) string arrays.

string squares[] = {"1","2","3","4","5","6","7","8","9"};
instead of 
string squares[] = {"0", "1","2","3","4","5","6","7","8","9"};

You could throw away the squares array, since you only use it to validate that user's move is within the accepted bounds (I haven't seen a similar construct before). You might do better by getting an integral value instead of strMove (and throwing away the for loop too).
Using arrays of std::string is a bit heavy-duty solution, a more simple type as char would serve as well.

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.