flash on 2011-9-5
package
{
import flash.display.Sprite;
public class Main extends Sprite
{
public static var instance:Main;
public var board:Board;
public var buttons:Array = [];
public var marker:Sprite = new Sprite();
public function Main()
{
this.x = this.y = 20
board = new Board(this);
buttons = [];
for (var y:int = 0; y < Board.HEIGHT; y++)
{
buttons[y] = [];
for (var x:int = 0; x < Board.WIDTH; x++)
{
var button:PushButtonEx = new PushButtonEx(this, board, this, x * (Board.SIZE + 1), y * (Board.SIZE + 1), board.board[y][x]);
button.width = Board.SIZE;
button.height = Board.SIZE;
button.score = board.board[y][x];
button.tileX = x;
button.tileY = y;
buttons[y][x] = button;
}
}
marker.graphics.lineStyle(2.0, 0xED1A3D);
marker.graphics.drawRect(0, 0, Board.SIZE, Board.SIZE);
marker.visible = false;
addChild(marker);
ScoreText.init(this, 330, 100);
}
public function draw():void
{
marker.visible = true;
marker.x = board.putPoint.x * (Board.SIZE + 1);
marker.y = board.putPoint.y * (Board.SIZE + 1);
}
}
}
import com.bit101.components.PushButton;
import flash.display.DisplayObjectContainer;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
class Board
{
public static const WIDTH:int = 8;
public static const HEIGHT:int = 8;
public static const SIZE:int = 30;
public var board:Array;
public var turn:int = Status.SENTE;
public var putPoint:Point = new Point(0, 0);
public var logs/*Log*/:Array = [];
public var turnCount:int = 0;
public var main:Main;
public function Board(main:Main)
{
this.main = main;
board = [];
for (var y:int = 0; y < HEIGHT; y++)
{
board[y] = [];
for (var x:int = 0; x < WIDTH; x++)
{
board[y][x] = int(Math.random() * 20 - 10);
}
}
}
public function put(x:int, y:int, score:int):Boolean
{
if (((turn == Status.SENTE && putPoint.y == y) ||
(turn == Status.GOTE && putPoint.x == x)) &&
board[y][x] != "*")
{
board[y][x] = "*";
Score.set(turn, Score.get(turn) + score);
turn = -turn;
turnCount++;
putPoint.x = x;
putPoint.y = y;
logs.push(new Log(score, putPoint.clone()));
return true;
}
return false;
}
public function undo():Boolean
{
if (turnCount == 0) return false;
turnCount--;
turn = -turn;
var log:Log = logs.pop();
putPoint = log.putPoint;
main.buttons[putPoint.y][putPoint.x].enabled = true;
main.buttons[putPoint.y][putPoint.x].label = log.score;
board[putPoint.y][putPoint.x] = log.score;
Score.set(turn, Score.get(turn) - log.score);
return true;
}
public function isGameOver():Boolean
{
var gameover:Boolean = true;
if (turn == Status.SENTE)
{
for (var tx:int = 0; tx < WIDTH; tx++)
{
if (board[putPoint.y][tx] != "*")
{
gameover = false;
break;
}
}
}
else
{
for (var ty:int = 0; ty < HEIGHT; ty++)
{
if (board[ty][putPoint.x] != "*")
{
gameover = false;
break;
}
}
}
return gameover;
}
}
class Log
{
public var score:int;
public var putPoint:Point;
public function Log(score:int, putPoint:Point)
{
this.score = score;
this.putPoint = putPoint;
}
}
class PushButtonEx extends PushButton
{
public var score:int;
public var tileX:int;
public var tileY:int;
public var main:Main;
public var board:Board;
public function PushButtonEx(main:Main, board:Board, parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, label:String="")
{
this.main = main;
this.board = board;
super(parent, xpos, ypos, label, onMouseClick);
}
public function onMouseClick(event:MouseEvent = null):void
{
if (board.put(tileX, tileY, score))
{
this.enabled = false;
this.label = "*";
main.draw();
ScoreText.draw();
if (board.isGameOver())
{
trace("ゲーム終了");
if (Score.get(Status.SENTE) - Score.get(Status.GOTE) > 0)
{
trace("先手の勝利!!");
}
else if (Score.get(Status.GOTE) - Score.get(Status.SENTE) > 0)
{
trace("後手の勝利");
}
else
{
trace("引き分け!!");
}
}
else if (board.turn == Status.GOTE)
{
var x:int = board.putPoint.x;
var maxScore:int = int.MIN_VALUE;
var best:Point = new Point();
for (var y:int = 0; y < Board.HEIGHT; y++)
{
if (board.put(x, y, board.board[y][x]))
{
var score:int = AI.minLevel(board, 3, int.MIN_VALUE);
if (maxScore < score)
{
maxScore = score;
best.x = x;
best.y = y;
}
board.undo();
}
}
main.buttons[best.y][best.x].onMouseClick();
}
}
}
}
class AI
{
public static function maxLevel(board:Board, depth:int, limit:int):int
{
if (board.isGameOver())
{
if (Score.get(Status.SENTE) - Score.get(Status.GOTE) > 0)
{
return 1000;
}
else
{
return -1000;
}
}
if (depth == 0)
{
return Score.get(Status.GOTE) - Score.get(Status.SENTE);
}
var x:int = board.putPoint.x;
var maxScore:int = int.MIN_VALUE;
for (var y:int = 0; y < Board.HEIGHT; y++)
{
if (board.board[y][x] != "*")
{
board.put(x, y, board.board[y][x]);
var score:int = minLevel(board, depth - 1, maxScore);
if (maxScore < score)
{
maxScore = score;
}
board.undo();
if (limit < maxScore) break;
}
}
return maxScore;
}
public static function minLevel(board:Board, depth:int, limit:int):int
{
if (board.isGameOver())
{
if (Score.get(Status.SENTE) - Score.get(Status.GOTE) > 0)
{
return -1000;
}
else
{
return 1000;
}
}
if (depth == 0)
{
return Score.get(Status.GOTE) - Score.get(Status.SENTE);
}
var y:int = board.putPoint.y;
var minScore:int = int.MAX_VALUE;
for (var x:int = 0; x < Board.WIDTH; x++)
{
if (board.board[y][x] != "*")
{
board.put(x, y, board.board[y][x]);
var score:int = maxLevel(board, depth - 1, minScore);
if (score < minScore)
{
minScore = score;
}
board.undo();
if (minScore < limit) break;
}
}
return minScore;
}
}
class Status
{
public static const SENTE:int = 1;
public static const GOTE:int = -1;
}
class Score
{
private static var data:Vector.<int> = new Vector.<int>(3);
public static function get(player:int):int
{
return data[player + 1];
}
public static function set(player:int, score:int):void
{
data[player + 1] = score;
}
}
class ScoreText
{
public static var tf:TextField;
public static function init(container:DisplayObjectContainer, x:Number, y:Number):void
{
container.addChild(tf = new TextField());
tf.defaultTextFormat = new TextFormat("_typeWriter", 20, 0x0, true);
tf.autoSize = "left";
tf.text = "0 - 0";
tf.x = x, tf.y = y;
}
public static function draw():void
{
tf.text = Score.get(Status.SENTE) + " - " + Score.get(Status.GOTE);
}
}