Skip to content

Commit

Permalink
Add parameter to useCards function. QuestionXMLDAO now accepts InputS…
Browse files Browse the repository at this point in the history
…tream as source for questions.
  • Loading branch information
lhcopetti committed May 23, 2015
1 parent a55c408 commit 0937a07
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
18 changes: 8 additions & 10 deletions src/com/copetti/core/MillionGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import com.copetti.core.Question.Difficulty;
Expand Down Expand Up @@ -117,23 +116,27 @@ public void skip()
}

@Override
public Set<Option> useCards()
public Set<Option> useCards(MillionCards card)
{

if (can(GameAction.USECARDS) > 0)
{
int useRemaining = availableActions.get(GameAction.USECARDS);
availableActions.put(GameAction.USECARDS, --useRemaining);

return getUseCardsOptions();
return getUseCardsOptions(card);
}

throw new IllegalStateException("Cannot use cards anymore!");
}

protected Set<Option> getUseCardsOptions()
protected Set<Option> getUseCardsOptions(MillionCards card)
{
int numberOfCardsToAdd = getRandomValue(Option.values().length);
// @formatter:off
int numberOfCardsToAdd = Option.values().length // 4
- card.numberOfOptionsToRemove() // Cards to remove
- 1; // The answer is always there.
// @formatter:on

// Add the answer
List<Option> options = new ArrayList<Option>(
Expand All @@ -150,11 +153,6 @@ protected Set<Option> getUseCardsOptions()
return Collections.unmodifiableSet(new HashSet<Option>(options));
}

private int getRandomValue(int upperBound)
{
return new Random().nextInt(upperBound);
}

public int getCurrentLevel()
{
return currentLevel;
Expand Down
17 changes: 16 additions & 1 deletion src/com/copetti/core/MillionShowGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ public enum GameAction {
SKIP, USECARDS
}

public enum MillionCards {
KING(0), ACE(1), TWO(2), THREE(3);

private MillionCards(int optionsToRemove) {
this.optionsToRemove = optionsToRemove;
}

private int optionsToRemove;

public int numberOfOptionsToRemove() {
return optionsToRemove;
}

}

/**
* Returns true if quitting is available. False, otherwise.
*
Expand Down Expand Up @@ -55,7 +70,7 @@ public enum GameAction {
* valid.
* @param number TODO
*/
public Set<Option> useCards();
public Set<Option> useCards(MillionCards card);

/**
* Attempts to answer the question given by {@link #getCurrentQuestion()}.
Expand Down
24 changes: 9 additions & 15 deletions src/com/copetti/core/dao/QuestionXMLDao.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.copetti.core.dao;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -13,6 +14,7 @@
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
Expand Down Expand Up @@ -55,7 +57,7 @@ public static QuestionXML fromString(String s) {
}
}

private File xmlFile;
private InputStream inputStream;
private DefaultHandler defaultHandler;
private StringBuilder accumulator;

Expand All @@ -69,12 +71,11 @@ public static QuestionXML fromString(String s) {
public Set<String> wrongOptions = new HashSet<String>();
public String difficulty;

private QuestionXMLDao(File xmlFile) {
public QuestionXMLDao(InputStream is) {

questions = new ArrayList<Question>();
accumulator = new StringBuilder();

this.xmlFile = xmlFile;
this.inputStream = is;

defaultHandler = new DefaultHandler() {
@Override
Expand Down Expand Up @@ -144,16 +145,9 @@ public void endElement(String uri, String localName, String qName)
};
}

public static QuestionXMLDao newInstance(String xmlQuestionFile)
throws FileNotFoundException {

File file = new File(xmlQuestionFile);

if (!file.exists())
throw new FileNotFoundException("The file: "
+ file.getAbsolutePath() + " was not found.");
public QuestionXMLDao(File xmlFile) throws IOException {

return new QuestionXMLDao(file);
this(new FileInputStream(xmlFile));
}

@Override
Expand All @@ -166,7 +160,7 @@ public List<Question> restoreAll() {
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser()
.getXMLReader();
xmlReader.setContentHandler(defaultHandler);
xmlReader.parse(xmlFile.getAbsolutePath());
xmlReader.parse(new InputSource(inputStream));
return questions;

} catch (ParserConfigurationException | SAXException | IOException e) {
Expand Down
12 changes: 8 additions & 4 deletions test/com/copetti/core/MillionGameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.Test;

import com.copetti.core.MillionShowGame.GameAction;
import com.copetti.core.MillionShowGame.MillionCards;
import com.copetti.core.MillionShowGame.PrizeValue;
import com.copetti.core.Question.Difficulty;
import com.copetti.core.Question.Option;
Expand Down Expand Up @@ -92,13 +93,13 @@ public void testCan()
assertTrue(mg.can(GameAction.USECARDS) > 0);

while (mg.can(GameAction.USECARDS) > 0)
mg.useCards();
mg.useCards(MillionCards.THREE);

assertTrue(mg.can(GameAction.USECARDS) == 0);

try
{
mg.useCards();
mg.useCards(MillionCards.ACE);
throw new AssertionError();
}
catch (IllegalStateException e)
Expand Down Expand Up @@ -157,16 +158,19 @@ public void testUseCards()
// The question should not change
// The currentLevel should not change

Set<Option> optionsLeft = mg.useCards();
MillionCards two = MillionCards.TWO;
Set<Option> optionsLeft = mg.useCards(two);

assertTrue(optionsLeft.contains(mg.getCurrentQuestion().getAnswer()));
assertTrue(optionsLeft.size() == Option.values().length
- two.numberOfOptionsToRemove());
}

@Test
public void testAnswerCorrect()
{
// Change currentLevel
// Change currentQuestion
//
Question cQ = mg.getCurrentQuestion();
int prizeScore = mg.getPrizes().get(PrizeValue.SCORE);
mg.answer(cQ.getAnswer());
Expand Down
2 changes: 1 addition & 1 deletion test/com/copetti/core/dao/TestXMLDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void testRestoreAll() {
fos.write(xmlFile.getBytes(), 0, xmlFile.length());
fos.flush();
fos.close();
qxd = QuestionXMLDao.newInstance(tempFile.getAbsolutePath());
qxd = new QuestionXMLDao(tempFile);

questions = qxd.restoreAll();

Expand Down

0 comments on commit 0937a07

Please sign in to comment.