import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Board extends Canvas{
private int cell,size;
private int[][] chess;
private Graphics gbuff;
private int NumOfChessman=0;
private final int O = 2, X=1;
private boolean enable=false;
public Board(int cell, int size){
this.cell = cell;
this.size = size;
chess = new int[size][size];
this.setSize(cell*size,cell*size);
this.setBackground(Color.yellow);
this.addMouseListener(new MouseHandler());
}
public void paint(Graphics g){
for(int i=0; i<=size; i++){
g.setColor(Color.black);
g.drawLine(0, cell*i, cell*size, cell*i);
g.drawLine(cell*i, 0, cell*i, cell*size);
}
Font f = new Font("楷体",1,30);
String msg = this.getPlayer()+"号选手下";
g.setColor(Color.GREEN);
g.fillRect(0, cell*size, cell*size, cell);
g.setColor(Color.black);
g.setFont(f);
g.drawString(msg,0,cell*size+30);
}
public void drawO(int line, int row){
gbuff = this.getGraphics();
int Line = line-1;
int Row = row-1;
chess[Line][Row] = O;
gbuff.setColor(Color.blue);
gbuff.drawOval(Line*cell, Row*cell, cell, cell);
}
public void drawX(int line, int row){
gbuff = this.getGraphics();
int Line = line-1;
int Row = row-1;
chess[Line][Row] = X;
gbuff.setColor(Color.blue);
gbuff.drawLine(Line*cell, Row*cell, line*cell, row*cell);
gbuff.drawLine(Line*cell, row*cell, line*cell, Row*cell);
}
public int getPlayer(){
System.out.print(NumOfChessman%2);
if(NumOfChessman%2==0)
return X;
else
return O;
}
public void SetEnable(){
this.enable = true;
}
public boolean checkWin(int currentLine, int currentRow){
int i;
for(i=0; i<size; i++)
if(chess[currentLine][i]!=chess[currentLine][0])
break;
if(i==size) return true;
for(i=0; i<size; i++)
if(chess[i][currentRow]!=chess[0][currentRow])
break;
if(i==size) return true;
for(i=0; i<size; i++)
if(chess[i][i]!=chess[0][0] || chess[i][i]==0)
break;
if(i==size) return true;
for(i=0; i<size; i++)
if(chess[2-i][i]!=chess[2][0] ||chess[2-i][i]==0)
break;
if(i==size) return true;
return false;
}
public void repaint(){
for(int i=0; i<size; i++)
for(int j=0; j<size; j++){
if(chess[i][j]==O)
this.drawO(i+1, j+1);
else if(chess[i][j]==X)
this.drawX(i+1, j+1);
}
Font f = new Font("楷体",1,30);
String msg = this.getPlayer()+"号选手下";
gbuff.setColor(Color.GREEN);
gbuff.fillRect(0, cell*size, cell*size, cell);
gbuff.setColor(Color.black);
gbuff.setFont(f);
gbuff.drawString(msg,0,cell*size+30);
}
class MouseHandler implements MouseListener{
public void mouseClicked(MouseEvent e){
if(enable == false) return;
int line = e.getX()/cell;
int row = e.getY()/cell;
if(chess[line][row]==O || chess[line][row]==X)
return;
if(NumOfChessman%2==0)
chess[line][row]=X;
else
chess[line][row]=O;
NumOfChessman++;
repaint();
if(checkWin(line,row)){
enable = false;
Font f = new Font("楷体",1,30);
String msg = getPlayer()+"号选手胜";
gbuff.setColor(Color.GREEN);
gbuff.fillRect(0, cell*size, cell*size, cell);
gbuff.setColor(Color.black);
gbuff.setFont(f);
gbuff.drawString(msg,0,cell*size+30);
}
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
}
一个小型的图形界面三子棋java程序
5星 · 超过95%的资源 需积分: 10 173 浏览量
2010-03-20
23:39:32
上传
评论 1
收藏 6KB RAR 举报
11cswyz
- 粉丝: 1
- 资源: 5
最新资源
- 111111111111111111
- Screenshot_2024-04-30-21-47-24-26.jpg
- Cpp1.cpp1111111111
- 利用ERP流程操作的整个订单流程.ppt
- 最新二开版本源码博客论坛源码,UI很漂亮,可切换皮肤界面.rar
- ModStartBlog现代化个人博客系统 v5.2.0源码.rar
- 带posix库的mingw编译器
- SoraAI是一款功能强大的AI助手,由OpenAI开发,以其出色的语音识别技术、广泛的知识库和高度的人工智能特性而备受瞩目
- Thinkphp开发大气响应式个人博客青春博客网站源码.rar
- 最新PHP博客网站程序源码 ThinkPHP.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈