import java.awt.*;
import javax.swing.*;
public class ChessFrame extends JFrame{
private ChessPanel cp;
private ChessModel cm;
public ChessFrame(){
this.setTitle("五子棋");
cm=new ChessModel();
cp=new ChessPanel(cm);
this.add(cp);
}
public void showMe(){
this.setVisible(true);
this.setSize(480,400);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ChessFrame().showMe();
}
}
----------------------------------------
import javax.swing.*;
public class ChessModel {
private int width,height;
private int x=0,y=0;
private int [][] arrMapShow;
private boolean isOdd,isExist;
public ChessModel(){
this.isOdd=true;
PaneInit(20,15);
}
private void PaneInit(int width,int height){
this.width=width;
this.height=height;
arrMapShow=new int[width+1][height+1];
for(int i=0;i<=width;i++){
for(int j=0;j<height+1;j++){
arrMapShow[i][j]=5;
}
}
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int[][] getArrMapShow() {
return arrMapShow;
}
public void setArrMapShow(int[][] arrMapShow) {
this.arrMapShow = arrMapShow;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean getisOdd() {
return isOdd;
}
public void setisOdd(boolean isOdd) {
if(isOdd)
this.isOdd = true;
else
this.isOdd=false;
}
public boolean isExist() {
return isExist;
}
public void setExist(boolean isExist) {
this.isExist = isExist;
}
private boolean badxy(int x,int y){
if(x>=width+20||x<0)
return true;
return y>=height+20||y<0;
}
public boolean chessExist(int i,int j){
if(this.arrMapShow[i][j]==1||this.arrMapShow[i][j]==2)
return true;
return false;
}
public void redyplay(int x,int y){
if(badxy(x,y))
return;
if(chessExist(x,y))
return;
this.arrMapShow[x][y]=3;
}
public void play(int x,int y){
if(badxy(x,y))
return;
if(chessExist(x,y)){
this.isExist=true;
return;
}else
this.isExist=false;
if(getisOdd()){
setisOdd(false);
this.arrMapShow[x][y]=1;
}else{
setisOdd(true);
this.arrMapShow[x][y]=2;
}
}
//判断胜利的条件
public boolean judgeSuccess(int x,int y,boolean isodd){
int num=1;
int arrvalue;
int x_temp=x,y_temp=y;
if(isodd)
arrvalue=2;
else
arrvalue=1;
int x_temp1=x_temp,y_temp1=y_temp;
//判断右边
for(int i=1;i<=6;i++){
x_temp1+=1;
if(x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
//判断左边
x_temp1=x_temp;
for(int i=1;i<=6;i++){
x_temp1-=1;
if(x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;
//判断上方
x_temp1=x_temp;
y_temp1=y_temp;
num=1;
for(int i=1;i<=6;i++){
y_temp1-=1;
if(y_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
//判断下方
y_temp1=y_temp;
for(int i=1;i<=6;i++){
y_temp1+=1;
if(y_temp1>this.height)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;
//判断左上
x_temp1=x_temp;
y_temp1=y_temp;
num=1;
for(int i=1;i<=6;i++){
x_temp1-=1;
y_temp1-=1;
if(y_temp1<0 || x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
//判断右下
x_temp1=x_temp;
y_temp1=y_temp;
for(int i=1;i<=6;i++){
x_temp1+=1;
y_temp1+=1;
if(y_temp1>this.height || x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;
//判断右上
x_temp1=x_temp;
y_temp1=y_temp;
num=1;
for(int i=1;i<=6;i++){
x_temp1+=1;
y_temp1-=1;
if(y_temp1<0 || x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
//判断左下
x_temp1=x_temp;
y_temp1=y_temp;
for(int i=1;i<=6;i++){
x_temp1-=1;
y_temp1+=1;
if(y_temp1>this.height || x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;
return false;
}
//五子成线后的提示
public void showSuccess(JPanel jp){
JOptionPane.showMessageDialog(jp,"你赢了,好厉害!","win",
JOptionPane.INFORMATION_MESSAGE);
new ChessFrame().showMe();
}
}
----------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
public class ChessPanel extends JPanel {
private int width, height;
private ChessModel cm;
public ChessPanel(ChessModel mm) {
cm = mm;
width = cm.getWidth();
height = cm.getHeight();
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
int x=(e.getX()-10)/20;
int y=(e.getY()-10)/20;
cm.play(x,y);
repaint();
if(cm.judgeSuccess(x,y,cm.getisOdd())){
cm.showSuccess(null);
}
}
});
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for(int j=0;j<=height;j++){
for(int i=0;i<width;i++){
int v=cm.getArrMapShow()[i][j];
draw(g,i,j,v);
}
}
}
public void setModel(ChessModel mm){
cm=mm;
width=cm.getWidth();
height=cm.getHeight();
}
public void draw(Graphics g,int i,int j,int v){
int x=20*i+20;
int y=20*j+20;
if(i!=width&&j!=height){
g.setColor(Color.black);
g.drawRect(x, y, 20, 20);
}
if(i!=width&&j!=height){
g.setColor(Color.black);
g.drawRect(x, y, 20, 20);
}
if(v==1){
g.setColor(Color.gray);
g.drawOval(x-8, y-8, 16, 16);
g.setColor(Color.black);
g.fillOval(x-8, y-8, 16, 16);
}
if(v==2){
g.setColor(Color.gray);
g.drawOval(x-8, y-8, 16, 16);
g.setColor(Color.white);
g.fillOval(x-8, y-8, 16, 16);
}
if(v==3){
g.setColor(Color.cyan);
g.drawOval(x-8, y-8, 316, 16);
}
}
}
三个类,可以直接使用了
追问能不能别复制一篇来