-
Notifications
You must be signed in to change notification settings - Fork 13
任务3 bugzero #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
任务3 bugzero #1
Changes from all commits
479b7ae
96eff23
b9c45d2
d9bc927
eb53f63
6324eb2
20c90ce
5dee0a0
1d6c45e
bbef036
508dbcb
10a71ba
f863303
61c8615
3a5b527
7117405
fd17f34
92ca351
b3320b5
12a280a
0001bfc
bc3c833
0c4b59d
2d6fb49
4df4924
78acc7a
a90554f
0b78454
3e78b6e
d8fe767
8db77d8
82382ba
92728d7
cab7f1c
6927913
e9ca4d8
3c16d88
a866404
4a7f0f9
0a62574
d6fff94
ef9011b
6305fc2
f0ca7d3
afb19cb
a54afe7
5890455
004933f
bd72a5a
650cb40
740333b
a2a6577
9a60576
cd63eef
e345f4e
d0e52b4
d32eab6
6e41254
5b11e15
73a4dea
ced1793
aca8e0e
2c41b46
79ab54f
6808399
8fa27db
c93428a
96d9d3c
9dc9ad7
65df120
4c6f0d8
912615a
39ead3d
e7d61bc
945283a
c794fc3
2773b68
3b260cb
e100ed8
924b0a8
a7e41e1
afbd2ad
5163db4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"); | ||
|
|
||
| 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; | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同理直接用F6快捷键搬过来就行,不需要手搬。 |
||
| } | ||
| 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(); | ||
| } | ||
| } |
This file was deleted.
| 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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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."); | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 后面提交还有些也同理,都可以用这个方法进行搬运,如 |
||
|
|
||
| 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"); | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不错哟,做到了只需要加一行代码就能新增特性。