//=========================================================C // Jyugo(十五) Game by Java applet //---------------------------------------------------------C // 開始ボタン: ゲーム開始(変更なしはクリック不要) // ゲーム選択: 3x3, 4x4, 5x5, 6x6, 7x7 (nxn) // 初期移動: シャッフル数(rot)、乱数で移動 // 空白を右下にする移動数は含まない // スライド: 数をクリック(空白まで、縦または横に移動) //     数にタッチして空白方向に移動 //---------------------------------------------------------C // 2012年11月10日 後 保範(早稲田大学) // Yasunori Ushiro (Waseda University) //=========================================================C import java.applet.*; import java.awt.*; import java.awt.event.*; public class Jyugo extends Applet implements MouseListener { //====== 共用変数設定 ====================================== int ix, iy; //クリック位置 int xsp, ysp; //空白の位置 int stone[][] = new int[7][7]; //ボックスの数値 int fuchi=10, aki=3, tbox, sbox, r2, ncmp, xstpt; int tstart; //開始時間 int slno=0; //スライド回数 String n_size[] = {"3x3", "4x4", "5x5", "6x6", "7x7"}; Button Stbtn; //開始ボタン設定 int n=4; //ゲーム選択(3,4,5,6,7) Choice n_a; // アイテム設定 int rot=100; //初期移動値設定 TextField rot_a; // アイテム設定 Dimension d; //html指定サイズ用 //======= 初期処理 ======================================== public void init() { // Set size d = this.getSize(); //html指定サイズ取得 addMouseListener(this); //マウス処理設定 // Add Button,Choice,Text Stbtn = new Button("開始"); //開始ボタン n_a = new Choice(); //整数nの選択 Label Lab1=new Label(" サイズ"); for (int i=0; i<5; i++) //n_aの選定リスト { n_a.addItem(n_size[i]); } Label Lab2=new Label("初期移動"); rot_a = new TextField("100"); //rotの入力画面 setFont(new Font("Serif",Font.PLAIN,25)); //文字サイズ setLayout(null); //レイアウトは位置指定 add(Stbtn); //パネルへ追加 add(Lab1); add(n_a); add(Lab2); add(rot_a); xstpt = d.height + 20; //アイテム表示位置 Stbtn.setBounds(xstpt+40,20,80,40); //ボタン Lab1.setBounds(xstpt,90,100,40); //ゲーム選択 n_a.setBounds(xstpt+110,90,70,40); Lab2.setBounds(xstpt,140,105,40); //初期移動 rot_a.setBounds(xstpt+110,140,70,40); Stbtn.addActionListener(new ActionAdp()); //ボタン動作設定 n_a.addItemListener(new ItemAdp()); //アイテム動作設定 n_a.select(1); //n=4(2番目)で表示 setstone(rot); //数字の初期設定 tstart = (int)(System.currentTimeMillis()/1000); slno = 0; //スライド回数リセット } //======= 開始ボタン処理 ================================= class ActionAdp implements ActionListener { int randv; public void actionPerformed(ActionEvent e) { if (e.getSource() == Stbtn) //開始ボタンON { rot = Integer.valueOf(rot_a.getText()).intValue(); setstone(rot); //数字の乱数設定 repaint(); //描画処理 tstart = (int)(System.currentTimeMillis()/1000); slno = 0; //スライド回数リセット } } } //======= ゲーム選択(3,4,5,6,7)処理 ====================== class ItemAdp implements ItemListener { public void itemStateChanged(ItemEvent e) { Object source = e.getSource(); if (source == n_a) //nの選定 { n = n_a.getSelectedIndex() + 3; } } } //======= マウスクリック処理 ============================== public void mouseClicked(MouseEvent e) { int i, x, y; x = e.getX(); //クリックしたX座標を得る y = e.getY(); //クリックしたY座標を得る ix = (x - fuchi)/sbox; //スライド開始位置 iy = (y - fuchi)/sbox; change(); //スライド処理 repaint(); } //====== マウス移動処理 =================================== public void mousePressed(MouseEvent e) { int i, x, y; x = e.getX(); //クリックしたX座標を得る y = e.getY(); //クリックしたY座標を得る ix = (x - fuchi)/sbox; //スライド開始位置 iy = (y - fuchi)/sbox; } public void mouseReleased(MouseEvent e) { change(); //スライド処理 repaint(); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} //====== 数字の初期設定(no回乱数移動) ======================= public void setstone(int no) { int k=0, i, j, rand; for (j=0; j=0)) //下にスライド { for (i=ysp; i>iy; i--) { stone[ix][i] = stone[ix][i-1]; } xsp = ix; ysp = iy; slno++; } if ((iy>ysp)&&(iy=0)) //左にスライド { for (i=xsp; i>ix; i--) { stone[i][iy] = stone[i-1][iy]; } xsp = ix; ysp = iy; slno++; } if ((ix>xsp)&&(ix= 10) tf -= 10; //2桁の位置 if (um >= 100) tf -= 10; //3桁の位置 int ts = xstpt + 140; //秒の位置 if (us >= 10) ts -= 10; //2桁の位置 g.setFont(new Font("Serif",Font.PLAIN,20)); //文字サイズ g.drawString("時間=",xstpt+10,300); g.drawString(String.valueOf(um), tf,300); //分 g.drawString("分",xstpt+105,300); g.drawString(String.valueOf(us), ts,300); //秒 g.drawString("秒",xstpt+155,300); int ct = xstpt + 80; g.drawString("回数=",xstpt+10,340); g.drawString(String.valueOf(slno), ct,340); //回数 } } //======= 画面表示 ======================================= public void paint(Graphics g) { int i, j, xv, yv; // 表示領域サイズ取得 tbox = d.height - 2*fuchi; sbox = tbox/n; tbox = n*sbox; //ボックス幅、枠幅 r2 = sbox - 2*aki; //表示ボックス幅 int m = 240/n; // ゲーム基盤設定 g.setColor(Color.lightGray); g.fillRect(0,0, d.width,d.height); g.setColor(Color.orange); //ゲーム基盤の色 g.fillRect(fuchi,fuchi, tbox,tbox); g.setFont(new Font("Serif",Font.PLAIN,m)); //文字サイズ // 表示枠の設定 g.setColor(Color.black); for (j=0; j<=n; j++) //枠の表示 { xv = j*sbox + fuchi; g.drawLine(xv, fuchi, xv, tbox+fuchi); yv = j*sbox + fuchi; g.drawLine(fuchi, yv, tbox+fuchi, yv); } g.drawRect(fuchi-1,fuchi-1, tbox+2,tbox+2); //外枠を太く // 数字と空白の表示 for (j=0; j= 10) xv -= m/4; //2桁数字位置 yv += r2/2 + m/4 + aki; g.drawString(String.valueOf(su), xv,yv); } } } // 終了チェックと表示 finish(g); } }