ovcc-player.vala 3.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 
 * 
 * Copyright (C) 2009-2011 Colomban Wendling <ban@herbesfolles.org>
 *                         Jonathan Michalon <studios.chalmion@no-log.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

21

22 23
const int N_NORMAL = 7;
const int N_DOUBLE = 1;
24 25


26 27 28 29
namespace OVCC
{
  public class Player : Object
  {
30 31
    private List<Pawn>  _pawns     = new List<Pawn> ();
    private uint[] remaining_pawns = new uint[PawnKind.N_KINDS];
32 33
    
    public PlayerID id { get; private set; }
34
    public uint     score { get; set; }
35
    [Description (nick = "Nick", blurb = "Player's nickname")]
36 37 38
    public string   nick { get; construct;}
    public Game     game { get; private set; default = null; }
    public uint     n_remaining_pawns { get { return _pawns.length (); } }
39 40 41 42
    
    public Player (string nick)
    {
      Object (nick: nick);
43 44
      /* fill the list of available pawns */
      for (int i = 0; i < N_NORMAL; i++) {
45 46
        _pawns.prepend (new Pawn (PawnKind.NORMAL, this));
      }
47 48 49 50 51 52
      remaining_pawns[PawnKind.NORMAL] = N_NORMAL;
      
      for (int i = 0; i < N_DOUBLE; i++) {
        _pawns.prepend (new Pawn (PawnKind.DOUBLE, this));
      }
      remaining_pawns[PawnKind.DOUBLE] = N_DOUBLE;
53
    }
54 55 56 57 58

    public void joined (Game g)
    {
      game = g;
    }
59
    
60 61
    public bool place_tile (Position  pos)
      requires (game != null)
62
    {
63
      return game.place_tile (this, pos);
64
    }
65 66 67 68 69 70 71

    /**
     * Make the player place a pawn
     *
     * This function is the one to call when a player wants to place a pawn
     * on a TileObject.
     * 
72
     * @param kind The kind of pawn to place
73 74 75 76
     * @param object The TileObject where to place it
     * @param pos The position on the board of the TileObject given
     * @return Whether the operation succeeded
     */
77
    public bool place_pawn (PawnKind kind, TileObject object, Position pos)
78
      requires (game != null)
79
    {
80
      Pawn? pawn = null;
81 82 83
      if (get_remaining_pawn_count(kind) == 0) {
        return false;
      }
84
      foreach (var p in _pawns) {
85
        if (p.kind == kind) {
86 87 88 89 90
          pawn = p;
          break;
        }
      }
      if (pawn != null) {
91 92 93 94
        var done = game.place_pawn (pawn, object, pos);
        if (done) {
          _pawns.remove (pawn);
          remaining_pawns[kind]--;
95
          notify_property ("n-remaining-pawns");
96 97
        }
        return done;
98 99
      } else {
        return false;
100 101
      }
    }
102

103 104 105 106 107 108 109 110 111
    /**
     * Retreive a pawn from board
     * 
     * @param pawn The {@link Pawn}
     */
    public void retrieve_pawn (Pawn pawn)
    {
      _pawns.prepend (pawn);
      remaining_pawns[pawn.kind]++;
112
      notify_property ("n-remaining-pawns");
113 114 115 116 117 118 119 120 121 122 123 124 125 126
      pawn.remove();
    }

    /**
     * Get the count of pawns available to that player for a {@link PawnKind}
     * 
     * @param kind The kind of pawn
     * @return The count available
     */
    public uint get_remaining_pawn_count (PawnKind kind)
    {
      return remaining_pawns[kind];
    }

127 128 129 130 131 132 133 134
    /**
     * Checks whether we have a given pawn
     * 
     * @param pawn A pawn
     * @return Whether the given pawn belong us
     */
    public bool has_pawn (Pawn pawn)
    {
135
      return pawn.player == this;
136
    }
137 138
  }
}