Tic-Tac-Toe (No AI)
A simple TicTacToe game with no AI implementation.
Select the starting symbol (X or O) and press "Start Game".
Note: Sorry for the spaghetti code. It's been more that 6 months since I've coded anything.
/**
* Copyright ram64 ( http://wonderfl.net/user/ram64 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/19cX
*/
package {
import com.bit101.components.PushButton;
import com.bit101.components.RadioButton;
import com.bit101.components.Label;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
/**
* A simple TicTacToe game with no AI.
* @author Alexxcz
*/
[SWF(width = 465, height = 465, frameRate = 30)]
public class TicTacToeGame extends Sprite {
private var _board:Board;
private var _grid:Array;
private var _turn:uint = 1;
private var _btnO:RadioButton;
private var _btnX:RadioButton;
private var _btnStart:PushButton;
private var _moves:int;
private var _label:Label;
public function TicTacToeGame() {
addBoard();
addInterfaceButtons();
}
private function addInterfaceButtons():void
{
_btnX = new RadioButton(this, _board.x + _board.width + 20, 20, "Start with X", true, setStartSymbol);
_btnO = new RadioButton(this, _btnX.x, _btnX.y + _btnX.height + 10, "Start with O", false, setStartSymbol);
_btnStart = new PushButton(this, _btnO.x, _btnO.y + _btnO.height + 10, "Start Game", startGame);
_label = new Label(this, _board.x, _board.y + _board.height + 5, "Game Stopped");
}
private function startGame(e:Event):void
{
createGrid();
turn = _btnX.selected ? 1 : 0;
_moves = 0;
_board.drawBoard();
_board.buttonMode = true;
_board.addEventListener(MouseEvent.CLICK, onCellClick);
_btnO.enabled = false;
_btnX.enabled = false;
}
private function setStartSymbol(e:Event):void
{
if (e.target == _btnX) _turn = 1;
if (e.target == _btnO) _turn = 0;
}
private function set turn(turn:uint):void
{
_turn = turn == 1 || turn == 0 ? turn : 1;
_label.text = turn == 1 ? "X turn" : "O turn";
}
private function get turn():uint { return _turn; }
private function addBoard():void
{
_board = new Board(this);
_board.x = _board.y = 20;
addChild(_board);
}
private function createGrid():void
{
_grid = [];
for(var i:uint = 0; i < 3; i++)
_grid[i] = new Array;
}
private function onCellClick(e:MouseEvent):void
{
var row:uint = e.target.mouseY < 50 ? 0 : ((e.target.mouseY < 100) && (e.target.mouseY >= 50) ? 1 : 2);
var col:uint = e.target.mouseX < 50 ? 0 : ((e.target.mouseX < 100) && (e.target.mouseX >= 50) ? 1 : 2);
if(_grid[row][col] != 0 && _grid[row][col] != 1)
{
if(turn == 0)
{
_grid[row][col] = 0;
_board.drawO(row, col);
}else if(turn == 1)
{
_grid[row][col] = 1;
_board.drawX(row, col);
}
_moves++;
checkWinner(row, col);
turn = turn == 0 ? 1 : 0;
}
}
private function checkWinner(row:uint, col:uint):void
{
if (checkRow(row)) _board.crossRow(row + 1);
else if (checkCol(col)) _board.crossCol(col + 1);
else if (checkDiagP()) _board.crossDiagP();
else if (checkDiagS()) _board.crossDiagS();
else if (_moves == 8) gameEnd();
}
private function checkCol(col:uint):Boolean
{
for (var i:int = 0; i < 3; i++)
if (_grid[i][col] != turn) return false;
return true;
}
private function checkRow(row:uint):Boolean
{
for (var i:int = 0; i < 3; i++)
if (_grid[row][i] != turn) return false;
return true;
}
private function checkDiagP():Boolean
{
for (var i:int = 0; i < 3; i++)
if (_grid[i][i] != turn) return false;
return true;
}
private function checkDiagS():Boolean
{
for (var i:int = 0; i < 3; i++)
if (_grid[2-i][i] != turn) return false;
return true;
}
public function gameEnd():void
{
_board.buttonMode = false;
_board.removeEventListener(MouseEvent.CLICK, onCellClick);
_btnO.enabled = true;
_btnX.enabled = true;
}
}
}
import flash.display.Sprite;
class Board extends Sprite {
private var _game:TicTacToeGame;
public function Board(game:TicTacToeGame) {
_game = game;
drawBoard();
}
public function drawBoard():void
{
graphics.clear();
graphics.lineStyle(4);
graphics.beginFill(0, .1);
for(var row:Number = 0; row < 3; row++)
for(var col:Number = 0; col < 3; col++)
graphics.drawRect(col * 50, row * 50, 50, 50);
graphics.endFill();
}
public function drawO(row:Number, col:Number):void
{
var x:Number = col * 50 + 25;
var y:Number = row * 50 + 25;
graphics.lineStyle(2, 0x4d8808);
graphics.drawCircle(x, y, 12);
}
public function drawX(row:Number, col:Number):void
{
var x:Number = col * 50 + 25;
var y:Number = row * 50 + 25;
graphics.lineStyle(2, 0xff0000);
graphics.moveTo(x - 10, y - 10);
graphics.lineTo(x + 10, y + 10);
graphics.moveTo(x - 10, y + 10);
graphics.lineTo(x + 10, y - 10);
}
public function crossCol(col:uint):void
{
graphics.lineStyle(20, 0, .8);
graphics.moveTo(col * 50 - 25, 25);
graphics.lineTo(col * 50 - 25, 125);
_game.gameEnd();
}
public function crossRow(row:uint):void
{
graphics.lineStyle(20, 0, .8);
graphics.moveTo(25, row * 50 - 25);
graphics.lineTo(125, row * 50 - 25);
_game.gameEnd();
}
public function crossDiagP():void
{
graphics.lineStyle(20, 0, .8);
graphics.moveTo(25, 25);
graphics.lineTo(125, 125);
_game.gameEnd();
}
public function crossDiagS():void
{
graphics.lineStyle(20, 0, .8);
graphics.moveTo(25, 125);
graphics.lineTo(125, 25);
_game.gameEnd();
}
}