10 Replies - 438 Views - Last Post: 30 August 2012 - 01:46 AM Rate Topic: ***** 1 Votes

#1 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

Tic-Tac-Toe - The Finished Masterpeace!

Posted 29 August 2012 - 10:43 AM

Hey,
You have probably seen my other thread for the development of my little game, and now YOU can play it!!!

This isn't the most optimised code ever, but I gave a shot at arrays, and as long as the game works, and the code looks pretty neat, its fine by me.

WARNING! GAME HAS A FLASHING SCREEN UPON FINISH!

So, here goes nothing:
// Tic_Tac_Toe.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
#include "windows.h"
#include <string>

using namespace std;

string start;

int x;
int y;

const char* player;
string turn;

const char* board[3][3];


bool turn_x = true;

int main()
{
	cout << "Welcome to 2 player Tic Tac Toe!" << endl;
	cout << "Use Num-Pad syntax! E.g. 7, 2, etc..." << endl << endl;
	cout << "Type 'HELP' or 'INFO' or 'START'" << endl << endl << endl;

	while(1)
	{
		cin >> start;

		cout << endl << endl;

		if(start == "HELP")
		{
			cout << "Use Num-Pad syntax! E.g." << endl << "789\n456\n123" << endl << endl;
		}
		if(start == "INFO")
		{
			cout << "Created by Lukeme99 using VCE2010" << endl << endl;
		}
		if(start == "START")
		{
			break;
		}
	}

	cout << "Starting Game!" << endl << endl;
	Sleep(500);


	for( x = 0; x < 3; x++ )
	{
		for( y = 0; y < 3; y++ )
		{
			board[x][y] = "/"; // Set each element to a value
		}
	}

	while(1)
	{
		if(turn_x)
		{
			player = "X";
		}
		else
		{
			player = "O";
		}

		for ( x = 0; x < 3;x++ )
		{
			for ( y = 0; y < 1; y++ )
			{
				cout << board[x][y];
			}
			for ( y = 1; y < 2; y++ )
			{
				cout << board[x][y];
			}
			for ( y = 2; y < 3; y++ )
			{
				cout << board[x][y];
			}
			cout << endl;
		}
		cout << endl;

		while(1)
		{
			cin >> turn;

			if(turn == "7" && board[0][0] == "/")
			{
				board[0][0] = player;
				break;
			}
			else if(turn == "8" && board[0][1] == "/")
			{
				board[0][1] = player;
				break;
			}
			else if(turn == "9" && board[0][2] == "/")
			{
				board[0][2] = player;
				break;
			}
			else if(turn == "4" && board[1][0] == "/")
			{
				board[1][0] = player;
				break;
			}
			else if(turn == "5" && board[1][1] == "/")
			{
				board[1][1] = player;
				break;
			}
			else if(turn == "6" && board[1][2] == "/")
			{
				board[1][2] = player;
				break;
			}
			else if(turn == "1" && board[2][0] == "/")
			{
				board[2][0] = player;
				break;
			}
			else if(turn == "2" && board[2][1] == "/")
			{
				board[2][1] = player;
				break;
			}
			else if(turn == "3" && board[2][2] == "/")
			{
				board[2][2] = player;
				break;
			}
		}
		cout << endl;

		if( (board[0][0] == "X" && board[0][1] == "X" && board[0][2] == "X") ||
			(board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X") || 
			(board[2][0] == "X" && board[2][1] == "X" && board[2][2] == "X") || 
			(board[0][0] == "X" && board[1][0] == "X" && board[2][0] == "X") || 
			(board[0][1] == "X" && board[1][1] == "X" && board[2][1] == "X") || 
			(board[0][2] == "X" && board[1][2] == "X" && board[2][2] == "X") || 
			(board[0][0] == "X" && board[1][1] == "X" && board[2][2] == "X") || 
			(board[0][2] == "X" && board[1][1] == "X" && board[2][0] == "X") )
		{
		
			for ( x = 0; x < 3;x++ )
			{
				for ( y = 0; y < 1; y++ )
				{
					cout << board[x][y];
				}
				for ( y = 1; y < 2; y++ )
				{
					cout << board[x][y];
				}
				for ( y = 2; y < 3; y++ )
				{
					cout << board[x][y];
				}
				cout << endl;
			}

			cout << "Well Done" << endl;
			cout << "Player 1 is the winner!" << endl;
			Sleep(1000);
			while(1)
			{
				system("COLOR 2");
				cout << "X Won!" << endl;
				Sleep(100);
				system("COLOR F2");
				cout << "X Won!" << endl;
				Sleep(100);
			}
		}

		if( (board[0][0] == "O" && board[0][1] == "O" && board[0][2] == "O") ||
			(board[1][0] == "O" && board[1][1] == "O" && board[1][2] == "O") || 
			(board[2][0] == "O" && board[2][1] == "O" && board[2][2] == "O") || 
			(board[0][0] == "O" && board[1][0] == "O" && board[2][0] == "O") || 
			(board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O") || 
			(board[0][2] == "O" && board[1][2] == "O" && board[2][2] == "O") || 
			(board[0][0] == "O" && board[1][1] == "O" && board[2][2] == "O") || 
			(board[0][2] == "O" && board[1][1] == "O" && board[2][0] == "O") )
		{

			for ( x = 0; x < 3;x++ )
			{
				for ( y = 0; y < 1; y++ )
				{
					cout << board[x][y];
				}
				for ( y = 1; y < 2; y++ )
				{
					cout << board[x][y];
				}
				for ( y = 2; y < 3; y++ )
				{
					cout << board[x][y];
				}
				cout << endl;
			}

			cout << "Well Done" << endl;
			cout << "Player 2 is the winner!" << endl;
			Sleep(1000);
			while(1)
			{
				system("COLOR 2");
				cout << "O Won!" << endl;
				Sleep(100);
				system("COLOR F2");
				cout << "O Won!" << endl;
				Sleep(100);
			}
		}


		if(board[0][0] != "/" && board[0][1] != "/" && board[0][2] != "/" && board[1][0] != "/" && board[1][1] != "/" && board[1][2] != "/" && board[2][0] != "/" && board[2][1] != "/" && board[2][2] != "/")
		{

			for ( x = 0; x < 3;x++ )
			{
				for ( y = 0; y < 1; y++ )
				{
					cout << board[x][y];
				}
				for ( y = 1; y < 2; y++ )
				{
					cout << board[x][y];
				}
				for ( y = 2; y < 3; y++ )
				{
					cout << board[x][y];
				}
				cout << endl;
			}

			cout << "GAME OVER!" << endl;
			cout << "There Are NO Winners!" << endl;
			Sleep(1000);
			while(1)
			{
				system("COLOR 2");
				cout << "GAME OVER!" << endl;
				Sleep(100);
				system("COLOR F2");
				cout << "GAME OVER!" << endl;
				Sleep(100);
			}
		}

		if(turn_x)
		{
			turn_x = false;
		}
		else
		{
			turn_x = true;
		}
	}
}



There you go,
Have FUN!


Thanks for playing,
L99

Is This A Good Question/Topic? 0
  • +

Replies To: Tic-Tac-Toe - The Finished Masterpeace!

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: Tic-Tac-Toe - The Finished Masterpeace!

Posted 29 August 2012 - 10:56 AM

Do you realize that since you are not using any functions you can easily stop using all the global variables? Global variables should be avoided when ever possible. For this small program they are not much of a problem but as your programs grow their use will lead to programs that are harder and harder to debug or modify. I recommend you stop using them now and learn to properly pass the variables to and from any functions you happen to use in the future.

Jim

This post has been edited by jimblumberg: 29 August 2012 - 10:57 AM

Was This Post Helpful? 2
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5760
  • View blog
  • Posts: 12,574
  • Joined: 16-October 07

Re: Tic-Tac-Toe - The Finished Masterpeace!

Posted 29 August 2012 - 11:08 AM

And use functions! Lots of functions. Ouch.

This is wrong:
const char* board[3][3];



I can't believe you didn't get a warning about that. Should be:
char board[3][3];



What do you believe "const char *" does for you here?

I'm glad you got it done and are happy enough to share, but "as long as it works" is really not something to aspire to. When you have time, see if you can make your new baby something to be unequivocally proud of.
Was This Post Helpful? 3
  • +
  • -

#4 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

Re: Tic-Tac-Toe - The Finished Masterpeace!

Posted 29 August 2012 - 11:08 AM

View Postjimblumberg, on 29 August 2012 - 06:56 PM, said:

Do you realize that since you are not using any functions you can easily stop using all the global variables? Global variables should be avoided when ever possible. For this small program they are not much of a problem but as your programs grow their use will lead to programs that are harder and harder to debug or modify. I recommend you stop using them now and learn to properly pass the variables to and from any functions you happen to use in the future.

Jim


Thanks for the tip, but this is nearly my first program, and, really, my first proper program that serves a purpose!
Was This Post Helpful? 0
  • +
  • -

#5 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

Re: Tic-Tac-Toe - The Finished Masterpeace!

Posted 29 August 2012 - 11:13 AM

View Postbaavgai, on 29 August 2012 - 07:08 PM, said:

And use functions! Lots of functions. Ouch.

This is wrong:
const char* board[3][3];



I can't believe you didn't get a warning about that. Should be:
char board[3][3];



What do you believe "const char *" does for you here?

I'm glad you got it done and are happy enough to share, but "as long as it works" is really not something to aspire to. When you have time, see if you can make your new baby something to be unequivocally proud of.


I wanted to use char, but VS spat out an error, I only assumed you had to use const char* for "/" because once I changed it the error went away!

Also, what I said about as long as it works... well... I didn't mean it like that really. What I meant was, this might not be the way you guys might do it, but I am proud of what I did, and I hope you guys enjoy it too!
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: Tic-Tac-Toe - The Finished Masterpeace!

Posted 29 August 2012 - 11:18 AM

Then I recommend you fix the problems so you will really have something to be proud of. Also the use of the system() command to pause, clear the screen, or change the color or the screen is also considered a bad programming practice.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

Re: Tic-Tac-Toe - The Finished Masterpeace!

Posted 29 August 2012 - 11:26 AM

View Postjimblumberg, on 29 August 2012 - 07:18 PM, said:

Then I recommend you fix the problems so you will really have something to be proud of. Also the use of the system() command to pause, clear the screen, or change the color or the screen is also considered a bad programming practice.

Jim


Yeah... I soughta' guessed that system was frowned upon.

Don't worry, I WILL clear this up (progressively), this is only a Version 1.0 Beta, so there will be more features and updates (and functions :P) in the future, this is the only c++ project I have made from scratch without copy and paste!

So every so often I will post an update on how I'm doing, and show you my code.

Hell, I might even turn this into an OpenGL based app! But for now, I got my messy little code. But that's for now...
Was This Post Helpful? 1
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: Tic-Tac-Toe - The Finished Masterpeace!

Posted 29 August 2012 - 11:30 AM

Quote

So every so often I will post an update on how I'm doing, and show you my code.

No problem but you may want to post your updates into the share your project forum.

Quote

this is the only c++ project I have made from scratch without copy and paste!

Doesn't it feel better to do the work yourself? You will learn so much more! Good work!


Jim
Was This Post Helpful? 1
  • +
  • -

#9 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

Re: Tic-Tac-Toe - The Finished Masterpeace!

Posted 29 August 2012 - 11:35 AM

View Postjimblumberg, on 29 August 2012 - 07:30 PM, said:

No problem but you may want to post your updates into the share your project forum.

Doesn't it feel better to do the work yourself? You will learn so much more! Good work!

Jim


Sure thing! Well... I will be here quit often because of problems, and advice on how I can tidy x, y and z up.

Yep, feels great to know that this work is ALL my hard work, but it would still be a rubbish string based piece of * if it wasn't for you guys in this forum.
Thank you!

This post has been edited by lukeme99: 29 August 2012 - 11:37 AM

Was This Post Helpful? 0
  • +
  • -

#10 rethc  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 76
  • Joined: 23-April 12

Re: Tic-Tac-Toe - The Finished Masterpeace!

Posted 29 August 2012 - 02:46 PM

View Postlukeme99, on 29 August 2012 - 11:13 AM, said:

I wanted to use char, but VS spat out an error, I only assumed you had to use const char* for "/" because once I changed it the error went away!


You have to use single quotes.

board[x][y] = '/';


instead of

board[x][y] = "/";

Was This Post Helpful? 1
  • +
  • -

#11 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

Re: Tic-Tac-Toe - The Finished Masterpeace!

Posted 30 August 2012 - 01:46 AM

View Postrethc, on 29 August 2012 - 10:46 PM, said:

View Postlukeme99, on 29 August 2012 - 11:13 AM, said:

I wanted to use char, but VS spat out an error, I only assumed you had to use const char* for "/" because once I changed it the error went away!


You have to use single quotes.

board[x][y] = '/';


instead of

board[x][y] = "/";



Ahh! Thank you so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1