Guest User

Untitled

a guest
May 26th, 2014
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. public class Player {
  2.     private final String name;
  3.     private final Hand hand;
  4.     private final Field field;
  5.     private final Deck deck;
  6.     private final Graveyard graveyard;
  7.    
  8.     private Game game;
  9.    
  10.     public Player(final String name, final Hand hand, final Field field, final Deck deck, final Graveyard graveyard) {
  11.         Objects.requireNonNull(name);
  12.         Arguments.requireMinimalLength(name, 1, "name");
  13.         this.name = name;
  14.         this.hand = Objects.requireNonNull(hand);
  15.         this.field = Objects.requireNonNull(field);
  16.         this.deck = Objects.requireNonNull(deck);
  17.         this.graveyard = Objects.requireNonNull(graveyard);
  18.     }
  19.    
  20.     public static Player createFromConfiguration(final PlayerConfiguration playerConfiguration, final String name) {
  21.         Objects.requireNonNull(playerConfiguration, "playerConfiguration");
  22.         Objects.requireNonNull(name, "name");
  23.         return new Player(
  24.                 name,
  25.                 new Hand(playerConfiguration.getHandCapacity()),
  26.                 new Field(playerConfiguration.getFieldMonsterCapacity()),
  27.                 Deck.newShuffledDeck(playerConfiguration.getDeckCards()),
  28.                 new Graveyard()
  29.         );
  30.     }
  31.    
  32.     public void setGame(final Game game) {
  33.         States.requireNull(this.game, IllegalStateException::new, "A game has already been assigned to this player");
  34.         this.game = Objects.requireNonNull(game);
  35.     }
  36.    
  37.     public void playTurn() {
  38.         Scanner scanner = new Scanner(System.in);
  39.        
  40.         new DrawCardAction().performAction(this);
  41.        
  42.         String command;
  43.         do {
  44.             command = scanner.next();
  45.             switch (command) {
  46.                 case "put":
  47.                     int handCardIndex = scanner.nextInt();
  48.                     int fieldCardIndex = scanner.nextInt();
  49.                     new PutMonsterOnFieldAction(handCardIndex, fieldCardIndex).performAction(this);
  50.                     break;
  51.                 case "attack":
  52.                     int selfMonsterIndex = scanner.nextInt();
  53.                     int opponentMonsterIndex = scanner.nextInt();
  54.                     new AttackMonsterAction(selfMonsterIndex, opponentMonsterIndex, getOpponent()).performAction(this);
  55.                     break;
  56.                 default:
  57.                     System.out.println("Unrecognized command");
  58.                     break;
  59.             }
  60.         } while (!command.equals("end"));
  61.     }
  62.    
  63.     public Game getGame() {
  64.         return game;
  65.     }
  66.    
  67.     public String getName() {
  68.         return name;
  69.     }
  70.    
  71.     public Hand getHand() {
  72.         return hand;
  73.     }
  74.    
  75.     public Field getField() {
  76.         return field;
  77.     }
  78.    
  79.     public Deck getDeck() {
  80.         return deck;
  81.     }
  82.    
  83.     public Graveyard getGraveyard() {
  84.         return graveyard;
  85.     }
  86.    
  87.     private Player getOpponent() {
  88.         return game.getPlayers().filter(x -> x != this).findFirst().orElseThrow(() -> new IllegalStateException("No opponent has been found"));
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment