JAVA(五子棋)

五子棋

主方法

	public static void main(String[] args) {
		new FiveGame();
	}	

FiveGame

	public FiveGame(){
		this.setTitle("五子棋1.0");
		this.setSize(500,500);
		this.setLocation((width - 500) / 2 , (height - 500) / 2 );
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);  //设置窗口不可改变,固定窗口大小
		this.setVisible(true);
		
		this.repaint();  //java里repaint()是重绘component的方法;
		this.addMouseListener(this);
 
		
	}

画棋盘

	public void paint(Graphics g){
		BufferedImage buf = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
		Graphics g1 =  buf.createGraphics();  // 创建画笔
		g1.setColor(new Color(0,169,158));
		g1.fill3DRect(43, 60, 375, 375, true);
			for (int i = 0; i <= 15; i++) {
				g1.setColor(Color.WHITE);
				g1.drawLine(43, 60+i*25, 375+43, 60+i*25);  //画棋盘横线
				g1.drawLine(43+i*25, 60, 43+i*25, 375+60);  //画棋盘竖线
			}	        
		        for(int i=0; i<15; i++){
				for (int j = 0; j < 15; j++) {
					//画实心黑子
					if(allChess[i][j] == 1){    
						int tempX = i*25+47;
						int tempY = j*25+64;
						g1.setColor(Color.BLACK);
						g1.fillOval(tempX, tempY, 16, 16);
						g1.setColor(Color.BLACK);
						g1.drawOval(tempX, tempY, 16, 16);
					}
					//画实心白子
					if(allChess[i][j] == 2){
						int tempX = i*25+47;
						int tempY = j*25+64;
						g1.setColor(Color.WHITE);
						g1.fillOval(tempX, tempY, 16, 16);
						g1.setColor(Color.WHITE);
						g1.drawOval(tempX, tempY, 16, 16);
					}
				}
			}			
			 g.drawImage(buf, 0, 0,this);	
	}

鼠标监听器

	public void mousePressed(MouseEvent e){
		if(canPlay){
			x=e.getX();
			y=e.getY();  // 用来获取鼠标坐标
			if(x>55 && x<= 405  && y>=72 && y<=420){
				//让鼠标在棋盘范围内
				if((x-55)%25>12){
					x=(x-55)/25 + 1;
				}else {
					x = (x-55)/25;
				}
				if((y-72)%25>12){
					y=(y-72)/25 + 1;
				}else {
					y=(y-72)/25;
				}	
				//落子
				if(allChess[x][y] == 0){
					chessX[countX++] = x;
					chessY[countY++] = y;
					if(isblack){
						allChess[x][y] = 1;
						isblack = false;
					}else {
						allChess[x][y] = 2;
						isblack = true;			
					}
					this.repaint();		
					if(this.isWin()){
						if(allChess[x][y] == 1){
							JOptionPane.showMessageDialog(this, "游戏结束,黑方胜利");
						}else {
							JOptionPane.showMessageDialog(this, "游戏结束,白方胜利");
						}
						this.canPlay = false;  //表示游戏结束
					}			
				}
			}
		}		
	}

判断方向

	public boolean isWin(){
		boolean flag = false;
		int count = 1;  //用来保存共有相同颜色多少棋子相连,初始值为1
		int color = allChess[x][y];  //color = 1 (黑子) color = 2(白子)
		
		//判断横向是否有5个棋子相连,特点:纵坐标是相同,即allChess[x][y] 中y值是相同
		count = this.checkCount(1,0,color);
		if(count >= 5){
			flag = true;
		}else {
			//判断纵向
			count = this.checkCount(0,1,color);
			if(count >= 5){
				flag = true;
			}else {
				 //判断右上,左下
				count = this.checkCount(1,-1,color);
				if(count >= 5){
					flag = true;
				}else {
					//判断右下,左上
					count = this.checkCount(1,1,color);
					if(count >= 5){
						flag =  true;
					}
				}
			}
		}
		
		return flag;
	}

判断一个五子连线

	public int checkCount(int xChange , int yChenge ,int color){
		int count = 1;
		int tempX = xChange;
		int tempy = yChenge;  //保存初始值	
		//全局变量x,y最初为鼠标点击的坐标,
		//经下棋方法已经x,y的将范围变成0-15(遍历整个棋盘,寻找相同颜色的棋子)
		while(x + xChange >=0 && x+xChange <15  && y+yChenge >=0 && y+yChenge < 15 && color == allChess[x+xChange][y+yChenge]){
			count++;
			if(xChange != 0)  xChange++;    
			if(yChenge != 0 ){      
				if(yChenge != 0){
					if(yChenge > 0) {   
						yChenge++;		
					}else {
						yChenge--;		
					}
				}
			}			
		}		
		xChange = tempX;
		yChenge = tempy;   // 恢复初始值				
		while(x-xChange >=0 && x-xChange <15 && y-yChenge >=0 &&
				y-yChenge <15 && color == allChess[x-xChange][y-yChenge]){		
			count++;
			if(xChange != 0){
				xChange++;
			}
			if(yChenge != 0){
				if (yChenge > 0) {
					yChenge++;			
				}else {
					yChenge--;			
				}
			}
		}		
		return count;
	}		
package 模拟练习.五子棋;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
public  class FiveGame extends JFrame implements MouseListener{
	int width = Toolkit.getDefaultToolkit().getScreenSize().width;
	int height = Toolkit.getDefaultToolkit().getScreenSize().height;
	int x,y;  // 定义鼠标的坐标
	int[][] allChess = new int[15][15];   // 用数组来保存棋子,0表示无子,1表示黑子,2表示白子
	boolean isblack = true;   //用来表示黑子还是白子, true表示黑子   false表示白子
	boolean canPlay = true;   // 用来表示当前游戏是否结束
	
	int[] chessX = new int[255];
	int[] chessY = new int[255];
	int countX,countY;
        
	public FiveGame(){
		this.setTitle("五子棋1.0");
		this.setSize(500,500);
		this.setLocation((width - 500) / 2 , (height - 500) / 2 );
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);  //设置窗口不可改变,固定窗口大小
		this.setVisible(true);
		
		this.repaint();  //java里repaint()是重绘component的方法;
		this.addMouseListener(this);		
	}
	public void paint(Graphics g){
		BufferedImage buf = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
		Graphics g1 =  buf.createGraphics();  // 创建画笔
		g1.setColor(new Color(135,206,250));
		g1.fill3DRect(43, 60, 375, 375, true);
			for (int i = 0; i <= 15; i++) {
				g1.setColor(Color.WHITE);
				g1.drawLine(43, 60+i*25, 375+43, 60+i*25);  //画棋盘横线
				g1.drawLine(43+i*25, 60, 43+i*25, 375+60);  //画棋盘竖线
			}	        
		        for(int i=0; i<15; i++){
				for (int j = 0; j < 15; j++) {
					//画实心黑子
					if(allChess[i][j] == 1){    
						int tempX = i*25+47;
						int tempY = j*25+64;
						g1.setColor(Color.BLACK);
						g1.fillOval(tempX, tempY, 16, 16);
						g1.setColor(Color.BLACK);
						g1.drawOval(tempX, tempY, 16, 16);
					}
					//画实心白子
					if(allChess[i][j] == 2){
						int tempX = i*25+47;
						int tempY = j*25+64;
						g1.setColor(Color.WHITE);
						g1.fillOval(tempX, tempY, 16, 16);
						g1.setColor(Color.WHITE);
						g1.drawOval(tempX, tempY, 16, 16);
					}
				}
			}			
			 g.drawImage(buf, 0, 0,this);	
	}
	public void mousePressed(MouseEvent e){
		if(canPlay){
			x=e.getX();
			y=e.getY();  // 用来获取鼠标坐标
			if(x>55 && x<= 405  && y>=72 && y<=420){
				//让鼠标在棋盘范围内
				if((x-55)%25>12){
					x=(x-55)/25 + 1;
				}else {
					x = (x-55)/25;
				}
				if((y-72)%25>12){
					y=(y-72)/25 + 1;
				}else {
					y=(y-72)/25;
				}	
				//落子
				if(allChess[x][y] == 0){
					chessX[countX++] = x;
					chessY[countY++] = y;
					if(isblack){
						allChess[x][y] = 1;
						isblack = false;
					}else {
						allChess[x][y] = 2;
						isblack = true;			
					}
					this.repaint();		
					if(this.isWin()){
						if(allChess[x][y] == 1){
							JOptionPane.showMessageDialog(this, "游戏结束,黑方胜利");
						}else {
							JOptionPane.showMessageDialog(this, "游戏结束,白方胜利");
						}
						this.canPlay = false;  //表示游戏结束
					}			
				}
			}
		}		
	}
	public boolean isWin(){
		boolean flag = false;
		int count = 1;  //用来保存共有相同颜色多少棋子相连,初始值为1
		int color = allChess[x][y];  //color = 1 (黑子) color = 2(白子)
		
		//判断横向是否有5个棋子相连,特点:纵坐标是相同,即allChess[x][y] 中y值是相同
		count = this.checkCount(1,0,color);
		if(count >= 5){
			flag = true;
		}else {
			//判断纵向
			count = this.checkCount(0,1,color);
			if(count >= 5){
				flag = true;
			}else {
				 //判断右上,左下
				count = this.checkCount(1,-1,color);
				if(count >= 5){
					flag = true;
				}else {
					//判断右下,左上
					count = this.checkCount(1,1,color);
					if(count >= 5){
						flag =  true;
					}
				}
			}
		}
		
		return flag;
	}
	public int checkCount(int xChange , int yChenge ,int color){
		int count = 1;
		int tempX = xChange;
		int tempy = yChenge;  //保存初始值	
		//全局变量x,y最初为鼠标点击的坐标,
		//经下棋方法已经x,y的将范围变成0-15(遍历整个棋盘,寻找相同颜色的棋子)
		while(x + xChange >=0 && x+xChange <15  && y+yChenge >=0 && y+yChenge < 15 && color == allChess[x+xChange][y+yChenge]){
			count++;
			if(xChange != 0)  xChange++;    
			if(yChenge != 0 ){      
				if(yChenge != 0){
					if(yChenge > 0) {   
						yChenge++;		
					}else {
						yChenge--;		
					}
				}
			}			
		}		
		xChange = tempX;
		yChenge = tempy;   // 恢复初始值				
		while(x-xChange >=0 && x-xChange <15 && y-yChenge >=0 &&
				y-yChenge <15 && color == allChess[x-xChange][y-yChenge]){		
			count++;
			if(xChange != 0){
				xChange++;
			}
			if(yChenge != 0){
				if (yChenge > 0) {
					yChenge++;			
				}else {
					yChenge--;			
				}
			}
		}		
		return count;
	}		
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
	}
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
	}
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub		
	}	
	public static void main(String[] args) {
		new FiveGame();
	}	
}

你可能感兴趣的:(Java)