Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Shangrila.java.old 2008-04-28 18:40:17.000000000 -0700
- +++ Shangrila.java 2008-04-28 19:13:20.000000000 -0700
- @@ -2,14 +2,16 @@
- import java.awt.Color;
- import javax.swing.JOptionPane;
- +import java.lang.reflect.*;
- +
- public class Shangrila
- {
- - public static final int NUM_PLAYERS = 2;
- - public static final int INITIAL_MATCH_SIZE = 1;
- + public static int NUM_PLAYERS = 2;
- + public static int INITIAL_MATCH_SIZE = 1;
- private static Player[] players = new Player[NUM_PLAYERS];
- private static int numPlayers = 0;
- private static int[] gamesWon = new int[NUM_PLAYERS];
- private static int[] cumulativeScores = new int[NUM_PLAYERS];
- private static int gamesPlayed, gamesRemaining;
- @@ -28,17 +30,39 @@
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- - addPlayer(HumanPlayer.class, numPlayers);
- - if (NUM_PLAYERS > 1)
- - addPlayer(Methodical.class, numPlayers);
- - if (NUM_PLAYERS > 2)
- - addPlayer(Sleepy.class, numPlayers);
- - if (NUM_PLAYERS > 3)
- - addPlayer(Sleepy.class, numPlayers);
- + try
- + {
- + if (args != null && args.length > 0)
- + {
- + //NUM_PLAYERS = args.length;
- + NUM_PLAYERS = 3;
- + System.out.println("Args: " + args.length);
- + players = new Player[NUM_PLAYERS];
- + gamesWon = new int[NUM_PLAYERS];
- + cumulativeScores = new int[NUM_PLAYERS];
- + for (String player : args)
- + {
- + addPlayer(Class.forName(player), numPlayers);
- + System.out.println(player);
- + }
- + }
- + else throw new Exception("");
- + }
- + catch (Exception e)
- + {
- + NUM_PLAYERS = 2;
- + addPlayer(HumanPlayer.class, numPlayers);
- + if (NUM_PLAYERS > 1)
- + addPlayer(Methodical.class, numPlayers);
- + if (NUM_PLAYERS > 2)
- + addPlayer(Sleepy.class, numPlayers);
- + if (NUM_PLAYERS > 3)
- + addPlayer(Sleepy.class, numPlayers);
- + }
- boardClass = Board.Board10.class;
- gamesPlayed = 0;
- gamesRemaining = INITIAL_MATCH_SIZE - 1;
- @@ -55,21 +79,30 @@
- }
- @SuppressWarnings("unchecked")
- public static void addPlayer(Class cls, Color c1, Color c2)
- {
- - Player player;
- - if (cls == HumanPlayer.class)
- - player = new HumanPlayer(c1, c2);
- - else if (cls == Sleepy.class)
- - player = new Sleepy(c1, c2);
- - else if (cls == Methodical.class)
- - player = new Methodical(c1, c2);
- - else
- - throw new RuntimeException("Unknown Player class");
- -
- - players[numPlayers++] = player;
- + try
- + {
- + Player player;
- + Constructor<Player> ctor = cls.getConstructor(Color.class, Color.class);
- + player = ctor.newInstance(c1, c2);
- + players[numPlayers++] = player;
- + }
- + catch (NoSuchMethodException nsme)
- + {
- + throw new RuntimeException("'" + cls.getName() + "' No Such Constructor -- your Player needs to " +
- + "have a Player(Color, Color) signature constructor.");
- + }
- + catch (InstantiationException ie)
- + {
- + throw new RuntimeException("Your player could not be instantiated");
- + }
- + catch (Exception e)
- + {
- + throw new RuntimeException(e); // lazy
- + }
- }
- public static void onGameOver()
- {
- gamesPlayed++;
Add Comment
Please, Sign In to add comment