本文项目为大家分享了Java实现无界面五子棋的具体代码,供大家参考,具体内容如下
项目介绍:
本次设计是基于知识点Java类和对象以及数组开发的一个小型五子棋游戏程序。游戏开始时,选择黑棋、白棋开局,将一枚棋子落在棋盘一坐标上,然后轮番落子,如此轮流下子,直到某一方首先在棋盘的竖、横或两斜四方向上的五子连成线,则该方该局获胜。
项目实现思路:
1、棋盘设计为10*10格,棋盘类型Chess[][] 二维数组,所含属性String chessType; 棋盘首先chessType值是”+”。
2、初始化二维数组
3、玩家选择黑白圈后,开始下棋。输入要下棋子的行列坐标,黑白棋子轮流落子,当一方连成五子或下满棋盘时,游戏结束(连成五子的一方获胜,下满棋盘为和棋)。
4、每一次落子成功后,马上判断以该位置为中心的八个方向:上、下、左、右、左上、左下、右上、右下是否有相同颜色的棋子连成五子,如果连成五子,则游戏结束,输出相应的信息。
5、当游戏一方胜利后显示胜利信息。从程序表面看,这是一个二维平面图,所以数据用二维数组来表示,数组两个下标可以表示棋盘上的位置,数组元素的值代表棋格上的状态,共有三种情况,分别是,o代表白棋,●代表黑棋,+代表格子。
源代码
1.棋子
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
|
/** * @author hudongsheng * @date 2020/10/29 - 9:28 */ public class ChessType { private String chessType; private int x; private int y; public int getX() { return x; } public void setX( int x) { this .x = x; } public int getY() { return y; } public void setY( int y) { this .y = y; } public ChessType() { } public String getChessType() { return chessType; } public void setChessType(String chessType) { this .chessType = chessType; } } |
2.下棋
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
|
/** * @author hudongsheng * @date 2020/10/29 - 9:27 */ public class Gobang { private int size = 1 ; private ChessType[][] chessTypes; private int row; private int colum; private int x; private int y; //创建一个棋盘 public Gobang( int row, int colum){ this .row = row; this .colum = colum; chessTypes = new ChessType[row][colum]; } //初始化棋盘 public void initChessType(){ for ( int i = 0 ; i< chessTypes.length; i++){ for ( int j = 0 ; j< chessTypes[i].length; j++){ chessTypes[i][j] = new ChessType(); chessTypes[i][j].setChessType( "+" ); } } } //下白棋 public void setWhiteChess( int x, int y){ chessTypes[x][y].setChessType( "o" ); } //下黑棋 public void setBlackChess( int x, int y){ chessTypes[x][y].setChessType( "●" ); } //判断是否胜利 public boolean checkWin( int i, int j) { // TODO Auto-generated method stub boolean flag = false ; //判断纵向是否有五个棋子是相同的颜色 int count1 = 1 ; //相同颜色棋子的个数 String color = chessTypes[i][j].getChessType(); //刚下的棋子的颜色 int a = 1 ; //棋子索引的增量 while ((i+a)<row && color == chessTypes[i+a][j].getChessType()){ count1++; a++; } a = 1 ; while ((i-a)>= 0 && color == chessTypes[i-a][j].getChessType()){ count1++; a++; } if (count1 >= 5 ){ flag = true ; } //判断纵向是否有五个棋子是相同的颜色 int count2 = 1 ; a = 1 ; while ((j+a)<colum && color == chessTypes[i][j+a].getChessType()){ count2++; a++; } a = 1 ; while ((j-a)>= 0 && color == chessTypes[i][j-a].getChessType()){ count2++; a++; } if (count2 >= 5 ){ flag = true ; } //右上 左下 是否有五个棋子是相同的颜色 int count3 = 1 ; a = 1 ; while ((i+a)<row && (j-a)>= 0 && color == chessTypes[i+a][j-a].getChessType()){ count3++; a++; } a = 1 ; while ((i-a)>= 0 && (j+a)<colum && color == chessTypes[i-a][j+a].getChessType()){ count3++; a++; } if (count3 >= 5 ){ flag = true ; } //左上 右下 是否有五个棋子是相同的颜色 int count4 = 1 ; a = 1 ; while ((i-a)> 0 && (j-a)>= 0 && color == chessTypes[i-a][j-a].getChessType()){ count4++; a++; } a = 1 ; while ((i+a)<row && (j+a)<colum && color == chessTypes[i+a][j+a].getChessType()){ count4++; a++; } if (count4 >= 5 ){ flag = true ; } return flag; } //落子后打印棋盘 public void print(){ for ( int i = 0 ; i< chessTypes.length; i++){ for ( int j = 0 ; j< chessTypes[i].length; j++){ System.out.print(chessTypes[i][j].getChessType()); } System.out.println(); } } } |
3.测试
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
|
** * @author hudongsheng * @date 2020 / 10 / 29 - 9 : 27 */ public class Test { public static void main(String[] args) { boolean flag = true ; int x; int y; Gobang gobang = new Gobang( 10 , 10 ); Scanner scanner = new Scanner(System.in); gobang.initChessType(); //下棋 System.out.println( "黑棋执先" ); while ( true ){ gobang.print(); System.out.println( "请输入下黑棋的坐标:" ); x = scanner.nextInt(); y = scanner.nextInt(); gobang.setBlackChess(x,y); if (gobang.checkWin(x,y)){ gobang.print(); System.out.println( "黑棋胜!" ); break ; } gobang.print(); System.out.println( "请输入下白棋的坐标:" ); x = scanner.nextInt(); y = scanner.nextInt(); gobang.setWhiteChess(x,y); if (gobang.checkWin(x,y)){ gobang.print(); System.out.println( "白棋胜!" ); break ; } } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_43722914/article/details/109574987