Hi !
Featured games (87)
games approved by the League of Dukes
Games in Showcase (671)
Games in Android Showcase (194)
games submitted by our members
Games in WIP (727)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Some TicTacToe issues (winning condition)  (Read 2567 times)
0 Members and 1 Guest are viewing this topic.
Offline Serethos

Junior Devvie

Java games rock!

« Posted 2005-06-05 19:32:15 »

a friend of mine came to me, because he is practicing java in writing his own tictactoe applet. he asked me a thing and im ashamed i have
no good solution.  its about the condition if the game has ended with a win. the naive way would be to test all three rows and columns plus
the two diagonals if there are equal either to a cross or a circle. now he is searching for a more elegant way. so i looked at the example from
sun where they code the winning states in a sequence of bits. though i got behind the idea i find it a little too overloaded, espacially for my
so does anyone know a more elegant way which is not too abstract?

Offline arne

Senior Devvie

money is the worst drug- we should not let it rule

« Reply #1 - Posted 2005-06-05 19:41:10 »

I would use the naive way, because it's simple and perfomance isn't an issue here. Every other way would be far too complicated, because then you'll loose the aspect: the learning of java and not the invention of some crappy algorithm.


:: JOODE :: Xith3d :: OdeJava ::
Offline K.I.L.E.R

Senior Devvie

Java games rock!

« Reply #2 - Posted 2005-06-06 06:08:02 »

Use matrices to do it.
Again as the poster above said, it's "difficult". You are better off sticking with straight logic.

It's too bad I can't find my old projects.
I made a O&X game and used a very basic algorithm (nested for loop) and it with only a few lines of logic it picked up the winner correctly under all circumstances and with whatever rule you used.

The rule is:
3 in a row/column/diagnal.

I changed it to many different values and they all worked perfectly.

Is there a name for a "redneck" programmer?

Unemployed. Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Serethos

Junior Devvie

Java games rock!

« Reply #3 - Posted 2005-06-29 20:22:35 »

after having some time of thinking about the problem i got a solution (a good one?), which is fine for me
but definitly no easy to understand for a beginner.

for who is interesed:

both players store their taken field in an array of boolean values (so nine for each).
e.g.:                 100010001
would express that this player won by having a winning diagonal (0, 4, Cool

the preparation is that i store all eight winning field conditions in this format and compare
them to actual combination, there fore i need two steps for each possible winning combination.


first compared winning combination:   111 000 000
player1's taken fields                           :   110 010 000

AND each value at same index at
compare to the winning combo         :   110 000 000    != 111 000 000

this way i also organize my "ai". an opponent wants to take his win or to block the enemy or else
to take the best field open.
the interesting part is to take the winning/blocking pos:


first compared winning combination:   111 000 111
player1's taken fields                           :   110 010 000

AND them to eliminate all positions
which hold no information                 :    110 000 000

XOR the result to the original winning
combo to get the desired field index :  001 000 000

and if the result only offers one true bit position its a blocking or a
winning field, else its useless and the comp should take the field of
highest priority
Offline Serethos

Junior Devvie

Java games rock!

« Reply #4 - Posted 2005-06-29 20:50:21 »

nearly forgot one thing: the fastest way of checking for a winner would have been to take a canonical sum/product
formula ... but ... not good to read i think ...  Grin
Offline kaffiene
« Reply #5 - Posted 2005-06-29 21:59:40 »

Shesh!  If you can't see that as a case of needless optimisation, I dispair for you Shocked)

Why over complicate things?  A couple of simple loops is hardly a big deal.
Offline bitsplitter

Junior Newbie

« Reply #6 - Posted 2005-06-30 19:11:44 »

My advice, each state of the beard in the game is 9 digits from 0 to 2, that easily fits in a long integer type.
if 0 is blank, 1 is x, and 2 is o, then

220 is

_  X _
O O _

check the digits for a win.

If you think about it, the check is so simple, complicating the code for a "more effective" method will result in code so bloated, it will be worthlessly slow.

So, to keep it short, KISS principle applies, don't fix what isn't broken.

Oh, and by the way, if you keep the state in just one location it's easier to represent game state, as you don't ahve to combine each player's active points on the board to come up with the total board state.
Offline Serethos

Junior Devvie

Java games rock!

« Reply #7 - Posted 2005-07-04 20:00:31 »

hmmmmmm, i really want to avoid this stupid brute force system. so in my opinion my
system isnt beautiful but the most attractive with a touch of ai.
at the beginning i used one field for both players too. but that didnt allow to use boolean
expressions ..

i know that i have taken a bigger distance to a simple to understand solution, but this
loop through all solutions thing ... brrrr ...
Offline ZaBlanc

Senior Newbie

« Reply #8 - Posted 2005-07-22 15:58:49 »

I agree...pointless, unless you are doing a (smirk) tic-tac-toe AI bot that needs to test for the best move. :-)  Then, checking for a win needs to be, uh, faster.

How about this?

Number the squares from the top left, row by row, ro the bottom right: 1 2 4 8 16 32 64 128 256

1  2  4
8  16  32
64  128  256

Then, create a Hashtable one time, at startup:

(BeanShell, ha!)

import java.util.*;

Hashtable winCombos = new Hashtable();

winCombos(["7"] = true;
winCombos["56"] = true;
winCombos["488"] = true;
winCombos["72"] = true;
winCombos["146"] = true;
winCombos["292"] = true;
winCombos["273"] = true;
winCombos["84"] = true;

Then, to test...simply add the squares with your mark in it and then:

if (winCombos[sumValue] == true) {
   YOU WIN!!!!

Sufficiently nasty enough? HAHAHA...that was amusing.
Offline Serethos

Junior Devvie

Java games rock!

« Reply #9 - Posted 2005-07-22 21:54:38 »

first of all, yes, a nasty all-winning (or not losing) ai-opponent was needed.
but the idea is very nice. searched for something like that, but its too late now.

did i miss something or is your use of the class hastable some kind of pseudocode(?!)

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline rdcarvallo

Senior Devvie

Projects: 5
Exp: 15 years

2D Java games forever!

« Reply #10 - Posted 2005-07-25 16:48:44 »

did i miss something or is your use of the class hastable some kind of pseudocode(?!)

I guess that  is Beanshell  sintaxis...

(BeanShell, ha!)

Very 'PERLish' to me  Smiley

Offline Serethos

Junior Devvie

Java games rock!

« Reply #11 - Posted 2005-07-25 18:02:17 »

ahh, ok .. never saw/used/contacted beanshell ...
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

IanParcs (39 views)
2016-04-18 14:18:53

KaiHH (38 views)
2016-04-18 08:35:41

KaiHH (69 views)
2016-04-15 12:43:58

theagentd (71 views)
2016-04-14 02:16:17

theagentd (83 views)
2016-04-14 02:15:43

IanParcs (96 views)
2016-04-12 03:51:16

IanParcs (39 views)
2016-04-12 03:50:03

IanParcs (35 views)
2016-04-12 03:49:54

IanParcs (32 views)
2016-04-12 03:49:52

IanParcs (40 views)
2016-04-12 03:49:52
Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17

List of Learning Resources
by SilverTiger
2016-02-05 09:39:47

List of Learning Resources
by SilverTiger
2016-02-05 09:38:38

List of Learning Resources
by SilverTiger
2016-02-05 09:35:50

Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!