forked from: 数独
数独
参考サイト:http://puzzle.gr.jp/show/Japanese
ライセンス:問題生成に上記サイトのアルゴリズムを利用しているので、GPLってことで。
// forked from termat's 数独
/*
* 数独
*
* 参考サイト:http://puzzle.gr.jp/show/Japanese
* ライセンス:問題生成に上記サイトのアルゴリズムを利用しているので、GPLってことで。
*
*/
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
[SWF(width = "430", height = "460", backgroundColor = "0xffffff", fps = "30")]
public class Practice39 extends Sprite{
private var panel:Panel;
private var g:Generator;
private var gene:Button;
private var ans:Button;
private var rs:Button;
private var numOfHint:int = 36; //ヒントの数
public function Practice39() {
panel = new Panel();
panel.x = 10; panel.y = 10;
addChild(panel);
g = new Generator(numOfHint);
panel.setHint(g.hint);
gene = new Button(60, 20, 8, "New Game");
gene.x = 210; gene.y = 430;
gene.addEventListener(MouseEvent.MOUSE_DOWN,newGame);
addChild(gene);
rs = new Button(60, 20, 8, "Restart");
rs.x = 280; rs.y = 430;
rs.addEventListener(MouseEvent.MOUSE_DOWN,restartGame);
addChild(rs);
ans = new Button(60, 20, 8, "Answer");
ans.x = 350; ans.y = 430;
ans.addEventListener(MouseEvent.MOUSE_DOWN,ansGame);
addChild(ans);
}
private function newGame(e:MouseEvent):void {
g.makeData(numOfHint);
panel.init();
panel.setHint(g.hint);
}
private function ansGame(e:MouseEvent):void {
panel.setAmswer(g.ans);
panel.checkBlock();
}
private function restartGame(e:MouseEvent):void {
panel.init();
panel.setHint(g.hint);
panel.checkBlock();
}
}
}
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Sprite;
import flash.filters.ColorMatrixFilter;
import flash.filters.GlowFilter;
class Panel extends MovieClip{
private var col:Vector.<Vector.<Block>>;
private var row:Vector.<Block>;
public function Panel() {
Block.panel = this;
col = new Vector.<Vector.<Block>>();
for (var i:int = 0; i < 9; i++) {
var py:int = i*Block.size+i*5;
var tmp:Vector.<Block> = new Vector.<Block>();
for (var j:int = 0; j < 9; j++) {
var px:int = j * Block.size + j * 5;
var b:Block = new Block();
b.x = px; b.y = py;
addChild(b);
tmp.push(b);
}
col[i] = tmp;
}
row = new Vector.<Block>();
graphics.lineStyle(2, 0xaaaaaa);
graphics.moveTo(0, Block.size * 3 + 12); graphics.lineTo(Block.size * 9 + 40, Block.size * 3 + 12);
graphics.moveTo(0, Block.size * 6 + 27); graphics.lineTo(Block.size * 9 + 40, Block.size * 6 + 27);
graphics.moveTo(Block.size * 3 + 12,0); graphics.lineTo(Block.size * 3 + 12,Block.size * 9 + 40);
graphics.moveTo(Block.size * 6 + 27,0); graphics.lineTo(Block.size * 6 + 27,Block.size * 9 + 40);
}
public function init():void {
for (var i:int = 0; i < 9; i++) {
for (var j:int = 0; j < 9; j++) {
col[i][j].setVal(0);
col[i][j].state = Block.DEFO;
}
}
for (i = 0; i < 9; i++) for (j = 0; j < 9; j++) col[i][j].update();
}
public function checkBlock():void {
for (var i:int = 0; i < 9; i++) for (var j:int = 0; j < 9; j++) {
if (col[i][j].state != Block.FIX)col[i][j].state = Block.DEFO;
}
for (i = 0; i < 9; i++)checkBlockState(col[i]);
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) row[j] = col[j][i];
checkBlockState(row);
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
row[0] = col[i*3][j*3]; row[1] = col[i*3][j*3+1]; row[2] = col[i*3][j*3+2];
row[3] = col[i*3+1][j*3]; row[4] = col[i*3+1][j*3+1]; row[5] = col[i*3+1][j*3+2];
row[6] = col[i*3+2][j*3]; row[7] = col[i*3+2][j*3+1]; row[8] = col[i*3+2][j*3+2];
checkBlockState(row);
}
}
for (i = 0; i < 9; i++) for (j = 0; j < 9; j++) col[i][j].update();
}
private function checkBlockState(c:Vector.<Block>):void {
var tmp:Array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var ck:int = 0;
for (var i:int = 0; i < 9; i++) {
tmp[c[i].val]++;
if (tmp[c[i].val] > 1) ck++;
}
if (ck == 0&&tmp[0]==0) {
for (i = 0; i < 9; i++) if (c[i].state != Block.FIX)c[i].state = Block.LINE;
}else {
for (i = 0; i < 9; i++) if (c[i].state != Block.FIX && c[i].val != 0 && tmp[c[i].val] > 1) c[i].state = Block.ERROR;
}
}
public function setHint(hint:Vector.<Vector.<int>>):void {
for (var i:int = 0; i < 9; i++) {
for (var j:int = 0; j < 9; j++) {
if (hint[i][j] != 0) {
col[i][j].setVal(hint[i][j]);
col[i][j].state = Block.FIX;
col[i][j].update();
}
}
}
}
public function setAmswer(ans:Vector.<Vector.<int>>):void {
for (var i:int = 0; i < 9; i++) {
for (var j:int = 0; j < 9; j++) {
if (col[i][j].state != Block.FIX) {
col[i][j].setVal(ans[i][j]);
col[i][j].update();
}
}
}
}
}
class Block extends MovieClip {
public static var panel:Panel;
public static var size:int=40;
public static const DEFO:int = 0;
public static const LINE:int = 1;
public static const ERROR:int = 2;
public static const FIX:int = 3;
private var color:uint=0x0000ff;
public var val:int = 0;
private var text:TextField;
public var state:int = DEFO;
private var tf:TextFormat;
private static var matrix:Matrix = createMat();
private static var COLORS:Array = [ [0xffae28, 0xf39800],[0x09bbff, 0x009AD6],
[0xfacfde, 0xF6ADC6], [0x5993d2, 0x3170b9]];
public function Block():void {
addEventListener(MouseEvent.CLICK, mouseClick);
tf = new TextFormat();
tf.size = 36; tf.bold = true;
text = new TextField();
text.defaultTextFormat = tf;
text.selectable = false;
text.x = 10;
addChild(text);
update();
}
public function setVal(v:int):void {
val = v;
if (val == 0) {
text.text = "";
}else {
text.text = val.toString();
}
}
private static function createMat():Matrix {
var matrix:Matrix = new Matrix();
matrix.createGradientBox(size, size, 45 * Math.PI / 180);
return matrix;
}
public function update():void {
graphics.clear();
graphics.beginGradientFill("linear", [COLORS[state][0], COLORS[state][1]], [1.0, 1.0], [0, 255], matrix);
graphics.drawRoundRect(0, 0, size, size, size / 4, size / 4);
graphics.endFill();
}
private function mouseClick(e:MouseEvent):void {
if (state == FIX) return;
val = (val + 1) % 10;
if (val == 0) {
text.text = "";
}else {
text.text = val.toString();
}
panel.checkBlock();
}
}
class Generator {
public var ans:Vector.<Vector.<int>>;
public var hint:Vector.<Vector.<int>>;
public function Generator(h:int):void {
makeData(h);
}
public function makeData(h:int):void {
init();
hint = createVector();
var count:int = 0;
while (count<h) {
var px:int = Math.floor(9 * Math.random()) as int;
var py:int = Math.floor(9 * Math.random()) as int;
if (hint[px][py] != 0) continue;
hint[px][py] = ans[px][py];
count++;
}
}
private function createVector():Vector.<Vector.<int>> {
var ret:Vector.<Vector.<int>> = new Vector.<Vector.<int>>();
for (var i:int = 0; i < 9; i++) {
var tmp:Vector.<int> = new Vector.<int>();
for (var j:int = 0; j < 9; j++) {
tmp[j] = 0;
}
ret[i] = tmp;
}
return ret;
}
private function init():void {
ans = createVector();
for (var k:int = 1; k < 10; k++) {
for (var i:int = 0; i < 3; i++) {
for (var j:int = 0; j < 3; j++) {
var ix:int = 3 * j + (i + k) % 3;
var iy:int = 3 * i + (j + k + (k / 3)) % 3;
ans[ix][iy] = k;
}
}
}
make();
}
private function make():void {
var ch:Vector.<Boolean> = new Vector.<Boolean>();
for (var i:int = 0; i < 9; i++) {
for (var j:int = 0; j < 9; j++) {
var r1:int = i;
var n1:int = ans[r1][j];
var a:int = Math.floor(Math.random() * 3.0) as int;
var r2:int = 3 * Math.floor(r1 / 3) + a;
var n2:int = ans[r2][j];
if (n1 != n2) {
for (var k:int = 0; k < 9; k++)ch[k] = false;
ch[j] = true;
while (n1 != n2) {
k = 0;
while(ans[r1][k]!= n2)k++;
ch[k] = true;
n2 = ans[r2][k];
}
for (k = 0; k < 9; k++) {
if (ch[k] == true) {
var wk:int = ans[r1][k];
ans[r1][k] = ans[r2][k];
ans[r2][k] = wk;
}
}
}
}
}
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
var c1:int = j;
n1 = ans[i][c1];
a = Math.floor(Math.random() * 3.0) as int;
var c2:int = 3 * Math.floor(c1 / 3) + a;
n2 = ans[i][c2];
if (n1 != n2) {
for (k = 0; k < 9; k++)ch[k] = false;
ch[i] = true;
while (n1 != n2) {
k = 0;
while (ans[k][c1] != n2)k++;
ch[k] = true;
n2 = ans[k][c2];
}
for (k = 0; k < 9; k++) {
if (ch[k] == true) {
wk = ans[k][c1];
ans[k][c1] = ans[k][c2];
ans[k][c2] = wk;
}
}
}
}
}
}
public function check():Boolean {
for (var i:int = 0; i < 9; i++) {
var s0:int = 0;
var s1:int = 0;
for (var j:int = 0; j < 9; j++) {
s0 += ans[i][j];
s1 += ans[j][i];
}
if (s0 != 45 || s1 != 45) return false;
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
var s:int = 0;
s += ans[i * 3][j * 3]; s += ans[i * 3][j * 3 + 1]; s += ans[i * 3][j * 3 + 2];
s += ans[i * 3 + 1][j * 3]; s += ans[i * 3 + 1][j * 3 + 1]; s += ans[i * 3 + 1][j * 3 + 2];
s += ans[i * 3 + 2][j * 3]; s += ans[i * 3 + 2][j * 3 + 1]; s += ans[i * 3 + 2][j * 3 + 2];
if (s != 45) return false;
}
}
return true;
}
}
class Button extends Sprite{
private static const mono:ColorMatrixFilter = new ColorMatrixFilter([
1 / 3, 1 / 3, 1 / 3, 0, 10, 1 / 3, 1 / 3, 1 / 3, 0, 10,
1 / 3, 1 / 3, 1 / 3, 0, 10, 0,0,0, 1, 0]);
private var _hover:Boolean = false;
public function get hover():Boolean{
return _hover;
}
public function set hover(value:Boolean):void{
if(_hover != value){
_hover = value;
filters = (_hover ? null : [mono]);
}
}
public function Button(W:Number, H:Number, R:Number, label:String = "", size:int = 11){
var matrix:Matrix = new Matrix();
matrix.createGradientBox(W, H, Math.PI / 2);
var bg:Sprite = new Sprite();
bg.graphics.beginGradientFill("linear", [0xDDE9F4, 0xD5E4F1, 0xBAD2E8], [1, 1, 1],[0, 120, 136], matrix);
bg.graphics.drawRoundRect(0, 0, W, H, R, R);
bg.graphics.endFill();
bg.filters = [new GlowFilter(0xFFFFBE, .5, 10, 10, 2, 1, true)];
addChild(bg);
var line:Sprite = new Sprite();
line.graphics.lineStyle(3, 0xBAD2E8);
line.graphics.drawRoundRect(0, 0, W, H, R, R);
addChild(line);
filters = [mono];
buttonMode = true;
mouseChildren = false;
if (label != ""){
var textField:TextField = new TextField();
textField.selectable = false;
textField.autoSize = "left";
textField.htmlText = <font size={size} color="#6B8399">{label}</font>.toXMLString();
textField.x = (W - textField.width) / 2;
textField.y = (H - textField.height) / 2;
addChild(textField);
}
addEventListener("rollOver", buttonRollOver);
addEventListener("rollOut", buttonRollOut);
addEventListener("removed", function(event:Event):void{
removeEventListener("rollOver", buttonRollOver);
removeEventListener("rollOut", buttonRollOut);
removeEventListener("removed", arguments.callee);
});
}
protected function buttonRollOver(event:Event):void{
hover = true;
}
protected function buttonRollOut(event:Event):void{
hover = false;
}
}