import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.geom.*;
class OmokBoard extends Canvas{
public static final int BLACK=1, WHITE=-1;
private int[][] map;
private int size, cell;
private String info="게임 중지";
private int color=BLACK;
private boolean enable=false;
private boolean running=false;
private PrintWriter writer;
private Graphics gboard, gbuff;
private Image buff;
OmokBoard(int s, int c){
this.size=s;this.cell=c;
map=new int[size+2][];
for(int i=0;i<map.length;i++)
map[i]=new int[size+2];
setBackground(new Color(200,200,100));
setSize(size*(cell+1)+size, size*(cell+1)+size);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me){
if(!enable)return;
int x=(int)Math.round(me.getX()/(double)cell);
int y=(int)Math.round(me.getY()/(double)cell);
if(x==0 || y==0 || x==size+1 || y==size+1)return;
if(map[x][y]==BLACK || map[x][y]==WHITE)return;
writer.println("[STONE]"+x+" "+y);
map[x][y]=color;
if(check(new Point(x, y), color)){
info="이겼습니다.";
writer.println("[WIN]");
}
else info="상대가 두기를 기다립니다.";
repaint();
enable=false;
}
});
}
public boolean isRunning(){
return running;
}
public void startGame(String col){
running=true;
if(col.equals("BLACK")){
enable=true; color=BLACK;
info="게임 시작... 두세요.";
}
else{
enable=false; color=WHITE;
info="게임 시작... 기다리세요.";
}
}
public void stopGame(){
reset();
writer.println("[STOPGAME]");
enable=false;
running=false;
}
public void putOpponent(int x, int y){
map[x][y]=-color;
info="상대가 두었습니다. 두세요.";
repaint();
}
public void setEnable(boolean enable){
this.enable=enable;
}
public void setWriter(PrintWriter writer){
this.writer=writer;
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(gbuff==null){
buff=createImage(getWidth(),getHeight());
gbuff=buff.getGraphics();
}
drawBoard(g);
}
public void reset(){
for(int i=0;i<map.length;i++)
for(int j=0;j<map[i].length;j++)
map[i][j]=0;
info="게임 중지";
repaint();
}
private void drawLine(){
gbuff.setColor(Color.black);
for(int i=1; i<=size;i++){
gbuff.drawLine(cell, i*cell, cell*size, i*cell);
gbuff.drawLine(i*cell, cell, i*cell , cell*size);
}
}
private void drawBlack(int x, int y){
Graphics2D gbuff=(Graphics2D)this.gbuff;
gbuff.setColor(Color.black);
gbuff.fillOval(x*cell-cell/2, y*cell-cell/2, cell, cell);
gbuff.setColor(Color.white);
gbuff.drawOval(x*cell-cell/2, y*cell-cell/2, cell, cell);
}
private void drawWhite(int x, int y){
gbuff.setColor(Color.white);
gbuff.fillOval(x*cell-cell/2, y*cell-cell/2, cell, cell);
gbuff.setColor(Color.black);
gbuff.drawOval(x*cell-cell/2, y*cell-cell/2, cell, cell);
}
private void drawStones(){
for(int x=1; x<=size;x++)
for(int y=1; y<=size;y++){
if(map[x][y]==BLACK)
drawBlack(x, y);
else if(map[x][y]==WHITE)
drawWhite(x, y);
}
}
synchronized private void drawBoard(Graphics g){
gbuff.clearRect(0, 0, getWidth(), getHeight());
drawLine();
drawStones();
gbuff.setColor(Color.red);
gbuff.drawString(info, 20, 15);
g.drawImage(buff, 0, 0, this);
}
private boolean check(Point p, int col){
if(count(p, 1, 0, col)+count(p, -1, 0, col)==4)
return true;
if(count(p, 0, 1, col)+count(p, 0, -1, col)==4)
return true;
if(count(p, -1, -1, col)+count(p, 1, 1, col)==4)
return true;
if(count(p, 1, -1, col)+count(p, -1, 1, col)==4)
return true;
return false;
}
private int count(Point p, int dx, int dy, int col){
int i=0;
for(; map[p.x+(i+1)*dx][p.y+(i+1)*dy]==col ;i++);
return i;
}
}
public class OmokClient extends Frame implements Runnable, ActionListener{
private TextArea msgView=new TextArea("", 1,1,1);
private TextField sendBox=new TextField("");
private TextField nameBox=new TextField();
private TextField roomBox=new TextField("0");
private Label pInfo=new Label("대기실: 명");
private java.awt.List pList=new java.awt.List();
private Button startButton=new Button("대국 시작");
private Button stopButton=new Button("기권");
private Button enterButton=new Button("입장 하기");
private Button exitButton=new Button("대기실로");
private Label infoView=new Label("< 생각하는 자바 >", 1);
private OmokBoard board=new OmokBoard(15,30);
private BufferedReader reader;
private PrintWriter writer;
private Socket socket;
private int roomNumber=-1;
private String userName=null;
public OmokClient(String title){
super(title);
setLayout(null);
msgView.setEditable(false);
infoView.setBounds(10,30,480,30);
infoView.setBackground(new Color(200,200,255));
board.setLocation(10,70);
add(infoView);
add(board);
Panel p=new Panel();
p.setBackground(new Color(200,255,255));
p.setLayout(new GridLayout(3,3));
p.add(new Label("이 름:", 2));p.add(nameBox);
p.add(new Label("방 번호:", 2)); p.add(roomBox);
p.add(enterButton); p.add(exitButton);
enterButton.setEnabled(false);
p.setBounds(500,30, 250,70);
Panel p2=new Panel();
p2.setBackground(new Color(255,255,100));
p2.setLayout(new BorderLayout());
Panel p2_1=new Panel();
p2_1.add(startButton); p2_1.add(stopButton);
p2.add(pInfo,"North"); p2.add(pList,"Center"); p2.add(p2_1,"South");
startButton.setEnabled(false); stopButton.setEnabled(false);
p2.setBounds(500,110,250,180);
Panel p3=new Panel();
p3.setLayout(new BorderLayout());
p3.add(msgView,"Center");
p3.add(sendBox, "South");
p3.setBounds(500, 300, 250,250);
add(p); add(p2); add(p3);
sendBox.addActionListener(this);
enterButton.addActionListener(this);
exitButton.addActionListener(this);
startButton.addActionListener(this);
stopButton.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==sendBox){
String msg=sendBox.getText();
if(msg.length()==0)return;
if(msg.length()>=30)msg=msg.substring(0,30);
try{
writer.println("[MSG]"+msg);
sendBox.setText("");
}catch(Exception ie){}
}
else if(ae.getSource()==enterButton){
try{
if(Integer.parseInt(roomBox.getText())<1){
infoView.setText("방번호가 잘못되었습니다. 1이상");
return;
}
writer.println("[ROOM]"+Integer.parseInt(roomBox.getText()));
msgView.setText("");
}catch(Exception ie){
infoView.setText("입력하신 사항에 오류가 았습니다.");
}
}
else if(ae.getSource()==exitButton){
try{
goToWaitRoom();
startButton.setEnabled(false);
stopButton.setEnabled(false);
}catch(Exception e){}
}
else if(ae.getSource()==startButton){
try{
writer.println("[START]");
infoView.setText("상대의 결정을 기다립니다.");
startButton.setEnabled(false);
}catch(Exception e){}
}
else if(ae.getSource()==stopButton){
try{
writer.println("[DROPGAME]");
endGame("기권하였습니다.");
}catch(Exception e){}
}
}
void goToWaitRoom(){
if(userNam
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Java+语言与面向对象程序设计基础.pdf (385个子文件)
flute+hrn+mrmba.aiff 516KB
spacemusic.au 47KB
baby.gif 76KB
baby.gif 40KB
wood8.gif 11KB
back3.gif 11KB
back1.gif 10KB
dukesign.gif 9KB
back2.gif 8KB
javalogo.gif 3KB
logoin.gif 1KB
logoout.gif 1KB
Green_Car.gif 1KB
Blue_Car.gif 1KB
land.gif 1KB
Red_Car.gif 1KB
Red_Car.gif 1KB
submarine1.gif 1KB
submarine2.gif 1KB
ship1.gif 1KB
ship2.gif 1KB
pung.gif 1KB
Green_Ball.gif 1005B
Blue_Ball.gif 1005B
Red_Ball.gif 1005B
torpedo.gif 934B
depthBomb.gif 895B
saveIcon.gif 863B
Applet6.htm 494B
Applet7.htm 355B
Applet5.htm 299B
Applet8.htm 226B
AppPageViewer.htm 151B
Applet4.htm 142B
Applet3.htm 142B
Applet2.htm 138B
AppChatClient.htm 137B
Applet1.htm 133B
WaterWar.htm 131B
Applet10.htm 130B
CarRace.htm 130B
Applet11.htm 130B
Applet9.htm 130B
PPuzzle.htm 130B
AppletOmok.htm 102B
OmokClient.java 12KB
AppletOmok.java 12KB
WaterWar.java 9KB
MyMemo.java 7KB
OmokServer.java 5KB
CarRace.java 5KB
PPuzzle.java 3KB
MyFindDialogTest.java 2KB
Server5.java 2KB
Applet11.java 2KB
Applet10.java 2KB
Client4.java 2KB
AppChatClient.java 2KB
Server3.java 2KB
Server4.java 2KB
Client5.java 2KB
Client3.java 1KB
Applet2.java 1KB
Transform3.java 1KB
Synchronization7.java 1KB
MyFileTest.java 1KB
MyMenuBarTest.java 1KB
Synchronization6.java 1KB
Texture1.java 1KB
PageViewer.java 1KB
Applet7.java 1KB
DBuffering1.java 1KB
MouseEvent1.java 1KB
FileDialog1.java 1KB
Client2.java 1KB
JDBC1.java 1KB
Transform2.java 1KB
Image2.java 1KB
KeyEvent1.java 1KB
Server2.java 1KB
Recursion2.java 1KB
Gradient1.java 1KB
Vector2.java 1KB
Scrollbar1.java 1KB
WindowEvent1.java 1007B
Swing2.java 1001B
Swing3.java 971B
KeyEvent3.java 957B
SinglyList1.java 937B
Applet5.java 932B
Menu2.java 915B
Stroke1.java 886B
MyClipboardTest.java 863B
WindowEvent2.java 834B
CloseFrameDialog.java 824B
KeyEvent2.java 808B
Abstract1.java 803B
ItemEvent1.java 800B
FocusEvent1.java 800B
MultiEvent1.java 798B
共 385 条
- 1
- 2
- 3
- 4
资源评论
zhd070341401
- 粉丝: 1
- 资源: 43
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 使用PYTHON编写一个程序,识别数字和字母的程序
- chuanghanshu.m
- 3-1same.cbp
- 电路硬件PCB设计-EMC电磁兼容PCB防干扰与防静电设计pcb电磁兼容EMC设计等文档资料合集(18个).zip
- tudou-android-release (2).apk
- 三维重建-基于NeRF实现的稳定+实时3D说话人脸生成-附项目源码-优质项目实战.zip
- oracle-10g-32bit.zip
- com.aesq.zb_v1.0.35_danji100.com.apk
- 760996331259605建立门派1.360.apk
- 下面提供一些C语言的入门示例代码,并附有注释,以帮助理解每个部分的功能 1. Hello World程序 #include
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功