Commit 689be033 authored by Colomban Wendling's avatar Colomban Wendling

Rewrite Player and Pawn in Vala

parent b7a8acb7
......@@ -6,8 +6,8 @@ libovcc_la_SOURCES = enumtypes.c \
ovcc-marshal.c \
board.vala \
game.c \
pawn.c \
player.c \
pawn.vala \
player.vala \
stack.vala \
tile.vala \
tileobject.vala \
......@@ -16,8 +16,7 @@ libovcc_la_SOURCES = enumtypes.c \
utils.vala \
xmlutils.c
ovccinclude_HEADERS = ovcc.h \
game.h \
player.h
game.h
test_LDADD = libovcc.la -lpthread
test_SOURCES = test.c
......
/*
*
* Copyright (C) 2009 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/>.
*
*/
#include "pawn.h"
#include <glib.h>
#include "player.h"
/**
* SECTION: pawn
* @short_description: Implementation of OVCC pawns
* @include: libovcc/pawn.h
*
* This is the API for managing pawns in OVCC.
*/
/**
* OVCCPawn:
*
* The opaque structure representing any pawn (like meeple, architect).
*/
struct _OVCCPawn
{
OVCCPawnKind kind;
OVCCPlayerID playerid;
};
/**
* ovcc_pawn_new:
* @kind: The #OVCCPawnKind representing which type of pawn this one is.
* @playerid: The #OVCCPlayerID identifying the player it belongs to.
*
* Creates a new #OVCCPawn.
*
* Returns: The newly created #OVCCPawn.
*/
OVCCPawn *
ovcc_pawn_new (OVCCPawnKind kind,
OVCCPlayerID playerid)
{
OVCCPawn *pawn;
pawn = g_malloc (sizeof *pawn);
if (pawn) {
pawn->kind = kind;
pawn->playerid = playerid;
}
return pawn;
}
/**
* ovcc_pawn_free:
* @pawn: The #OVCCPawn to free.
*
* Frees an #OVCCPawn.
*/
void
ovcc_pawn_free (OVCCPawn *pawn)
{
if (pawn) {
g_free (pawn);
}
}
/**
* ovcc_pawn_goto_board:
* @pawn: The #OVCCPawn to move.
* @player: An #OVCCPlayer, whom belongs the pawn.
* @board: An #OVCCBoard, on which to put the pawn.
* @pos: The #OVCCPosition where to put the pawn on the board.
*
* Puts a pawn from a player on the board.
*/
void
ovcc_pawn_goto_board (OVCCPawn *pawn,
OVCCPlayer *player,
OVCCBoard *board,
OVCCPosition *pos)
{
}
/**
* ovcc_pawn_release:
* @pawn: The #OVCCPawn to release.
* @player: An #OVCCPlayer, whom belongs the pawn.
* @board: An #OVCCBoard, from where it is to be released.
* @pos: The #OVCCPosition where the pawn is currently on the board.
*
* Releases a pawn from a given position of the board to his player.
*/
void
ovcc_pawn_release (OVCCPawn *pawn,
OVCCPlayer *player,
OVCCBoard *board,
OVCCPosition *pos)
{
}
/*
*
* Copyright (C) 2009 Colomban Wendling <ban@herbesfolles.org>
* Jonathan Michalon <studios.chalmion@no-log.org>
* 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
......@@ -18,37 +18,27 @@
*
*/
#ifndef H_OVCC_PAWN
#define H_OVCC_PAWN
#include <glib.h>
G_BEGIN_DECLS
typedef struct _OVCCPawn OVCCPawn;
/**
* OVCCObjectKind:
* @OVCC_PAWN_MEEPLE: The pawn is a meeple
* "A small person-shaped figure used as a player's
* token in a board game" (WP)
* @OVCC_PAWN_BIG_MEEPLE: The pawn is a big meeple
* @OVCC_PAWN_ARCHITECT: The pawn is an architect
*
* Different kind of pawns possible.
*/
typedef enum
namespace OVCC
{
OVCC_PAWN_MEEPLE,
OVCC_PAWN_BIG_MEEPLE,
OVCC_PAWN_ARCHITECT
} OVCCPawnKind;
G_END_DECLS
#endif /* guard */
public enum PawnKind
{
NORMAL,
DOUBLE,
ARCHITECT,
PIG
}
public class Pawn : Object
{
[Description (nick = "Kind", blurb = "The kind of this pawn")]
public PawnKind kind { get; construct set; }
[Description (nick = "Player", blurb = "The player owning this pawn")]
public Player player { get; construct set; }
public Pawn (PawnKind kind,
Player player)
{
Object (kind: kind, player: player);
}
}
}
/*
*
* Copyright (C) 2009 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/>.
*
*/
#include "player.h"
#include <glib.h>
#include "tile.h"
#include "board.h"
/**
* SECTION: player
* @short_description: Implementation of OVCC players
* @include: libovcc/player.h
*
* This is the API of the players for OVCC.
*/
#define DEFAULT_NICK "Anonymous"
struct _OVCCPlayerPrivate
{
OVCCPlayerID id;
GSList *pawns;
guint score;
gchar *nick;
};
enum
{
PROP_0,
PROP_NICK
};
G_DEFINE_TYPE (OVCCPlayer, ovcc_player, G_TYPE_OBJECT)
static void
ovcc_player_finalize (GObject *object)
{
OVCCPlayer *self = OVCC_PLAYER (object);
g_free (self->priv->nick);
G_OBJECT_CLASS (ovcc_player_parent_class)->finalize (object);
}
static void
ovcc_player_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
OVCCPlayer *self = OVCC_PLAYER (object);
switch (prop_id)
{
case PROP_NICK:
ovcc_player_set_nick (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
ovcc_player_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
OVCCPlayer *self = OVCC_PLAYER (object);
switch (prop_id)
{
case PROP_NICK:
g_value_set_string (value, ovcc_player_get_nick (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
ovcc_player_init (OVCCPlayer *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
OVCC_TYPE_PLAYER,
OVCCPlayerPrivate);
self->priv->id = 0;
self->priv->pawns = NULL;
self->priv->score = 0;
self->priv->nick = g_strdup (DEFAULT_NICK);
}
static void
ovcc_player_class_init (OVCCPlayerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = ovcc_player_finalize;
object_class->set_property = ovcc_player_set_property;
object_class->get_property = ovcc_player_get_property;
g_object_class_install_property (object_class, PROP_NICK,
g_param_spec_string ("nick",
"Nick",
"Player's nickname",
DEFAULT_NICK,
G_PARAM_READWRITE));
g_type_class_add_private (klass, sizeof (OVCCPlayerPrivate));
}
/**
* ovcc_player_new:
* @nick: Nickname of the new player.
*
* Creates a new player.
*
* Returns: A new #OVCCPlayer
*/
OVCCPlayer *
ovcc_player_new (const gchar *nick)
{
return g_object_new (OVCC_TYPE_PLAYER, "nick", nick, NULL);
}
/**
* ovcc_player_add_tile:
* @board: An #OVCCBoard, on which to add the tile.
* @tile: An #OVCCTile, the tile to add.
* @pos: The #OVCCPosition where to add the tile on the board.
*
* This routine manages the request of a player to add a tile on the
* board, making all tests needed.
*
* Returns: whether the addition was possible and is effective.
*/
gboolean
ovcc_player_add_tile (OVCCBoard *board,
OVCCTile *tile,
OVCCPosition *pos)
{
return ovcc_board_add_tile (board, tile, pos);
}
/**
* ovcc_player_set_nick:
* @player: An #OVCCPlayer
* @nick: The new nickname of the player (non-%NULL).
*
* Sets the nickname of a player.
*/
void
ovcc_player_set_nick (OVCCPlayer *player,
const gchar *nick)
{
g_return_if_fail (OVCC_IS_PLAYER (player));
g_return_if_fail (nick != NULL);
g_free (player->priv->nick);
player->priv->nick = g_strdup (nick);
}
/**
* ovcc_player_get_nick:
* @player: An #OVCCPlayer
*
* Gets the nickname of a player.
*
* Returns: the nickname of @player.
*/
const gchar *
ovcc_player_get_nick (const OVCCPlayer *player)
{
g_return_val_if_fail (OVCC_IS_PLAYER (player), NULL);
return player->priv->nick;
}
/*
*
* Copyright (C) 2009 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/>.
*
*/
#ifndef H_OVCC_PLAYER
#define H_OVCC_PLAYER
#include <glib.h>
#include "tile.h"
#include "board.h"
G_BEGIN_DECLS
#define OVCC_TYPE_PLAYER (ovcc_player_get_type ())
#define OVCC_PLAYER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), OVCC_TYPE_PLAYER, OVCCPlayer))
#define OVCC_IS_PLAYER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), OVCC_TYPE_PLAYER))
#define OVCC_PLAYER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), OVCC_TYPE_PLAYER, OVCCPlayerClass))
#define OVCC_IS_PLAYER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), OVCC_TYPE_PLAYER))
#define OVCC_PLAYER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), OVCC_TYPE_PLAYER), OVCCPlayerClass)
typedef struct _OVCCPlayer OVCCPlayer;
typedef struct _OVCCPlayerPrivate OVCCPlayerPrivate;
typedef struct _OVCCPlayerClass OVCCPlayerClass;
/**
* OVCCPlayer:
*
* The object representing a player.
*/
struct _OVCCPlayer
{
GObject parent_instance;
/*< private >*/
OVCCPlayerPrivate *priv;
};
struct _OVCCPlayerClass
{
GObjectClass parent_class;
};
/**
* OVCCPlayerID:
*
* Type representing a Player ID.
*
* A player ID is unique, and is used to distinguish them.
*/
typedef guint OVCCPlayerID;
GType ovcc_player_get_type (void) G_GNUC_CONST;
OVCCPlayer *ovcc_player_new (const gchar *nick);
void ovcc_player_set_nick (OVCCPlayer *player,
const gchar *nick);
const gchar *ovcc_player_get_nick (const OVCCPlayer *player);
gboolean ovcc_player_add_tile (OVCCBoard *board,
OVCCTile *tile,
OVCCPosition *pos);
G_END_DECLS
#endif /* guard */
/*
*
* 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/>.
*
*/
namespace OVCC
{
public struct PlayerID : uint {}
public class Player : Object
{
private List<Pawn> _pawns = new List<Pawn> ();
private string _nick = "Anonymous";
public PlayerID id { get; private set; }
public uint score { get; private set; }
[Description (nick = "Nick", blurb = "Player's nickname")]
public string nick {
get { return _nick; }
set { _nick = value; }
}
public Player (string nick)
{
Object (nick: nick);
}
/* FIXME: WTF if this method? */
public bool add_tile (Board board,
Tile tile,
Position pos)
{
return board.add_tile (tile, pos);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment