/*****************************/ /* */ /*   ナイトの周遊      */ /* */ /* 1998/ 6/ 3 宍戸 輝光 */ /* */ /*****************************/ import java.awt.*; class Knightjp extends Canvas { // 磐面用クラス private Image img; private Graphics g_img; private Button bkB,ntB; private int w,h,x,y,bs=6,cs=20; private int cell[][]=new int[6][6]; private Point oldP[]=new Point[37]; public int n; Knightjp(Image im,Button b,Button b2) { // コンストラクタ int i; w=127; // キャンバスの大きさ設定 h=127; resize(w,h); img=im; // 渡されたイメージをキャンバスのイメージに設定 g_img=img.getGraphics(); // キャンバスに描画するGrahics g_img.setColor(Color.black); // キャンバス初期化 g_img.fillRect(0,0,w,h); bkB=b; // メインクラスのクリアボタンへの参照 ntB=b2; clearCell(); // 盤面クリア } public void clearCell() { // 盤面クリア int i,j; for (i=0;i=bs ||wy<0 ||wy>=bs) return false; drawCell(); g_img.setColor(Color.red); // カーソル位置に枠を描く g_img.drawRect(wx*(cs+1)+1,wy*(cs+1)+1,cs-1,cs-1); repaint(); return true; } private void putKnight(int bx,int by) { // ナイトを置く int i,j,wx,wy; for (i=0;i=0) && (wx=0 && (by-wy)=0 && (by+wy)=0) && (wx=0 && (by-wy)=0 && (by+wy)1) // 2手目以降はBackボタン有効 bkB.enable(true); if (oldP[n].x==-1) ntB.enable(false); drawCell(); } public void backGame() { // Backボタン処理 cell[oldP[n-1].x][oldP[n-1].y]=0; // ナイトの現在位置をクリア n=n-2; // 一手前の状態に戻す putKnight(oldP[n].x,oldP[n].y); drawCell(); if (n<2) // 最初まで戻ったらBackボタン無効 bkB.enable(false); ntB.enable(true); } public void nextGame() { putKnight(oldP[n].x,oldP[n].y); drawCell(); } public void paint(Graphics g){ g.drawImage(img,0,0,this); // イメージを表示 } public void update(Graphics g){ paint(g); } } public class Knightj extends java.applet.Applet { // メインクラス Knightjp cv; Button clB,bkB,ntB; Panel p,p2; public void init(){ setBackground(Color.orange); setLayout(new BorderLayout()); bkB=new Button("Back"); ntB=new Button("Next"); cv=new Knightjp(createImage(127,127),bkB,ntB); // 磐面作成 p=new Panel(); p.add("Center",cv); // 磐面配置 add("Center",p); clB=new Button("Clear"); p2=new Panel(); p2.setLayout(new FlowLayout(FlowLayout.CENTER,12,5)); p2.add(clB); p2.add(bkB); p2.add(ntB); add("South",p2); } public boolean action(Event evt,Object What) { int i,j; if (evt.target==clB) { // Clearボタン cv.clearCell(); return true; } if (evt.target==bkB) { // Backボタン cv.backGame(); return true; } if (evt.target==ntB) { // Nextボタン cv.nextGame(); return true; } return false; } }