五子棋
来源:互联网 发布:java调用方法返回数组 编辑:程序博客网 时间:2024/04/29 20:50
经过了左哥今天的第三次课,我的五子棋大概已经做好了。
其实棋盘面板上节课已经做好了,但是忘了写上,今天就一并写上吧。
经过了这几天的课程,我觉得我对面向对象的理解比以前要好多了。
在华信,大家都在上课和写程序,学习氛围很好,过得很充实。加油!
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;}}
2 0
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- 五子棋
- opencv中Mat、CvMat和IplImage的相互转化
- jquery学习2
- Winload.efi Error Code 0xc0000225
- Android手机靠近蓝牙模块自动连接蓝牙模块,断后自动重连。也可以手机靠近手机自动连接。连接成功率百分之百,可后台运行
- 1004.成绩排名
- 五子棋
- (python)下载喜马拉雅电台的音频
- 编写代码的151个建议
- Openstack在centos上的配置--(一)前期环境准备
- hdu 4614 线段树+二分
- Hibernate反向工程可能存在的问题
- 在线编程及其学习平台
- Tomcat6.0的安装与配置
- 安装系统出现Winload.exe错误0xc000000e解决方法