import java.applet.*;
import java.awt.*;
public class Hakoiri extends Applet implements Runnable{
int md[][] = {{-2,-2,-2,-2,-2,-2,-2},
{-2, 1, 1, 3, 3, 6,-2},
{-2, 0, 0, 5, 7,-1,-2},
{-2, 0, 0, 5, 8,-1,-2},
{-2, 2, 2, 4, 4, 9,-2},
{-2,-2,-2,-2,-2,-2,-2}};
int mdc[][]= {{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}};
int px[] = {2,1,4,1,4,2,1,2,3,4};
int py[] = {1,1,1,3,3,3,5,4,4,5};
int pxc[] = {0,0,0,0,0,0,0,0,0,0};
int pyc[] = {0,0,0,0,0,0,0,0,0,0};
int sx,sy;
int cx,cy;
int bx[] = {10,230,230,170,170,220,220,20, 20, 70, 70, 10,10};
int by[] = {10, 10,280,280,270,270, 20,20,270,270,280,280,10};
int pd;
int count = 0;
Image p1,p2,p3,p4;
Button rep = new Button(" replay ");
Thread th = null;
Image os;
Graphics og;
public void init(){
setLayout(new BorderLayout());
Panel pan = new Panel();
pan.setLayout(new FlowLayout());
pan.add(rep);
add("South",pan);
p1 = getImage(getDocumentBase(),"Picture/p_dai.gif");
p2 = getImage(getDocumentBase(),"Picture/p_tate.gif");
p3 = getImage(getDocumentBase(),"Picture/p_yoko.gif");
p4 = getImage(getDocumentBase(),"Picture/p_syo.gif");
setBackground(Color.white);
os = createImage(240,330);
og = os.getGraphics();
for(int i = 0;i < 7;i ++){
for(int j = 0;j < 6;j ++){
mdc[j][i] = md[j][i];
}
}
for(int k = 0;k < 10;k ++){
pxc[k] = px[k];
pyc[k] = py[k];
}
}
public void paint(Graphics g){
og.setColor(Color.white);
og.fillRect(0,0,240,330);
og.setColor(Color.black);
og.setColor(Color.darkGray);
og.fillPolygon(bx,by,13);
og.setFont(new Font("Century",Font.PLAIN,48));
og.setColor(Color.red);
og.drawString("exit",85,260);
og.drawImage(p1,(px[0]-1)*50+20,(py[0]-1)*50+20,this);
for(int i = 1;i < 5;i ++){
og.drawImage(p2,(px[i]-1)*50+20,(py[i]-1)*50+20,this);
}
og.drawImage(p3,(px[5]-1)*50+20,(py[5]-1)*50+20,this);
for(int i = 6;i < 10;i ++){
og.drawImage(p4,(px[i]-1)*50+20,(py[i]-1)*50+20,this);
}
og.setFont(new Font("Century",Font.PLAIN,16));
og.setColor(Color.black);
og.drawString("Count = "+count,140,295);
g.drawImage(os,0,0,this);
}
public boolean mouseDown(Event e,int x,int y){
cx = (x - 20) / 50 + 1;
cy = (y - 20) / 50 + 1;
pd = md[cx][cy];
if(pd == 0){
sx = (px[pd]-1)*50+20 + 50;
sy = (py[pd]-1)*50+20 + 50;
if((x - sx > sy - y) && (x - sx > y - sy) &&
(md[px[pd]+2][py[pd]] == -1) && (md[px[pd]+2][py[pd]+1] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]][py[pd]+1] = -1;
md[px[pd]+2][py[pd]] = pd;
md[px[pd]+2][py[pd]+1] = pd;
px[pd] ++;
count ++;
}else if((x - sx < sy - y) && (x - sx > y - sy) &&
(md[px[pd]][py[pd]-1] == -1) && (md[px[pd]+1][py[pd]-1] == -1)){
md[px[pd]][py[pd]+1] = -1;
md[px[pd]+1][py[pd]+1] = -1;
md[px[pd]][py[pd]-1] = pd;
md[px[pd]+1][py[pd]-1] = pd;
py[pd] --;
count ++;
}else if((x - sx > sy - y) && (x - sx < y - sy) &&
(md[px[pd]][py[pd]+2] == -1) && (md[px[pd]+1][py[pd]+2] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]+1][py[pd]] = -1;
md[px[pd]][py[pd]+2] = pd;
md[px[pd]+1][py[pd]+2] = pd;
py[pd] ++;
count ++;
}else if((x - sx < sy - y) && (x - sx < y - sy) &&
(md[px[pd]-1][py[pd]] == -1) && (md[px[pd]-1][py[pd]+1] == -1)){
md[px[pd]+1][py[pd]] = -1;
md[px[pd]+1][py[pd]+1] = -1;
md[px[pd]-1][py[pd]] = pd;
md[px[pd]-1][py[pd]+1] = pd;
px[pd] --;
count ++;
}else{
}
}else if((pd > 0) && (pd < 5)){
sx = (px[pd]-1)*50+20 + 25;
sy = (py[pd]-1)*50+20 + 50;
if((x - sx > 2*(sy - y)) && (x - sx > 2*(y - sy)) &&
(md[px[pd]+1][py[pd]] == -1) && (md[px[pd]+1][py[pd]+1] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]][py[pd]+1] = -1;
md[px[pd]+1][py[pd]] = pd;
md[px[pd]+1][py[pd]+1] = pd;
px[pd] ++;
count ++;
}else if((x - sx < 2*(sy - y)) && (x - sx > 2*(y - sy)) &&
(md[px[pd]][py[pd]-1] == -1)){
md[px[pd]][py[pd]+1] = -1;
md[px[pd]][py[pd]-1] = pd;
py[pd] --;
count ++;
}else if((x - sx > 2*(sy - y)) && (x - sx < 2*(y - sy)) &&
(md[px[pd]][py[pd]+2] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]][py[pd]+2] = pd;
py[pd] ++;
count ++;
}else if((x - sx < 2*(sy - y)) && (x - sx < 2*(y - sy)) &&
(md[px[pd]-1][py[pd]] == -1) && (md[px[pd]-1][py[pd]+1] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]][py[pd]+1] = -1;
md[px[pd]-1][py[pd]] = pd;
md[px[pd]-1][py[pd]+1] = pd;
px[pd] --;
count ++;
}else{
}
}else if(pd == 5){
sx = (px[pd]-1)*50+20 + 50;
sy = (py[pd]-1)*50+20 + 25;
if((2*(x - sx) > sy - y) && (2*(x - sx) > y - sy) && (md[px[pd]+2][py[pd]] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]+2][py[pd]] = pd;
px[pd] ++;
count ++;
}else if((2*(x - sx) < sy - y) && (2*(x - sx) > y - sy) &&
(md[px[pd]][py[pd]-1] == -1) && (md[px[pd]+1][py[pd]-1] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]+1][py[pd]] = -1;
md[px[pd]][py[pd]-1] = pd;
md[px[pd]+1][py[pd]-1] = pd;
py[pd] --;
count ++;
}else if((2*(x - sx) > sy - y) && (2*(x - sx) < y - sy) &&
(md[px[pd]][py[pd]+1] == -1) && (md[px[pd]+1][py[pd]+1] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]+1][py[pd]] = -1;
md[px[pd]][py[pd]+1] = pd;
md[px[pd]+1][py[pd]+1] = pd;
py[pd] ++;
count ++;
}else if((2*(x - sx) < sy - y) && (2*(x - sx) < y - sy) &&
(md[px[pd]-1][py[pd]] == -1)){
md[px[pd]+1][py[pd]] = -1;
md[px[pd]-1][py[pd]] = pd;
px[pd] --;
count ++;
}else{
}
}else if((pd > 5) && (pd < 10)){
sx = (px[pd]-1)*50+20 + 25;
sy = (py[pd]-1)*50+20 + 25;
if((x - sx > sy - y) && (x - sx > y - sy) && (md[px[pd]+1][py[pd]] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]+1][py[pd]] = pd;
px[pd] ++;
count ++;
}else if((x - sx < sy - y) && (x - sx > y - sy) && (md[px[pd]][py[pd]-1] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]][py[pd]-1] = pd;
py[pd] --;
count ++;
}else if((x - sx > sy - y) && (x - sx < y - sy) && (md[px[pd]][py[pd]+1] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]][py[pd]+1] = pd;
py[pd] ++;
count ++;
}else if((x - sx < sy - y) && (x - sx < y - sy) && (md[px[pd]-1][py[pd]] == -1)){
md[px[pd]][py[pd]] = -1;
md[px[pd]-1][py[pd]] = pd;
px[pd] --;
count ++;
}else{
}
}else{
}
repaint();
return true;
}
public boolean action(Event e,Object o){
if(e.target == rep){
for(int i = 0;i < 7;i ++){
for(int j = 0;j < 6;j ++){
md[j][i] = mdc[j][i];
}
}
for(int k = 0;k < 10;k ++){
px[k] = pxc[k];
py[k] = pyc[k];
}
}
count = 0;
repaint();
return true;
}
public void start(){
if(th == null){
th = new Thread(this);
th.start();
}
}
public void stop(){
th = null;
}
public void run(){
while(th != null){
try{
Thread.sleep(100);
}
catch (InterruptedException e){
}
}
}
public void update( Graphics g ){
paint( g ) ;
}
}
