Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (777) Games in Android Showcase (231) games submitted by our members Games in WIP (856) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Some TicTacToe issues (winning condition)  (Read 3659 times) 0 Members and 1 Guest are viewing this topic.
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
friend.
so does anyone know a more elegant way which is not too abstract?

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.

Arne

:: JOODE :: Xith3d :: OdeJava ::
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.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed.
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,

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.

eg.:

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:

e.g.

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
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 ...
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 )

Why over complicate things?  A couple of simple loops is hardly a big deal.
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

010
211
220 is

_  X _
O X 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.
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 ...
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.

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.
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.

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

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

Rafael.-
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

 hadezbladez (349 views) 2018-11-16 13:46:03 hadezbladez (180 views) 2018-11-16 13:41:33 hadezbladez (347 views) 2018-11-16 13:35:35 hadezbladez (84 views) 2018-11-16 13:32:03 EgonOlsen (2177 views) 2018-06-10 19:43:48 EgonOlsen (2206 views) 2018-06-10 19:43:44 EgonOlsen (1378 views) 2018-06-10 19:43:20 DesertCoockie (2011 views) 2018-05-13 18:23:11 nelsongames (1655 views) 2018-04-24 18:15:36 nelsongames (2300 views) 2018-04-24 18:14:32
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 java-gaming.org 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‑gaming.org