2009년 6월 29일 월요일

Othello (미완성)

http://namsuck.cafe24.com/projects/othello/othello.html

기본 알고리즘 까지만 구현해 놓은 소스이다..
아직 승부 처리 및 여러가지가 필요하다.
            
-양 남 석-


----othello.html------------------------------------------------------------------
<html>

<body>

<br>
<br>
<center>
<!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<object
    classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
    codebase = "http://java.sun.com/update/1.6.0/jinstall-6u13-windows-i586.cab#Version=6,0,0,3"
    WIDTH="700" HEIGHT = "500" >
    <PARAM NAME = CODE VALUE = "Othello.class" >
    <PARAM NAME = "codebase" value = "projects/othello/" >
    <param name = "type" value = "application/x-java-applet;version=1.6">
    <param name = "scriptable" value = "false">

    <comment>
<embed
            type = "application/x-java-applet;version=1.6" \
            CODE = "Othello.class" \
            codebase = "projects/othello/" \
            WIDTH="700" HEIGHT = "500"
   scriptable = false
   pluginspage = "http://java.sun.com/products/plugin/index.html#download">
   <noembed>
            
            </noembed>
</embed>
    </comment>
</object>

</center>
</body>

</html>

----Othello.java------------------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.awt.Image;
import java.awt.event.*;


public class Othello extends Applet implements MouseListener
{

private static final long serialVersionUID = 9721014L;  

private Image           imgOffScr;                      
private Graphics        grpOffScr;  

int sizeH=40;
int sizeW=40;
int partition=8;

int mouseX=0;
int mouseY=0;
int number=1;
int data[][];

    public void init()
    {
        this.setBackground(Color.white);

data=new int[partition][partition];
for(int i=0;i<partition;i++)
for(int j=0;j<partition;j++)
data[i][j]=0;

addMouseListener(this);
    }
    
    public void start()
{
imgOffScr = createImage(getWidth(),getHeight());
        grpOffScr = imgOffScr.getGraphics();
}
public void update(Graphics g)
{
        paint(g);
}

public void paint(Graphics g)
    {
   grpOffScr.setColor(Color.white);            
        grpOffScr.fillRect(0, 0, getWidth(),getHeight());  

///////////////판에 무늬넣기/////////////////////////////////////////////////
for (int i=0;i<partition;i++)
{
for (int j=0;j<partition;j++)
{
if((i+j)%2==0)grpOffScr.setColor(new Color(200,200,200));
else grpOffScr.setColor(new Color(220,220,220));
grpOffScr.fillRect(i*sizeH,j*sizeW,sizeH,sizeW);
}
}

///////////////판에 줄그리기/////////////////////////////////////////////////
grpOffScr.setColor(Color.black);
for (int i=0;i<partition+1;i++ )              
{
grpOffScr.drawLine(0,i*sizeH, sizeH*partition,i*sizeW);    //세로줄
grpOffScr.drawLine(i*sizeW,0, i*sizeW,sizeH*partition);   //가로줄
}
///////////////바둑알그리기/////////////////////////////////////////////////
for(int i=0;i<partition;i++)                            
for(int j=0;j<partition;j++)
if(data[i][j]==1 || data[i][j]==2)
{
if(data[i][j]==1)grpOffScr.setColor(new Color(100,100,200));
else if(data[i][j]==2)grpOffScr.setColor(new Color(200,100,100));
grpOffScr.fillOval(i*sizeH+3,j*sizeW+3,sizeH-6,sizeW-6);  //속이 찬 원그리기
grpOffScr.setColor(Color.black);                                    
grpOffScr.drawOval(i*sizeH+3,j*sizeW+3,sizeH-6,sizeW-6);  //원 윤각테두리
}

///////////////상황창보이기/////////////////////////////////////////////////

if(number==1)grpOffScr.setColor(new Color(80,80,200));
else if(number==2)grpOffScr.setColor(new Color(200,80,80));
grpOffScr.fillOval(partition*sizeH+100,0,sizeH-6,sizeW-6);


grpOffScr.setColor(new Color(200,200,200));
grpOffScr.fillRect(partition*sizeH+20,10,60,30);
grpOffScr.setColor(Color.black);
grpOffScr.drawRect(partition*sizeH+20,10,60,30);
grpOffScr.drawString("다시",partition*sizeH+70,10);

grpOffScr.setColor(Color.black);
grpOffScr.drawString("박스크기X:"+Integer.toString(getHeight()),350,50);
grpOffScr.drawString("박스크기Y:"+Integer.toString(getWidth()),350,80);
grpOffScr.drawString("칸X:"+Integer.toString(mouseX),350,110);
grpOffScr.drawString("칸Y:"+Integer.toString(mouseY),350,140);
grpOffScr.drawString("파란돌수:"+Integer.toString(bluecount()),350,170);
grpOffScr.drawString("빨간돌수:"+Integer.toString(redcount()),350,200);
grpOffScr.drawString(win(),350,230);
///////////////버퍼에 있는 이미지 출력///////////////////////////////////////
g.drawImage(imgOffScr, 0, 0, this);    
    }



public void mousePressed(MouseEvent e)
{
if(e.getX()<=sizeW*partition && e.getY()<=sizeH*partition )
{
mouseX=(e.getX()/sizeH);
mouseY=(e.getY()/sizeW);
algorithm();
}
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}

/*
    public void run()
    {
    }

    
    public void stop()
    {
    }

    public void destroy()
    {
    }
*/

public void change_Data(String d,int select)   //d는 좌표값이 들어있는 항상 XY짝수이다.
{
for(int i=0;i<d.length()/2;i++)  //좌표의 갯수
{
  data[Integer.parseInt(d.substring(i*2,i*2+1))][Integer.parseInt(d.substring(i*2+1,i*2+2))]=select;  //좌표를 저장
}
}


public int bluecount()
{
int count=0;
for(int i=0;i<partition;i++)
for(int j=0;j<partition;j++)
if(data[i][j]==1)count++;
return count;
}

public int redcount()
{
int count=0;
for(int i=0;i<partition;i++)
for(int j=0;j<partition;j++)
if(data[i][j]==2)count++;
return count;
}

public String win()
{
int countb=0;
int countr=0;

for(int i=0;i<partition;i++)
for(int j=0;j<partition;j++)
{
if(data[i][j]==1)countb++;
if(data[i][j]==2)countr++;
}
if(countb>countr)return "파란색이 많음";
else if(countb<countr)return "빨간색이 많음";
else return "파란색과 빨간색 갯수 같음";
}


public void algorithm()
{
if(data[mouseX][mouseY]==0)
{
//선택 3시쪽 돌 변경
String temp="";
for(int i=mouseX+1;i<partition && data[i][mouseY]!=0;i++)    
if(data[i][mouseY]!=number)temp+=Integer.toString(i)+Integer.toString(mouseY);
else{change_Data(temp,number);break;}
//선택 9시쪽 돌 변경
temp="";
for(int i=mouseX-1;i>=0 && data[i][mouseY]!=0;i--)    
if(data[i][mouseY]!=number)temp+=Integer.toString(i)+Integer.toString(mouseY);
else {change_Data(temp,number);break;}
//선택 12시쪽 돌 변경
temp="";
for(int i=mouseY-1;i>=0 && data[mouseX][i]!=0;i--)    
if(data[mouseX][i]!=number)temp+=Integer.toString(mouseX)+Integer.toString(i);
else {change_Data(temp,number);break;}
//선택 6시쪽 돌 변경
temp="";
for(int i=mouseY+1;i<partition && data[mouseX][i]!=0;i++)    
if(data[mouseX][i]!=number)temp+=Integer.toString(mouseX)+Integer.toString(i);
else {change_Data(temp,number);break;}
//선택 1시반쪽 돌 변경
temp="";
for(int i=mouseX+1,j=mouseY-1;i<partition && j>=0  && data[i][j]!=0;i++,j--)    
if(data[i][j]!=number)temp+=Integer.toString(i)+Integer.toString(j);
else {change_Data(temp,number);break;}
//선택 7시반쪽 돌 변경
temp="";
for(int i=mouseX-1,j=mouseY+1;i>=0 && j<partition  && data[i][j]!=0;i--,j++)    
if(data[i][j]!=number)temp+=Integer.toString(i)+Integer.toString(j);
else {change_Data(temp,number);break;}
//선택 4시반쪽 돌 변경
temp="";
for(int i=mouseX+1,j=mouseY+1;i<partition && j<partition  && data[i][j]!=0;i++,j++)    
if(data[i][j]!=number)temp+=Integer.toString(i)+Integer.toString(j);
else {change_Data(temp,number);break;}
//선택 11시반쪽 돌 변경
temp="";
for(int i=mouseX-1,j=mouseY-1;i>=0 && j>=0  && data[i][j]!=0;i--,j--)    
if(data[i][j]!=number)temp+=Integer.toString(i)+Integer.toString(j);
else {change_Data(temp,number);break;}



// 선택한 돌 놓고... 차례 바꾸기
if(number==1)
{
data[mouseX][mouseY]=1;
number=2;
}
else  if(number==2)
{
data[mouseX][mouseY]=2;
number=1;
}

// 화면 다시 그리기
repaint();
}
}

}

댓글 없음:

댓글 쓰기