用JAVA寫一個雙人羽球(站位,發球權,計分)的規則

前言

最近參加公司的羽毛球社團,結果發現完全不懂雙人羽球的規則!!!
再球權換來換去,還有位置換來換去的過程中,我已經暈了~
還是清楚狀況 XD

於是,身為一個工程師,那就用工程師的方法來記住規則吧~~~
😜😜😜 解決方法 寫一個程式 實際了解規則XD


1. 先來定義 class -> BadmintonDoubles.java

主要會分成

1.兩支隊伍 teamA 跟 teamB
2.兩支隊伍 各自的分數 scoreA 跟 scoreB
3.發球權 server
4.比賽是否結束 isOver
5.四名參賽者 playerA, playerB, playerC, playerD

與諸多方法,如:拿到分數、球員是不是要換位置等等

話不多說直接上程式~~~

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
package Badminton;

import org.apache.commons.lang3.StringUtils;

/*
* 雙人羽球 defult 站位
*
* |-------------
* | | |
* | C | D |(teamB)
* | | |
* |-------
* | | |
* | B | A | (teamA, A發球)
* | | |
* |-------------
* */
public class BadmintonDoubles {
private String teamA;
private String teamB;
private int scoreA;
private int scoreB;
private String server;
private boolean isOver;
private Player playerA, playerB, playerC, playerD;

public BadmintonDoubles(String teamA, String teamB, Player playerA, Player playerB, Player playerC,
Player playerD) {
this.teamA = teamA;
this.teamB = teamB;
this.scoreA = 0;
this.scoreB = 0;
this.server = StringUtils.join(playerA.getName(), "(", playerA.getPosition() + ")");
this.isOver = false;
this.playerA = playerA;
this.playerB = playerB;
this.playerC = playerC;
this.playerD = playerD;
}

// 用來回傳發球人
public String getServer() {
return server;
}

// 回傳指定球隊的分數
public int getScore(String team) {
if (team.equals(teamA)) {
return scoreA;
} else if (team.equals(teamB)) {
return scoreB;
}
return -1;
}

// 分數為偶數(ex:0、2、4、6、8、10),則由站在右發球區的球員發球
// 分數為奇數(ex:1、3、5、7、9),則由站左邊發球區球員發球。
// 傳入得分的team
public String getPosition(String team) {
// 偶數分 起始0
if (getScore(team) % 2 == 0) {
return "right side";
} else {// 奇數分
return "left side";
}
}

// 傳入的隊伍增加一分,並檢查是否有隊伍已經贏了比賽
public void scorePoint(String team) {
if (team.equals(teamA)) {
scoreA++;
} else if (team.equals(teamB)) {
scoreB++;
}
checkForWin();
}

// 用來回傳比賽是否已經結束
public boolean isOver() {
return isOver;
}

// 每局21分
// 若雙方在將近終局時,同時拿到20-20的分數,則進入DEUCE規則(任一方需取得兩分領先,才算得勝。例如:22-20、23-21、24-22)
private void checkForWin() {
if (scoreA >= 20 && scoreB >= 20) {
// DEUCE
if (scoreA - scoreB >= 2) {
System.out.println(teamA + " wins the match!");
isOver = true;
} else if (scoreB - scoreA >= 2) {
System.out.println(teamB + " wins the match!");
isOver = true;
}
} else {
if (scoreA >= 21) {
System.out.println(teamA + " wins the match!");
isOver = true;
}

if (scoreB >= 21) {
System.out.println(teamB + " wins the match!");
isOver = true;
}
}
}

// 更換球員站位
public void swapPositions(String team) {
if (team.equals(teamA)) {
String positionTemp = playerA.getPosition();
playerA.setPosition(playerB.getPosition());
playerB.setPosition(positionTemp);
} else if (team.equals(teamB)) {
String positionTemp = playerC.getPosition();
playerC.setPosition(playerD.getPosition());
playerD.setPosition(positionTemp);
}
}

// 傳入:得分的隊 & 此次發球人
public String winOnePoint(String team, String playerName) {
// 此隊得分
scorePoint(team);
System.out.println(getNowScore());

// A隊得分 , 看現在比數 由A得誰發球
if (team.equals(teamA)) {
// 發球方得分 才換邊
if (team.equals(teamA) && StringUtils.equalsAny(playerName, playerA.getName(), playerB.getName())) {
swapPositions(team);
}
if (getPosition("teamA").equals(playerA.getPosition())) {
return "發球權:" + teamA + ", 發球邊:" + getPosition(team) + ", 發球者:" + playerA.getName();
} else {
return "發球權:" + teamA + ", 發球邊:" + getPosition(team) + ", 發球者:" + playerB.getName();
}
} else {
// B隊得分 看現在比數 由B得誰發球
// 發球方得分 才換邊
if (team.equals(teamB) && StringUtils.equalsAny(playerName, playerC.getName(), playerD.getName())) {
swapPositions(team);
}
if (getPosition("teamB").equals(playerC.getPosition())) {
return "發球權:" + teamB + ", 發球邊:" + getPosition(team) + ", 發球者:" + playerC.getName();
} else {
return "發球權:" + teamB + ", 發球邊:" + getPosition(team) + ", 發球者:" + playerD.getName();
}
}
}

public String getNowScore() {
return "目前比數: (A)" + getScore("teamA") + ":" + getScore("teamB") + "(B)";

}
}

class Player {
private String name;
private String position;

public Player(String name, String position) {
this.name = name;
this.position = position;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void setPosition(String position) {
this.position = position;
}

public String getPosition() {
return position;
}

}


2. 然後寫main方法測試

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
package Badminton;

public class TestGame {
public static void main(String[] args) {
BadmintonDoubles game = new BadmintonDoubles("teamA", "teamB",
new Player("A","right side"), new Player("B","left side"),
new Player("C","right side"), new Player("D","left side"));

System.out.println(game.getNowScore());

//開局
System.out.println("team A 開局發球員: " + game.getServer());
System.out.println("--------start--------");

//開局發球員得分
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");

System.out.println(game.winOnePoint("teamB","A"));
System.out.println("----------------");

System.out.println(game.winOnePoint("teamA","D"));
System.out.println("----------------");

System.out.println(game.winOnePoint("teamB","B"));
System.out.println("----------------");

System.out.println(game.winOnePoint("teamB","C"));

System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","C"));

System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));

System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));

//以下測試
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));

System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamB","C"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
System.out.println("----------------");
System.out.println(game.winOnePoint("teamA","A"));
}
}


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
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
目前比數: (A)0:0(B)
team A 開局發球員: A(right side)
--------start--------
目前比數: (A)1:0(B)
發球權:teamA, 發球邊:left side, 發球者:A
----------------
目前比數: (A)1:1(B)
發球權:teamB, 發球邊:left side, 發球者:D
----------------
目前比數: (A)2:1(B)
發球權:teamA, 發球邊:right side, 發球者:B
----------------
目前比數: (A)2:2(B)
發球權:teamB, 發球邊:right side, 發球者:C
----------------
目前比數: (A)2:3(B)
發球權:teamB, 發球邊:left side, 發球者:C
----------------
目前比數: (A)3:3(B)
發球權:teamA, 發球邊:left side, 發球者:A
----------------
目前比數: (A)4:3(B)
發球權:teamA, 發球邊:right side, 發球者:A
----------------
目前比數: (A)5:3(B)
發球權:teamA, 發球邊:left side, 發球者:A
----------------
目前比數: (A)6:3(B)
發球權:teamA, 發球邊:right side, 發球者:A
----------------
目前比數: (A)7:3(B)
發球權:teamA, 發球邊:left side, 發球者:A
----------------
目前比數: (A)8:3(B)
發球權:teamA, 發球邊:right side, 發球者:A
----------------
目前比數: (A)9:3(B)
發球權:teamA, 發球邊:left side, 發球者:A
----------------
目前比數: (A)10:3(B)
發球權:teamA, 發球邊:right side, 發球者:A
----------------
目前比數: (A)11:3(B)
發球權:teamA, 發球邊:left side, 發球者:A
----------------
目前比數: (A)12:3(B)
發球權:teamA, 發球邊:right side, 發球者:A
----------------
目前比數: (A)13:3(B)
發球權:teamA, 發球邊:left side, 發球者:A
----------------
目前比數: (A)14:3(B)
發球權:teamA, 發球邊:right side, 發球者:A
----------------
目前比數: (A)15:3(B)
發球權:teamA, 發球邊:left side, 發球者:A
----------------
目前比數: (A)16:3(B)
發球權:teamA, 發球邊:right side, 發球者:A
----------------
目前比數: (A)17:3(B)
發球權:teamA, 發球邊:left side, 發球者:A
----------------
目前比數: (A)18:3(B)
發球權:teamA, 發球邊:right side, 發球者:A
----------------
目前比數: (A)19:3(B)
發球權:teamA, 發球邊:left side, 發球者:A
----------------
目前比數: (A)20:3(B)
發球權:teamA, 發球邊:right side, 發球者:A
----------------
目前比數: (A)20:4(B)
發球權:teamB, 發球邊:right side, 發球者:C
----------------
目前比數: (A)20:5(B)
發球權:teamB, 發球邊:left side, 發球者:C
----------------
目前比數: (A)20:6(B)
發球權:teamB, 發球邊:right side, 發球者:C
----------------
目前比數: (A)20:7(B)
發球權:teamB, 發球邊:left side, 發球者:C
----------------
目前比數: (A)20:8(B)
發球權:teamB, 發球邊:right side, 發球者:C
----------------
目前比數: (A)20:9(B)
發球權:teamB, 發球邊:left side, 發球者:C
----------------
目前比數: (A)20:10(B)
發球權:teamB, 發球邊:right side, 發球者:C
----------------
目前比數: (A)20:11(B)
發球權:teamB, 發球邊:left side, 發球者:C
----------------
目前比數: (A)20:12(B)
發球權:teamB, 發球邊:right side, 發球者:C
----------------
目前比數: (A)20:13(B)
發球權:teamB, 發球邊:left side, 發球者:C
----------------
目前比數: (A)20:14(B)
發球權:teamB, 發球邊:right side, 發球者:C
----------------
目前比數: (A)20:15(B)
發球權:teamB, 發球邊:left side, 發球者:C
----------------
目前比數: (A)20:16(B)
發球權:teamB, 發球邊:right side, 發球者:C
----------------
目前比數: (A)20:17(B)
發球權:teamB, 發球邊:left side, 發球者:C
----------------
目前比數: (A)20:18(B)
發球權:teamB, 發球邊:right side, 發球者:C
----------------
目前比數: (A)20:19(B)
發球權:teamB, 發球邊:left side, 發球者:C
----------------
目前比數: (A)20:20(B)
發球權:teamB, 發球邊:right side, 發球者:C
----------------
目前比數: (A)20:21(B)
發球權:teamB, 發球邊:left side, 發球者:C
----------------
目前比數: (A)21:21(B)
發球權:teamA, 發球邊:left side, 發球者:B
----------------
目前比數: (A)22:21(B)
發球權:teamA, 發球邊:right side, 發球者:B
----------------
目前比數: (A)22:22(B)
發球權:teamB, 發球邊:right side, 發球者:C
----------------
目前比數: (A)23:22(B)
發球權:teamA, 發球邊:left side, 發球者:B
----------------
teamA wins the match!
目前比數: (A)24:22(B)
發球權:teamA, 發球邊:right side, 發球者:B

這樣就記住規則拉!!! 開心灑花 ~~~

規則參考:(雙打發球說明範例)
1.https://www.victorsport.com.tw/badmintonaz/5072


用JAVA寫一個雙人羽球(站位,發球權,計分)的規則
http://example.com/2023/01/17/用JAVA寫一個雙人羽球(站位,發球權,計分)的規則/
作者
Tayli Kuan
發布於
2023年1月17日
許可協議