Guest User

Untitled

a guest
Sep 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. package pokemon;
  2. import java.io.IOException;
  3. public class Game {
  4. public static enum Type {
  5. FIRE, WATER, GRASS, GROUND, ELECTRIC;
  6. boolean Advantage(Type t){
  7. if(this == FIRE)
  8. return t == GRASS;
  9. else if(this == WATER)
  10. return (t == FIRE || t==GROUND);
  11. else if(this == GRASS)
  12. return (t == WATER || t==GROUND);
  13. else if(this == GROUND)
  14. return (t == ELECTRIC);
  15. else if(this == ELECTRIC)
  16. return t == WATER;
  17. return false;
  18. }
  19. }
  20.  
  21. public Player one;
  22. public Player two;
  23. private Player[] players;
  24. PokeIO io;
  25. private int turn;
  26. /*
  27. * choosePlayer = gets the user to choose a player
  28. *@no params
  29. *@return a player depending on which input - 0 for P1, 1 for P2 - the player returns*/
  30. public Player choosePlayer(){
  31. Player p = null;
  32. while(p == null){
  33. System.out.println("Choose a player by their index");
  34. for(int i=0;i<players.length;i++){
  35. System.out.println(i+":"+players[i].name+", ");
  36. }
  37. try{
  38. String strindex = io.getNextInput();
  39. p = players[Integer.parseInt(strindex)];
  40. }catch(NumberFormatException e){
  41. System.out.println("Please type in a number next time...*sigh*");
  42. }catch(IOException e){
  43. System.out.println("IOEXception!");
  44.  
  45. }catch(ArrayIndexOutOfBoundsException e){
  46. System.out.println("That isn't a valid player index, try again");
  47. }
  48. }
  49. return p;
  50. }
  51.  
  52. /*
  53. * chooseFieldPoke = chooses a Pokemon of each player
  54. *@param p: a player
  55. *@returns a pokemon based on which number the user chose*/
  56. public Pokemon chooseFieldPoke(Player p){
  57. Pokemon m = null;
  58. while (m == null){
  59. int index = choose(p);
  60. System.out.println("chose " + index);
  61. if(index == -1)
  62. return p.field.battlestation;
  63. try{
  64. return p.field.bench[index];}
  65. catch(ArrayIndexOutOfBoundsException e){
  66. System.out.println("not a bench position!");}
  67. }
  68. return m;
  69. }
  70.  
  71. /*
  72. * choose = takes in a player and returns an integer input (which the user gives into the IO method)
  73. *@param p: a player
  74. *@returns the int the player chose: is used for chooseFieldPoke*/
  75. public int choose(Player p){
  76. System.out.println("Choose Pokemon (index), -1 is the index for battlestation");
  77. int choice = -2;
  78. while(choice < -1 || choice >= p.field.bench.length){
  79. try{
  80. String strindex = io.getNextInput();
  81. return Integer.parseInt(strindex);
  82. }catch(NumberFormatException e){
  83. System.out.println("Please type in a number next time...*sigh*");
  84. }catch(IOException e){
  85. System.out.println("IOEXception!");
  86. }
  87. }
  88. return choice;
  89. }
  90.  
  91. /*
  92. * Game = takes in 2 player names, and 2 decks
  93. *@param name1: player1 name
  94. * *@param name2: player2 named
  95. * *@param deck1: player1 deck
  96. * *@param deck2: player2 deck
  97. *@initialises the game, returns the object of game. is the constructor*/
  98. public Game(String name1, String name2, String deck1, String deck2){
  99. one = new Player(name1,Deck.createDeck(deck1));
  100. two = new Player(name2,Deck.createDeck(deck2));
  101. players = new Player[2];
  102. players[0] = one;
  103. players[1]= two;
  104. io = new PokeIO();
  105. turn = 0;
  106. /*
  107. * nextTurn = goes to the next turn
  108. *@no params
  109. *@returns the next turn*/
  110. }
  111. private void nextTurn(){
  112. turn = (turn+1)%(players.length);
  113. }
  114. /*
  115. * getNextPlayer = goes to the next turn
  116. *@no params
  117. *@returns the next turn*/
  118. public Player getNextPlayer(){
  119. return players[(turn+1)%players.length];
  120. }
  121. /*
  122. * gets the current player
  123. * @return Player, the current player of the game*/
  124. public Player getPlayer(){
  125. return players[turn];
  126. }
  127.  
  128. /*
  129. * prints the fields of both players
  130. * @return void
  131. * */
  132. private void printFields(){
  133. for(Player p : players){
  134. System.out.println(p.name+":");
  135. p.field.printField();
  136. }
  137. }
  138. /*
  139. * prints the hands of both the players
  140. * @return void
  141. * */
  142. private void printHands(){
  143. for(Player p : players){
  144. System.out.print(p.name+":");
  145. p.printHand();
  146. System.out.println(p.name+" has "+p.deck.size()+" cards in the deck");
  147. }
  148. }
  149. /*
  150. * uses the given card in a game
  151. * @param index, the int of the index of the current player's hand where the card resides
  152. * @return void, mutates the game so the cards effect is felt
  153. * */
  154. private void playCard(int index){
  155. if(getPlayer().hand.isEmpty())
  156. System.out.println("You have no cards left!");
  157. else
  158. getPlayer().useCard(index, this);
  159. }
  160. /*
  161. * the current player attack the next player
  162. * if an attack is possible
  163. * @return boolean, whether or not the player has won(attacked an empty battlefield)
  164. * */
  165. private boolean attack(){
  166.  
  167. if(getNextPlayer().field.battlestation == null){
  168. System.out.println(players[turn].name+ " WINS!");
  169. return true;
  170. }
  171. else{
  172. Pokemon opponent = getNextPlayer().field.battlestation;
  173. if(getPlayer().field.battlestation.attack(opponent))
  174. getNextPlayer().field.battlestation = null;
  175. }
  176.  
  177. return false;
  178. }
  179.  
  180. /*
  181. * runs a turn of the game
  182. * @return boolean, whether or not the game is over
  183. * */
  184. private boolean turn(){
  185. getPlayer().draw();
  186. String action = "";
  187. while(! (action.equals("pass") || action.equals("attack"))){
  188. try{
  189. System.out.print(players[turn].name+":");
  190. action = io.getNextInput();
  191. String[] words = action.split(" ");
  192.  
  193. if(action.equals("print field"))
  194. printFields();
  195.  
  196. else if(action.equals("print hand"))
  197. printHands();
  198.  
  199. else if(words[0].equals("play") && words.length>1){
  200. try{playCard(Integer.parseInt(words[1]));}
  201. catch(NumberFormatException e){
  202. System.out.println("didn't type in a valid number");}
  203. }
  204.  
  205. else if(words[0].equals("switch")&& words.length>1){
  206. try{
  207. getPlayer().switchPoke(Integer.parseInt(words[1]));
  208. }catch(NumberFormatException e){
  209. System.out.println("Please type in a number next time...*sigh*");
  210. }
  211. }
  212. else if(action.equals("attack")){
  213. if(getPlayer().field.hasAttackable()){
  214. if(attack()){return true;};
  215. }
  216. else{
  217. action = "";//so we don't end the turn'
  218. System.out.println("You don't have an attackable pokemon in your battlestation");
  219. }
  220. }
  221. }catch(IOException e){
  222. System.out.println("weird input, try again!");
  223. }
  224. }
  225. getPlayer().endTurn();
  226. nextTurn();
  227. return false;
  228. }
  229.  
  230. /*
  231. * plays a given game
  232. * @return void
  233. * */
  234. public void playGame(){
  235. boolean done = false;
  236. while (done == false){
  237. done = turn();
  238. }
  239. }
  240.  
  241.  
  242. public static void main(String[] args){
  243. Game g = new Game("Charlie","Kenneth","cards","cards");
  244. g.playGame();
  245. }
  246. }
Add Comment
Please, Sign In to add comment