Skip to content
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

H-5: Refactoring Skunk in Eclipse #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Binary file modified H5_Skunk/bin/SkunkDomain.class
Binary file not shown.
65 changes: 44 additions & 21 deletions H5_Skunk/src/SkunkDomain.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,57 @@ public SkunkDomain(SkunkUI ui)
this.wantsToQuit = false;
this.oneMoreRoll = false;
}

public boolean run()

// everything is packed inside the run method. This practice makes the code hard to read and understand
// and even very hard to debug.
// The run method have way to many tasks, does many things and has so many responsibilities.
// To improve its legibility and structure; I'm going to create methods that are easy to read,
// well structure and perform one task.

// this method will get the number of players and pass them to other method when needed.
public int getNumberOfPlayers()
{
ui.println("Welcome to Skunk 0.47\n");

String numberPlayersString = skunkUI.promptReadAndReturn("How many players?");
this.numberOfPlayers = Integer.parseInt(numberPlayersString);

return this.numberOfPlayers;
}

// this method getPlayersNames and will pass them to the run method
public void getPlayersNames()
{
int numberOfPlayers = getNumberOfPlayers();

for (int playerNumber = 0; playerNumber < numberOfPlayers; playerNumber++)
{
ui.print("Enter name of player " + (playerNumber + 1) + ": ");
playerNames[playerNumber] = StdIn.readLine();
this.players.add(new Player(50));
}
}

// This method process the last roll. Which also remove duplicates while looping wantsToRoll
public void processLastRoll(int number, boolean value)
{
ui.println(number + " Skunk! You lose the turn, the turn score, plus pay " + number + " chip(s) to the kitty");
kitty += number;
activePlayer.setNumberChips(activePlayer.getNumberChips() - number);
activePlayer.setTurnScore(0);

if (number == 4)
{
activePlayer.setRoundScore(0);
}
value = false;
}

public boolean run()
{
ui.println("Welcome to Skunk 0.47\n");

// getting players info before the game begin
getPlayersNames();

activePlayerIndex = 0;
activePlayer = players.get(activePlayerIndex);

Expand All @@ -61,32 +98,18 @@ public boolean run()
skunkDice.roll();
if (skunkDice.getLastRoll() == 2)
{
ui.println("Two Skunks! You lose the turn, the round score, plus pay 4 chips to the kitty");
kitty += 4;
activePlayer.setNumberChips(activePlayer.getNumberChips() - 4);
activePlayer.setTurnScore(0);
activePlayer.setRoundScore(0);
wantsToRoll = false;
this.processLastRoll(4, wantsToRoll);
break;
}
else if (skunkDice.getLastRoll() == 3)
{
ui.println("Skunks and Deuce! You lose the turn, the turn score, plus pay 2 chips to the kitty");
kitty += 2;
activePlayer.setNumberChips(activePlayer.getNumberChips() - 2);
activePlayer.setTurnScore(0);
wantsToRoll = false;
this.processLastRoll(2, wantsToRoll);
break;
}
else if (skunkDice.getDie1().getLastRoll() == 1 || skunkDice.getDie2().getLastRoll() == 1)
{
ui.println("One Skunk! You lose the turn, the turn score, plus pay 1 chip to the kitty");
kitty += 1;
activePlayer.setNumberChips(activePlayer.getNumberChips() - 1);
activePlayer.setTurnScore(0);
wantsToRoll = false;
this.processLastRoll(1, wantsToRoll);
break;

}

activePlayer.setRollScore(skunkDice.getLastRoll());
Expand Down