Commit 04cdeb3b authored by Jonathan Michalon's avatar Jonathan Michalon

OVCCGtk: few discussed tide ups

parent d7fbae9f
......@@ -38,277 +38,6 @@
#define GAME_TILESET "tileset.xml"
/* bad copy of the bot implemented in libovcc's test */
static gboolean
do_try_place_tile (OVCCBoard *board,
OVCCPosition *pos,
OVCCTile *tile,
gpointer user_data)
{
gsize i;
gboolean keep_doing = TRUE;
OVCCTile *new_tile = user_data;
g_return_val_if_fail (new_tile != NULL, FALSE);
for (i = 0; keep_doing && i < 4; i++) {
OVCCPosition npos = *pos;
int r;
switch (i) {
case 0: npos.y--; break;
case 1: npos.y++; break;
case 2: npos.x--; break;
case 3: npos.x++; break;
}
for (r = 0; keep_doing && r < 4; r++) {
ovcc_tile_rotate (new_tile, 1);
if (ovcc_board_add_tile_check (board, new_tile, &npos)) {
ovcc_board_add_tile (board, new_tile, &npos);
keep_doing = FALSE;
}
}
}
return keep_doing;
}
static gboolean
try_place_tile (OVCCBoard *board,
OVCCTile *tile)
{
return ovcc_board_foreach (board, do_try_place_tile, tile) == FALSE;
}
static gboolean
test_board (OVCCBoard *board,
OVCCStack *stack)
{
OVCCTile *tile;
gsize n_success = 0;
gsize n_failed = 0;
while (NULL != (tile = ovcc_stack_pop (stack))) {
if (! try_place_tile (board, tile)) {
g_warning ("Failed to place tile %u", ovcc_tile_get_id (tile));
n_failed ++;
} else {
n_success ++;
}
g_object_unref (tile);
}
g_debug ("%zu tiles over %zu placed (%.3u%%)",
n_success, (n_success + n_failed),
n_success * 100 / (n_success + n_failed));
return n_failed == 0;
}
static gboolean
game_do_try_place_tile (OVCCBoard *board,
OVCCPosition *pos,
OVCCTile *tile,
gpointer user_data)
{
gsize i;
gboolean keep_doing = TRUE;
OVCCGame *game = user_data;
OVCCTile *new_tile = ovcc_game_get_current_tile (game);
g_return_val_if_fail (new_tile != NULL, FALSE);
for (i = 0; keep_doing && i < 4; i++) {
OVCCPosition npos = *pos;
int r;
switch (i) {
case 0: npos.y--; break;
case 1: npos.y++; break;
case 2: npos.x--; break;
case 3: npos.x++; break;
}
for (r = 0; keep_doing && r < 4; r++) {
if (ovcc_game_place_tile (game, ovcc_game_get_current_player (game), &npos)) {
keep_doing = FALSE;
} else {
ovcc_tile_rotate (new_tile, 1);
}
}
}
return keep_doing;
}
static gboolean
game_try_place_tile (OVCCGame *game,
OVCCTile *tile)
{
OVCCBoard *board;
board = ovcc_game_get_board (game);
return ovcc_board_foreach (board, game_do_try_place_tile, game) == FALSE;
}
struct _PlaceNextData {
OVCCGame *game;
guint n_success;
guint n_failed;
};
static gboolean
place_next (gpointer data)
{
struct _PlaceNextData *pdata = data;
OVCCTile *tile;
tile = ovcc_game_get_current_tile (pdata->game);
if (! tile) {
g_debug ("%u tiles over %u placed (%.3u%%)",
pdata->n_success, (pdata->n_success + pdata->n_failed),
(pdata->n_success + pdata->n_failed) > 0
? pdata->n_success * 100 / (pdata->n_success + pdata->n_failed)
: 0);
return FALSE;
}
if (! game_try_place_tile (pdata->game, tile)) {
g_warning ("Failed to place tile %u", ovcc_tile_get_id (tile));
pdata->n_failed ++;
} else {
pdata->n_success ++;
}
return TRUE;
}
static void
test_game (OVCCGame *game)
{
struct _PlaceNextData *pdata;
pdata = g_malloc (sizeof *pdata);
pdata->game = game;
pdata->n_success = 0;
pdata->n_failed = 0;
g_timeout_add_full (G_PRIORITY_DEFAULT, 100, place_next, pdata, g_free);
}
static OVCCStack *
create_stack (void)
{
gchar *path;
OVCCTilesDef *tiles;
GFile *tiles_file;
OVCCTileSet *set;
GFile *set_file;
OVCCStack *stack = NULL;
GError *err = NULL;
tiles = ovcc_tiles_def_new ();
path = data_get_path (GAME_TILES);
tiles_file = g_file_new_for_path (path);
g_free (path);
if (! ovcc_tiles_def_load (tiles, tiles_file, &err)) {
msg_error (_("Failed to load tiles: %s"), err->message);
g_error_free (err);
} else {
set = ovcc_tile_set_new ();
path = data_get_path (GAME_TILESET);
set_file = g_file_new_for_path (path);
g_free (path);
if (! ovcc_tile_set_load (set, tiles, set_file, &err)) {
msg_error (_("Failed to load tile set: %s"), err->message);
g_error_free (err);
} else {
stack = ovcc_stack_new_from_tileset (set);
}
g_object_unref (set_file);
g_object_unref (set);
}
g_object_unref (tiles_file);
g_object_unref (tiles);
return stack;
}
static OVCCBoard *
create_board (void)
{
OVCCStack *stack;
OVCCBoard *board = NULL;
stack = create_stack ();
if (stack) {
OVCCTile *tile;
tile = ovcc_stack_pop (stack);
board = ovcc_board_new (tile);
g_object_unref (tile);
test_board (board, stack);
}
g_object_unref (stack);
return board;
}
static void
on_unplaceable_tile (OVCCGame *game,
OVCCTile *tile,
gpointer data)
{
g_warning ("Cannot place tile #%u, reshaking.", ovcc_tile_get_id (tile));
}
static OVCCGame *
create_game (void)
{
gchar *path;
OVCCTilesDef *tiles;
GFile *tiles_file;
OVCCTileSet *set;
GFile *set_file;
OVCCGame *game = NULL;
GError *err = NULL;
tiles = ovcc_tiles_def_new ();
path = data_get_path (GAME_TILES);
tiles_file = g_file_new_for_path (path);
g_free (path);
if (! ovcc_tiles_def_load (tiles, tiles_file, &err)) {
msg_error (_("Failed to load tiles: %s"), err->message);
g_error_free (err);
} else {
set = ovcc_tile_set_new ();
path = data_get_path (GAME_TILESET);
set_file = g_file_new_for_path (path);
g_free (path);
if (! ovcc_tile_set_load (set, tiles, set_file, &err)) {
msg_error (_("Failed to load tile set: %s"), err->message);
g_error_free (err);
} else {
game = ovcc_game_new (set, NULL);
g_signal_connect (game, "unplaceable-tile",
G_CALLBACK (on_unplaceable_tile), NULL);
ovcc_game_add_player (game, ovcc_player_new ("A"), NULL);
ovcc_game_add_player (game, ovcc_player_new ("B"), NULL);
}
g_object_unref (set_file);
g_object_unref (set);
}
g_object_unref (tiles_file);
g_object_unref (tiles);
return game;
}
/* end of bad game fake */
typedef struct _App App;
struct _App
......@@ -447,9 +176,9 @@ update_tile_image (App *app)
gtk_image_set_from_pixbuf (GTK_IMAGE (app->current_tile_image), pix);
g_object_unref (pix);
} else {
gtk_image_set_from_stock (GTK_IMAGE (app->current_tile_image),
GTK_STOCK_MISSING_IMAGE,
GTK_ICON_SIZE_LARGE_TOOLBAR);
gtk_image_set_from_icon_name (GTK_IMAGE (app->current_tile_image),
"image-missing",
GTK_ICON_SIZE_LARGE_TOOLBAR);
}
}
......
......@@ -756,13 +756,6 @@ draw_tile_cb (OVCCBoard *board,
g_object_unref (pix);
draw_pawns (self, pos, tile, dest_x, dest_y, size);
if (pos->x == 0 && pos->y == 0) {
cairo_set_source_rgb (self->priv->cr, 0.0, 0.0, 0.0);
cairo_arc (self->priv->cr, dest_x + size / 2.0, dest_y + size / 2.0,
size / 4.0, 0.0, G_PI * 2);
cairo_stroke (self->priv->cr);
}
}
return TRUE;
......
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