![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
[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! |
|
|
|
|
|
#2 | |
|
Professional Programmer
|
everything compiled fine, asked me "Would you like to play? <y/n>:" then i entered y, pressed enter.. the console closes.
__________________
▄▄▄▄ Quote:
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! ▄▄▄▄ |
|
|
|
|
|
|
#3 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Quote:
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! |
|
|
|
|
|
|
#4 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Ok, everything should be working now. Sorry about that
![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#5 | |
|
Professional Programmer
|
same problem. Any special additions for the compilation we have to do? Linkers or anything?
__________________
▄▄▄▄ Quote:
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! ▄▄▄▄ |
|
|
|
|
|
|
#6 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Sep 2005
Posts: 28
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#8 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|