Skip to content

Commit

Permalink
code refactoring weapon storage
Browse files Browse the repository at this point in the history
  • Loading branch information
bugthesystem committed Dec 10, 2015
1 parent 0398941 commit dada6e4
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 65 deletions.
4 changes: 2 additions & 2 deletions src/main/java/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public static void main(String[] arguments) throws Exception {

IGameLogicStrategyResolver strategyResolver = new GameLogicStrategyResolver(strategies);

IGameLogicStrategy currentStrategy = strategyResolver.resolve("Basic");
IGameLogicStrategy currentStrategy = strategyResolver.resolve(StrategyType.Basic);

UserWeaponChoiceProvider userWeaponChoiceProvider = new UserWeaponChoiceProvider(currentStrategy);
UserWeaponChoiceProvider userWeaponChoiceProvider = new UserWeaponChoiceProvider(weaponStorage);
IRandomProvider randomProvider = new RandomProvider();

IPlayerFactory playerFactory = new PlayerFactory(currentStrategy, userWeaponChoiceProvider, randomProvider);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/lib/BasicGameLogicStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class BasicGameLogicStrategy implements IGameLogicStrategy {
private IWeaponStorage weaponStorage;

public BasicGameLogicStrategy(IWeaponStorage weaponStorage) {

this.weaponStorage = weaponStorage;
}

Expand Down Expand Up @@ -59,7 +58,7 @@ public Set<String> getWeapons() {
return weaponStorage.getWeaponNames();
}

public String getName() {
return "Basic";
public StrategyType getStrategyType() {
return StrategyType.Basic;
}
}
7 changes: 3 additions & 4 deletions src/main/java/lib/ExtendedGameLogicStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,20 @@ public void addWeapons(String beater, String beaten, String beaterExistingWeapon
int beaterExistingWeaponId = 0;

HashMap<String, Integer> weapons = this.weaponStorage.getWeapons();
//TODO:

for (Map.Entry<String, Integer> entry : weapons.entrySet()) {
int currentWeaponId = entry.getValue();
if (!beaterExistingWeapon.equals(entry.getKey())) {
this.weaponStorage.put(entry.getKey(), currentWeaponId + 2);
} else beaterExistingWeaponId = currentWeaponId;
}


this.weaponStorage.put(beater, beaterExistingWeaponId + 1);
this.weaponStorage.put(beaten, beaterExistingWeaponId + 2);
}

public String getName() {
return "Extended";
public StrategyType getStrategyType() {
return StrategyType.Extended;
}

}
14 changes: 6 additions & 8 deletions src/main/java/lib/GameLogicStrategyResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,22 @@ public GameLogicStrategyResolver(Set<IGameLogicStrategy> strategies) {
this.strategies = strategies;
}

public IGameLogicStrategy resolve(String name) throws Exception {
public IGameLogicStrategy resolve(StrategyType strategyType) throws Exception {

if (name.isEmpty()) {
throw new InvalidParameterException("Name must be not empty.");
if (strategyType == null) {
throw new InvalidParameterException("strategyType must be not null.");
}

if (strategies.isEmpty()) {
throw new Exception("There is no strategy has registered.");
}

IGameLogicStrategy result = null;
for (IGameLogicStrategy strategy : this.strategies) {
if (strategy.getName().equals(name)) {
result = strategy;
break;
if (strategy.getStrategyType().equals(strategyType)) {
return strategy;
}
}

return result;
return null;
}
}
1 change: 1 addition & 0 deletions src/main/java/lib/PlayerType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ public enum PlayerType {
UserPlayer,
ComputerPlayer
}

6 changes: 6 additions & 0 deletions src/main/java/lib/StrategyType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package lib;

public enum StrategyType {
Basic,
Extended
}
14 changes: 7 additions & 7 deletions src/main/java/lib/UserWeaponChoiceProvider.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package lib;

import com.google.common.collect.Multiset;
import com.google.inject.Inject;
import lib.interfaci.IGameLogicStrategy;
import lib.interfaci.IUserWeaponChoiceProvider;
import lib.interfaci.IWeaponStorage;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.*;

public class UserWeaponChoiceProvider implements IUserWeaponChoiceProvider {

static final String DEFAULT_WEAPON = "paper";
private IGameLogicStrategy gameLogicStrategy;
private IWeaponStorage weaponStorage;

@Inject
public UserWeaponChoiceProvider(IGameLogicStrategy gameLogicStrategy) {
this.gameLogicStrategy = gameLogicStrategy;
public UserWeaponChoiceProvider(IWeaponStorage weaponStorage) {
this.weaponStorage = weaponStorage;
}

public String getInput() {

List<String> weapons = new ArrayList<String>();

Object[] array = this.gameLogicStrategy.getWeapons().toArray();
Object[] array = this.weaponStorage.getWeaponNames().toArray();

for (int i = 0; i < array.length; i++) {
weapons.add(String.format("%s(%d)", array[i], i));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/lib/interfaci/IGameLogicStrategy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package lib.interfaci;

import lib.StrategyType;

import java.util.Set;

public interface IGameLogicStrategy {
Expand All @@ -10,6 +12,6 @@ public interface IGameLogicStrategy {

Set<String> getWeapons();

String getName();
StrategyType getStrategyType();
}

4 changes: 3 additions & 1 deletion src/main/java/lib/interfaci/IGameLogicStrategyResolver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lib.interfaci;

import lib.StrategyType;

public interface IGameLogicStrategyResolver {

IGameLogicStrategy resolve(String name) throws Exception;
IGameLogicStrategy resolve(StrategyType strategyType) throws Exception;
}
4 changes: 2 additions & 2 deletions src/test/java/lib/BasicGameLogicStrategyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testCanBeat_rock_beats_scissors() throws Exception {
when(weaponStorageMock.get(scissors)).thenReturn(scissorsId);
when(weaponStorageMock.get(rock)).thenReturn(rockId);

int beat = strategy.canBeat(rock,scissors);
int beat = strategy.canBeat(rock, scissors);

assertThat(beat).isEqualTo(1);

Expand Down Expand Up @@ -188,7 +188,7 @@ public void testGetWeapons() throws Exception {
@Test
public void testGetName() throws Exception {
String expected = "Basic";
String actual = strategy.getName();
String actual = strategy.getStrategyType().toString();

assertThat(actual).isEqualTo(expected);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/lib/ExtendedGameLogicStrategyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void testCanBeat_lizard_beats_spock() throws Exception {
public void testGetName() throws Exception {

String expectedName = "Extended";
String actual = strategy.getName();
String actual = strategy.getStrategyType().toString();

assertThat(actual).isEqualTo(expectedName);
}
Expand Down
53 changes: 28 additions & 25 deletions src/test/java/lib/GameLogicStrategyResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,52 +37,55 @@ public void setUp() throws Exception {
public void testResolve_should_throw_InvalidParameterException_strategy_name_is_empty() throws Exception {

assertThatThrownBy(() -> {
strategyResolver.resolve("");
strategyResolver.resolve(null);
})
.isInstanceOf(InvalidParameterException.class)
.hasMessageContaining("Name must be not empty.");
.hasMessageContaining("strategyType must be not null.");
}

@Test
public void testResolve_should_fail_when_strategies_empty() throws Exception {
assertThatThrownBy(() -> {
strategyResolver.resolve("test");
})
.isInstanceOf(Exception.class)
.hasMessageContaining("There is no strategy has registered.");
}

@Test
public void testResolve_should_return_null_when_strategy_not_found() throws Exception {
public void testResolve() throws Exception {

String strategyName = "basic";
StrategyType strategyType = StrategyType.Basic;

strategies.add(gameLogicStrategyMock);

when(gameLogicStrategyMock.getName()).thenReturn(strategyName);
when(gameLogicStrategyMock.getStrategyType()).thenReturn(strategyType);

IGameLogicStrategy strategy = strategyResolver.resolve(strategyType);

IGameLogicStrategy strategy = strategyResolver.resolve("test");
assertThat(strategy).isNotNull();

assertThat(strategy).isNull();
verify(gameLogicStrategyMock, times(1)).getStrategyType();

verify(gameLogicStrategyMock, times(1)).getName();
assertThat(strategy.getStrategyType()).isEqualTo(strategyType);
}

@Test
public void testResolve() throws Exception {
public void testResolve_throws_Exception_when_strategies_is_empty() throws Exception {

String strategyName = "basic";
strategies = new HashSet<>();
strategyResolver = new GameLogicStrategyResolver(strategies);
assertThatThrownBy(() -> {
strategyResolver.resolve(StrategyType.Basic);
})
.isInstanceOf(Exception.class)
.hasMessageContaining("There is no strategy has registered.");

strategies.add(gameLogicStrategyMock);

when(gameLogicStrategyMock.getName()).thenReturn(strategyName);
}

IGameLogicStrategy strategy = strategyResolver.resolve(strategyName);
@Test
public void testResolve_returns_null_when_strategy_not_found() throws Exception {

assertThat(strategy).isNotNull();
strategies = new HashSet<>();
strategies.add(gameLogicStrategyMock);
strategyResolver = new GameLogicStrategyResolver(strategies);

when(gameLogicStrategyMock.getStrategyType()).thenReturn(StrategyType.Basic);

verify(gameLogicStrategyMock, times(1)).getName();
IGameLogicStrategy result = strategyResolver.resolve(StrategyType.Extended);

assertThat(strategy.getName()).isEqualTo(strategyName);
assertThat(result).isNull();
}
}
22 changes: 11 additions & 11 deletions src/test/java/lib/UserWeaponChoiceProviderTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lib;

import lib.interfaci.IGameLogicStrategy;
import lib.interfaci.IUserWeaponChoiceProvider;
import lib.interfaci.IWeaponStorage;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -25,11 +25,11 @@ public class UserWeaponChoiceProviderTest {
IUserWeaponChoiceProvider provider;

@Mock
IGameLogicStrategy gameLogicStrategyMock;
IWeaponStorage weaponStorageMock;

@Before
public void setUp() throws Exception {
provider = new UserWeaponChoiceProvider(gameLogicStrategyMock);
provider = new UserWeaponChoiceProvider(weaponStorageMock);
}

@Test
Expand All @@ -44,13 +44,13 @@ public void testGetInput() throws Exception {
weapons.add("b");
weapons.add("c");

when(gameLogicStrategyMock.getWeapons()).thenReturn(weapons);
when(weaponStorageMock.getWeaponNames()).thenReturn(weapons);

String input = provider.getInput();

assertThat(weapons).contains(input);

verify(gameLogicStrategyMock).getWeapons();
verify(weaponStorageMock).getWeaponNames();

System.setIn(original);
}
Expand All @@ -68,13 +68,13 @@ public void testGetInput_if_user_enter_wrong_input_should_return_default_weapon(
weapons.add("b");
weapons.add("c");

when(gameLogicStrategyMock.getWeapons()).thenReturn(weapons);
when(weaponStorageMock.getWeaponNames()).thenReturn(weapons);

String input = provider.getInput();

assertThat(input).isEqualTo(defaultWeapon);

verify(gameLogicStrategyMock).getWeapons();
verify(weaponStorageMock).getWeaponNames();

System.setIn(original);
}
Expand All @@ -96,15 +96,15 @@ public void testGetInput_if_user_enter_wrong_input_system_err_should_write_messa
weapons.add("b");
weapons.add("c");

when(gameLogicStrategyMock.getWeapons()).thenReturn(weapons);
when(weaponStorageMock.getWeaponNames()).thenReturn(weapons);

String input = provider.getInput();

assertThat(input).isEqualTo(defaultWeapon);
assertThat(expectedMessage)
.isEqualTo(outContent.toString());

verify(gameLogicStrategyMock).getWeapons();
verify(weaponStorageMock).getWeaponNames();
}

@Test
Expand All @@ -120,12 +120,12 @@ public void testGetInput_if_user_weapon_is_empty_should_return_default_weapon()
weapons.add("c");
weapons.add("");

when(gameLogicStrategyMock.getWeapons()).thenReturn(weapons);
when(weaponStorageMock.getWeaponNames()).thenReturn(weapons);

String input = provider.getInput();

assertThat(input).isEqualTo(defaultWeapon);

verify(gameLogicStrategyMock).getWeapons();
verify(weaponStorageMock).getWeaponNames();
}
}

0 comments on commit dada6e4

Please sign in to comment.