-
-
Save anonymous/53bad714592792316b4d to your computer and use it in GitHub Desktop.
Choose your own adventure game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package adventuregame; | |
import java.util.*; | |
public class Adventure { | |
private String title; | |
private String author; | |
private ArrayList<Enemy> enemies; | |
public Adventure() { | |
enemies = new ArrayList<Enemy>(); | |
} | |
public String getTitle() { | |
return title; | |
} | |
public String getAuthor() { | |
return author; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public void setAuthor(String author) { | |
this.author = author; | |
} | |
public void addEnemy(Enemy e) { | |
enemies.add(e); | |
} | |
public void addEnemies(ArrayList<Enemy> enemyList) { | |
enemies.addAll(enemyList); | |
} | |
public Enemy getEnemy(String id) { | |
for ( Enemy e : enemies ) { | |
if ( e.getId() == id ) { | |
return e; | |
} | |
} | |
return new Enemy(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package adventuregame; | |
public class Enemy { | |
public enum Difficulty { | |
NOEFFORT, EASY, MODERATE, HARD, INSANE | |
} | |
private String id; | |
private String name; | |
private Difficulty difficulty; | |
public Enemy() { | |
} | |
public Enemy(String id, String name, Difficulty diff) { | |
this.id = id; | |
this.name = name; | |
this.difficulty = diff; | |
} | |
public String getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getDifficulty() { | |
int diff = 0; | |
switch ( difficulty ) { | |
case NOEFFORT: diff = 5; break; | |
case EASY: diff = 10; break; | |
case MODERATE: diff = 20; break; | |
case HARD: diff = 30; break; | |
case INSANE: diff = 50; break; | |
} | |
return diff; | |
} | |
public String getDifficultyString() { | |
String diff = ""; | |
switch(difficulty) { | |
case NOEFFORT: | |
diff = "NOEFFORT"; | |
break; | |
case EASY: | |
diff = "EASY"; | |
break; | |
case MODERATE: | |
diff = "MODERATE"; | |
break; | |
case HARD: | |
diff = "HARD"; | |
break; | |
case INSANE: | |
diff = "INSANE"; | |
break; | |
} | |
return diff; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package adventuregame; | |
import java.util.Random; | |
import javax.swing.SwingUtilities; | |
public class Game { | |
private Gui gui; | |
private Parser parser; | |
private Adventure adventure; | |
private Parser.Decision actualDecision; | |
private Random random; | |
private boolean gameInProgress; | |
public Game() { | |
gui = new Gui(this); | |
parser = new Parser(); | |
adventure = new Adventure(); | |
random = new Random(); | |
gameInProgress = false; | |
} | |
public void play() { | |
adventure.setTitle( parser.parseTitle() ); | |
adventure.setAuthor( parser.parseAuthor() ); | |
adventure.addEnemies( parser.parseEnemies() ); | |
gui.output("Now playing: " + adventure.getTitle() + " by " + adventure.getAuthor()); | |
gameInProgress = true; | |
processStory(); | |
} | |
private void processStory() { | |
Parser.NodeType nodeType = parser.readFirstElement(); | |
processElement(nodeType); | |
while ( !end() ) { | |
nodeType = parser.readNextElement(); | |
processElement(nodeType); | |
} | |
} | |
private void processElement(Parser.NodeType nodeType) { | |
switch ( nodeType ) { | |
case NARRATION: | |
processNarration(); break; | |
case DECISION: | |
processDecision(); break; | |
case FIGHT: | |
processFight(); break; | |
} | |
} | |
private void processNarration() { | |
Parser.Narration narration = parser.buildNarration(); | |
gui.output(narration.text); | |
parser.setAction(narration.action); | |
} | |
private void processDecision() { | |
actualDecision = parser.buildDecision(); | |
gui.output(actualDecision.description); | |
for ( int i = 0; i < actualDecision.choices.size(); ++i ) { | |
gui.output(i+1 + " - " + actualDecision.choices.get(i).text ); | |
} | |
gui.getInput(); | |
} | |
private boolean validInput(String input, int choiceCount ) { | |
int n = Integer.parseInt(input); | |
if ( n < 1 || n > choiceCount ) { | |
return false; | |
} | |
return true; | |
} | |
private void processFight() { | |
Parser.Fight fight = parser.buildFight(); | |
gui.output("You are in a fight!"); | |
Enemy enemy = adventure.getEnemy(fight.enemyId); | |
gui.output("Adventurer vs. " + enemy.getName() + " (" + enemy.getDifficultyString() + ")"); | |
int outcome = random.nextInt(100) + 1; | |
gui.output("You rolled: " + Integer.toString(outcome)); | |
if ( outcome > enemy.getDifficulty() ) { | |
gui.output("You won the fight!"); | |
parser.setAction(fight.winAction); | |
} | |
else { | |
gui.output("You lost the fight."); | |
parser.setAction(fight.loseAction); | |
} | |
} | |
private boolean end() { | |
return ( parser.getAction() == "end" ); | |
} | |
public Parser getParser() { | |
return parser; | |
} | |
public void receiveInput(String input) { | |
int n = Integer.parseInt(input); | |
if ( validInput(input, actualDecision.choices.size()) ) { | |
parser.setAction(actualDecision.choices.get(n-1).action); | |
} | |
} | |
public boolean isInProgress() { | |
return gameInProgress; | |
} | |
public void setInProgress(boolean b) { | |
gameInProgress = b; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package adventuregame; | |
import java.awt.GridBagConstraints; | |
import java.awt.GridBagLayout; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JButton; | |
import javax.swing.JFileChooser; | |
import javax.swing.JFrame; | |
import javax.swing.JMenu; | |
import javax.swing.JMenuBar; | |
import javax.swing.JMenuItem; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTextArea; | |
import javax.swing.JTextField; | |
import javax.swing.SwingUtilities; | |
import javax.swing.plaf.SliderUI; | |
public class Gui { | |
Game game; | |
JFrame frame; | |
JPanel panel; | |
GridBagConstraints gbc; | |
JTextArea textArea; | |
JTextField textField; | |
JScrollPane scrollPane; | |
JMenuBar menuBar; | |
JMenu menu; | |
JMenuItem newGameItem; | |
JMenuItem saveGameItem; | |
JMenuItem loadGameItem; | |
JButton okButton; | |
public Gui(Game game) { | |
this.game = game; | |
initialize(); | |
} | |
public void initialize() { | |
frame = new JFrame(); | |
frame.setTitle("Choose your own adventure"); | |
frame.setSize(800, 600); | |
//frame.setResizable(false); | |
frame.setLocationRelativeTo(null); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setVisible(true); | |
panel = new JPanel(new GridBagLayout()); | |
gbc = new GridBagConstraints(); | |
textArea = new JTextArea(); | |
textArea.setLineWrap(true); | |
textArea.setWrapStyleWord(true); | |
textArea.setAutoscrolls(true); | |
textField = new JTextField(); | |
okButton = new JButton("OK"); | |
scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, | |
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); | |
menuBar = new JMenuBar(); | |
menu = new JMenu("Menu"); | |
newGameItem = new JMenuItem("New Game"); | |
menu.add(newGameItem); | |
saveGameItem = new JMenuItem("Save Game"); | |
menu.add(saveGameItem); | |
loadGameItem = new JMenuItem("Load Game"); | |
menu.add(loadGameItem); | |
menuBar.add(menu); | |
gbc.gridx = 0; | |
gbc.gridy = 0; | |
gbc.gridwidth = 2; | |
gbc.ipadx = 800; | |
gbc.ipady = 20; | |
gbc.weighty = 1; | |
gbc.anchor = GridBagConstraints.NORTH; | |
panel.add(menuBar, gbc); | |
gbc.gridx = 0; | |
gbc.gridy = 1; | |
gbc.gridwidth = 2; | |
gbc.ipadx = 750; | |
gbc.ipady = 400; | |
gbc.weighty = 1; | |
panel.add(scrollPane, gbc); | |
textField.setVisible(false); | |
gbc.gridx = 0; | |
gbc.gridy = 2; | |
gbc.ipadx = 50; | |
gbc.ipady = 20; | |
gbc.anchor = GridBagConstraints.CENTER; | |
gbc.weightx = 0.5d; | |
gbc.weighty = 0.5d; | |
panel.add(textField, gbc); | |
okButton.setVisible(false); | |
gbc.gridy = 3; | |
gbc.ipadx = 100; | |
gbc.ipady = 40; | |
panel.add(okButton); | |
newGameItem.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
newGameAction(); | |
} | |
}); | |
saveGameItem.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent arg0) { | |
saveGameAction(); | |
} | |
}); | |
loadGameItem.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
loadGameAction(); | |
} | |
}); | |
okButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent arg0) { | |
okAction(textField.getText()); | |
textField.setVisible(false); | |
okButton.setVisible(false); | |
textField.setText(""); | |
} | |
}); | |
frame.add(panel); | |
frame.validate(); | |
} | |
private void newGameAction() { | |
JFileChooser fileChooser = new JFileChooser(); | |
fileChooser.showOpenDialog(frame); | |
game.getParser().loadFile(fileChooser.getSelectedFile()); | |
game.play(); | |
} | |
private void saveGameAction() { | |
if ( !game.isInProgress() ) { | |
return; | |
} | |
} | |
private void loadGameAction() { | |
} | |
private void okAction(String input) { | |
game.receiveInput(input); | |
} | |
public void output(String msg) { | |
textArea.append(msg + "\n"); | |
} | |
public void getInput() { | |
textField.setVisible(true); | |
okButton.setVisible(true); | |
textField.setText(""); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package adventuregame; | |
public class Main { | |
public static void main(String[] args) { | |
Game game = new Game(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package adventuregame; | |
import java.io.File; | |
import java.util.ArrayList; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.NamedNodeMap; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
public class Parser { | |
File file; | |
Document doc; | |
//Element storyNode; | |
Node storyNode; | |
//Element actualNode; | |
Node actualNode; | |
String actualAction; | |
public enum NodeType { | |
NARRATION, DECISION, FIGHT | |
} | |
public class Narration { | |
public String action, text; | |
} | |
public class Choice { | |
public String action, text; | |
} | |
public class Decision { | |
public String description; | |
public ArrayList<Choice> choices; | |
} | |
public class Fight { | |
public String enemyId, winAction, loseAction; | |
} | |
public void loadFile(File file) { | |
try { | |
this.file = file; | |
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); | |
doc = docBuilder.parse(file); | |
} catch( Exception e ) { | |
e.printStackTrace(); | |
} | |
} | |
public String parseTitle() { | |
return doc.getDocumentElement().getAttribute("title"); | |
} | |
public String parseAuthor() { | |
return doc.getDocumentElement().getAttribute("author"); | |
} | |
public ArrayList<Enemy> parseEnemies() { | |
ArrayList<Enemy> enemies = new ArrayList<Enemy>(); | |
NodeList nodeList = doc.getElementsByTagName("enemy"); | |
for ( int i = 0; i < nodeList.getLength(); ++i ) { | |
Node node = nodeList.item(i); | |
Element elem = (Element)node; | |
String id = elem.getAttribute("id"); | |
String name = elem.getTextContent(); | |
Enemy.Difficulty diff = Enemy.Difficulty.NOEFFORT; | |
switch ( Integer.parseInt(elem.getAttribute("difficulty")) ) { | |
case 1: diff = Enemy.Difficulty.NOEFFORT; break; | |
case 2: diff = Enemy.Difficulty.EASY; break; | |
case 3: diff = Enemy.Difficulty.MODERATE; break; | |
case 4: diff = Enemy.Difficulty.HARD; break; | |
case 5: diff = Enemy.Difficulty.INSANE; break; | |
} | |
Enemy enemy = new Enemy(id, name, diff); | |
enemies.add(enemy); | |
} | |
return enemies; | |
} | |
public NodeType readFirstElement() { | |
NodeList root = doc.getChildNodes(); | |
Node adventureNode = getNode("adventure", root); | |
storyNode = getNode("story", adventureNode.getChildNodes()); | |
NodeList l = storyNode.getChildNodes(); | |
for ( int i = 0; i < l.getLength(); ++i ) { | |
String name = l.item(i).getNodeName(); | |
if ( name == "narration" || name == "decision" || name == "fight" ) { | |
actualNode = l.item(i); | |
break; | |
} | |
} | |
return determineType(actualNode); | |
} | |
private NodeType determineType(Node node) { | |
if ( node.getNodeName() == "narration" ) { | |
return NodeType.NARRATION; | |
} | |
else if ( node.getNodeName() == "decision" ) { | |
return NodeType.DECISION; | |
} | |
else if ( node.getNodeName() == "fight" ) { | |
return NodeType.FIGHT; | |
} | |
//TODO: do this properly | |
return NodeType.NARRATION; | |
} | |
public String getAction() { | |
return actualAction; | |
} | |
public void setAction(String action) { | |
this.actualAction = action; | |
} | |
public Narration buildNarration() { | |
Narration n = new Narration(); | |
n.text = actualNode.getTextContent(); | |
//n.action = actualNode.getAttribute("action"); | |
n.action = getNodeAttr("action", actualNode); | |
return n; | |
} | |
public Decision buildDecision() { | |
Decision d = new Decision(); | |
d.choices = new ArrayList<Choice>(); | |
NodeList nodeList = actualNode.getChildNodes(); | |
for ( int i = 0; i < nodeList.getLength(); ++i ) { | |
if ( nodeList.item(i).getNodeName() == "description" ) { | |
d.description = nodeList.item(i).getTextContent(); | |
} | |
else if ( nodeList.item(i).getNodeName() == "choice" ) { | |
Choice c = new Choice(); | |
c.text = nodeList.item(i).getTextContent(); | |
Element elem = (Element)nodeList.item(i); | |
c.action = elem.getAttribute("action"); | |
d.choices.add(c); | |
} | |
} | |
return d; | |
} | |
public Fight buildFight() { | |
Fight f = new Fight(); | |
// f.enemyId = actualNode.getAttribute("enemy_id"); | |
// f.winAction = actualNode.getAttribute("win_action"); | |
// f.loseAction = actualNode.getAttribute("lose_action"); | |
return f; | |
} | |
public NodeType readNextElement() { | |
actualNode = findNode(actualAction); | |
return determineType(actualNode); | |
} | |
private Node findNode(String id) { | |
NodeList nodes = storyNode.getChildNodes(); | |
for ( int i = 0; i < nodes.getLength(); ++i ) { | |
if ( nodes.item(i).hasAttributes() ) { | |
if ( getNodeAttr("id", nodes.item(i)).equals(id) ) { | |
return nodes.item(i); | |
} | |
} | |
} | |
return null; | |
} | |
/* -----------------UTIL--------------------- */ | |
protected Node getNode(String tagName, NodeList nodes) { | |
for ( int x = 0; x < nodes.getLength(); x++ ) { | |
Node node = nodes.item(x); | |
if (node.getNodeName().equalsIgnoreCase(tagName)) { | |
return node; | |
} | |
} | |
return null; | |
} | |
protected String getNodeValue( Node node ) { | |
NodeList childNodes = node.getChildNodes(); | |
for (int x = 0; x < childNodes.getLength(); x++ ) { | |
Node data = childNodes.item(x); | |
if ( data.getNodeType() == Node.TEXT_NODE ) | |
return data.getNodeValue(); | |
} | |
return ""; | |
} | |
protected String getNodeValue(String tagName, NodeList nodes ) { | |
for ( int x = 0; x < nodes.getLength(); x++ ) { | |
Node node = nodes.item(x); | |
if (node.getNodeName().equalsIgnoreCase(tagName)) { | |
NodeList childNodes = node.getChildNodes(); | |
for (int y = 0; y < childNodes.getLength(); y++ ) { | |
Node data = childNodes.item(y); | |
if ( data.getNodeType() == Node.TEXT_NODE ) | |
return data.getNodeValue(); | |
} | |
} | |
} | |
return ""; | |
} | |
protected String getNodeAttr(String attrName, Node node ) { | |
NamedNodeMap attrs = node.getAttributes(); | |
for (int y = 0; y < attrs.getLength(); y++ ) { | |
Node attr = attrs.item(y); | |
if (attr.getNodeName().equalsIgnoreCase(attrName)) { | |
return attr.getNodeValue(); | |
} | |
} | |
return ""; | |
} | |
protected String getNodeAttr(String tagName, String attrName, NodeList nodes ) { | |
for ( int x = 0; x < nodes.getLength(); x++ ) { | |
Node node = nodes.item(x); | |
if (node.getNodeName().equalsIgnoreCase(tagName)) { | |
NodeList childNodes = node.getChildNodes(); | |
for (int y = 0; y < childNodes.getLength(); y++ ) { | |
Node data = childNodes.item(y); | |
if ( data.getNodeType() == Node.ATTRIBUTE_NODE ) { | |
if ( data.getNodeName().equalsIgnoreCase(attrName) ) | |
return data.getNodeValue(); | |
} | |
} | |
} | |
} | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment