Jump to content

Recommended Posts

Posted (edited)

Hey guys, I wrote a C++ tic tac toe program in my Comp Sci class last year, I was just wondering if anyone had any constructive criticism for it. It was written on Turbo C++ for an XP machine.

 

Here's the code, you can c/p it into your compiler

 

//tictac.cpp

/*

       	Illustratespassing a 2-dimensional array to a function.

*/



#include <iostream.h>

#include <h:\boolean.h>



//initializes the boards to all '*'

void initialize(char T[3][3]);



//prints the grid onto the screen.

void printArray(char T[3][3]);



//player's turn

//element R,C will be set to 'X' or 'O'

//returns true if successful, false if not.

boolean playerTurn(char T[3][3], int turncount, int R, intC);



//check win

//returns true if there is a winner

boolean checkWin(char T[3][3], int turncount);



int main()

{

char T[3][3];

int turncount = 0,winner = 0;

boolean turncheck =false, win = false;





initialize(T);

printArray(T);



for (turncount = 0;turncount < 9; turncount++)

{



cout << endl;



if (turncount % 2 ==0)

       	{cout<< "Player 1 turn." << endl;}

else

       	{cout<< "Player 2 turn." << endl;}



do{

                   	cout<< endl << "Input a row # followed by a column #"<< endl;



                   	intr = -1, c = -1;



                   	do{

                   	cout<< "Row: ";

                   	cin>> r;

                   	}while(((r< 1) || (r > 3)));



                   	do{

                   	cout<< "Column: ";

                   	cin>> c;

                   	}while(((c< 1) || (c > 3)));



                   	turncheck= false;



                   	turncheck= playerTurn(T, turncount, r, c);

                   	cout<< endl;



}while(turncheck ==false);



printArray(T);



win = false;



win = checkWin(T,turncount);



if (win == true)

       	{

        	winner = turncount % 2;

        	turncount = 9;

       	}

}



if (winner == 0&& win == true)

       	cout<< endl << "Player 1 wins!" << endl;

else if (winner == 1&& win == true)

       	cout<< endl << "Player 2 wins!" << endl;

else

       	cout<< endl << "It's a tie! You suck!" << endl;



return 0;

}//main



void initialize(char T[3][3])

{

int i, j;

for (j = 0; j < 3;j++)

       	for (i = 0;i < 3; i++)

                   	T[i][j]= '*';

}



void printArray(char T[3][3])

{

int i, j;



for (j = 0; j < 3;j++)

        	{for (i = 0; i < 3; i++)

                    	{

                    	cout << T[i][j];

                               	if(i != 2)

                                           	{cout<< " | ";}

                    	}

                    	cout << endl;

                               	if(j != 2)

                                           	{cout<< "-----------" << endl;}

                    	}

}



boolean playerTurn(char T[3][3], int turncount, int R, intC)

{

if (T[(C - 1)][(R -1)] != '*')

       	returnfalse;

else

       	{

        	if (turncount % 2 == 0)

                   	{T[(C- 1)][(R - 1)] = 'x';}

        	else

                   	{T[(C- 1)][(R - 1)] = 'o';}

        	return true;

       	}

}



boolean checkWin(char T[3][3], int turncount)

{

if (turncount % 2 ==0)

       	{if((T[0][0] == 'x') && (T[0][1] == 'x') && (T[0][2] == 'x'))

                   	returntrue;

        	else if ((T[0][0] == 'x') && (T[1][0]== 'x') && (T[2][0] == 'x'))

                   	returntrue;

        	else if ((T[0][0] == 'x') && (T[1][1]== 'x') && (T[2][2] == 'x'))

                   	returntrue;

        	else if ((T[1][0] == 'x') && (T[1][1]== 'x') && (T[1][2] == 'x'))

                   	returntrue;

        	else if ((T[0][1] == 'x') && (T[1][1]== 'x') && (T[2][1] == 'x'))

                   	returntrue;

        	else if ((T[0][2] == 'x') && (T[1][2]== 'x') && (T[2][2] == 'x'))

                   	returntrue;

        	else if ((T[0][2] == 'x') && (T[1][1]== 'x') && (T[2][0] == 'x'))

                   	returntrue;

        	else if ((T[2][0] == 'x') && (T[2][1]== 'x') && (T[2][2] == 'x'))

                   	returntrue;

        	else

                   	returnfalse;

        	}

else

       	{if((T[0][0] == 'o') && (T[0][1] == 'o') && (T[0][2] == 'o'))

                   	returntrue;

        	else if ((T[0][0] == 'o') && (T[1][0]== 'o') && (T[2][0] == 'o'))

                   	returntrue;

        	else if ((T[0][0] == 'o') && (T[1][1]== 'o') && (T[2][2] == 'o'))

                   	returntrue;

        	else if ((T[1][0] == 'o') && (T[1][1]== 'o') && (T[1][2] == 'o'))

                   	returntrue;

        	else if ((T[0][1] == 'o') && (T[1][1]== 'o') && (T[2][1] == 'o'))

                   	returntrue;

        	else if ((T[0][2] == 'o') && (T[1][2]== 'o') && (T[2][2] == 'o'))

                   	returntrue;

        	else if ((T[0][2] == 'o') && (T[1][1]== 'o') && (T[2][0] == 'o'))

                   	returntrue;

        	else if ((T[2][0] == 'o') && (T[2][1]== 'o') && (T[2][2] == 'o'))

                   	returntrue;

        	else

                   	returnfalse;}



}


 

note a lot of the spacing got messed up when i copied it here... assume its correct

Edited by DaLastBoss
Posted

The first critique would be your terrific line spacing, and eight spaces between an outer and an inner block is complete overkill to me. More spaces and more tabs do not necessarily make a code mode readable. You probably didn't do that on purpose, but please note that "please invest some time in commenting on my code, I didn't bother to spend the two minutes of my time to properly format it" is just plain rude.

Posted

My compiler is telling me you haven't declared some variables that are being used , probably partly to do with a few of the following missing ;

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.