| 情報学部 | 菅沼ホーム | JavaScript 目次 | 索引 |

<!DOCTYPE HTML> <HTML> <HEAD> <TITLE>8パズル:ステップ1</TITLE> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> <META NAME=viewport CONTENT="width=device-width, initial-scale=1"> <LINK REL="stylesheet" TYPE="text/css" HREF="../../../master.css"> <SCRIPT TYPE="text/javascript" SRC="main/MainPanel.js"></SCRIPT> <SCRIPT TYPE="text/javascript" SRC="start/StartPanel.js"></SCRIPT> <SCRIPT TYPE="text/javascript" SRC="game/GamePanel.js"></SCRIPT> <SCRIPT TYPE="text/javascript" SRC="clear/GameClearPanel.js"></SCRIPT> </HEAD> <BODY CLASS="eeffee" onLoad="mp_start()"> <H1>8パズル:ステップ1</H1> <CANVAS ID="canvas_e" STYLE="background-color: #ffffff;" WIDTH="190" HEIGHT="190"></CANVAS><BR> <A HREF="method.htm" TARGET="method"><BUTTON ID="method" CLASS="std">遊び方</BUTTON></A> <BUTTON ID="start" CLASS="std" onClick="gp_start()">ゲーム開始</BUTTON> <BUTTON ID="first" CLASS="std" onClick="st_start()">最初から再開</BUTTON> <BUTTON ID="finish" CLASS="std" onClick="mp.finish()">ゲーム終了</BUTTON> </BODY> </HTML>
mp = null; // MainPanel オブジェクト
//
// MainPanel の開始
//
function mp_start()
{
// キャンバス情報
let canvas = document.getElementById('canvas_e'); // キャンバス要素の取得
let ctx = canvas.getContext('2d'); // キャンバスからコンテキストを取得
// MainPanel オブジェクト
mp = new MainPanel(canvas, ctx);
// StartPanel の表示
st_start();
}
//
// MainPanel オブジェクト(プロパティ)
//
function MainPanel(canvas, ctx)
{
this.canvas = canvas; // キャンバス要素
this.ctx = ctx; // キャンバスのコンテキスト
this.level = 1; // ゲームレベル
return this;
}
//
// MainPanel オブジェクト(メソッド)
//
MainPanel.prototype.finish = function()
{
// キャンバスのクリア
mp.ctx.clearRect(0, 0, mp.canvas.width, mp.canvas.height);
// ボタンを非表示
document.getElementById('method').style.display = "none";
document.getElementById('start').style.display = "none";
document.getElementById('first').style.display = "none";
document.getElementById('finish').style.display = "none";
}
//
// StartPanel の開始
//
function st_start()
{
mp.level = 1; // ゲームレベルの設定
// キャンバスのクリア
mp.ctx.clearRect(0, 0, mp.canvas.width, mp.canvas.height);
// ゲームタイトルの表示
mp.ctx.font = "40px 'MS ゴシック'";
mp.ctx.textBaseline = "middle";
mp.ctx.textAlign = "center";
mp.ctx.fillStyle = "rgb(0, 0, 0)";
mp.ctx.fillText("8パズル", mp.canvas.width/2, mp.canvas.height/2);
// ボタンの表示制御
document.getElementById('method').style.display = "";
document.getElementById('start').style.display = "";
document.getElementById('first').style.display = "none";
document.getElementById('finish').style.display = "none";
document.getElementById('start').innerHTML = "ゲーム開始";
}
01 gp = null; // GamePanel オブジェクト
02
03 //
04 // GamePanel の開始
05 //
06 function gp_start()
07 {
08 // GamePanel オブジェクト
09 gp = new GamePanel();
10 // 描画
11 let timerID = setTimeout("gp.draw()", 100);
12 // ボタンの表示制御
13 document.getElementById('method').style.display = "none";
14 document.getElementById('start').style.display = "none";
15 document.getElementById('first').style.display = "none";
16 document.getElementById('finish').style.display = "none";
17 }
18 //
19 // GamePanel オブジェクト(プロパティ)
20 //
21 function GamePanel()
22 {
23 this.sz = 50; // コマの大きさ
24 this.gap = 10; // コマ間のギャップ
25 this.i_state = new Array(); // 盤面の初期状態
26 this.i_state[0] = new Array(1, 2, 3);
27 this.i_state[1] = new Array(8, 0, 4);
28 this.i_state[2] = new Array(7, 6, 5);
29 this.g_state = new Array(); // 盤面の目標状態
30 this.g_state[0] = new Array(1, 2, 3);
31 this.g_state[1] = new Array(8, 0, 4);
32 this.g_state[2] = new Array(7, 6, 5);
33 this.img = new Array; // コマの画像の読み込み
34 for (let i1 = 1; i1 <= 8; i1++) {
35 this.img[i1-1] = new Image();
36 this.img[i1-1].src = "image/" + i1 + ".gif";
37 }
38 return this;
39 }
40 //
41 // GamePanel オブジェクト(メソッド draw)
42 //
43 GamePanel.prototype.draw = function()
44 {
45 // キャンバスのクリア
46 mp.ctx.clearRect(0, 0, mp.canvas.width, mp.canvas.height);
47 // 描画
48 for (let i1 = 0; i1 < 3; i1++) {
49 for (let i2 = 0; i2 < 3; i2++) {
50 let k = gp.i_state[i1][i2] - 1;
51 if (k >= 0)
52 mp.ctx.drawImage(gp.img[k], gp.gap+i2*(gp.gap+gp.sz), gp.gap+i1*(gp.gap+gp.sz));
53 }
54 }
55 }
let a = new Array(2); // let a = new Array(); でも可 for (let i1 = 0; i1 < 2; i1++) a[i1] = new Array(3);
let a = new Array(2); // let a = new Array(); でも可 a[0] = new Array(1, 2, 3); a[1] = new Array(4, 5, 6);
//
// GameClearPanel の開始
//
function gcp_start()
{
// キャンバスのクリア
mp.ctx.clearRect(0, 0, mp.canvas.width, mp.canvas.height);
// タイトルの表示
mp.ctx.font = "40px 'MS ゴシック'";
mp.ctx.textBaseline = "middle";
mp.ctx.textAlign = "center";
mp.ctx.fillStyle = "rgb(0, 0, 0)";
mp.ctx.fillText("Game", mp.canvas.width/2, mp.canvas.height/2-20);
mp.ctx.fillText("Clear!", mp.canvas.width/2, mp.canvas.height/2+20);
// ボタンの表示制御
document.getElementById('method').style.display = "none";
if (mp.level > 1) { // 最初からゲーム再開
document.getElementById('start').style.display = "none";
document.getElementById('first').style.display = "";
}
else { // レベルアップ
mp.level++;
document.getElementById('start').style.display = "";
document.getElementById('first').style.display = "none";
document.getElementById('start').innerHTML = "次のレベル";
}
document.getElementById('finish').style.display = "";
}
001 gp = null; // GamePanel オブジェクト
002
003 //
004 // GamePanel の開始
005 //
006 function gp_start()
007 {
008 // GamePanel オブジェクト
009 gp = new GamePanel();
010 // 初期状態の生成
011 gp.create();
012 // 描画
013 let timerID = setTimeout("gp.draw()", 100);
014 // ボタンの表示制御
015 document.getElementById('method').style.display = "none";
016 document.getElementById('start').style.display = "none";
017 document.getElementById('first').style.display = "none";
018 document.getElementById('finish').style.display = "none";
019 }
020 //
021 // GamePanel オブジェクト(プロパティ)
022 //
023 function GamePanel()
024 {
025 this.sz = 50; // コマの大きさ
026 this.gap = 10; // コマ間のギャップ
027 this.i_state = new Array(); // 盤面の初期状態
028 this.i_state[0] = new Array(1, 2, 3);
029 this.i_state[1] = new Array(8, 0, 4);
030 this.i_state[2] = new Array(7, 6, 5);
031 this.g_state = new Array(); // 盤面の目標状態
032 this.g_state[0] = new Array(1, 2, 3);
033 this.g_state[1] = new Array(8, 0, 4);
034 this.g_state[2] = new Array(7, 6, 5);
035 this.img = new Array; // コマの画像の読み込み
036 for (let i1 = 1; i1 <= 8; i1++) {
037 this.img[i1-1] = new Image();
038 this.img[i1-1].src = "image/" + i1 + ".gif";
039 }
040 return this;
041 }
042 //
043 // GamePanel オブジェクト(メソッド create)
044 //
045 GamePanel.prototype.create = function()
046 {
047 let ct = (mp.level == 1) ? 10 : 100;
048 let sw = true;
049
050 while (sw) {
051 let k1 = 1;
052 let k2 = 1;
053 // 移動
054 for (let i1 = 0; i1 < ct; i1++) {
055 let dr = Math.floor(4 * Math.random());
056 switch (dr) {
057 case 0: // 上
058 if (k1 > 0) {
059 wk = gp.i_state[k1-1][k2];
060 gp.i_state[k1-1][k2] = 0;
061 gp.i_state[k1][k2] = wk;
062 k1--;
063 }
064 break;
065 case 1: // 下
066 if (k1 < 2) {
067 wk = gp.i_state[k1+1][k2];
068 gp.i_state[k1+1][k2] = 0;
069 gp.i_state[k1][k2] = wk;
070 k1++;
071 }
072 break;
073 case 2: // 左
074 if (k2 > 0) {
075 wk = gp.i_state[k1][k2-1];
076 gp.i_state[k1][k2-1] = 0;
077 gp.i_state[k1][k2] = wk;
078 k2--;
079 }
080 break;
081 default: // 右
082 if (k2 < 2) {
083 wk = gp.i_state[k1][k2+1];
084 gp.i_state[k1][k2+1] = 0;
085 gp.i_state[k1][k2] = wk;
086 k2++;
087 }
088 break;
089 }
090 }
091 // ゴールと同じか否かのチェック
092 for (let i1 = 0; i1 < 3 && sw; i1++) {
093 for (let i2 = 0; i2 < 3 && sw; i2++) {
094 if (gp.i_state[i1][i2] != gp.g_state[i1][i2])
095 sw = false;
096 }
097 }
098 }
099 }
100 //
101 // GamePanel オブジェクト(メソッド draw)
102 //
103 GamePanel.prototype.draw = function()
104 {
105 // キャンバスのクリア
106 mp.ctx.clearRect(0, 0, mp.canvas.width, mp.canvas.height);
107 // 描画
108 for (let i1 = 0; i1 < 3; i1++) {
109 for (let i2 = 0; i2 < 3; i2++) {
110 let k = gp.i_state[i1][i2] - 1;
111 if (k >= 0)
112 mp.ctx.drawImage(gp.img[k], gp.gap+i2*(gp.gap+gp.sz), gp.gap+i1*(gp.gap+gp.sz));
113 }
114 }
115 }
変数 = (論理式) ? 式1 : 式2
switch (式) { [case 定数式1 : ] [文1] [case 定数式2 : ] [文2] ・・・・・ [default : ] [文n] }
001 gp = null; // GamePanel オブジェクト
002
003 //
004 // GamePanel の開始
005 //
006 function gp_start()
007 {
008 // GamePanel オブジェクト
009 gp = new GamePanel();
010 // 初期状態の生成
011 gp.create();
012 // 描画
013 let timerID = setTimeout("gp.draw()", 100);
014 // マウスクリックに対するイベントリスナ
015 mp.canvas.addEventListener("click", gp.mouseClick);
016 // ボタンの表示制御
017 document.getElementById('method').style.display = "none";
018 document.getElementById('start').style.display = "none";
019 document.getElementById('first').style.display = "none";
020 document.getElementById('finish').style.display = "none";
021 }
022 //
023 // GamePanel オブジェクト(プロパティ)
024 //
025 function GamePanel()
026 {
027 this.sz = 50; // コマの大きさ
028 this.gap = 10; // コマ間のギャップ
029 this.i_state = new Array(); // 盤面の初期状態
030 this.i_state[0] = new Array(1, 2, 3);
031 this.i_state[1] = new Array(8, 0, 4);
032 this.i_state[2] = new Array(7, 6, 5);
033 this.g_state = new Array(); // 盤面の目標状態
034 this.g_state[0] = new Array(1, 2, 3);
035 this.g_state[1] = new Array(8, 0, 4);
036 this.g_state[2] = new Array(7, 6, 5);
037 this.img = new Array; // コマの画像の読み込み
038 for (let i1 = 1; i1 <= 8; i1++) {
039 this.img[i1-1] = new Image();
040 this.img[i1-1].src = "image/" + i1 + ".gif";
041 }
042 return this;
043 }
044 //
045 // GamePanel オブジェクト(メソッド mouseClick)
046 //
047 GamePanel.prototype.mouseClick = function(event)
048 {
049 let x_base = mp.canvas.offsetLeft; // キャンバスの左上のx座標
050 let y_base = mp.canvas.offsetTop; // キャンバスの左上のy座標
051 let x = event.pageX - x_base; // キャンバス内のクリックされた位置(x座標)
052 let y = event.pageY - y_base; // キャンバス内のクリックされた位置(y座標)
053 // クリックされたコマ
054 let k1 = -1;
055 let k2 = -1;
056 for (let i1 = 0; i1 < 3; i1++) {
057 if (y >= gp.gap+i1*(gp.gap+gp.sz) && y <= (i1+1)*(gp.gap+gp.sz)) {
058 k1 = i1;
059 break;
060 }
061 }
062 for (let i1 = 0; i1 < 3; i1++) {
063 if (x >= gp.gap+i1*(gp.gap+gp.sz) && x <= (i1+1)*(gp.gap+gp.sz)) {
064 k2 = i1;
065 break;
066 }
067 }
068 // コマを移動
069 if (k1 >= 0 && k2 >= 0 && gp.i_state[k1][k2] > 0) {
070 let sw = false;
071 if (k1 > 0 && gp.i_state[k1-1][k2] == 0) { // 上
072 sw = true;
073 gp.i_state[k1-1][k2] = gp.i_state[k1][k2];
074 gp.i_state[k1][k2] = 0;
075 }
076 else if (k1 < 2 && gp.i_state[k1+1][k2] == 0) { // 下
077 sw = true;
078 gp.i_state[k1+1][k2] = gp.i_state[k1][k2];
079 gp.i_state[k1][k2] = 0;
080 }
081 else if (k2 > 0 && gp.i_state[k1][k2-1] == 0) { // 左
082 sw = true;
083 gp.i_state[k1][k2-1] = gp.i_state[k1][k2];
084 gp.i_state[k1][k2] = 0;
085 }
086 else if (k2 < 2 && gp.i_state[k1][k2+1] == 0) { // 右
087 sw = true;
088 gp.i_state[k1][k2+1] = gp.i_state[k1][k2];
089 gp.i_state[k1][k2] = 0;
090 }
091 // ゴールか?
092 if (sw) {
093 gp.draw();
094 for (let i1 = 0; i1 < 3 && sw; i1++) {
095 for (let i2 = 0; i2 < 3 && sw; i2++) {
096 if (gp.i_state[i1][i2] != gp.g_state[i1][i2])
097 sw = false;
098 }
099 }
100 if (sw)
101 gcp_start(); // ゲームクリア
102 }
103 }
104 }
105 //
106 // GamePanel オブジェクト(メソッド create)
107 //
108 GamePanel.prototype.create = function()
109 {
110 let ct = (mp.level == 1) ? 10 : 100;
111 let sw = true;
112
113 while (sw) {
114 let k1 = 1;
115 let k2 = 1;
116 // 移動
117 for (let i1 = 0; i1 < ct; i1++) {
118 let dr = Math.floor(4 * Math.random());
119 switch (dr) {
120 case 0: // 上
121 if (k1 > 0) {
122 wk = gp.i_state[k1-1][k2];
123 gp.i_state[k1-1][k2] = 0;
124 gp.i_state[k1][k2] = wk;
125 k1--;
126 }
127 break;
128 case 1: // 下
129 if (k1 < 2) {
130 wk = gp.i_state[k1+1][k2];
131 gp.i_state[k1+1][k2] = 0;
132 gp.i_state[k1][k2] = wk;
133 k1++;
134 }
135 break;
136 case 2: // 左
137 if (k2 > 0) {
138 wk = gp.i_state[k1][k2-1];
139 gp.i_state[k1][k2-1] = 0;
140 gp.i_state[k1][k2] = wk;
141 k2--;
142 }
143 break;
144 default: // 右
145 if (k2 < 2) {
146 wk = gp.i_state[k1][k2+1];
147 gp.i_state[k1][k2+1] = 0;
148 gp.i_state[k1][k2] = wk;
149 k2++;
150 }
151 break;
152 }
153 }
154 // ゴールと同じか否かのチェック
155 for (let i1 = 0; i1 < 3 && sw; i1++) {
156 for (let i2 = 0; i2 < 3 && sw; i2++) {
157 if (gp.i_state[i1][i2] != gp.g_state[i1][i2])
158 sw = false;
159 }
160 }
161 }
162 }
163 //
164 // GamePanel オブジェクト(メソッド draw)
165 //
166 GamePanel.prototype.draw = function()
167 {
168 // キャンバスのクリア
169 mp.ctx.clearRect(0, 0, mp.canvas.width, mp.canvas.height);
170 // 描画
171 for (let i1 = 0; i1 < 3; i1++) {
172 for (let i2 = 0; i2 < 3; i2++) {
173 let k = gp.i_state[i1][i2] - 1;
174 if (k >= 0)
175 mp.ctx.drawImage(gp.img[k], gp.gap+i2*(gp.gap+gp.sz), gp.gap+i1*(gp.gap+gp.sz));
176 }
177 }
178 }
| 情報学部 | 菅沼ホーム | JavaScript 目次 | 索引 |