三子相连的游戏

      3*3的棋盘,三个子在一条线者胜,或一竖线或一横线或斜线。

      算法当然是超级简单的:

      两方间隔着下子,有子的格不能下,每下一个子,就检查是否有3子在一线上的格局,来判断胜负。

     

运行结果:

代码:

  1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4
5 public class Game
6 {
7 public static void main(String[] args)
8 {
9 new NoughtsAndCrosses();
10 }
11 }
12
13 class NoughtsAndCrosses extends JFrame implements ActionListener
14 {
15 private boolean gameOver; // to keep track of the game status
16 private String player; // to keep track of the current player
17 private JPanel board = new JPanel(); // to hold the nine cells
18 private JButton [][]cell= new JButton [3][3]; // the cells
19 // these next label provides a border around the cells
20 private JLabel blank = new JLabel(" ");
21 // the next two labels are centre alligned
22 private JLabel error = new JLabel(" ", JLabel.CENTER);
23 private JLabel info = new JLabel ("player X to start", JLabel.CENTER);
24
25 // the constructor
26 public NoughtsAndCrosses()
27 {
28 gameOver = false;
29 player = "X"; // player X to start the game
30 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
31 setTitle("三字一线游戏");
32 setSize(250, 250);
33 setLocation(300, 100);
34 getContentPane().setBackground(Color.yellow);
35 board.setLayout(new GridLayout(3,3)); // discussed later
36 // creates and adds nine buttons to the board
37 for (int r = 0; r<3; r++)
38 {
39 for (int c=0; c<3; c++)
40 {
41 cell[r][c] = new JButton();//申请一个按钮
42 cell[r][c].addActionListener(this);//鼠标监听
43 board.add(cell[r][c]);//将按钮添加到面板里去
44 }
45 }
46 // positions the items on the screen
47 add("Center", board);
48 add ("West", blank);
49 add("East", blank);
50 add("North", info);
51 add("South", error);
52 setVisible(true);
53 }
54
55 public void actionPerformed(ActionEvent e)
56 {
57 if (!gameOver)// process mouse click only if game is not over
58 {
59 for (int r = 0; r<3; r++)
60 for (int c=0; c<3; c++)
61 if (e.getSource()== cell[r][c])
62 processEvent(r,c); // call helper method to process event
63 }
64 }
65
66 // helper method to process a given button press
67 private void processEvent(int rowIn, int colIn)
68 {
69 // check no attempt made to move into an occupied cell
70 if (cell[rowIn][colIn].getText().equals("X") ||
71 cell[rowIn][colIn].getText().equals("O"))
72 {
73 error.setText("This cell already taken!!");
74 }
75 else
76 {
77 // clear any error messages
78 error.setText(" ");
79 // change button caption to current player
80 cell[rowIn][colIn].setText(player);
81 // check whether this moves results in game over
82 if (hasWon(rowIn, colIn))// process game over
83 {
84 info.setText(" player " + player + " has won!");
85 gameOver = true;
86 }
87 else // process game not over
88 {
89 // change player
90 if (player.equals("X"))
91 player = "O";
92 else
93 player = "X";
94
95 info.setText("player "+player+" to go next");
96 }
97 }
98 }
99
100 // helper method to check if game over
101 private boolean hasWon(int rowIn, int colIn)
102 {
103 boolean won;
104 // check current row
105 won = true;
106 for(int c = 0; c <3; c++)
107 if (!cell[rowIn][c].getText().equals(player))
108 won = false;
109
110 if (!won)
111 {
112 // check current column
113 won = true;
114 for(int r = 0; r <3; r++)
115 if (!cell[r][colIn].getText().equals(player))
116 won = false;
117 }
118 if (!won)
119 {
120 // check left diagonal
121 won = true;
122 for(int num = 0; num<3; num++)
123 if (!cell[num][num].getText().equals(player))
124 won = false;
125 }
126 if (!won)
127 {
128 // check right diagonal
129 won = true;
130 for(int c = 0; c<3; c++)
131 if (!cell[2-c][c].getText().equals(player))
132 won = false;
133 }
134 return won;
135 }
136 }
原文地址:https://www.cnblogs.com/HpuAcmer/p/2390712.html