Code:
package ;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.Lib;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.ui.Keyboard;
/**
* ...
* @author alijaya
*/
class Main
{
static public var PLAYERTURN:Int = 0;
static public var CPUTURN:Int = 1;
static public var size:Int = 50;
public var turn(get_turn, set_turn) : Int;
private var _turn : Int;
private function get_turn() : Int
{
return _turn;
}
private function set_turn(value : Int) : Int
{
_turn = value;
if (value == CPUTURN && !over)
{
cpuTurn();
}
return value;
}
var remain:Int;
var board:Array<Array<Keping>>;
var neighbors:Array < Array < Int >> ;
var over:Bool;
var cell:Class<Sprite>;
var profile:Profile;
public function new()
{
over = false;
remain = 64;
board = [];
for (j in 0...8)
{
board[j] = [];
for (i in 0...8)
{
board[j][i] = null;
}
}
neighbors = [[0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1]];
cell = Cell;
var p:Class<Sprite> = Player;
var c:Class<Sprite> = Cpu;
profile = { player:p, cpu:c };
drawBoard();
drawKeping(3, 3, 0);
drawKeping(4, 4, 0);
drawKeping(3, 4, 1);
drawKeping(4, 3, 1);
turn = PLAYERTURN;
Lib.current.stage.addEventListener(MouseEvent.CLICK, clickHandler);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
public function drawBoard()
{
for (j in 0...8)
{
for (i in 0...8)
{
var c:Sprite = Type.createInstance(cell, []);
c.x = i * size;
c.y = j * size;
Lib.current.addChild(c);
}
}
}
public function drawKeping(x:Int, y:Int, status:Int)
{
if (board[y][x] == null)
{
var k:Keping = Type.createInstance(Keping, [profile, status]);
k.x = x * size;
k.y = y * size;
Lib.current.addChild(k);
board[y][x] = k;
remain -= 1;
} else
{
board[y][x].status = status;
}
}
public function find(x:Int, y:Int, delta:Array<Int>, piece:Int) : Array<Array<Int>>
{
var result:Array<Array<Int>> = [[x,y]];
var dx:Int = x + delta[0];
var dy:Int = y + delta[1];
if (dx < 0 || dx >= 8 || dy < 0 || dy >= 8) return [];
if (board[dy][dx] == null)
{
return [];
} else if (board[dy][dx].status == piece)
{
return result;
} else
{
var tmp:Array<Array<Int>> = find(dx, dy, delta, piece);
if (tmp.length > 0) result = result.concat(tmp);
}
if (result.length > 1)
{
return result;
} else
{
return [];
}
}
public function score(x:Int, y:Int) : Array<Array<Int>>
{
var opposite:Int = (turn == 0)? 1 : 0;
var tmp:Array<Array<Int>> = [];
for (n in neighbors)
{
var dx:Int = x + n[0];
var dy:Int = y + n[1];
if (dx < 0 || dx >= 8 || dy < 0 || dy >= 8) continue;
if (board[dy][dx] == null) continue;
if (board[dy][dx].status == opposite)
{
var tmp2:Array<Array<Int>> = find(dx, dy, n, turn);
if(tmp2.length > 0) tmp = tmp.concat(tmp2);
}
}
return tmp;
}
public function pick(x:Int, y:Int) : Bool
{
var tmp:Array<Array<Int>> = score(x, y);
if (tmp.length == 0) return false;
for (p in tmp)
{
drawKeping(p[0], p[1], turn);
}
drawKeping(x, y, turn);
if(remain == 0) theEnd();
return true;
}
public function cpuTurn()
{
var count:Int = 0;
var px:Int = 0;
var py:Int = 0;
for (j in 0...8)
{
for (i in 0...8)
{
if (board[j][i] != null) continue;
var tmp:Array<Array<Int>> = score(i, j);
if (tmp.length > count)
{
count = tmp.length;
px = i;
py = j;
}
}
}
if (count > 0)
{
pick(px, py);
}
turn = PLAYERTURN;
}
public function theEnd()
{
over = true;
var n1:Int = 0;
var n2:Int = 0;
for (j in 0...8)
{
for (i in 0...8)
{
var c:Keping = board[j][i];
if (c == null) continue;
if (c.status == PLAYERTURN)
{
n1++;
} else if (c.status == CPUTURN)
{
n2++;
}
}
}
var tmp:Int = n1 - n2;
var msg:String = "";
if (tmp > 0)
{
msg = "Player Wins";
} else if (tmp < 0)
{
msg = "CPU Wins";
} else
{
msg = "DRAW";
}
var tf:TextField = new TextField();
var tft:TextFormat = new TextFormat();
tft.size = 75;
tft.color = 0xff0000;
tf.text = msg;
tf.setTextFormat(tft);
tf.autoSize = TextFieldAutoSize.LEFT;
tf.x = Lib.current.stage.stageWidth / 2 - tf.width / 2;
tf.y = Lib.current.stage.stageHeight / 2 - tf.height / 2;
Lib.current.addChild(tf);
}
public function clickHandler(e:MouseEvent)
{
if (turn != PLAYERTURN && !over) return;
var x:Int = Std.int(e.stageX / size);
var y:Int = Std.int(e.stageY / size);
if (board[y][x] != null) return;
if (x < 0 || x >= 8 || y < 0 || y >= 8) return; // lebih dari bound
if (pick(x, y)) turn = CPUTURN;
}
public function keyDownHandler(e:KeyboardEvent)
{
if (turn != PLAYERTURN && !over) return;
switch(e.keyCode)
{
case Keyboard.SPACE: turn = CPUTURN;
case Keyboard.ESCAPE: theEnd();
}
}
static function main()
{
var m:Main = new Main();
}
}
class Keping extends Sprite
{
var player:Sprite;
var cpu:Sprite;
var curSprite:Sprite;
public var status(get_status, set_status) : Int;
private var _status : Int;
private function get_status() : Int
{
return _status;
}
private function set_status(value : Int) : Int
{
_status = value;
if (curSprite != null) removeChild(curSprite);
curSprite = switch(_status)
{
case Main.PLAYERTURN: player;
case Main.CPUTURN: cpu;
}
addChild(curSprite);
return value;
}
public function new(profile:Profile, initStatus:Int)
{
super();
player = Type.createInstance(profile.player, []);
cpu = Type.createInstance(profile.cpu, []);
status = initStatus;
}
}
typedef Profile = { player:Class<Sprite>, cpu:Class<Sprite>}
class Cell extends Sprite
{
public function new()
{
super();
graphics.lineStyle(2, 0x663399);
graphics.beginFill(0x9966cc);
graphics.drawRect(0, 0, Main.size, Main.size);
}
}
class Player extends Sprite
{
public function new()
{
super();
graphics.lineStyle(1, 0xcccccc);
graphics.beginFill(0xffffff);
graphics.drawCircle(Main.size/2, Main.size/2, Main.size/2*0.9);
}
}
class Cpu extends Sprite
{
public function new()
{
super();
graphics.lineStyle(2, 0x333333);
graphics.beginFill(0x000000);
graphics.drawCircle(Main.size/2, Main.size/2, Main.size/2*0.9);
}
}