0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

対戦オセロwebapi その3

Posted at

#概要

対戦オセロwebapiを作ってみる。
人対ai、作ってみた。
ライブラリーは、jquery,tatenoを使う。

#写真

image.png

#サンプルコード


window.onload = function() {
	var coma = 'https://camo.qiitausercontent.com/7873b55118f9bac84a20de35c02770aac1d1f02a/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f31383130342f33656230653537342d643430362d326537342d663238312d3561663132396130353834362e706e67';
	var pl0 = 'anta';
	var pl1 = 'ore';
	var ban = [];
	var flg = 1;
	var game = new Game();
	game.fps(5);
	game.preload([coma]);
	game.start();
	var aLabel = new Label('白 : ' + pl0 + '<br>黒: ' + pl1, 50, 330);
	aLabel.size(20);
	ban = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 1, 1, 1,
		1, 1, 1, 2, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
	var field = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
	var suji = [0, 7, 56, 63, 18, 21, 42, 45, 2, 16, 5, 23, 40, 58, 47, 61, 3, 4, 11, 12, 19, 20, 24, 25, 26, 32, 33, 34, 29, 30, 31, 37,
		38, 39, 43, 44, 51, 52, 59, 60, 1, 8, 9, 10, 17, 6, 13, 14, 15, 22, 41, 48, 49, 50, 57, 46, 53, 54, 55, 62];
	function check(put, d) {
		var x = put % 8;
		var y = Math.floor(put / 8);
		var res = 0;
		if (x == 0 && (d == -9 || d == -1 || d ==  7)) res = 1;
		if (x == 7 && (d == -7 || d ==  1 || d ==  9)) res = 1;
		if (y == 0 && (d == -9 || d == -8 || d == -7)) res = 1;
		if (y == 7 && (d ==  7 || d ==  8 || d ==  9)) res = 1;
		return res;
	}
	function sasu(ban) {
		var res;
		var all = 0;
		var iro = 2;
		var turn = 0;
		var dir = Array(-9, 9, -7, 7, -1, 1, -8, 8);
		var i;
		var j;
		var put;
		var count;
		var put1;
		var f1;
		for (j = 0; j < 60; j++)
		{
			put = suji[j];
			if (ban[put] == 1)
			{
				for (i = 0; i < 8; i++)
				{
					count = 0;
					if (check(put, dir[i]) == 0)
					{
						put1 = put + dir[i];
						f1 = 0;
						do
						{
							if (ban[put1] == iro)
							{
					  			count++;
								if (check(put1, dir[i]) == 0)
								{
									put1 += dir[i];
								}
								else
								{
									f1 = 1;
								}
							}
							else
							{
								f1 = 1;
							}
						} while (f1 == 0);
					}
					if ((count > 0) && (ban[put1] == turn))
					{
						all += count;
					}
				}
			}
			if (all > 0)
			{
				res = put;
			 	break;
			}
		}
		return res;
	}
	function oku(put, iro) {
		var res = 0;
		var turn = 0;
		if (iro == 0) turn = 2;
		var dir = new Array(-9, -8, -7, -1, 1, 7, 8, 9);
		var tugi;
		var i;
		var count;
		if (ban[put] == 1)
		{
			for (i = 0; i < 8; i++)
			{
				count = 0;
				tugi = put;
				do
				{
					if (check(tugi, dir[i]) == 1) break;
					count++;
					tugi += dir[i];
				}
				while (ban[tugi] == turn);
				if ((count > 1) && (ban[tugi] == iro))
				{
					res = -1;
					tugi = put;
	   				do
					{
	   					ban[tugi] = iro;
						tugi += dir[i];
					}
	   				while (ban[tugi] == turn);
				}
			}
		}
		return res;
	}
	function test(n) {
		var put;
		var res;
		if (flg > 0)
		{
			put = n;
			res = oku(put, 2);
			if (res == -1)
			{
				put = sasu(ban);
				res = oku(put, 0);
				var end = -1;
				for (var i = 0; i < 64; i++)
				{
					field[i].frame(ban[i]);
					if (ban[i] == 1) end = 0;
				}
				if (end == -1) alert("game over");
			}
		}
	}
	for (var i = 0; i < 64; i++)
	{
		field[i] = new Sprite(40, 40, coma, 4, 1);
		field[i].frame(ban[i]);
		field[i].x((i % 8) * 40);
		field[i].y(Math.floor(i / 8) * 40);
		field[i].jq.click(function() {
			var n = this.id;
			n = n.replace("tsGameNode", "");
			n = n - 2;
			test(n);
		});
	}
};



#成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?