//
// Slide1 2001/11/25 by ONOZATO Takehisa
//
//
//
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Slide1 extends Applet implements ActionListener,MouseListener{
Image ofs; Graphics grf; //オフスクリーン
int ofsWidth=350; int ofsHeight=240;
Color ofsBG=new Color(255,239,223); //背景色
Font fnt; //フォント
String fname; int fstyle;
Button btn[]=new Button[3]; //ボタン
int btnLeft[]={250,250,250};
int btnTop[]={70,110,150};
String btnCaption[]={"シャッフル","始めに戻す","むずかしく"};
String btnAction[]={"0","1","2"};
int btnIndex;
int step; //手数
String str[][]=new String[3][3]; //表示する数[行][列]
String str0[][]={{"-1"," 4","-3"},
{"-2"," 0"," 2"},
{" 3","-4"," 1"}}; //初期値
String str1[][]={{" 2","17","-4"},
{"-1"," 5","11"},
{"14","-7"," 8"}}; //むずかしく
String str2[][]=new String[3][3];
public void init() {
ofs=createImage(ofsWidth,ofsHeight); grf=ofs.getGraphics();
fnt=grf.getFont(); //フォント
fname=fnt.getName(); fstyle=fnt.getStyle();
for(int i=0; i=0 && index<12){Exchange(index); step++;} //入れ替え
repaint();
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
/*** ボタンのイベントプロシージャ ***/
public void actionPerformed(ActionEvent e){
btnIndex=Integer.parseInt(e.getActionCommand());
if(btnIndex==0){ //シャッフル
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){str[i][j]=str0[i][j];}
}
step=0; Shuffle();
}
if(btnIndex==1){ //始めに戻す
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){str[i][j]=str2[i][j];}
}
step=0;
}
if(btnIndex==2){ //むずかしく
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){str[i][j]=str1[i][j];}
}
step=0; Shuffle();
}
repaint();
}
/*** シャッフル ***/
void Shuffle(){
for(int i=0; i<20; i++){ //20回入れ替え
double d=12*Math.random();
int ii=(int)d;
Exchange(ii);
}
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
str2[i][j]=str[i][j]; //始めの状態を保存
}
}
}
/*** マウスクリックの位置取得 0〜5:上下 6〜11:左右 ***/
int MPoint(int x, int y){
int ii=-1; int i1,i2;
for(int i=0; i<6; i++){
i1=i/3; i2=i%3;
if(x>30+i2*50 && x<70+i2*50 && y>70+i1*50 && y<80+i1*50){ii=i;}
}
for(int i=6; i<12; i++){
i1=i/3-2; i2=i%3;
if(y>30+i2*50 && y<70+i2*50 && x>70+i1*50 && x<80+i1*50){ii=i;}
}
return ii;
}
/*** 数の入れ替え 0〜5:上下 6〜11:左右 ***/
void Exchange(int i){
int i1,i2; String s1,s2;
if(i>=0 && i<=5){
i1=i/3; i2=i%3;
s1=str[i1][i2]; s2=str[i1+1][i2];
str[i1][i2]=s2; str[i1+1][i2]=s1;
}
if(i>=6 && i<=11){
i1=i/3-2; i2=i%3;
s1=str[i2][i1]; s2=str[i2][i1+1];
str[i2][i1]=s2; str[i2][i1+1]=s1;
}
}
/*** 和の表示 ***/
void RepSum(){
int ss[][]=new int[3][3];
int sRow[]=new int[3]; int sColumn[]=new int[3];
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
ss[i][j]=Integer.parseInt(str[i][j].trim());
}
}
grf.setColor(new Color(192,192,192));
for(int i=0; i<3; i++){
grf.fillRoundRect(180,37+50*i,40,26,10,10);
grf.fillRoundRect(30+50*i,180,40,26,10,10);
}
grf.setColor(Color.black);
for(int i=0; i<3; i++){
sRow[i]=ss[i][0]+ss[i][1]+ss[i][2];
sColumn[i]=ss[0][i]+ss[1][i]+ss[2][i];
grf.drawString(""+sRow[i],190,55+50*i);
grf.drawString(""+sColumn[i],42+50*i,198);
}
}
}