User:Jtwe

From Dvorak - A Blank-Card Game
Jump to navigationJump to search
package demo;

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class Dvorak {

	public class Card {
		private HashMap<String, String> attrHash = new HashMap<String, String>();
		private final Pattern p = Pattern.compile("^\\s*(\\S+)\\s*=\\s*(.+?)\\s*$");

		public boolean parseToken(String attr) {
			if (attr.toLowerCase().equals("card")) return true;

			boolean dbug = false;
			Matcher m = p.matcher(attr);
			if (m.find()) {
				String at = m.group(1).toLowerCase();
				String val = m.group(2).replaceAll("\"", "\"\"");;
				if (dbug) System.out.println("In parseToken, attr = " + attr + ", at = <" + at + ">, val = <" + val + ">");

				attrHash.put(at, val);
				return true;
			}
			if (dbug) System.out.println("In parseToken, attr = " + attr + ", no match");
			return false;
		}

		public String toString() {
			return "\"" + attrHash.get("title") + "\",\"" + (attrHash.get("bgcolor")!=null?attrHash.get("bgcolor"):"") 
			+ "\",\"" + (attrHash.get("cornervalue")!=null?attrHash.get("cornervalue"):"")
			+ "\",\"" + attrHash.get("type") + "\",\"" + attrHash.get("text") + "\",\"" + attrHash.get("creator") + "\"";
		}

		public Card() {
		}

		public String getAttribute(String attrName) {
			return attrHash.get(attrName);
		}

		public void setAttribute(String attrName, String attrVal) {
			attrHash.put(attrName, attrVal);
		}
	}

	private ArrayList cards;

	public void printAll() {
		printAll(new PrintWriter(System.out), -1);
	}

	public void printAll(PrintWriter pw) {
		printAll(pw, -1);
	}

	public void printAll(PrintWriter pw, int startingAt) {
		for (int i=0; i<cards.size(); i++) {
			pw.println((startingAt>=0?((i+startingAt) + ","):"") + cards.get(i));
		}
		pw.flush();
	}

	public Dvorak(String filename) throws IOException {
		boolean dbug = false;
		cards = new ArrayList<Card>();

		BufferedReader br = new BufferedReader(new FileReader(filename));
		String lineIn = ".";
		String currText = "";
		Card currCard = null;
		while (lineIn!=null && br.ready()) {
			lineIn = br.readLine().trim();
			if (dbug) System.out.println("Contructor read line: " + lineIn);
			if (lineIn.length()==0) continue;

			currText = (currText + " " + lineIn).trim();
			boolean done = false;
			while (!done && currText!=null && currText.length()>0) {
				if (currCard != null) {
					int pipe = currText.indexOf('|');
					int end = currText.indexOf("}}");
					if (pipe<0 && end<0) {
						done = true;
					} else {
						int sep = Math.min(pipe, end);
						if (pipe<0) sep=end; else if (end<0) sep=pipe;

						String token = currText.substring(0, sep);
						if (!currCard.parseToken(token)) System.out.println("Card = " + currCard.getAttribute("title") + ", token rejected: " + token);
						currText = currText.substring(sep + (sep==end?2:1)).trim();
						if (sep==end) {
							cards.add(currCard);
							currCard = null;
						}
					}
				} else {
					int start = currText.indexOf("{{");
					if (start<0) {
						done = true;
					} else {
						currCard = new Card();
						currText = currText.substring(start+2);
					}
				}
			}
		}

	}

	private Dvorak() {
	}

	public static void main2(String[] args) {
		Dvorak d = new Dvorak();
		Card c = d.new Card();
		c.parseToken(" BgColor = 660000 ");
		c.parseToken("corner=1U");
		c.parseToken("creator=Kevan");
		c.parseToken("text = Take an extra \"turn\" after \tthis one.");
		c.parseToken(" title=Time, Walk  ");
		c.parseToken("TYPE=Action");
		System.out.println(c);
	}

	public static void main(String[] args) throws IOException {
		if (args.length<2) {
			System.out.println("Usage: Dvorak [inputfile] [outputfile] [first number, default 1])");
			return;
		}
		int firstNum = -1;
		if (args.length>2) {
			try {
				firstNum = Integer.parseInt(args[2]);
			} catch (NumberFormatException e) {
				System.out.println(args[2] + " is not an integer, not numbering");
			}
		}
		Dvorak d = new Dvorak(args[0]);
		d.printAll(new PrintWriter(new FileWriter(args[1])), firstNum);
	}

}