四子棋

这是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
	import java.util.*;
public class ConnectFour {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[][] chess = new String[6][7];
int round = 1;
String[] symbol = {"×","○"};

for(int a=0;a<chess.length;a++){
for(int b=0;b<chess[0].length;b++)
chess[a][b] = " ";
}
System.out.println("-------------------------四子棋-------------------------");
System.out.println("兩人玩,每人各據一符號,猜拳贏先選擇您要的位置擺放符號,依序你下完,換我下\n" +
"直到相同符號能夠連成四子一直線即獲勝(縱.橫.斜三相同符號一直線均可,先達成者贏)\n" +
",應盡阻擋對方連成四符號一直線唷!");
System.out.println("--------------------------------------------------------");
System.out.println();

//先畫參考圖
System.out.println("Y座標");
System.out.println(" ↓");
System.out.println(" ┌───┬───┬───┬───┬───┬───┬───┐");
System.out.println(" 6 │ │ │ │ │ │ │ │");
System.out.println(" ├───┼───┼───┼───┼───┼───┼───┤");
System.out.println(" 5 │ │ │ │ │ │ │ │");
System.out.println(" ├───┼───┼───┼───┼───┼───┼───┤");
System.out.println(" 4 │ │ │ │ │ │ │ │");
System.out.println(" ├───┼───┼───┼───┼───┼───┼───┤");
System.out.println(" 3 │ │ │ │ │ │ │ │");
System.out.println(" ├───┼───┼───┼───┼───┼───┼───┤");
System.out.println(" 2 │ │ │ │ │ │ │ │");
System.out.println(" ├───┼───┼───┼───┼───┼───┼───┤");
System.out.println(" 1 │ │ │ │ │ │ │ │");
System.out.println(" └───┴───┴───┴───┴───┴───┴───┘");
System.out.println(" 1 2 3 4 5 6 7 ← X座標");
System.out.println("--------------------------------------------------------");
System.out.println();
while(true){
//座標 (x-1)*1+(?-1)*7 剛好對應到 陣列的元素代碼
System.out.println("請輸入您要擺放的位置");
while(true){
System.out.print("X座標: ");
int keyinX = sc.nextInt();
if(!(keyinX>=1 && keyinX<=7)){
System.out.println("輸入錯誤,重新輸入");
continue;
}

int number = (keyinX-1);
if(!chess[5][number].equals(" ")){
System.out.println("此處已經滿棋了!換一個地方 好么!");
break;
}
int n =0;
while(!chess[n][number].equals(" ")){
n++;
}
if(round % 2 == 0){
chess[n][number] = "×";
}else{
chess[n][number] = "○";
}
break;
}
//重繪圖
System.out.println(" ↓");
System.out.println(" ┌───┬───┬───┬───┬───┬───┬───┐");
System.out.println(" 6 │ "+chess[5][0]+" │ "+chess[5][1]+" │ "+chess[5][2]+" │ "+chess[5][3]+" │ "+chess[5][4]+" │ "+chess[5][5]+" │ "+chess[5][6]+" │ ");
System.out.println(" ├───┼───┼───┼───┼───┼───┼───┤");
System.out.println(" 5 │ "+chess[4][0]+" │ "+chess[4][1]+" │ "+chess[4][2]+" │ "+chess[4][3]+" │ "+chess[4][4]+" │ "+chess[4][5]+" │ "+chess[4][6]+" │ ");
System.out.println(" ├───┼───┼───┼───┼───┼───┼───┤");
System.out.println(" 4 │ "+chess[3][0]+" │ "+chess[3][1]+" │ "+chess[3][2]+" │ "+chess[3][3]+" │ "+chess[3][4]+" │ "+chess[3][5]+" │ "+chess[3][6]+" │ ");
System.out.println(" ├───┼───┼───┼───┼───┼───┼───┤");
System.out.println(" 3 │ "+chess[2][0]+" │ "+chess[2][1]+" │ "+chess[2][2]+" │ "+chess[2][3]+" │ "+chess[2][4]+" │ "+chess[2][5]+" │ "+chess[2][6]+" │ ");
System.out.println(" ├───┼───┼───┼───┼───┼───┼───┤");
System.out.println(" 2 │ "+chess[1][0]+" │ "+chess[1][1]+" │ "+chess[1][2]+" │ "+chess[1][3]+" │ "+chess[1][4]+" │ "+chess[1][5]+" │ "+chess[1][6]+" │ ");
System.out.println(" ├───┼───┼───┼───┼───┼───┼───┤");
System.out.println(" 1 │ "+chess[0][0]+" │ "+chess[0][1]+" │ "+chess[0][2]+" │ "+chess[0][3]+" │ "+chess[0][4]+" │ "+chess[0][5]+" │ "+chess[0][6]+" │ ");
System.out.println(" └───┴───┴───┴───┴───┴───┴───┘");
System.out.println(" 1 2 3 4 5 6 7 ← X座標");

//判斷輸贏
boolean check = false;
//橫直線
for(int n1=0;n1<chess.length;n1++){
for(int n2=0;n2<4;n2++)
if(chess[n1][0+n2].equals(chess[n1][1+n2])&&chess[n1][0+n2].equals(chess[n1][2+n2])&&chess[n1][0+n2].equals(chess[n1][3+n2])&& !(chess[n1][0+n2].equals(" ")))
check = true;
}
//豎直線
for(int n1=0;n1<chess[0].length;n1++){
for(int n2=0;n2<3;n2++)
if(chess[0+n2][n1].equals(chess[1+n2][n1])&&chess[0+n2][n1].equals(chess[2+n2][n1])&&chess[0+n2][n1].equals(chess[3+n2][n1])&& !(chess[0+n2][n1].equals(" ")))
check = true;
}
//右上斜直線
for(int n1=0;n1<3;n1++){
for(int n2=0;n2<4;n2++)
if(chess[0+n1][0+n2].equals(chess[1+n1][1+n2])&&chess[0+n1][0+n2].equals(chess[2+n1][2+n2])&&chess[0+n1][0+n2].equals(chess[3+n1][3+n2])&& !(chess[0+n1][0+n2].equals(" ")))
check = true;
}
//右下斜直線
for(int n1=3;n1<chess.length;n1++){
for(int n2=0;n2<3;n2++)
if(chess[0+n1][0+n2].equals(chess[n1-1][n2+1])&&chess[0+n1][0+n2].equals(chess[n1-2][2+n2])&&chess[0+n1][0+n2].equals(chess[n1-3][3+n2])&& !(chess[0+n1][0+n2].equals(" ")))
check = true;
}
if(check == true){
System.out.print("Congratulation!!!㊗️玩家 "+symbol[round % 2]); //判斷為當局下棋者得勝
System.out.println(" 獲勝");
break;
}
//預測輸贏
boolean check1 = false;
//橫直線
for(int n1=0;n1<chess.length;n1++){
for(int n2=0;n2<5;n2++)
if(chess[n1][0+n2].equals(chess[n1][1+n2])&&chess[n1][0+n2].equals(chess[n1][2+n2])&& !(chess[n1][0+n2].equals(" ")))
check1 = true;
}
//豎直線
for(int n1=0;n1<chess[0].length;n1++){
for(int n2=0;n2<4;n2++)
if(chess[0+n2][n1].equals(chess[1+n2][n1])&&chess[0+n2][n1].equals(chess[2+n2][n1])&& !(chess[0+n2][n1].equals(" ")))
check1 = true;
}
//右上斜直線
for(int n1=0;n1<4;n1++){
for(int n2=0;n2<5;n2++)
if(chess[0+n1][0+n2].equals(chess[1+n1][1+n2])&&chess[0+n1][0+n2].equals(chess[2+n1][2+n2])&& !(chess[0+n1][0+n2].equals(" ")))
check1 = true;
}
//右下斜直線
for(int n1=2;n1<chess.length;n1++){
for(int n2=0;n2<4;n2++)
if(chess[0+n1][0+n2].equals(chess[n1-1][n2+1])&&chess[0+n1][0+n2].equals(chess[n1-2][2+n2])&& !(chess[0+n1][0+n2].equals(" ")))
check1 = true;
}
if(check1 == true){
System.out.print("注意⚠️!!!!!玩家 "+symbol[round % 2]); //判斷為當局下棋者即將得勝
System.out.println(" 即將獲勝");

}
//判斷平手,擺此處是為了最後也能顯示圖...否則習慣往後擺放
int count = 0; //計數目前場上的棋子數,如滿42棋 沒勝利 則平手..
for(int a=0;a<chess.length;a++){
for(int b=0;b<chess[0].length;b++) {
if(!(chess[a][b].equals(" "))){
count++;
}
}
}
if(count==42){
System.out.println("平手");
break;
}

System.out.println("--------------------------------------------------------");
System.out.println();
round++;//準備到下一回合

}
}
}

示例图如下:

ConnectFour.png

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×