Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 18th, 2006, 8:48 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
[C++] Tic-Tic-Toe game (with html docs!)

Well, I would say this is my first "major" (relatively) project in programming.
It is about 600 lines (maybe more), and it is a tic-tac-toe game, complete with documentation! (Thanks to doxygen!)

I would still like to see if I can improve it, but I think most bugs have been squashed.

Any advice would be great, though I realise I haven't commented the definitions of methods (which is next on my list).

tarball: http://hakuch.tripod.com/jesse/ticta...0.1-src.tar.gz
docs: http://hakuch.tripod.com/jesse/tictactoe/doc

I can honestly say that I am proud of this accomplishment, though I would definitly be open to constructive criticism.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jan 18th, 2006, 8:56 PM   #2
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
everything compiled fine, asked me "Would you like to play? <y/n>:" then i entered y, pressed enter.. the console closes.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Jan 18th, 2006, 8:58 PM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by jayme
everything compiled fine, asked me "Would you like to play? <y/n>:" then i entered y, pressed enter.. the console closes.

I accidently posted it with an exception to test my code. I have reposted it, and it should be working now. (But please post if it doesn't! )

EDIT: Don't download! Error. be back up in about 5 minutes.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jan 18th, 2006, 9:26 PM   #4
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Ok, everything should be working now. Sorry about that
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jan 18th, 2006, 9:49 PM   #5
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
same problem. Any special additions for the compilation we have to do? Linkers or anything?
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Jan 18th, 2006, 9:57 PM   #6
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by jayme
same problem. Any special additions for the compilation we have to do? Linkers or anything?
Just booted into windows for something else. I downloaded the files, and compiled with g++, and everything workd fine.

g++ player.cpp board.cpp cboard.cpp main.cpp -o tictactoe

Are you sure something went wrong?

There is also a bug where if you select yes to play again, nothing will happen. I am aware of it, but am currently not able to do anything about it as I am on windows.

It will be fixed soon.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!

Last edited by Jessehk; Jan 18th, 2006 at 10:09 PM.
Jessehk is offline   Reply With Quote
Old Jan 19th, 2006, 4:50 AM   #7
Symptom
Newbie
 
Symptom's Avatar
 
Join Date: Sep 2005
Posts: 28
Rep Power: 0 Symptom is on a distinguished road
Nice work Jessehk.

My only comments is that you should take the user arguments as a set of ints ( both between 1 and 3) as it's easier to calculate where you want to play that way.

I had also a tic-tac-toe ready some time back. I never did the gui though (and the whole project is laid to waste now) and the reason was that I didn't like the program. As I can see you "brute force" the solution checking all available moves. That was what I had done as well but I thing it'll be much more elegant if you implement a nice algorithm (min-max for example) despite being a lot of work for no reason...

Nevertheless congrats.

P.S: 7-9-5 always wins

[edit]This is the main.cpp, a little changed, so that replayability works. I only moved the return inside the if/else statement and added a while(1) for the program loop. Also, when asked if I want to play/replay, if I enter a number or random char other than 'y' the program exits...


#include "board.h"
#include "cboard.h"
#include "player.h"
#include <cctype>

using namespace std;

int main() {
	srand(time(0));
	
	cout << "\nWelcome to TicTacToe!" << endl;
	cout << "---------------------" << endl;
	cout << "\n\tWould you like to play? <y/n>: ";
	char ch;
	cin >> ch;
	
	if(tolower(ch) != 'y')
		return 0;
	
	cout << "\n\nInstructions:" << endl;
	cout << "------------\n" << endl;
	cout << "Play against the computer in one of the most simple, yet enjoyable games.\n"
	     << "The computer will both prevent you from winning, and attempt to win itself.\n"
	     << "Place a move by first making sure numLock is off, and then using the numpad\n"
	     << "to represent the board.\n\n";

	while(1)
	{
	try
	{
		Board b1('o', 'x');
		CBoard b2('x', 'o');

		cout << "You are \'o\', the computer is \'x\'" << endl;
	
		bool done = false;
		
		b2.reset();
	
		b1.show();
		while(1)
		{
			while(!done)
			{
				int move;
				
				while(1)
				{	
					cout << "\n\nEnter move: ";
					cin >> move;
					if(!cin.good())
					{
						cin.clear();
						cin.ignore(numeric_limits<streamsize>::max(), '\n');
						cout << "Bad input. Try again. (Make sure Num Lock is not enabled)\n";
						continue;
					}
					
					if(!b1.makeMove(move)) //bad move
						cout << "bad move" << endl;
					else
						break;
				}
				
				b1.show();
				if(b1.checkWin())
				{
					cout << "o wins!" << endl;
					done = true;
					continue;
				}
				
				if(b1.checkCats())
				{
					cout << "cat's game" << endl;
					done = true;
					continue;
				}
				
				b2.makeMove();
				b2.show();
				if(b2.checkWin())
				{
					cout << "x wins!" << endl;
					done = true;
					continue;
				}
				
				if(b2.checkCats())
				{
					cout << "cat's game" << endl;
					done = true;
					continue;
				}
			}
			cout << "\nWould you like to play again? <y/n>: ";
			char ch;
			cin >> ch;
		
			if(tolower(ch) == 'y')
			{
				cout << endl<< "A new game begins!" << endl;
				break;
			}
			else
			{
				cout << "Thanks for playing!" << endl;				
				return 0;
			}			
		}
		//cout << "Thanks for playing!" << endl;
		//return 0;
	}
	
	catch(std::invalid_argument &e)
	{
		cout << e.what() << endl;
	}
	}
}
__________________
The geeks shall inherit the earth.

Last edited by Symptom; Jan 19th, 2006 at 5:05 AM.
Symptom is offline   Reply With Quote
Old Jan 19th, 2006, 6:24 PM   #8
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Thanks for the info Symptom. I don't know any algorithms, and information seems to be sparse. If I ever pursue a software engeneering or computer science degree, then I am sure I will learn more in that area.
As it stands, this was the first time I had used any "AI" type thing.


This is release 0.1.2, which is available here: http://hakuch.tripod.com/jesse/ticta...-0.1.2-src.zip
and the same address with the *tar.gz extension for a tarball.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:39 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC