Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
479b7ae
重构:修改不规范范型容器声明
Feb 2, 2020
96eff23
重构:重命名 purse->goldCoins
Feb 2, 2020
b9c45d2
重构:将字符串比较 == -> equals
Feb 2, 2020
d9bc927
重构:去掉多余局部变量winner
Feb 2, 2020
eb53f63
重构:修改拼写错误 corrent -> correct
Feb 2, 2020
6324eb2
重构:修改测试中的拼写错误 corrent -> correct
Feb 2, 2020
20c90ce
重构:拆分movePlayerAndAskQuestion方法为 movePlayer和askQuestion两个方法,一个方法只做一件事
Feb 2, 2020
5dee0a0
重构:提取 nextPlayer 方法
Feb 2, 2020
1d6c45e
重构:提取 gainGoldCoin 方法
Feb 2, 2020
bbef036
重构:提取 getOutOfPenaltyBox 方法
Feb 2, 2020
508dbcb
重构:提取 stayInPenaltyBox 方法
Feb 2, 2020
10a71ba
重构:提取 getCurrentPlayer 方法
Feb 2, 2020
f863303
重构:创建 Player 类用于封装玩家信息
Feb 2, 2020
61c8615
重构:创建 tempPlayers 容器
Feb 2, 2020
3a5b527
重构:将 name 属性移入Player类,并在addPlayer时初始化
Feb 2, 2020
7117405
重构:替换 getCurrentPlayer 中 players 为 tempPlayers
Feb 2, 2020
fd17f34
重构:为 Player 类添加place字段
Feb 2, 2020
92ca351
重构:提取 getCurrentPlace 方法
Feb 2, 2020
b3320b5
重构:将 places 替换为 tempPlayers.get().place
Feb 2, 2020
12a280a
重构:去掉 places
Feb 2, 2020
0001bfc
重构:为 Player 增加 goldCoin 字段
Feb 2, 2020
bc3c833
重构:将 goldCoins 替换为 tempPlayers.get().goldCoin
Feb 2, 2020
0c4b59d
重构:去掉 goldCoin
Feb 2, 2020
2d6fb49
重构:为 Player 增加 isInPenaltyBox 字段
Feb 2, 2020
4df4924
重构:将 isInPenaltyBox 替换为 tempPlayers.get().isInPenaltyBox
Feb 2, 2020
78acc7a
重构:去掉isInPenaltyBox
Feb 2, 2020
a90554f
重构:去掉howManyPlayers
Feb 2, 2020
0b78454
重构:为 Player 添加 moveTo 方法
Feb 2, 2020
3e78b6e
重构:将 movePlayer 方法替换为 tempPlayers.get(currentPlayer).moveTo
Feb 2, 2020
d8fe767
重构:去掉 movePlayer 方法
Feb 2, 2020
8db77d8
重构:为 Player 添加 gainGoldCoin方法
Feb 2, 2020
82382ba
重构:将 gainGoldCoin 方法替换为 tempPlayers.get(currentPlayer).gainGoldCoin
Feb 2, 2020
92728d7
重构:去掉 gainGoldCoin 方法
Feb 2, 2020
cab7f1c
重构:为 Player 添加 isWin 方法
Feb 2, 2020
6927913
重构:将 didPlayerWin 方法替换为 tempPlayers.get(currentPlayer).isWin
Feb 2, 2020
e9ca4d8
重构:去掉 didPlayerWin 方法
Feb 2, 2020
3c16d88
重构:创建HashMap 用于映射 Category 和 questionList
Feb 2, 2020
a866404
重构:为Category添加4种枚举类型,并在Game构造器中初始化
Feb 2, 2020
4a7f0f9
重构:将askQuestion中直接从list中取值 替换为通过map取值
Feb 2, 2020
0a62574
重构:将4个list变为局部变量,封装进Map中
Feb 2, 2020
d6fff94
重构:合并初始化Map时创建list的重复代码
Feb 2, 2020
ef9011b
重构:去掉多余的 createRockQuestion 方法
Feb 2, 2020
6305fc2
重构:添加 tempCurrentCategory 方法 返回Category类型枚举值
Feb 2, 2020
f0ca7d3
重构:添加 tempAskQuestion 方法 调用tempCurrentCategory 方法
Feb 2, 2020
afb19cb
重构:替换调用 askQuestion 为 tempAskQuestion
Feb 2, 2020
a54afe7
重构:重命名 tempCurrentCategory->currentCategory, tempAskQuestion->askQues…
Feb 2, 2020
5890455
重构:去掉currentCategory方法中重复
Feb 2, 2020
004933f
重构:为 Game 添加 run方法
Feb 2, 2020
bd72a5a
重构:去掉GameRunner类
Feb 2, 2020
650cb40
重构:将do while 换为 while
Feb 2, 2020
740333b
重构:提取sendToPenaltyBox 方法
Feb 2, 2020
a2a6577
重构:去掉wrongAnswer 返回值
Feb 2, 2020
9a60576
重构:重命名wasCorrectlyAnswered->correctAnswer
Feb 2, 2020
cd63eef
重构:将nextPlayer()移动到while循环中去
Feb 2, 2020
e345f4e
重构:新增 tempGetOutOfPenaltyBox 方法
Feb 2, 2020
d0e52b4
重构:修改测试,并替换原来调用getOutOfPenaltyBox->tempGetOutOfPenaltyBox
Feb 2, 2020
d32eab6
重构:替换getOutOfPenaltyBox方法
Feb 2, 2020
6e41254
重构:替换stayInPenaltyBox调用点
Feb 2, 2020
5b11e15
重构:覆盖原有stayInPenaltyBox方法
Feb 2, 2020
73a4dea
重构:在 Player中创建sendToPenaltyBox方法
Feb 2, 2020
ced1793
重构:替换 sendToPenaltyBox 为 players.get().sendToPenaltyBox
Feb 2, 2020
aca8e0e
重构:在 Player 中创建 getOutOfPenaltyBox 方法
Feb 2, 2020
2c41b46
重构:替换 getOutOfPenaltyBox 为 players.get().getOutOfPenaltyBox
Feb 2, 2020
79ab54f
重构:简化correctAnswer中的逻辑
Feb 2, 2020
6808399
修复Bug: 1. 修改测试,去掉在惩罚区中回答问题的情况。
Feb 2, 2020
8fa27db
重构:去掉isGettingOutOfPenaltyBox变量
Feb 2, 2020
c93428a
重构:1. 修改测试,添加Blues和History类别问题
Feb 2, 2020
96d9d3c
重构:在Category类中添加 getCurrentCategory 方法
Feb 2, 2020
9dc9ad7
重构:将Game类中getCurrentCategory 类替换为 Category.getCurrentCategory
Feb 2, 2020
65df120
重构:创建 PlayerContainer类
Feb 2, 2020
4c6f0d8
重构:在 PlayerContainer 类中创建 addPlayer 方法
Feb 2, 2020
912615a
重构:在 PlayerContainer 类中创建 nextPlayer 方法
Feb 2, 2020
39ead3d
重构:在 PlayerContainer 类中创建 getCurrentPlayer 方法
Feb 2, 2020
e7d61bc
重构:替换 ArrayList<Player> 为 PlayerContainer
Feb 2, 2020
945283a
重构:将Player中place字段改为private并添加getter
Feb 2, 2020
c794fc3
重构:将Player中isInPenaltyBox字段改为private并添加getter
Feb 2, 2020
2773b68
重构:将 stayInPenaltyBox 移入player类中
Feb 2, 2020
3b260cb
重构:创建 QuestionContainer 类,封装question HashMap
Feb 2, 2020
e100ed8
重构:在 QuestionContainer 类中创建 getNextQuestion 方法
Feb 2, 2020
924b0a8
重构:在 QuestionContainer 类中创建构造器
Feb 2, 2020
a7e41e1
重构:替换Game 类中 questionMap 为 QuestionContainer
Feb 2, 2020
afbd2ad
重构:拆分 roll 方法中的条件判断语句,解除if之间的嵌套
Feb 2, 2020
5163db4
重构:去掉只有一行的getXXX方法,将Game的方法减少为6个
Feb 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/com/adaptionsoft/games/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.adaptionsoft.games;

/**
* Created with IntelliJ IDEA.
* User: lai.yi
* Date: 2020/2/2
* Description:
**/
public enum Category {
POP("Pop"),
SCIENCE("Science"),
SPORTS("Sports"),
ROCK("Rock"),
BLUES("Blues"),
HISTORY("History");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不错哟,做到了只需要加一行代码就能新增特性。


private String value;

Category(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public static Category getCurrentCategory(int place) {
int index = place % Category.values().length;
Category category = Category.values()[index];
System.out.println("The category is " + category.getValue());
return category;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同理直接用F6快捷键搬过来就行,不需要手搬。

}
210 changes: 56 additions & 154 deletions src/main/java/com/adaptionsoft/games/Game.java
Original file line number Diff line number Diff line change
@@ -1,166 +1,68 @@
package com.adaptionsoft.games;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;

public class Game {
ArrayList players = new ArrayList();
int[] places = new int[6];
int[] purses = new int[6];
boolean[] inPenaltyBox = new boolean[6];

LinkedList popQuestions = new LinkedList();
LinkedList scienceQuestions = new LinkedList();
LinkedList sportsQuestions = new LinkedList();
LinkedList rockQuestions = new LinkedList();

int currentPlayer = 0;
boolean isGettingOutOfPenaltyBox;

public Game(){
for (int i = 0; i < 50; i++) {
popQuestions.addLast("Pop Question " + i);
scienceQuestions.addLast(("Science Question " + i));
sportsQuestions.addLast(("Sports Question " + i));
rockQuestions.addLast(createRockQuestion(i));
}
PlayerContainer players = new PlayerContainer();
QuestionContainer questions = new QuestionContainer();

public void run(Random rand) {
while (players.nobodyWin()) {
roll(rand.nextInt(5) + 1);
if (rand.nextInt(9) == 7) {
wrongAnswer();
} else {
correctAnswer();
}
players.nextPlayer();
}
}

public String createRockQuestion(int index){
return "Rock Question " + index;
}

public boolean isPlayable() {
return (howManyPlayers() >= 2);
}

public boolean add(String playerName) {


players.add(playerName);
places[howManyPlayers()] = 0;
purses[howManyPlayers()] = 0;
inPenaltyBox[howManyPlayers()] = false;

System.out.println(playerName + " was added");
System.out.println("They are player number " + players.size());
return true;
}

public int howManyPlayers() {
return players.size();
}

public void roll(int roll) {
System.out.println(players.get(currentPlayer) + " is the current player");
System.out.println("They have rolled a " + roll);

if (inPenaltyBox[currentPlayer]) {
if (roll % 2 != 0) {
isGettingOutOfPenaltyBox = true;

System.out.println(players.get(currentPlayer) + " is getting out of the penalty box");
movePlayerAndAskQuestion(roll);
} else {
System.out.println(players.get(currentPlayer) + " is not getting out of the penalty box");
isGettingOutOfPenaltyBox = false;
}

} else {

movePlayerAndAskQuestion(roll);
}

}

private void movePlayerAndAskQuestion(int roll) {
places[currentPlayer] = places[currentPlayer] + roll;
if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12;

System.out.println(players.get(currentPlayer)
+ "'s new location is "
+ places[currentPlayer]);
System.out.println("The category is " + currentCategory());
askQuestion();
}

private void askQuestion() {
if (currentCategory() == "Pop")
System.out.println(popQuestions.removeFirst());
if (currentCategory() == "Science")
System.out.println(scienceQuestions.removeFirst());
if (currentCategory() == "Sports")
System.out.println(sportsQuestions.removeFirst());
if (currentCategory() == "Rock")
System.out.println(rockQuestions.removeFirst());
}


private String currentCategory() {
if (places[currentPlayer] == 0) return "Pop";
if (places[currentPlayer] == 4) return "Pop";
if (places[currentPlayer] == 8) return "Pop";
if (places[currentPlayer] == 1) return "Science";
if (places[currentPlayer] == 5) return "Science";
if (places[currentPlayer] == 9) return "Science";
if (places[currentPlayer] == 2) return "Sports";
if (places[currentPlayer] == 6) return "Sports";
if (places[currentPlayer] == 10) return "Sports";
return "Rock";
}

public boolean wasCorrectlyAnswered() {
if (inPenaltyBox[currentPlayer]){
if (isGettingOutOfPenaltyBox) {
System.out.println("Answer was correct!!!!");
currentPlayer++;
if (currentPlayer == players.size()) currentPlayer = 0;
purses[currentPlayer]++;
System.out.println(players.get(currentPlayer)
+ " now has "
+ purses[currentPlayer]
+ " Gold Coins.");

boolean winner = didPlayerWin();

return winner;
} else {
currentPlayer++;
if (currentPlayer == players.size()) currentPlayer = 0;
return true;
}



} else {

System.out.println("Answer was corrent!!!!");
purses[currentPlayer]++;
System.out.println(players.get(currentPlayer)
+ " now has "
+ purses[currentPlayer]
+ " Gold Coins.");

boolean winner = didPlayerWin();
currentPlayer++;
if (currentPlayer == players.size()) currentPlayer = 0;
public void addPlayer(String playerName) {
players.addPlayer(playerName);
}

return winner;
}
}
public void roll(int roll) {
System.out.println(players.getCurrentPlayer().getName() + " is the current player");
System.out.println("They have rolled a " + roll);

if (!isCurrentPlayerInPenaltyBox()) {
players.getCurrentPlayer().moveTo(roll);
askQuestion();
return;
}

if (roll % 2 != 0) {
players.getCurrentPlayer().getOutOfPenaltyBox();
players.getCurrentPlayer().moveTo(roll);
askQuestion();
} else {
players.getCurrentPlayer().stayInPenaltyBox();
}
}

public boolean wrongAnswer(){
System.out.println("Question was incorrectly answered");
System.out.println(players.get(currentPlayer)+ " was sent to the penalty box");
inPenaltyBox[currentPlayer] = true;
private void askQuestion() {
int currentPlace = players.getCurrentPlayer().getPlace();
Category currentCategory = Category.getCurrentCategory(currentPlace);
String question = questions.getNextQuestion(currentCategory);
System.out.println(question);
}

currentPlayer++;
if (currentPlayer == players.size()) currentPlayer = 0;
return true;
}
public void correctAnswer() {
if (!isCurrentPlayerInPenaltyBox()) {
System.out.println("Answer was correct!!!!");
players.getCurrentPlayer().gainGoldCoin();
}
}

public void wrongAnswer() {
if (!isCurrentPlayerInPenaltyBox()) {
System.out.println("Question was incorrectly answered");
players.getCurrentPlayer().sendToPenaltyBox();
}
}

private boolean didPlayerWin() {
return !(purses[currentPlayer] == 6);
}
private boolean isCurrentPlayerInPenaltyBox() {
return players.getCurrentPlayer().isInPenaltyBox();
}
}
39 changes: 0 additions & 39 deletions src/main/java/com/adaptionsoft/games/GameRunner.java

This file was deleted.

62 changes: 62 additions & 0 deletions src/main/java/com/adaptionsoft/games/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.adaptionsoft.games;

/**
* Created with IntelliJ IDEA.
* User: lai.yi
* Date: 2020/2/2
* Description:
**/
public class Player {
private String name;
private int place = 0;
private int goldCoin = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,这个地方把字段用getter函数封装起来了很好。

private boolean isInPenaltyBox = false;

public Player(String name) {
System.out.println(name + " was added");
this.name = name;
}

public String getName() {
return name;
}

public void moveTo(int roll) {
place += roll;
if (place > 11) {
place -= 12;
}
System.out.println(name + "'s new location is " + place);
}

public void gainGoldCoin() {
goldCoin++;
System.out.println(name + " now has " + goldCoin + " Gold Coins.");
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方可不必手搬,见下个commit comment。


public void sendToPenaltyBox() {
isInPenaltyBox = true;
System.out.println(name + " was sent to the penalty box");
}

public void getOutOfPenaltyBox() {
isInPenaltyBox = false;
System.out.println(name + " is getting out of the penalty box");
}

public boolean isWin() {
return goldCoin >= 6;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isWin()可跟上述手法同理搬运。然后搞到这就发现两次为了搬运做了提炼getCurrentPlayer()方法的操作,不如重构完就提炼出这个方法。我做完这个提炼后发现一共有10处地方在用getCurrentPlayer(),也能一定程度为后面“搬移哪些字段”做提示。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

后面提交还有些也同理,都可以用这个方法进行搬运,如penaltyBox几个处理方法那块。


public int getPlace() {
return place;
}

public boolean isInPenaltyBox() {
return isInPenaltyBox;
}

public void stayInPenaltyBox() {
System.out.println(name + " is not getting out of the penalty box");
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/adaptionsoft/games/PlayerContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.adaptionsoft.games;

import java.util.ArrayList;

/**
* Created with IntelliJ IDEA.
* User: lai.yi
* Date: 2020/2/2
* Description:
**/
public class PlayerContainer {
private ArrayList<Player> players = new ArrayList<>();
private int currentPlayer = 0;

public void addPlayer(String playerName) {
players.add(new Player(playerName));
System.out.println("They are player number " + players.size());
}

public void nextPlayer() {
currentPlayer++;
if (currentPlayer == players.size()) currentPlayer = 0;
}

public Player getCurrentPlayer() {
return players.get(currentPlayer);
}

public boolean nobodyWin(){
return players.stream().noneMatch(Player::isWin);
}
}
Loading