服务器之家

服务器之家 > 正文

Java编程实现五子棋人人对战代码示例

时间:2021-02-23 11:05     来源/作者:在成为巨擘的路上

利用java,在控制台操作下,编写的五子棋,作为复习二维数组,面向对象等基础知识。w表示白棋,b表示黑棋

Java编程实现五子棋人人对战代码示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import java.util.scanner;
 
public class mainmethod {
 
    public static char[][] c = new char[10][10];
 
    public static void main(string[] args) {
        mainmethod mainmethod = new mainmethod();
        mainmethod.init();
        int i;
        boolean color;
        int[] point = new int[2];
        scanner scanner = new scanner(system.in);
 
        for (i = 1; i < 100; i++) {
            mainmethod.draw();// 绘制棋盘
            system.out.println("请输入坐标,范围为(0,0)到(9,9)");
            int x = scanner.nextint();
            int y = scanner.nextint();
            if (i % 2 == 1)
                color = true;
            else
                color = false;
            point[0] = x;
            point[1] = y;
            if (mainmethod.paint(point, color) == false) {//
                system.out.println("下的位置不合理,请重新开始");
                break;
            }
            if (mainmethod.win() == 1) {
                mainmethod.draw();
                system.out.println("恭喜白棋赢!!");
                break;
            } else if (mainmethod.win() == -1) {
                mainmethod.draw();
                system.out.println("恭喜黑棋赢!!");
                break;
            }
 
        }
 
        if (i == 100)
            system.out.println("平局");
 
    }
 
    public char[][] init() {// 初始化棋盘
        for (int i = 0; i < c.length; i++) {
            for (int j = 0; j < c.length; j++) {
                c[i][j] = '+';
            }
        }
        return c;
    }
 
    public boolean paint(int[] point, boolean colr) {// 判断下的位置是否正确,若正确改变棋盘
        boolean b = true;
        int x = point[0];
        int y = point[1];
 
        if (inboard(point) == false || c[x][y] == 'w' || c[x][y] == 'b' || x < 0) {
            b = false;
            return b;
        }
 
        else if (colr == true) {// 白棋下
            if (c[x][y] == '+') {
                c[x][y] = 'w';
            }
        }
 
        else if (colr == false) {// 黑棋下
            if (c[x][y] == '+') {
                c[x][y] = 'b';
            }
        }
        return b;
    }
 
    public boolean inboard(int[] point) {// 判断point是否在棋盘上
        int x = point[0];
        int y = point[1];
        if (x < 0 || y < 0 || x >= c.length || y >= c.length)
            return false;
        else
            return true;
 
    }
 
    public int win() {// 判断输赢,1表示白的赢,-1黑的赢,0暂无输赢
        int whowin = 0;
 
        int[] point = new int[2];
        outer: while (true) {
            for (int i = 0; i < c.length; i++) {
                for (int j = 0; j < c.length; j++) {
 
                    if (c[i][j] == 'w') {// 遍历数组,找到的第一个白棋
                        point[0] = i;
                        point[1] = j + 4;
 
                        if (inboard(point)) {// 如果存在向右数4个的棋盘
                            if (c[i][j + 1] == 'w' && c[i][j + 2] == 'w' && c[i][j + 3] == 'w' && c[i][j + 4] == 'w') {
                                whowin = 1;
                                break outer;
                            }
                        }
 
                        point[0] = i + 4;
                        point[1] = j + 4;
 
                        if (inboard(point)) {// 如果存在向右,向下数4个的棋盘
                            if (c[i + 1][j + 1] == 'w' && c[i + 2][j + 2] == 'w' && c[i + 3][j + 3] == 'w'
                                    && c[i + 4][j + 4] == 'w') {
                                whowin = 1;
                                break outer;
                            }
                        }
 
                        point[0] = i + 4;
                        point[1] = j;
 
                        if (inboard(point)) {// 如果存在向下数4个的棋盘
                            if (c[i + 1][j] == 'w' && c[i + 2][j] == 'w' && c[i + 3][j] == 'w' && c[i + 4][j] == 'w') {
                                whowin = 1;
                                break outer;
                            }
                        }
 
                        point[0] = i - 4;
                        point[1] = j + 4;
 
                        if (inboard(point)) {// 如果存在向左,向下数4个的棋盘
                            if (c[i - 1][j + 1] == 'w' && c[i - 2][j + 2] == 'w' && c[i - 3][j + 3] == 'w'
                                    && c[i - 4][j + 4] == 'w') {
                                whowin = 1;
                                break outer;
                            }
                        }
 
                    } else if (c[i][j] == 'b') {
                        point[0] = i;
                        point[1] = j + 4;
 
                        if (inboard(point)) {// 如果存在向右数4个的棋盘
                            if (c[i][j + 1] == 'b' && c[i][j + 2] == 'b' && c[i][j + 3] == 'b' && c[i][j + 4] == 'b') {
                                whowin = -1;
                                break outer;
                            }
                        }
 
                        point[0] = i + 4;
                        point[1] = j + 4;
 
                        if (inboard(point)) {// 如果存在向右,向下数4个的棋盘
                            if (c[i + 1][j + 1] == 'b' && c[i + 2][j + 2] == 'b' && c[i + 3][j + 3] == 'b'
                                    && c[i + 4][j + 4] == 'b') {
                                whowin = -1;
                                break outer;
                            }
                        }
 
                        point[0] = i + 4;
                        point[1] = j;
 
                        if (inboard(point)) {// 如果存在向下数4个的棋盘
                            if (c[i + 1][j] == 'b' && c[i + 2][j] == 'b' && c[i + 3][j] == 'b' && c[i + 4][j] == 'b') {
                                whowin = -1;
                                break outer;
                            }
                        }
 
                        point[0] = i - 4;
                        point[1] = j + 4;
 
                        if (inboard(point)) {// 如果存在向左,向下数4个的棋盘
                            if (c[i - 1][j + 1] == 'b' && c[i - 2][j + 2] == 'b' && c[i - 3][j + 3] == 'b'
                                    && c[i - 4][j + 4] == 'b') {
                                whowin = -1;
                                break outer;
                            }
                        }
 
                    }
 
                }
            }
            whowin = 0;
            break outer;
 
        }
        return whowin;
    }
 
    public void draw() {// 绘制棋盘
        for (int i = 0; i < c.length; i++) {
            for (int j = 0; j < c.length; j++) {
                system.out.print(c[i][j] + " ");
            }
            system.out.println();
        }
    }
 
}

运行结果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
请输入坐标,范围为(0,0)到(9,9
4
6
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + w + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
请输入坐标,范围为(0,0)到(9,9
6
5
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + w + + + 
+ + + + + + + + + + 
+ + + + + b + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
请输入坐标,范围为(0,0)到(9,9

谁输谁赢我就不弄出来了,太长,占地。。

总结

以上就是本文关于java编程实现五子棋人人对战代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/qq_24065713/article/details/71509200

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部