Tic Tac Toe

Started by
5 comments, last by GameDev.net 17 years, 10 months ago
hey, This is the first game ive made in C++ im still in Middle School so im not sure how itll be, so far there are no graphics and all its just text, but I want to make sure that my code works before i implement the graphics. So could you guys just run it and make sure that the win and draw things are working and that ther are no combinations that arent working. (Dont look at the main() code I know its shit but its just temporary until I get graphics). Could you just compile the code and run it? (I made it in Turbo C++, so if you get any errors in DevC++ could you fix them? ) Thanx alot.
#include<iostream.h>
#include<conio.h>

int norepeat(int[5], int[5], int, int);
void sort();
int check(int[5], int[5], int);
int draw(int[5], int[5]);


int x[5] = { 10, 10, 10, 10, 10 };
int o[5] = { 10, 10, 10, 10, 10 };




int main()
{

	int choicex, choiceo;
	int turn = 0;
	int rep = 0;
	int turn1 = 0;
	int draw1 = 0;


	while (rep == 0 && draw1 == 0) {


	choose:
	cout << endl << "X chooses: ";
	cin >> choicex;


	int b = norepeat(x, o, choicex, turn);


	if (b == 0)
	{
		goto choose;
	}



	x[turn] = choicex;
	++turn;

	sort();

	rep = check(x, o, turn);
	draw1 = draw(x, o);

	if (rep != 0 || draw1 != 0)
	{
		goto end;
	}

	choose1:
	cout << endl << "O chooses: ";
	cin >> choiceo;

	b = norepeat(x, o, choiceo, turn1);

	if (b == 0)
	{
		goto choose1;

	}
	o[turn1] = choiceo;
	++turn1;

	sort();


	rep = check(x, o, turn1);
	draw1 = draw(x, o);

	end:


	}


	getch();
	return 0;
}

void sort()
{
	int temp;

	for (int sort1 = 0; sort1 < 4; sort1++)
	{
		for (int sort2 = sort1 + 1; sort2 < 5; sort2++)
		{
			if (x[sort1] > x[sort2])
			{
				temp = x[sort2];
				x[sort2] = x[sort1];
				x[sort1] = temp;
			}

			if (o[sort1] > o[sort2])
			{
				temp = o[sort2];
				o[sort2] = o[sort1];
				o[sort1] = temp;
			}
		}
	}

}


int norepeat(int x[5], int o[5], int choice, int turn)
{
	int norepeat = 1;

	if (choice > 0 && choice < 10)
	{

		for (int repeat = 0; repeat < turn + 1; repeat++)
		{
			if ( x[repeat] == choice || o[repeat] == choice)
			{
				norepeat = 0;
			}

		}
	}

	else
	{
		norepeat = 0;
	}


	return norepeat;



}

int check (int x[5], int o[5], int turn)
{
	int check = 0;

	for (int z = 0; z < 5; z++)
	{
		for (int j = 0; j < turn - 2; j++)
		{
			for (int i = j + 1; i < turn - 1; i++)
			{
				for (int k = i + 1; k < turn; k++)
				{
					if( x == x[j] + z && x[k] == x + z)
					{
						if ( ( x[j] > 0 && x[j] < 4 ) || x[j] == 4 || x[j] == 7 )
						{

							if ( x[j] != 3 && z == 2 && x[k] != 7 )
							{
								check = 0;
								cout << endl << "Cond 2";
								return check;
							}

							if ( (x[j] != 1 || x[j] != 4 || x[j] != 7) && z == 1)
							{
								check = 0;
								cout << endl << "Cond 4";
							}
							else
							{
								check = 1;
								cout << endl << "Cond 1 X Wins";
								return check;
							}
						}


						if ( (x[j] == 1 || x[j] == 4 || x[j] == 7) && z == 1)
						{
							check = 1;
							cout << endl << "Cond 5 X wins";
						}


						if ( x[j] == 3 && z == 2 && x[k] == 7)
						{
							check = 1;
							cout << endl << "Cond 3 X wins";
						}

						return check;
					}

					if( o == o[j] + z && o[k] == o + z)
					{

						if ( ( o[j] > 0 && o[j] < 4 ) || o[j] == 4 || o[j] == 7 )
						{

							if ( o[j] != 3 && z == 2 && o[k] != 7 )
							{
								check = 0;
								cout << endl << "Cond 2";
								return check;
							}

							if ( (o[j] != 1 || o[j] != 4 || o[j] != 7) && z == 1)
							{
								check = 0;
								cout << endl << "Cond 4";
							}

							else
							{
								check = 2;
								cout << endl << "Cond 1 O Wins";
								return check;
							}
						}


						if ( (o[j] == 1 || o[j] == 4 || o[j] == 7) && z == 1)
						{
							check = 2;
							cout << endl << "Cond 5 O wins";
						}


						if ( o[j] == 3 && z == 2 && o[k] == 7)
						{
							check = 2;
							cout << endl << "Cond 3 O Wins ";
						}

						return check;

					}
				}
			}
		}
	}





	return check;
}


int draw (int x[5], int o[5])
{
	int check = 0;
	int draw = 0;

	for (int l = 0; l < 5; l++)
	{
		check = check + x[l] + o[l];


		if ( check == 55 && l == 4)
		{
			draw = 1;
			cout << endl << "Its a Draw" << endl;
			return draw;

		}
	}


	return draw;
}
EDIT: Large blocks of code should be wrapped in 'source' tags...
Advertisement
sorry about no indents, its not letting me do them
Quote:Original post by General Zayd
sorry about no indents, its not letting me do them
You need to use [source]...[/source] tags to get proper text formatting. I've edited your post for you.

Also, I'm going to push this over to 'For Beginners' - Maybe someone there will be so kind as to offer you a code review. I can't really see any reason why this landed in DirectX [smile].

Cheers,
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

There were a few things that I really didn't like about your code. Your use of goto(not really needed) and the fact you didn't use a game board in your code. It was very hard to figure out how your code worked without comments as well, so I decided to just write my own tic-tac-toe game to offer some measure of inspiration.

This code isn't nearly the best, just something quick and dirty I wrote up to better show how you might organize your code. It makes use of vectors for the board, and strings for anything input related. If you want to know how things like vector, string, getline, etc works, just do a search for something like "vector c++ tutorial" and you'll likely get a good result.
// Two player tic-tac-toe, written by Dan J.#include<iostream>#include<vector>#include<string>using std::string;using std::cout;using std::vector;using std::endl;using std::cin;string get_user_name();string get_symbol(string user_name);void print_board(vector<char> board);void main_game(string player_name[2]);bool valid_position(int position, vector<char> board);bool check_for_win(vector<char> board);int main(){	// who is playing	string player_name[2];	// Title screen	cout << "Welcome to tic-tac-toe!" << endl;	// get user names:	player_name[0] = get_user_name();	player_name[1] = get_user_name();	// continue playing until users want to quit	string user_choice;	do	{		main_game(player_name);		cout << "Do you wish to (q)uit? ";		cin.ignore(80, '\n');		getline(cin, user_choice);	}while(user_choice != "q" && user_choice != "Q");	return 0;}string get_user_name() {	cout << "Enter player name: ";	string player;	getline(cin, player);	return player;}void print_board(vector<char> board){	assert(board.size() == 9);	cout << board[0] << " | " << board[1] << " | " << board[2] << endl;	cout << "---------" << endl;	cout << board[3] << " | " << board[4] << " | " << board[5] << endl;	cout << "---------" << endl;	cout << board[6] << " | " << board[7] << " | " << board[8] << endl << endl;}void main_game(string player_name[2]){	// the default board the users start playing on	// only used to initialize the vector	char def_board[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9' };	// initialize playing board	vector<char> board(def_board, def_board + 9);	int turn = 0; // first player is X, second O	do {		print_board(board);		cout << player_name[turn] << ", enter the number where you wish to move: ";		int position;		cin >> position;		position--;		if(!valid_position(position, board))		{			cout << "I'm sorry, you can't move there." << endl;			continue;		}		// mark the position on the board		if(turn == 0)		{			board[position] = 'X';		}		else		{			board[position] = 'O';		}		// check for if the last move won		if(check_for_win(board))		{			cout << "Congratulations, " << player_name[turn] << ", you won!" << endl;			return;		}		turn = (turn + 1) % 2;	}while(true);}bool valid_position(int position, vector<char> board){	if(position < 0 || position > 8)	{		return false;	}	if(board[position] == 'X')	{		return false;	}	if(board[position] == 'O')	{		return false;	}	return true;}bool check_for_win(vector<char> board){	// there are 8 possible ways to win.	// Three horizontal	if(board[0] == board[1] && board[1] == board[2])	{		return true;	}	if(board[3] == board[4] && board[4] == board[5])	{		return true;	}	if(board[6] == board[7] && board[7] == board[8])	{		return true;	}	// Three verticle	if(board[0] == board[3] && board[3] == board[6])	{		return true;	}	if(board[1] == board[4] && board[4] == board[7])	{		return true;	}	if(board[2] == board[5] && board[5] == board[8])	{		return true;	}	// Two diagonal	if(board[0] == board[4] && board[4] == board[8])	{		return true;	}	if(board[2] == board[4] && board[4] == board[6])	{		return true;	}	// otherwise no one won	return false;}


This was written in Code::Blocks, with the mingw compiler.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Thank you for taking the time to write out a whole program :). I was looking at your comments and the gotos in the main function were only temporary until I implemented a whole graphics interface (which I am working on right now). Basically my program initializes all 9 squares to values in a array by the users input. I then derived an alogorithm of possible wins by tic tac toe and my program checks that if the values of the arrays fit into the equation through the use of for loops nd ifs.
So having goto's is an excuse for not using graphics? Wonder how all the textbased rpg's would look like :-)
------------Something useful could be written here...
Yeah it is do you pay any attention to what I was writing (textbased is only temp.)

This topic is closed to new replies.

Advertisement