Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / A "Noughts and Crosses" (Tic-tac-toe) game

Author
Message
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 18th Oct 2011 00:52 Edited at: 18th Oct 2011 00:55
Coded up a quick Noughts and Crosses game. I thought it might help to share the code. I put this together rather quickly, so it's not the best example, but it works well and I think it illustrates a few concepts.

The check for a win condition could use a better algorithm, and it really only needs to check the last player that made a play. But I'm ok with the brute force method of checking coulumns, then rows, then the two diagonals. I'm sure there is a simpler method, but when I tried to combine them in a single loop, the concept for anyone learning from the code seemed to get lost.

If you spot an error,, let me know.
(I might add drawing a line through the win)


The media (a simple .png file for the X and O's )is attached.
version 1.3


Your signature has been erased by a mod please reduce it to 600 x 120.

Attachments

Login to view attachments
Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 18th Oct 2011 05:24
Now just to make it multiplayer over LAN/internet

Nice little game there!

"Insert funny coding-related joke here"
nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 18th Oct 2011 11:05
Here's a sort of remix of the logic and structure. Just a template suggestion that might help you in terms of simplifying the code. Should (hopefully) be bug free.


You'll need to still add your gfx, gameplay etc.

zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 18th Oct 2011 12:26
Thanks for the advice non-zero. That does look much cleaner. I think I may have over-complicated things. I coded the game in about an 1 1/2 hours, but oddly when I got down to the check-win part a few things happened.
1. I wanted to finish and wrap it up.
2. I nearly started similar (definitely not as efficient) as this, and then said "No, No, Ill loop through the indexes..."
3. After I got into it, It was hard to throw away what I had.

I knew someone would have a better solution. And now looking at it, I wonder what I was thinking!!! lol

So I appreciate your suggestion. It's actually better than I would of come up with. I may have time to make some changes tomorrow.

Your signature has been erased by a mod please reduce it to 600 x 120.
nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 18th Oct 2011 17:10
No problem at all.

Quote: "
3. After I got into it, It was hard to throw away what I had.
"


I've been there too. It's never pleasant... *Shudders at traumatic childhood memory no. 168*

zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 19th Oct 2011 01:01
Even though I don't really need it for a 3x3 grid,, I think I'll combine your solution with the addition of...

-only checking win lines that the last play could have an effect on
-only checking the value based on which player's turn it was

While the efficiency is not needed here, and it would mainly go unnoticed to the player, I know it will bother me to leave it as is. There are also plenty of other areas to rewrite.

Your signature has been erased by a mod please reduce it to 600 x 120.
nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 19th Oct 2011 12:06
You must do whatever suits you best. Programming is about what method makes you most comfortable (so long as it's not the "we're on a deadline, let's just release the game buggy and then we can always add 300mb download patches later" method).

Agent
19
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 6th Nov 2011 00:35 Edited at: 6th Nov 2011 00:36
Lol. I'm compelled to post this link, even though it's of no help to anyone whatsoever I'm kind of proud of it lol.

Agent's Tic Tac Toe Challenge

Login to post a reply

Server time is: 2024-05-20 03:46:36
Your offset time is: 2024-05-20 03:46:36