五子棋

经过了左哥今天的第三次课,我的五子棋大概已经做好了。

其实棋盘面板上节课已经做好了,但是忘了写上,今天就一并写上吧。

经过了这几天的课程,我觉得我对面向对象的理解比以前要好多了。

在华信,大家都在上课和写程序,学习氛围很好,过得很充实。加油!奋斗

package wuziqi;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Wuziqi extends JFrame{
	
	private boolean flag = true;
	private int[][] str = new int [10][15];
	
	public static void main(String[] args) {
		Wuziqi jc = new Wuziqi();
		jc.initFrame();
	}
		
	public void initFrame(){
				this.setSize(1000, 700);
				
				JPanel panel = new JPanel(){
					ImageIcon icon = new ImageIcon("image\\backgroundFive.png");
					protected void paintComponent(Graphics g) {
						super.paintComponent(g);
						g.drawImage(icon.getImage(), 0, 0, null);
						for(int i=0;i<15;i++){
							g.drawLine(50+50*i, 100, 50+50*i, 550);
						}
						for(int i=0;i<10;i++){
							g.drawLine(50, 100+50*i, 750, 100+50*i);
						}
					}
				};
					
				panel.setLayout(null);
				
				
				panel.setBackground(Color.gray);
				this.add(panel);
				this.setDefaultCloseOperation(3);
				
				this.setVisible(true);
				Graphics g = panel.getGraphics();
				
				MouseListener mou = new MouseListener(){

			
					public void mouseClicked(MouseEvent e) {
							int x0 = e.getX();
							int y0 = e.getY();
							
						for(int i=0;i<10;i++){
							for(int j=0;j<15;j++){
								int x = 50+50*j;
								int y = 100+50*i;
								
								if(x0>x-25&&x0<x+25&&y0>y-25&&y0<y+25){
									if(str[i][j]==0){
										if(flag){
											str[i][j]=1;
											g.setColor(Color.yellow);
											flag = false;
										}else{
											str[i][j]=2;
											g.setColor(Color.BLACK);
											flag = true;
										}
									}
									g.fillOval(x-25, y-25, 50, 50);	
									if(checkRow(i,j)||checkColumn(i,j)||checkRightXie(i,j)||checkLeftXie(i,j)){
										System.out.println("赢了");
										int value = JOptionPane.showConfirmDialog(null,"赢了");
									}
								}
							}
						}
					}
					public void mouseEntered(MouseEvent e) {
					}
					public void mouseExited(MouseEvent e) {
					}
					public void mousePressed(MouseEvent e) {
					}
					public void mouseReleased(MouseEvent e) {
					}
					
				};
				
				panel.addMouseListener(mou);
		
	}
	public boolean checkRow(int Row,int Column){
		int count =1;
		for(int i=Column+1;i<str[0].length;i++){
			if(str[Row][i]==str[Row][Column]){
				count++;
			}else{
				break;
			}
		}
		for(int i=Column-1;i>=0;i--){
			if(str[Row][i]==str[Row][Column]){
				count++;
			}else{
				break;
			}
		}
		if(count>=5){return true;}
		return false;
	}
	public boolean checkColumn(int Row,int Column){
		int count = 1;
		for(int i=Row+1;i<str.length;i++){
			if(str[i][Column]==str[Row][Column]){
				count++;
			}else{
				break;
			}
		}
		for(int i=Row-1;i>=0;i--){
			if(str[i][Column]==str[Row][Column]){
				count++;
			}else{
				break;
			}
		}
		if(count>=5){return true;}
		return false;
	}
	public boolean checkRightXie(int Row,int Column){
		int count =1;
		for(int i=Row+1, j=Column+1;i<str.length&&j<str[0].length ;i++,j++ ){
			if(str[i][j]==str[Row][Column]){
				count++;
			}else{
				break;
			}
		}
		for(int i=Row-1,j=Column-1;i>=0&&j>=0;i--,j--){
			if(str[i][j]==str[Row][Column]){
				count++;
			}else{
				break;
			}
		}
		if(count >=5){return true;}
		return false;
	}
	
	public boolean checkLeftXie(int Row,int Column){
		int count = 1;
		for(int i=Row+1,j=Column-1;i<str.length&&j>=0;i++,j--){
			if(str[i][j]==str[Row][Column]){
				count++;
			}else{
				break;
			}
		}
		for(int i=Column+1,j=Row-1;i<str[0].length&&j>=0;i++,j--){
			if(str[j][i]==str[Row][Column]){
				count++;
			}else{
				break;
			}
		}
		if(count>=5){return true;}
		return false;
	}

}

你可能感兴趣的:(五子棋)