0
パズルの入れ替えを上下左右に
おねがいします
import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.lang.*;
import java.awt.event.*;
public class puzzle extends Applet implements Runnable, MouseListener{
MediaTracker mt;
Image orgpict;
Image pict[] = new Image[16];
int PX = 4, PY = 4;
int pieceWidth = 50, pieceHeight = 50;
int piece[] = new int[PX * PY];
int hole;
int score = 0;
int x[] = new int[PX * PY];
int y[] = new int[PX * PY];
int margin = 10;
boolean complete = false;
boolean hint = false;
Thread kicker = null;
Dimension d;
Image offs;
Graphics grf;
Image wrkpict;
Graphics wrkgrf;
public void init(){
int i, j;
int k;
int yy;
mt = new MediaTracker(this);
//イメージファイルの読み込み
orgpict = getImage(getCodeBase(), "rogo.jpg");
mt.addImage(orgpict, 0);
//ピースの位置(x,y座標)の設定
k = 0;
for(i = 0 ; i < 4 ; i++){
yy= i* pieceHeight + margin;
for(j = 0 ; j < 4 ; j++){
x[k] = j * pieceWidth + margin;
y[k] = yy;
k++;
}
}
//オフスクリーン設定
d = getSize();
offs = createImage(d.width, d.height);
grf = offs.getGraphics();
//ピースをセットする
Shuffle();
//マウスリスナーとして自分自身を登録
addMouseListener(this);
}
public void paint(Graphics g){
update(g);
}
public void update(Graphics g){
int i;
//読み込み時のメッセージの表示
if(!mt.checkID(0)){
g.clearRect(0, 0, d.width, d.height);
g.drawString("Please wait....", d.width / 4, d.height / 2);
return;
}
grf.setColor(Color.orange);
grf.fillRect(0, 0, PX * pieceWidth + 100,300);
grf.setColor(Color.red);
grf.drawString("PUZZLE", PX * pieceWidth + 35, margin * 2);
//ピースの描画
if(complete || hint){
//絵が揃ったら下りになるの絵を描く
grf.drawImage(orgpict, 10, 10, this);
} else {
for(i = 0 ; i < 16 ; i++){
if(i == hole){
//空白=白い四角を描く
grf.setColor(Color.white);
grf.fillRect(x[i], y[i], pieceWidth, pieceHeight);
} else {
//セットされている絵を描く
grf.drawImage(pict[piece[i]], x[i], y[i], this);
//ピースに枠をつける
grf.setColor(Color.white);
grf.drawLine(x[i], y[i], x[i] + pieceWidth, y[i]);
grf.drawLine(x[i], y[i], x[i], y[i] + pieceHeight);
grf.setColor(Color.black);
grf.drawLine(x[i] - 1, y[i] + pieceWidth - 1,
x[i] + pieceWidth - 1, y[i] + pieceWidth - 1);
grf.drawLine(x[i] + pieceWidth - 1, y[i] + pieceWidth - 1,
x[i] + pieceWidth - 1, y[i] - 1);
}
}
}
//n SHUFFLEボタンの描画
grf.setColor(Color.green);
grf.fillRect(PX * pieceWidth + 20, 60, 70, 25);
grf.setColor(Color.black);
grf.drawString("SHEFFLE", PX * pieceWidth + 25, 78);
//HIT ボタンの描画
grf.setColor(Color.yellow);
grf.fillRect(PX * pieceWidth + 25, 95, 40, 25);
grf.setColor(Color.black);
grf.drawString("HINT", PX * pieceWidth + 35, 113);
grf.drawString("得点 : " + score, PX * pieceWidth + 30, 50);
//オフスクリーンのイメージを一挙に実際の表示領域に描く
g.drawImage(offs, 0, 0, this);
}
public void mousePressed(MouseEvent e){
int i;
int row, col;
int nn;
int move[] = new int[4];
int work;
int ix, iy;
//マウスが押された座標を得る
ix = e.getX();
iy = e.getY();
if(ix > PX * pieceWidth + 20 && ix < PX * pieceWidth + 90 && iy > 60 && iy < 85){
Shuffle();
score = 0;
repaint();
}
//マウス座標(ix,iy)がHINTボタン内だったら
if(ix > PX * pieceWidth + 25 && ix < PX * pieceWidth + 65 && iy > 95 && iy < 120){
hint = true;
repaint();
}
//マウスが何列目で押されたか
col = -1;
if(ix >= margin && ix <= pieceWidth * PX && iy >= margin && iy <= pieceWidth * PY){
if(ix > margin) col = 0;
if(ix > pieceWidth + margin) col = 1;
if(ix > pieceWidth * 2 + margin) col = 2;
if(ix > pieceWidth * 3 + margin) col = 3;
if(ix > pieceWidth * 4 + margin) col = 4;
}
//マウスが何行目で押されたか
row = -1;
if(ix >= margin && ix <= pieceWidth * PX && iy >= margin && iy <= pieceWidth * PY){
if(iy > margin) row = 0;
if(iy > pieceHeight + margin) row = 1;
if(iy > pieceHeight * 2 + margin) row = 2;
if(iy > pieceHeight * 3 + margin) row = 3;
if(iy > pieceHeight * 4 + margin) row = 4;
}
if(col >= 0 && row >= 0 && !complete){
//動かすピース番号の計算
nn = row * col;
score = score + 1; //点数加算
//ピースの入れ替え
change(nn);
//絵が揃ったかどうかのチェック
check();
}
}
public void mouseReleased(MouseEvent e){
//マウスボタンが離れたら
hint = false;
repaint();
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
void Shuffle(){
int i, k;
int no;
int work;
//絵が揃ったかどうかの変数をfalseにする
complete = false;
//空白のピース
hole = 15;
//ピースに絵の番号をセット
for(i = 0 ; i < 16 ; i++){
piece[i] = i;
}
//ピースの順番を入れ替える
for(k = 0 ; k < 16 ; k++){
//乱数で入れ替えるピースを決める
no = (int)(Math.random() * (float)(16));
//ピースの入れ替え
change(no);
}
}
void change(int nn){
int i;
int move[] = new int[4];
int work;
move[0] = nn - 4;
move[1] = nn + 4;
move[2] = nn - 1;
move[3] = nn + 1;
for(i = 0 ; i < 4 ; i++){
if(move[i] == hole){
//ピースの入れ替え
work = piece[nn];
piece[nn] = piece[hole];
piece[hole] = work;
repaint();
hole = nn;
return;
}
}
return;
}
void check (){
int i;
//絵が全て揃ったかどうかのチェック
for(i = 0 ; i < 16 ; i++){
if(piece[i] != i){
return;
}
}
complete = true;
return;
}
public void start(){
if(kicker == null){
//スレッドの開始
kicker = new Thread(this);
kicker.start();
}
}
public void stop(){
//スレッドの停止
//点数の表示
kicker = null;
}
public void run(){
int i;
//全てのイメージの読み込みを待つ
try{
mt.waitForID(0);
}
catch(InterruptedException e)
{
return;
}
//イメージの切り出し
for(i = 0 ; i < 16 ; i++){
wrkpict = createImage(pieceWidth, pieceHeight);
wrkgrf = wrkpict.getGraphics();
wrkgrf.drawImage(orgpict, -(x[i] - margin), -(y[i] -margin), this);
pict[i] = wrkpict;
}
repaint();
}
}