这次的五子棋应用了上次的算法,但是添加了界面。
package chesses;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static net.mindview.util.SwingConsole.*;
public class playchess extends JFrame implements ActionListener,MouseListener{
private JTextArea t=new JTextArea(200,200);
private JButton
start=new JButton("开始");
//back=new JButton("悔棋");
static enum P{Blank,Black,White};
P player=P.Black;
P map[][]=new P [18][18];
public playchess(){
this.setLayout(null);
start.setBounds(400,50,100,50);
this.add(start);
start.addActionListener(this);
//等待处理
/*t.setBounds(380,150,150,150);
this.add(t);*/
//this.addMouseListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(550,400);
this.setTitle("五子棋游戏");
}
int w=20;
int bx=50,by=50;
int bwidth=w*16,bheight=w*16;
public void paint(Graphics g){
g.clearRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.BLACK);
g.drawRect(bx, by, bwidth-w, bheight-w);
for(int i=0;i<16;i++){
int x1=bx+w*i;
int y1=by;
int x2=bx+w*i;
int y2=by+bheight-w;
g.drawLine(x1,y1,x2,y2);
x1=bx;
y1=by+w*i;
x2=bx+bwidth-w;
y2=by+w*i;
g.drawLine(x1,y1,x2,y2);
}
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
if(map[i][j]==P.White){
g.setColor(Color.WHITE);
g.fillArc(i*w+bx-w/2,j*w+by-w/2,w,w,0,360);
}else if(map[i][j]==P.Black){
g.setColor(Color.BLACK);
g.fillArc(i*w+bx-w/2,j*w+by-w/2,w,w,0,360);
}
}
}
}
public void Clear(){
for(int i=0;i<=16;i++){
for(int j=0;j<=16;j++){
map[i][j]=P.Blank;
}
}
}
//核心部分——算法
public boolean Win(int x,int y){
boolean ok=false;
try{
if(LevelIsOk(x,y)||UpRightIsOk(x,y)||LeftUpIsOk(x,y)||RightUpIsOk(x,y))
ok=true;
System.out.println(ok);
}catch(Exception e){
e.printStackTrace();
}finally{
}
return ok;
}
public boolean LevelIsOk(int x,int y){
int tx=x;
int count=0;
while(tx>=0&&map[tx][y]==player)
tx--;
tx++;
while((tx<=16&&map[tx][y]==player)){
tx++;
count++;
}
if(count==5)
return true;
return false;
}
public boolean UpRightIsOk(int x,int y){
int ty=y;
int count=0;
while(ty>=0&&map[x][ty]==player)
ty--;
ty++;
while(ty<=16&&map[x][ty]==player){
ty++;
count++;
}
if(count==5)
return true;
return false;
}
public boolean LeftUpIsOk(int x,int y){
int tx=x,ty=y;
int count=0;
while(tx>=0&&ty>=0&&map[tx][ty]==player){
tx--;
ty--;
}
tx++;
ty++;
while(tx<=16&&ty<=16&&map[tx][ty]==player){
tx++;
ty++;
count++;
}
if(count==5)
return true;
return false;
}
public boolean RightUpIsOk(int x,int y){
int tx=x,ty=y;
int count=0;
while(tx<=16&&ty>=0&&map[tx][ty]==player){
tx++;
ty--;
}
tx--;
ty++;
while(tx>=0&&ty<=16&&map[tx][ty]==player){
tx--;
ty++;
count++;
}
if(count==5)
return true;
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new playchess();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==start){
JOptionPane.showMessageDialog(null,"开始游戏!");
this.addMouseListener(this);
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getButton()==e.BUTTON1){
try{
System.out.println("点击了一次,准备下棋");
boolean t=false;
int x=e.getX();
int y=e.getY();
x=(x-x%w)+(x%w>w/2?w:0);
y=(y-y%w)+(y%w>w/2?w:0);
x=(x-bx+w)/w;
y=(y-by+w)/w;
if(map[x][y]==P.Black||map[x][y]==P.White){
JOptionPane.showMessageDialog(null,"此处有棋了!");
t=true;
}else{
map[x][y]=player;
}
System.out.println(player);
this.repaint();
if(Win(x,y)){
JOptionPane.showMessageDialog(null,player+"赢了!");
Clear();
this.repaint();
}
if(t==false)
player=(player==P.Black)?P.White:P.Black;
}catch(Exception ag){
ag.printStackTrace();
}finally{
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}