x Tic Toc Remake v1.00 Beta in Flash AS3.0


Hi, this is a remake of classic game Tic Toc or Tic Tac Toc , I don't know the original developer/publishers… This is an example with source code of the game Tic Toc or Tic Toe ,with simple artificial intelligence ( AI ). So this is one of my attempts to make a game from scratch, I completed it in 1 day (2009-04-11). So it's been more than a year and I have forgotten about it .( I did not comment in my code.) The Artificial Intelligence of the computer play is so simple logically. He watches any possible win by the player and try to suppress Player Win. He does not try to win a game. I left it to be completed in future ( More than one year and still waiting :-p ); The game gives very simple interface to user !! I Just wanted to share it for beginners. I hope you like . Please comment and share your views..Take care. Flash version is Flash CS4 , Flashplayer 10 , Action Script 3.0. There are only four things displayed in game.


pButton , it's the buttons that user click in game. screenButton. Its two instance on stage , signCells: 3 frames total, 1 frame is blank, and frame 2 and 3 contains two signs that is cross and circle. msg:TextArea // its empty dynamic text on frame 1,to show results at game end. There are two frame on main FLA file. At frame one,there are two instances of screenButtons(play1 , play2 ),and in actions panel:


var playMode:String;
play1.addEventListener(MouseEvent.CLICK,playSelection);
play2.addEventListener(MouseEvent.CLICK,playSelection);
stop();
function playSelection(event:MouseEvent):void {
if (event.currentTarget == play1)
{
playMode = "computer";
}
else
{
playMode = "human";
}
play1.removeEventListener(MouseEvent.CLICK,playSelection);
play2.removeEventListener(MouseEvent.CLICK,playSelection);
gotoAndPlay(2);
}
At frame 2:
TicTocClass_start();
stop();
Document Class.
package
{
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.net.*;
import flash.ui.*;
import flash.utils.*;
import fl.transitions.*;
import fl.transitions.easing.*;
public class TicTocClass extends MovieClip
{
var boardArray:Array;
var gameButtons:Array;
var offsetX:int = 160;
var offsetY:int = 100;
var curPlayer:String;
var curSign:int;
var winner:String;
var movesCount:int;
public function TicTocClass_start()
{
gameButtons=new Array();
movesCount = 0;
for (var i:int=0; i<3; i++)
{
for (var j:int=0; j<3; j++)
{
var sCell:signCells=new signCells();
var cBtn:pButton=new pButton();
cBtn.name = "b_" + i + "_" + j;
cBtn.i = i;
cBtn.j = j;
sCell.name = "s_" + i + "_" + j;
sCell.x=cBtn.x = offsetX+(i * (cBtn.width+5));
sCell.y=cBtn.y = offsetY+(j * (cBtn.height+5));
sCell.mouseEnabled = false;
sCell.gotoAndStop(1);
cBtn.n = gameButtons.length;
gameButtons.push(cBtn);
cBtn.addEventListener(MouseEvent.CLICK,pButtonClickEvent);
addChild(cBtn);
addChild(sCell);
}
}
boardArray = new Array([0,0,0],[0,0,0],[0,0,0]);
curPlayer = "you";
curSign = 2;
}//ends Constructor
public function pButtonClickEvent(event:MouseEvent)
{
if (playMode == "computer" && curPlayer == "computer")
{
return;
}
var mc:Object = event.currentTarget;
var smc:MovieClip = MovieClip(getChildByName("s_" + mc.i + "_" + mc.j));
smc.gotoAndStop(curSign);
event.currentTarget.removeEventListener(MouseEvent.CLICK,pButtonClickEvent);
event.currentTarget.enabled = false;
boardArray[mc.i][mc.j] = curSign;
movesCount++;
var gEnd:Boolean = checkGameEnd();
turnPlayerMove(gEnd);
}//ends pButtonClickEvent
public function checkGameEnd():Boolean
{
var end:Boolean = false;
var arr = boardArray;
for (var i:int=0; i<3; i++)
{
if (checkRows(boardArray,i,curSign))
{
end = true;
break;
}
else if (checkCols(boardArray,i,curSign))
{
end = true;
break;
}
else if (checkDiag(boardArray,curSign))
{
end = true;
break;
}
else if (movesCount==9)
{
end = true;
break;
}
}
if (end && movesCount < 9)
{
if (curSign == 2)
{
winner="You"
;
}
else
{
winner = "You (II)";
if (playMode == "computer")
{
winner = "Computer";
}
}
}
else
{
winner = "NO ONE ";
}
return end;
}//ends checkGameEnd
public function playAI()
{
//check Possible win of USER
var myArr:Array;
var threat:Boolean = false;
var pi;
var pj;
var pos:Array=new Array();
for (var i=0; i<3; i++)
{
for (var j=0; j<3; j++)
{
if (curPlayer != "computer")
{
return;
}
if (boardArray[i][j] == 0)
{
pos.push({xi:i,xj:j});
myArr=new Array();
myArr = copyArray(boardArray);
myArr[i][j] = 2;
if (checkPossibleEnd(myArr,2))
{
threat = true;
pi = i;
pj = j;
break;
}
}
}
}
if (threat)
{
markComputer(pi,pj);
}
else
{
// if mode is hard , Computer calculates to make a win;
var n=Math.floor(Math.random()*(pos.length));
markComputer(pos[n].xi,pos[n].xj);
}
//ends check possible win of USER
}//ends play AI
function markComputer(i,j)
{
var mc = getChildByName("b_" + i + "_" + j);
var smc = getChildByName("s_" + i + "_" + j);
smc.gotoAndStop(3);
mc.removeEventListener(MouseEvent.CLICK,pButtonClickEvent);
mc.enabled = false;
boardArray[i][j] = 3;
movesCount++;
var gEnd:Boolean = checkGameEnd();
turnPlayerMove(gEnd);
}//ends markComputer
private function checkRows(arr,row:int,sign):Boolean
{
var bol:Boolean = false;
if (arr[0][row] == arr[1][row] && arr[1][row] == arr[2][row] && arr[0][row] == sign)
{
bol = true;
}
return bol;
}//ends checkRows
private function checkCols(arr,col:int,sign):Boolean
{
var bol:Boolean = false;
if (arr[col][0] == arr[col][1] && arr[col][1] == arr[col][2] && arr[col][0] == sign)
{
bol = true;
}
return bol;
}//ends checkCols
private function checkDiag(arr,sign):Boolean
{
var bol:Boolean = false;
if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[0][0] == sign)
{
bol = true;
}
else if (arr[0][2]==arr[1][1]&&arr[1][1]==arr[2][0] && arr[0][2]==sign)
{
bol = true;
}
return bol;
}//ends checkDiag
function checkPossibleEnd(arr:Array,sign):Boolean
{
var end:Boolean = false;
for (var i:int=0; i<3; i++)
{
if (checkRows(arr,i,sign))
{
end = true;
break;
}
else if (checkCols(arr,i,sign))
{
end = true;
break;
}
else if (checkDiag(arr,sign))
{
end = true;
break;
}
else if (movesCount==9)
{
end = true;
break;
}
}
return end;
}//end checkPossibleEnd
function copyArray(arr:Array):Array
{
var res:Array=new Array();
for (var t=0; t<3; t++)
{
res[t] = arr[t].concat();
}
return res;
}//ends copy Arry
function turnPlayerMove(isEnd)
{
if (! isEnd)
{
if (playMode == "computer")
{
if (curPlayer == "you")
{
curPlayer = "computer";
curSign = 3;
playAI();
}
else
{
curPlayer = "you";
curSign = 2;
}
}
else
{
if (curPlayer == "you")
{
curPlayer = "you2";
curSign = 3;
}
else
{
curPlayer = "you";
curSign = 2;
}
}
}
else
{
for (var i:int=0; i<3; i++)
{
for (var j:int=0; j<3; j++)
{
removeChild(getChildByName("s_"+i+"_"+j));
removeChild(getChildByName("b_"+i+"_"+j));
}
}
gotoAndPlay(1);
msg.text=winner+" Win !";
}
}
}
}

Comments

Popular posts from this blog

Free x-SMS Flood v2.00/v2.02 and v3.00 Registration Key...

xSMS Flood v3.00 Touch For s60v5

Example For wxPython pureMVC Using Eclipse IDE with PyDev Plugin : Tryout