12 Replies - 1261 Views - Last Post: 29 August 2012 - 05:31 AM Rate Topic: -----

#1 lukeme99  Icon User is offline

  • D.I.C Head

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

How can I improve my Tic Tac Toe program?

Posted 28 August 2012 - 01:27 AM

Hi,
I designed a really basic Tic Tac Toe program, but one that uses no arrays or matrixes, just a simple string system.
But if you look at it, there are about a million improvements I could make, but I dont know where to start. What do you guys suggest? I thought of using a matrix, but dont know how to use one...

Any help is welcome.

// 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;

string pl1;
string pl2;

string a1 = "/";
string a2 = "/";
string a3 = "/";
string b1 = "/";
string b2 = "/";
string b3 = "/";
string c1 = "/";
string c2 = "/";
string c3 = "/";

int main()
{
	cout << "Welcome to 2 pl1ayer 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);
	
	while(1)
	{
		cout << endl << a1 << a2 << a3 << endl << b1 << b2 << b3 << endl << c1 << c2 << c3 << endl << endl;

		cin >> pl1;

		if(pl1 == "7")
		{
			a1 = "X";
		}
		if(pl1 == "8")
		{
			a2 = "X";
		}
		if(pl1 == "9")
		{
			a3 = "X";
		}
		if(pl1 == "4")
		{
			b1 = "X";
		}
		if(pl1 == "5")
		{
			b2 = "X";
		}
		if(pl1 == "6")
		{
			b3 = "X";
		}
		if(pl1 == "1")
		{
			c1 = "X";
		}
		if(pl1 == "2")
		{
			c2 = "X";
		}
		if(pl1 == "3")
		{
			c3 = "X";
		}


		cout << endl << a1 << a2 << a3 << endl << b1 << b2 << b3 << endl << c1 << c2 << c3 << endl << endl;

		cin >> pl2;

		if(pl2 == "7")
		{
			a1 = "O";
		}
		if(pl2 == "8")
		{
			a2 = "O";
		}
		if(pl2 == "9")
		{
			a3 = "O";
		}
		if(pl2 == "4")
		{
			b1 = "O";
		}
		if(pl2 == "5")
		{
			b2 = "O";
		}
		if(pl2 == "6")
		{
			b3 = "O";
		}
		if(pl2 == "1")
		{
			c1 = "O";
		}
		if(pl2 == "2")
		{
			c2 = "O";
		}
		if(pl2 == "3")
		{
			c3 = "O";
		}
	}
	return 0;
}



Thanks for any replies!

Is This A Good Question/Topic? 0
  • +

Replies To: How can I improve my Tic Tac Toe program?

#2 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: How can I improve my Tic Tac Toe program?

Posted 28 August 2012 - 01:37 AM

For start you should go more in-depth with C++ . Go look on http://www.cplusplus.com/doc/tutorial/ for some good tutorials .
Also I would recommend a beginner's programming book such as "Beginning C++ through game programming" by Michael Dawson . This book also includes a very nice Tic-Tac-Toe example .
Was This Post Helpful? 1
  • +
  • -

#3 lukeme99  Icon User is offline

  • D.I.C Head

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

Re: How can I improve my Tic Tac Toe program?

Posted 28 August 2012 - 01:38 AM

View PostTwoOfDiamonds, on 28 August 2012 - 09:37 AM, said:

For start you should go more in-depth with C++ . Go look on http://www.cplusplus.com/doc/tutorial/ for some good tutorials .
Also I would recommend a beginner's programming book such as "Beginning C++ through game programming" by Michael Dawson . This book also includes a very nice Tic-Tac-Toe example .


Thank you very much, I will check those out!
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: How can I improve my Tic Tac Toe program?

Posted 28 August 2012 - 01:39 AM

Use an array. Lines 18 and 26 is easier to look at as an array of Strings. Use arrays! ^^
Was This Post Helpful? 1
  • +
  • -

#5 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: How can I improve my Tic Tac Toe program?

Posted 28 August 2012 - 01:41 AM

View Postlukeme99, on 28 August 2012 - 11:38 AM, said:

View PostTwoOfDiamonds, on 28 August 2012 - 09:37 AM, said:

For start you should go more in-depth with C++ . Go look on http://www.cplusplus.com/doc/tutorial/ for some good tutorials .
Also I would recommend a beginner's programming book such as "Beginning C++ through game programming" by Michael Dawson . This book also includes a very nice Tic-Tac-Toe example .


Thank you very much, I will check those out!


My pleasure ! And thank you for reputation .
Was This Post Helpful? 0
  • +
  • -

#6 lukeme99  Icon User is offline

  • D.I.C Head

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

Re: How can I improve my Tic Tac Toe program?

Posted 28 August 2012 - 01:45 AM

View PostfromTheSprawl, on 28 August 2012 - 09:39 AM, said:

Use an array. Lines 18 and 26 is easier to look at as an array of Strings. Use arrays! ^^


Ok, well, I havnt really looked into arrays much, but google is my friend, so, thanks for the tip!
Was This Post Helpful? 0
  • +
  • -

#7 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: How can I improve my Tic Tac Toe program?

Posted 28 August 2012 - 01:55 AM

Yep, that is the next logical step. I did the tic-tac-toe your way before too. ^^
Was This Post Helpful? 0
  • +
  • -

#8 lukeme99  Icon User is offline

  • D.I.C Head

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

Re: How can I improve my Tic Tac Toe program?

Posted 28 August 2012 - 02:25 AM

View PostfromTheSprawl, on 28 August 2012 - 09:55 AM, said:

Yep, that is the next logical step. I did the tic-tac-toe your way before too. ^^


For legacy purposes, I will complete my crappy program equipped with full(ish) logic. Just because I can and would like to!
Was This Post Helpful? 0
  • +
  • -

#9 rethc  Icon User is offline

  • D.I.C Head

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

Re: How can I improve my Tic Tac Toe program?

Posted 28 August 2012 - 05:00 AM

I would do something like this:

while(1)
{
	if(x_turn)
    {
        player = "X";
    }
    else
    {
        player = "O";
    } 
        
    cout << endl << a1 << a2 << a3 << endl << b1 << b2 << b3 << endl << c1 << c2 << c3 << endl << endl;	
	cin >> move;

	if(move == "7")
	{
		a1 = player;
	}
	if(move == "8")
	{
		a2 = player;
	}
	if(move == "9")
	{
		a3 = player;
	}
	if(move == "4")
	{
		b1 = player;
	}
	if(move == "5")
	{
		b2 = player;
	}
	if(move == "6")
	{
		b3 = player;
	}
	if(move == "1")
	{
		c1 = player;
	}
	if(move == "2")
	{
		c2 = player;
	}
	if(move == "3")
	{
		c3 = player;
	}
	
	//switch turns
    if(x_turn)
	{
        x_turn = false;
    }
    else
    {
        x_turn = true;
    }
}



You should use char instead of string if the outputs are only 1 character long
Was This Post Helpful? 1
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3569
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: How can I improve my Tic Tac Toe program?

Posted 28 August 2012 - 09:55 AM

Do you really need the Sleep() call on line 55? If not, just delete it and the corresponding incorrect inclusion of Windows.h.

I say incorrect, because you should use the angle brackets instead of the double quotes. The usual convention is that angle brackets are used to search for files that belong to the system or external library and live in another directory, while double quotes are for files that belong to you and usually are in the same directory as your program.
Was This Post Helpful? 0
  • +
  • -

#11 Welly  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 07-December 08

Re: How can I improve my Tic Tac Toe program?

Posted 28 August 2012 - 10:20 AM

Yes not related to your code but doing a tic-tac-toe program is fun and a great first learning experience.
Was This Post Helpful? 0
  • +
  • -

#12 lukeme99  Icon User is offline

  • D.I.C Head

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

Re: How can I improve my Tic Tac Toe program?

Posted 29 August 2012 - 02:35 AM

View Postrethc, on 28 August 2012 - 01:00 PM, said:

I would do something like this:

while(1)
{
	if(x_turn)
    {
        player = "X";
    }
    else
    {
        player = "O";
    } 
        
    cout << endl << a1 << a2 << a3 << endl << b1 << b2 << b3 << endl << c1 << c2 << c3 << endl << endl;	
	cin >> move;

	if(move == "7")
	{
		a1 = player;
	}
	if(move == "8")
	{
		a2 = player;
	}
	if(move == "9")
	{
		a3 = player;
	}
	if(move == "4")
	{
		b1 = player;
	}
	if(move == "5")
	{
		b2 = player;
	}
	if(move == "6")
	{
		b3 = player;
	}
	if(move == "1")
	{
		c1 = player;
	}
	if(move == "2")
	{
		c2 = player;
	}
	if(move == "3")
	{
		c3 = player;
	}
	
	//switch turns
    if(x_turn)
	{
        x_turn = false;
    }
    else
    {
        x_turn = true;
    }
}



You should use char instead of string if the outputs are only 1 character long


I wont directly copy your code, but I'll check something like that out, thanks for the inspiration!
Was This Post Helpful? 0
  • +
  • -

#13 lukeme99  Icon User is offline

  • D.I.C Head

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

Re: How can I improve my Tic Tac Toe program?

Posted 29 August 2012 - 05:31 AM

Hey,
So I rebuilt my Tic-Tac-Toe program to use arrays, there is some unused code (ignore that).
As far as I can tell, the logic of my code is bang on, but the exe doesn't think so! For example on press "7" it fills the center "block", and that makes no sense... OH GOD! I just realised what I did!!! I thpugh of top left as [1][1], not [0][0]... Have you guys ever done that?

Sorry for the disturbance!

L99
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1