进击的五子棋第二季

  1 package com.lovo;
  2 
  3 import java.awt.BasicStroke;
  4 import java.awt.Color;
  5 import java.awt.Graphics;
  6 import java.awt.Graphics2D;
  7 import java.awt.Stroke;
  8 
  9 import org.w3c.dom.css.Counter;
 10 
 11 public class Board {
 12     private int currentColor;
 13     private int[][] b = new int[15][15];
 14 
 15     /**
 16      * 画棋盘
 17      * 
 18      * @param g
 19      */
 20     public void draw(Graphics g) {
 21         g.setColor(Color.BLACK);
 22         Graphics2D g2d = (Graphics2D) g; // 用Graphics2D可以调粗细
 23         Stroke oldsStroke = g2d.getStroke();// 保存现场
 24         g2d.setStroke(new BasicStroke(3)); // 设置画笔粗细
 25         g.drawRect(50, 50, 700, 700); // 矩形框
 26         g2d.setStroke(oldsStroke); // 回复现场
 27         for (int i = 0; i < 13; i++) {
 28             g.drawLine(50, 100 + i * 50, 750, 100 + i * 50);
 29             g.drawLine(100 + i * 50, 50, 100 + i * 50, 750);
 30         }
 31         // 画天元和三三点
 32         g.fillOval(390, 390, 20, 20);// 实心椭圆
 33         g.fillOval(195, 195, 10, 10);
 34         g.fillOval(595, 195, 10, 10);
 35         g.fillOval(595, 595, 10, 10);
 36         g.fillOval(195, 595, 10, 10);
 37         for (int i = 0; i < b.length; i++) {// 行的循环,控制纵坐标
 38             for (int j = 0; j < b[i].length; j++) {// 列的循环,控制横坐标
 39                 if (b[i][j] != 0) {
 40                     g.setColor(b[i][j] == 1 ? Color.BLACK : Color.WHITE);
 41                     g.fillOval(25 + 50 * j, 25 + 50 * i, 50, 50);
 42                 }
 43             }
 44         }
 45     }
 46     /**
 47      * 走棋
 48      * @param row 行
 49      * @param col 列
 50      * @param isBlack 是否是黑棋
 51      * @return    boolean
 52      */
 53     public boolean playChess(int row, int col, boolean isBlack) {
 54         if (b[row][col] == 0) {
 55             b[row][col] = isBlack ? 1 : 2;
 56             return true;
 57         }
 58         return false;
 59     }
 60     /**
 61      * 判断是否决出胜负
 62      * @param row 行
 63      * @param col 列
 64      * @param isBlack 是否是黑棋
 65      * @return boolean
 66      */
 67     public boolean judge(int row, int col, boolean isBlack) {
 68         currentColor = isBlack ? 1 : 2;
 69         return compareVer(row, col, currentColor) == 5 || compareHor(row, col, currentColor) == 5||
 70                 compareX1(row, col, currentColor) == 5|| compareX2(row, col, currentColor) == 5;
 71     }
 72     /**
 73      * 判断垂直方向上是否有五子相连
 74      * @param row 行
 75      * @param col 列
 76      * @param currentColor 当前棋子的颜色
 77      * @return    相连的棋子数
 78      */
 79     private int compareVer(int row,int col, int currentColor) {
 80         int counter = 1;
 81         int tempRow = row;
 82         while (tempRow - 1 >= 0 && b[tempRow - 1][col] == currentColor) {
 83             tempRow--;
 84             counter++;
 85         }
 86         tempRow = row;
 87         while (tempRow + 1 <= b.length && b[tempRow + 1][col] == currentColor) {
 88             tempRow++;
 89             counter++;
 90         }
 91         return counter;
 92     }
 93     /**
 94      * 判断水平方向上是否有五子相连
 95      * @param row 行
 96      * @param col 列
 97      * @param currentColor 当前棋子的颜色
 98      * @return    相连的棋子数
 99      */    
100     private int compareHor(int row,int col, int currentColor) {
101         int counter = 1;
102         int tempCol = col;
103         while (tempCol - 1 >= 0 && b[row][tempCol - 1] == currentColor) {
104             tempCol--;
105             counter++;
106         }
107         tempCol = col;
108         while (tempCol + 1 <= b[row].length && b[row][tempCol + 1] == currentColor) {
109             tempCol++;
110             counter++;
111         }
112         return counter;
113     }
114     /**
115      * 判断对角线方向"\"上是否有五子相连
116      * @param row 行
117      * @param col 列
118      * @param currentColor 当前棋子的颜色
119      * @return    相连的棋子数
120      */    
121     private int compareX1(int row,int col, int currentColor) {
122         int counter = 1;
123         int tempRow = row;
124         int tempCol = col;
125         while (tempRow - 1 >= 0 && tempCol - 1 >= 0 && b[tempRow - 1][tempCol - 1] == currentColor) {
126             tempCol--;
127             tempRow--;
128             counter++;
129         }
130         tempRow = row;
131         tempCol = col;
132         while (tempRow + 1 <= b.length && tempCol + 1 <= b.length && b[tempRow + 1][tempCol + 1] == currentColor) {
133             tempCol++;
134             tempRow++;
135             counter++;
136         }
137         return counter;
138     }
139     /**
140      * 判断对角线方向"/"上是否有五子相连
141      * @param row 行
142      * @param col 列
143      * @param currentColor 当前棋子的颜色
144      * @return    相连的棋子数
145      */    
146     private int compareX2(int row,int col, int currentColor) {
147         int counter = 1;
148         int tempRow = row;
149         int tempCol = col;
150         while (tempRow - 1 >= 0 && tempCol + 1 <= b[tempRow].length && b[tempRow - 1][tempCol + 1] == currentColor) {
151             tempCol++;
152             tempRow--;
153             counter++;
154         }
155         tempRow = row;
156         tempCol = col;
157         while (tempRow + 1 <= b.length && tempCol - 1 >= 0 && b[tempRow + 1][tempCol - 1] == currentColor) {
158             tempCol--;
159             tempRow++;
160             counter++;
161         }
162         return counter;
163     }
164     // public void makeAMove() {
165     // int row = (int) (Math.random() * 15);
166     // int col = (int) (Math.random() * 15);
167     // if (b[row][col] == 0) {
168     // b[row][col] = blackTurn ? 1 : 2;//0表示没有走棋,1表示黑棋,2表示白棋
169     // blackTurn = !blackTurn; //交换走棋
170     // }
171     // }
172 }
 1 package com.lovo;
 2 
 3 import java.awt.Color;
 4 import java.awt.Font;
 5 import java.awt.Graphics;
 6 import java.awt.Image;
 7 import java.awt.event.ActionEvent;
 8 import java.awt.event.ActionListener;
 9 import java.awt.event.KeyAdapter;
10 import java.awt.event.KeyEvent;
11 import java.awt.event.KeyListener;
12 import java.awt.event.MouseAdapter;
13 import java.awt.event.MouseEvent;
14 import java.awt.image.BufferedImage;
15 
16 import javax.swing.JFrame;
17 import javax.swing.Timer;
18 
19 public class MyFrame extends JFrame{
20     private static final long serialVersionUID = -3544738451252364271L;
21     private boolean isBlackTurn = true;
22     private Board board = new Board();
23     private boolean isContinue = true;
24     //在内存中创建一张图
25     private Image offImage = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
26     public MyFrame(){
27         this.setTitle("五子棋");
28         this.setSize(800, 800);
29         this.setResizable(false);
30         this.getContentPane().setBackground(new Color(197, 194, 129));//设置窗口背景
31         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
32         this.setLocationRelativeTo(null);    //窗口居中
33         this.addKeyListener(new KeyAdapter() {//添加一个键盘的监听,按F2重置游戏
34             @Override
35             public void keyPressed(KeyEvent e) {
36                 if (e.getKeyCode() == 113) {
37                     board = new Board();//新建一个棋盘
38                     isContinue =true;    
39                     repaint();
40                 }
41             }
42             
43         });
44         this.addMouseListener(new MouseAdapter() {//添加一个鼠标的监听
45 
46             @Override
47             public void mousePressed(MouseEvent e) {
48                 int x = e.getX();
49                 int y = e.getY();
50                 if (isContinue) {//判断是否继续游戏
51                     if (x >= 50 && x <= 750 && y >=50 && y <= 750) {
52                         int row = Math.round((y - 50) / 50.0F);
53                         int col = Math.round((x - 50) / 50.0F);
54                         //走棋,返回true走棋成功,返回false走棋失败(如走棋的地方已经有棋了)
55                         if (board.playChess(row, col, isBlackTurn)) {
56                             repaint();
57                             //判断是否决出胜负
58                             if (board.judge(row, col, isBlackTurn)) {
59                                 isContinue = false;                        //游戏结束
60                             }else {
61                                 isBlackTurn = !isBlackTurn;
62                             }
63                         }
64                     }
65                 }
66                 
67             }
68             
69         });
70     }
71 
72     @Override
73     public void paint(Graphics g) {//操作系统调用该方法绘图
74         Graphics newG = offImage.getGraphics();//得到内存中图片的画笔
75         super.paint(newG);                //有才能设置背景,在内存中画出窗口
76         board.draw(newG);                //在内存中画出棋盘
77         if (!isContinue) {
78             newG.setFont(new Font("微软雅黑", Font.BOLD, 100));
79             if (isBlackTurn) {
80                 newG.setColor(new Color(62, 62, 62));
81                 newG.drawString("黑方胜利", 200, 400);
82             }else {
83                 newG.setColor(new Color(222, 222, 222));
84                 newG.drawString("白方胜利", 200, 400);
85             }
86             newG.setFont(new Font("微软雅黑", Font.BOLD, 20));
87             newG.setColor(new Color(90, 90, 255));
88             newG.drawString("按F2键重置棋盘", 300, 490);
89         }
90         g.drawImage(offImage, 0, 0, 800, 800,null);//把内存里的棋盘放到屏幕上
91     }
92 }
1 package com.lovo;
2 
3 public class GameRun {
4     public static void main(String[] args) {
5         new MyFrame().setVisible(true);
6     }
7 }

 

posted @ 2014-10-31 20:25  FJ凉风  阅读(177)  评论(0编辑  收藏  举报