FlexPDX

A Portland, OR. Adobe Flex User Group

Module Madness Challenge #1

Are you any good at Tic-Tac-Toe? Is there really any way win? Does it depend on who goes first, a winning strategy or, is it just random chance?

Take the mathematical representation of a Tic-Tac-Toe board:

11 12 13
21 22 23
31 32 33

where 11 represents the top left "spot", 33 represents the lower right "spot", etc.

Here's your challenge! Given two arrays, "ownedByX" and "ownedByO" write an ActionScript function the defines "your turn". The function should return a single integer value that represents the "spot" you would like to occupy. The function will be called over and over until you either win, lose, or tie.

Here are some gotchas to remember:
1. You won't know if you're the X or the O until the function is called, and even then, the only information you are given is the spots "ownedByX" and "ownedByO".
2. If you try to take a spot that's already taken, you lose.
3. If your code throws an exception, you lose.

* DON'T POST YOUR CODE * Instead, bring it with you to the user group meeting. If you won't be bringing a laptop, and don't have any other media to bring it on, feel free to send it to me directly and I'll make sure to bring it for you.

Confused?

Here's a example "Player" that randomly picks from the unowned spots (and hopes for the best):

package {
import com.sc8pe.examples.modulemadness.sdk.tictactoe.BoardState;
import com.sc8pe.examples.modulemadness.sdk.tictactoe.Player;
import mx.collections.ArrayCollection;

public class RandomTicTacToePlayer extends Player {

public function RandomTicTacToePlayer() {
super();
} // constuctor

override public function onMyTurn(currentBoardState:BoardState):int {
// Create an ArrayCollection of all the possible spots
var notOwned:ArrayCollection = new ArrayCollection([11,12,13,
21,22,23,
31,32,33]);
// Exclude the spots already owned by x
for each (var xSpot:int in currentBoardState.ownedByX) {
notOwned.removeItemAt(notOwned.getItemIndex(xSpot));
} // for
// Exclude the spots already owned by o
for each (var oSpot:int in currentBoardState.ownedByO) {
notOwned.removeItemAt(notOwned.getItemIndex(oSpot));
} // for
// Pick a random spot from the remaining spots
return notOwned[Math.round(Math.random()*(notOwned.length-1))];
} // onMyTurn
} // class
} // package

Still confused? Ping me with questions, and I'll give more in-depth instructions.

* EXTRA CREDIT!!! * Bring (or send to me) a full length digital image of yourself (or your favorite persona) that's cropped to 200x425 :)

Share 

1 Comment

Matt Garland Comment by Matt Garland on January 24, 2009 at 7:43pm
Is this like Karate Kid, where the exercises are suprisingly applicable to Karate/modules?

I'm so tempted to google the most efficient tic-tac-toe algorithm...but I'll resist temptation. I'm stretching back to 3rd grade...let's see...take the center...take a corner, then react...if I remember nearly 35 years back.

Add a Comment

You need to be a member of FlexPDX to add comments!

Join this social network

About

Integranium Integranium created this social network on Ning.

Create your own social network!

Badge

Loading…

© 2009   Created by Integranium on Ning.   Create Your Own Social Network

Badges  |  Report an Issue  |  Privacy  |  Terms of Service