Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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 1875 times)
0 Members and 1 Guest are viewing this topic.
Offline Serethos

Junior Member




Java games rock!


« Posted 2005-06-05 21: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?

Offline arne

Senior Member




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


« Reply #1 - Posted 2005-06-05 21: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 ::
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #2 - Posted 2005-06-06 08: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. Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline IR0NMAN

Innocent Bystander





« Reply #3 - Posted 2005-06-19 22:52:30 »

If you are still interested in a tictactoe logic, email me at kimphamg@gmail.com

I wrote a simple and efficient way of checking a tic tac toe grid of any length (it can be 3x3 or 100x100)
Offline Serethos

Junior Member




Java games rock!


« Reply #4 - Posted 2005-06-29 22: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.

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
Offline Serethos

Junior Member




Java games rock!


« Reply #5 - Posted 2005-06-29 22: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 #6 - Posted 2005-06-29 23: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 #7 - Posted 2005-06-30 21: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.
Offline Serethos

Junior Member




Java games rock!


« Reply #8 - Posted 2005-07-04 22: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 #9 - Posted 2005-07-22 17: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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Serethos

Junior Member




Java games rock!


« Reply #10 - Posted 2005-07-22 23: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(?!)

Offline rdcarvallo

Senior Member


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #11 - Posted 2005-07-25 18: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

  Rafael.-
Offline Serethos

Junior Member




Java games rock!


« Reply #12 - Posted 2005-07-25 20: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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (28 views)
2014-04-15 18:08:23

BurntPizza (25 views)
2014-04-15 03:46:01

UprightPath (40 views)
2014-04-14 17:39:50

UprightPath (22 views)
2014-04-14 17:35:47

Porlus (38 views)
2014-04-14 15:48:38

tom_mai78101 (62 views)
2014-04-10 04:04:31

BurntPizza (121 views)
2014-04-08 23:06:04

tom_mai78101 (221 views)
2014-04-05 13:34:39

trollwarrior1 (188 views)
2014-04-04 12:06:45

CJLetsGame (195 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
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!