Guest User

Untitled

a guest
Apr 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. --- Shangrila.java.old 2008-04-28 18:40:17.000000000 -0700
  2. +++ Shangrila.java 2008-04-28 19:13:20.000000000 -0700
  3. @@ -2,14 +2,16 @@
  4.  
  5. import java.awt.Color;
  6.  
  7. import javax.swing.JOptionPane;
  8.  
  9. +import java.lang.reflect.*;
  10. +
  11. public class Shangrila
  12. {
  13. - public static final int NUM_PLAYERS = 2;
  14. - public static final int INITIAL_MATCH_SIZE = 1;
  15. + public static int NUM_PLAYERS = 2;
  16. + public static int INITIAL_MATCH_SIZE = 1;
  17. private static Player[] players = new Player[NUM_PLAYERS];
  18. private static int numPlayers = 0;
  19. private static int[] gamesWon = new int[NUM_PLAYERS];
  20. private static int[] cumulativeScores = new int[NUM_PLAYERS];
  21. private static int gamesPlayed, gamesRemaining;
  22. @@ -28,17 +30,39 @@
  23. /**
  24. * @param args
  25. */
  26. public static void main(String[] args)
  27. {
  28. - addPlayer(HumanPlayer.class, numPlayers);
  29. - if (NUM_PLAYERS > 1)
  30. - addPlayer(Methodical.class, numPlayers);
  31. - if (NUM_PLAYERS > 2)
  32. - addPlayer(Sleepy.class, numPlayers);
  33. - if (NUM_PLAYERS > 3)
  34. - addPlayer(Sleepy.class, numPlayers);
  35. + try
  36. + {
  37. + if (args != null && args.length > 0)
  38. + {
  39. + //NUM_PLAYERS = args.length;
  40. + NUM_PLAYERS = 3;
  41. + System.out.println("Args: " + args.length);
  42. + players = new Player[NUM_PLAYERS];
  43. + gamesWon = new int[NUM_PLAYERS];
  44. + cumulativeScores = new int[NUM_PLAYERS];
  45. + for (String player : args)
  46. + {
  47. + addPlayer(Class.forName(player), numPlayers);
  48. + System.out.println(player);
  49. + }
  50. + }
  51. + else throw new Exception("");
  52. + }
  53. + catch (Exception e)
  54. + {
  55. + NUM_PLAYERS = 2;
  56. + addPlayer(HumanPlayer.class, numPlayers);
  57. + if (NUM_PLAYERS > 1)
  58. + addPlayer(Methodical.class, numPlayers);
  59. + if (NUM_PLAYERS > 2)
  60. + addPlayer(Sleepy.class, numPlayers);
  61. + if (NUM_PLAYERS > 3)
  62. + addPlayer(Sleepy.class, numPlayers);
  63. + }
  64.  
  65. boardClass = Board.Board10.class;
  66.  
  67. gamesPlayed = 0;
  68. gamesRemaining = INITIAL_MATCH_SIZE - 1;
  69. @@ -55,21 +79,30 @@
  70. }
  71.  
  72. @SuppressWarnings("unchecked")
  73. public static void addPlayer(Class cls, Color c1, Color c2)
  74. {
  75. - Player player;
  76. - if (cls == HumanPlayer.class)
  77. - player = new HumanPlayer(c1, c2);
  78. - else if (cls == Sleepy.class)
  79. - player = new Sleepy(c1, c2);
  80. - else if (cls == Methodical.class)
  81. - player = new Methodical(c1, c2);
  82. - else
  83. - throw new RuntimeException("Unknown Player class");
  84. -
  85. - players[numPlayers++] = player;
  86. + try
  87. + {
  88. + Player player;
  89. + Constructor<Player> ctor = cls.getConstructor(Color.class, Color.class);
  90. + player = ctor.newInstance(c1, c2);
  91. + players[numPlayers++] = player;
  92. + }
  93. + catch (NoSuchMethodException nsme)
  94. + {
  95. + throw new RuntimeException("'" + cls.getName() + "' No Such Constructor -- your Player needs to " +
  96. + "have a Player(Color, Color) signature constructor.");
  97. + }
  98. + catch (InstantiationException ie)
  99. + {
  100. + throw new RuntimeException("Your player could not be instantiated");
  101. + }
  102. + catch (Exception e)
  103. + {
  104. + throw new RuntimeException(e); // lazy
  105. + }
  106. }
  107.  
  108. public static void onGameOver()
  109. {
  110. gamesPlayed++;
Add Comment
Please, Sign In to add comment