int x;
int y;
int selected;
+ int owned;
+ int in_hand;//Draw the tile in the hand, or elsewhere on the board?
} tile_t;
#define DECK_MAX_TILES 104
int num_tiles;
} tile_group_t;
-#define BOARD_MAX_TILE_GROUPS (DECK_MAX_TILES / 3)
+#define BOARD_MAX_TILE_GROUPS DECK_MAX_TILES
typedef struct board {
tile_group_t groups[BOARD_MAX_TILE_GROUPS];
#define GAME_WINDOW_DEFAULT_WIDTH 800
#define GAME_WINDOW_DEFAULT_HEIGHT 600
+typedef struct state {
+ player_t players[GAME_MAX_PLAYERS];
+ board_t board;
+ deck_t deck;
+} state_t;
+
typedef struct game {
player_t players[GAME_MAX_PLAYERS];
int num_players;
board_t board;
deck_t deck;
selection_box_t selection_box;
+ state_t state;
RsvgHandle *blanktile;
RsvgHandle *selectedtile;
+ RsvgHandle *ownedtile;
- int current_tile;
+ int current_player;
+ tile_t *current_tile;
+// int current_tile;
int select_mode;
+ int drag_group_mode;
int diff_x, diff_y;
int click_x, click_y;
int release_x, release_y; /*Currently unused*/
tile->x = 0;
tile->y = 0;
tile->selected = 0;
+ tile->owned = 0;
+ tile->in_hand = 1;
}
static void tile_set_x_y (tile_t *tile, int x, int y)
if (tile->selected)
rsvg_handle_render_cairo (game->selectedtile, cr);
+ if (tile->owned)
+ rsvg_handle_render_cairo (game->ownedtile, cr);
else
rsvg_handle_render_cairo (game->blanktile, cr);
{
const tile_t *tile_one = one;
const tile_t *tile_two = two;
- return tile_one->number - tile_two->number;
+ return tile_one->number - tile_two->number; //Sort lowest to highest
+}
+
+static int int_compare(const void *vx, const void *vy)
+{
+ const int *x = vx, *y = vy;
+ return *y - *x; //Sort highest to lowest
}
+/*
+static int tile_in_box(game_t *game, tile_t *tile)
+{
+
+}
+*/
+
static int tile_group_is_run_one(tile_group_t *tile_group)
{
int i;
return 1;
}
+static gboolean board_valid(board_t *board)
+{
+ int i;
+ gboolean valid = TRUE;
+ for (i = 0; i < board->num_groups; ++i)
+ {
+ if (!(tile_group_is_run_one(&board->groups[i])) &&
+ !(tile_group_is_set(&board->groups[i])))
+ {
+ valid = FALSE;
+ }
+ }
+ return valid;
+}
+
static void deck_deal(game_t *game, deck_t *deck)
{
tile_t temp;
temp = deck->tiles[rand_tile];
deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
game->players[i].hand.tiles[j] = temp;
+ game->players[i].hand.tiles[j].owned = 1;
deck->num_tiles -= 1;
game->players[i].hand.num_tiles += 1;
}
}
}
+static void board_draw(game_t *game, cairo_t *cr, GdkRegion *region)
+{
+ int i, j;
+ for (i = 0; i < game->board.num_groups; i++)
+ {
+ for (j = 0; j < game->board.groups[i].num_tiles; j++)
+ {
+ tile_draw(game, &game->board.groups[i].tiles[j], cr, region);
+ //tile_print(game->board.groups[i].tiles[j]);
+ }
+ }
+}
+
+static void board_print(game_t *game)
+{
+ int i, j;
+ printf("\tBegin board print\n");
+ for (i = 0; i < game->board.num_groups; i++)
+ {
+ printf("\tBegin group %d\n", i);
+ for (j = 0; j < game->board.groups[i].num_tiles; j++)
+ {
+ tile_print(game->board.groups[i].tiles[j]);
+ }
+ printf("\tEnd group %d\n", i);
+ }
+}
+
static void hand_print(game_t *game, int player)
{
int i;
}
}
-static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
+static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region, GtkWidget *widget)
{
int i;
- int gwdw = GAME_WINDOW_DEFAULT_WIDTH;
- int gwdh = GAME_WINDOW_DEFAULT_HEIGHT;
- for (i = 0; i < game->players[player].hand.num_tiles; i++)
+ int width = widget->allocation.width;
+// int width = GAME_WINDOW_DEFAULT_WIDTH;
+ int height = widget->allocation.height;
+// int height = GAME_WINDOW_DEFAULT_HEIGHT;
+ int num_tiles = game->players[player].hand.num_tiles;
+ for (i = 0; i < num_tiles; i++)
{
- tile_set_x_y(&game->players[player].hand.tiles[i],
- ((gwdw / game->players[player].hand.num_tiles)) * i,
- (gwdh - TILE_HEIGHT - 6) );
+ if (game->players[player].hand.tiles[i].in_hand)
+ tile_set_x_y(&game->players[player].hand.tiles[i],
+ ((width/num_tiles)) * i, (height - TILE_HEIGHT - 6) );
}
for (i = 0; i < game->players[player].hand.num_tiles; i++)
{
- tile_draw(game, &game->players[player].hand.tiles[i], cr, region);
+// if (game->players[player].hand.tiles[i].in_hand)
+ tile_draw(game, &game->players[player].hand.tiles[i], cr, region);
}
}
+static void save_state(game_t *game)
+{
+ printf("State saved\n");
+ game->state.board = game->board;
+ game->state.deck = game->deck;
+ int i;
+ for (i=0; i < game->num_players; i++)
+ game->state.players[i] = game->players[i];
+}
+
+static void restore_state(game_t *game)
+{
+ game->board = game->state.board;
+ game->deck = game->state.deck;
+ int i;
+ for (i=0; i < game->num_players; i++)
+ game->players[i] = game->state.players[i];
+}
+
static void game_init(game_t *game)
{
int i;
player_init(&game->players[i]);
game->num_players += 1;
}
+ game->current_player = 0;
selection_box_init(&game->selection_box);
board_init(&game->board);
deck_init(&game->deck);
deck_shuffle(&game->deck);
- game->selectedtile = rsvg_handle_new_from_file ("tiles/selectedtile.svg", &error);
+ game->selectedtile = rsvg_handle_new_from_file ("tiles/selected_tile.svg", &error);
+ if (error)
+ FATAL_ERROR (error->message);
+
+ game->ownedtile = rsvg_handle_new_from_file ("tiles/owned_tile.svg", &error);
if (error)
FATAL_ERROR (error->message);
- game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
+ game->blanktile = rsvg_handle_new_from_file ("tiles/blank_tile.svg", &error);
if (error)
FATAL_ERROR (error->message);
- game->current_tile = game->deck.num_tiles - 1;
+// game->current_tile = game->deck.num_tiles - 1;
+ game->current_tile = &game->deck.tiles[0];
game->select_mode = 1;
+ game->drag_group_mode = 0;
game->diff_x = game->diff_y = 0;
}
cr = gdk_cairo_create (widget->window);
deck_draw(game, cr, event->region);
+ board_draw(game, cr, event->region);
if (game->selection_box.visible)
selection_box_draw(&game->selection_box, cr);
- hand_draw(game, 0, cr, event->region);
+ hand_draw(game, game->current_player, cr, event->region, widget);
+ //hand_draw(game, 0, cr, event->region);
cairo_destroy (cr);
return TRUE;
}
+/*
+static gboolean on_configure_event (GtkWidget *widget, GdkEventConfigure *event, game_t *game)
+{
+ cairo_t *cr;
+ cr = gdk_cairo_create (event->window);
+
+ hand_draw(game, game->current_player, cr, event->window, widget);
+// gtk_widget_queue_draw(widget);
+}
+*/
static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
{
+ player_t *player = &game->players[game->current_player];
+ cairo_t *cr;
+ cr = gdk_cairo_create (widget->window);
printf ("You pressed key %d\n", event->keyval);
+ if (event->keyval == 100) //HIT "d"
+ {
+ //Draw(take) top tile from deck and put in hand
+ if (game->deck.num_tiles > 0)
+ {
+ tile_t top_tile = game->deck.tiles[game->deck.num_tiles-1];
+ game->deck.num_tiles--;
+ player->hand.tiles[player->hand.num_tiles] = top_tile;
+ player->hand.tiles[player->hand.num_tiles].owned=1;
+ player->hand.num_tiles++;
+ printf("Tile added to hand\n");
+ gtk_widget_queue_draw(widget);
+ }
+ }
+ else if (event->keyval == 115) //HIT "s"
+ save_state(game);
+ else if (event->keyval == 65293) //HIT ENTER
+ {
+ player_t *player = &game->players[game->current_player];
+ int i;
+ for (i = 0; i < player->hand.num_tiles; i++)
+ player->hand.tiles[i].in_hand = 1;
+ save_state(game);
+ printf ("\tEnd of player %d's turn\n", game->current_player+1);
+ if (game->current_player == game->num_players-1)
+ game->current_player = 0;
+ else
+ game->current_player += 1;
+ gtk_widget_queue_draw(widget);
+ }
+ else if (event->keyval == 65307) //HIT ESCAPE
+ {
+ restore_state(game);
+ printf ("\tChanges Reverted\n");
+ gtk_widget_queue_draw(widget);
+ }
+ else if (event->keyval == 65474) //HIT "F5"
+ {
+ gtk_widget_queue_draw(widget);
+ }
+ else if (event->keyval == 65505 || event->keyval == 65506)
+ game->drag_group_mode = 1;
return TRUE;
}
static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
{
int i, tile_x, tile_y;
- tile_t *curr_tile = &game->deck.tiles[game->current_tile];
+ tile_t *curr_tile;
+ player_t *curr_player = &game->players[game->current_player];
+ /*Handle tiles in player's hand */
+ for (i = 0; i < curr_player->hand.num_tiles; i++)
+ {
+ curr_tile = &curr_player->hand.tiles[i];
+ if (curr_tile->selected)
+ {
+ curr_tile->selected = 0;
+ gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+ }
+ tile_x = curr_player->hand.tiles[i].x;
+ tile_y = curr_player->hand.tiles[i].y;
+ if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
+ event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
+ {
+ game->select_mode = 0;
+ game->current_tile = curr_tile;
+ curr_tile->in_hand = 0;
+ if (!curr_tile->selected)
+ curr_tile->selected = 1;
+ else
+ curr_tile->selected = 0;
+ gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+ game->diff_x = event->x - tile_x;
+ game->diff_y = event->y - tile_y;
+ }
+ }
+
+ /*Handle tiles in deck */
for (i = 0; i < game->deck.num_tiles; i++)
{
+ curr_tile = &game->deck.tiles[i];
+ if (curr_tile->selected)
+ {
+ curr_tile->selected = 0;
+ gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+ }
+
tile_x = game->deck.tiles[i].x;
tile_y = game->deck.tiles[i].y;
if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
{
game->select_mode = 0;
- curr_tile = &game->deck.tiles[game->current_tile];
- game->deck.tiles[game->current_tile].selected = 0;
- gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+// game->current_tile = i;
+ game->current_tile = curr_tile;
- game->current_tile = i;
- curr_tile = &game->deck.tiles[game->current_tile];
+//delete_this? curr_tile = &game->deck.tiles[game->current_tile];
if (!curr_tile->selected)
curr_tile->selected = 1;
else
}
if (game->select_mode)
{
- game->deck.tiles[game->current_tile].selected = 0;
+// game->deck.tiles[game->current_tile].selected = 0;
+ game->current_tile->selected = 0;
gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
game->selection_box.visible = 1;
- /*These next two lines are likely to be replaced by...*/
+ /*These next two lines appear to be dead
game->click_x = event->x;
- game->click_y = event->y;
- /*...these two lines*/
+ game->click_y = event->y;*/
+
game->selection_box.x1 = event->x;
game->selection_box.x2 = event->x;
game->selection_box.y1 = event->y;
group.num_tiles = 0;
int i, tile_x, tile_y, tile_x2, tile_y2;
+ int tiles_to_remove[game->deck.num_tiles];
for (i = 0; i < game->deck.num_tiles; i++)
{
tile_x = game->deck.tiles[i].x;
/*or bottom-left corner*/
(tile_x >= x_min && tile_x <= x_max &&
tile_y2 >= y_min && tile_y2 <= y_max) ||
- /*or top-right corner of tile selected*/
+ /*or top-right corner*/
(tile_x2 >= x_min && tile_x2 <= x_max &&
tile_y >= y_min && tile_y <= y_max) ||
/*or left edge*/
/*or right edge*/
(y_min >= tile_y && y_min <= tile_y2 &&
x_min >= tile_x && x_min <= tile_x2) ||
- /*or bottom edge*/
+ /*or bottom edge of tile selected*/
(x_min >= tile_x && x_min <= tile_x2 &&
y_min >= tile_y && y_min <= tile_y) )
{
+ tiles_to_remove[group.num_tiles] = i;
+
group.tiles[group.num_tiles] = game->deck.tiles[i];
group.num_tiles++;
}
}
printf("is run %d\n", tile_group_is_run_one(&group) );
printf("is set %d\n", tile_group_is_set(&group) );
+
+ qsort (tiles_to_remove, group.num_tiles, sizeof (int), int_compare);
+
for (i = 0; i < group.num_tiles; i++)
+ {
tile_print(group.tiles[i]);
+ gtk_widget_queue_draw_area (widget, group.tiles[i].x - 1,
+ group.tiles[i].y - 1, TILE_WIDTH + 1,
+ TILE_HEIGHT + 2);
+
+ group.tiles[i].x = x_min + (i * (TILE_WIDTH));
+ group.tiles[i].y = y_min;
+
+ gtk_widget_queue_draw_area (widget, group.tiles[i].x - 1,
+ group.tiles[i].y - 1, TILE_WIDTH + 1,
+ TILE_HEIGHT + 2);
+
+ //Remove tile from deck
+ if (tiles_to_remove[i] != game->deck.num_tiles - 1)
+ game->deck.tiles[tiles_to_remove[i]] = game->deck.tiles[game->deck.num_tiles-1];
+ game->deck.num_tiles--;
+ }
+
+ if (group.num_tiles > 0)
+ {
+ game->board.groups[game->board.num_groups] = group;
+ game->board.num_groups++;
+ }
+ board_print(game);
+ printf("\nBut is the board valid?\t\t%s\n", board_valid(&game->board) ? "yes" : "no");
}
game->select_mode = 1;
box = &game->selection_box;
box->visible = 1;
- gtk_widget_queue_draw_area ( widget, MIN(box->x1, box->x2), MIN(box->y1,box->y2), abs(box->x2 - box->x1), abs(box->y2 - box->y1) );
+ int x_min = MIN(box->x1, box->x2);
+ int x_max = MAX(box->x1, box->x2);
+ int y_min = MIN(box->y1, box->y2);
+ int y_max = MAX(box->y1, box->y2);
+ int width = abs(box->x2 - box->x1);
+ int height = abs(box->y2 - box->y1);
+
+ gtk_widget_queue_draw_area ( widget, x_min, y_min, width, height );
box->x2 = event->x;
box->y2 = event->y;
gtk_widget_queue_draw_area ( widget, MIN(box->x1, box->x2), MIN(box->y1, box->y2), abs(box->x2 - box->x1), abs(box->y2 - box->y1) );
- int i;
+ int i, tile_x, tile_y, tile_x2, tile_y2;
tile_t *curr_tile;
for (i = 0; i < game->deck.num_tiles; i++)
- {
- if ((event->x >= game->deck.tiles[i].x &&
- event->x <= game->deck.tiles[i].x + TILE_WIDTH) &&
- (event->y >= game->deck.tiles[i].y &&
- event->y <= game->deck.tiles[i].y + TILE_HEIGHT))
+ {
+ curr_tile = &game->deck.tiles[i];
+
+ tile_x = game->deck.tiles[i].x;
+ tile_y = game->deck.tiles[i].y;
+ tile_x2 = tile_x + TILE_WIDTH;
+ tile_y2 = tile_y + TILE_HEIGHT;
+ if (/*If top-left corner*/
+ (tile_x >= x_min && tile_x <= x_max &&
+ tile_y >= y_min && tile_y <= y_max) ||
+ /*or bottom-right corner*/
+ (tile_x2 >= x_min && tile_x2 <= x_max &&
+ tile_y2 >= y_min && tile_y2 <= y_max) ||
+ /*or bottom-left corner*/
+ (tile_x >= x_min && tile_x <= x_max &&
+ tile_y2 >= y_min && tile_y2 <= y_max) ||
+ /*or top-right corner*/
+ (tile_x2 >= x_min && tile_x2 <= x_max &&
+ tile_y >= y_min && tile_y <= y_max) ||
+ /*or left edge*/
+ (y_min >= tile_y && y_min <= tile_y2 &&
+ x_min <= tile_x && x_max >= tile_x) ||
+ /*or top edge*/
+ (x_min >= tile_x && x_min <= tile_x2 &&
+ y_min <= tile_y && y_max >= tile_y) ||
+ /*or right edge*/
+ (y_min >= tile_y && y_min <= tile_y2 &&
+ x_min >= tile_x && x_min <= tile_x2) ||
+ /*or bottom edge of tile selected*/
+ (x_min >= tile_x && x_min <= tile_x2 &&
+ y_min >= tile_y && y_min <= tile_y) )
{
- curr_tile = &game->deck.tiles[i];
curr_tile->selected = 1;
gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
}
+
+ else
+ {
+ if (curr_tile->selected)
+ {
+ curr_tile->selected = 0;
+ gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+ }
+ }
}
}
else
{
+ if (game->drag_group_mode)
+ {
+
+ }
+
tile_t *tile;
- tile = &game->deck.tiles[game->current_tile];
+// tile = &game->deck.tiles[game->current_tile];
+ tile = game->current_tile;
/* First, invalidate the region where the tile currently is. */
gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
deck_print(&game.deck);
deck_spread(&game.deck);
deck_deal(&game, &game.deck);
+
+ game.state.board = game.board;
+ game.state.deck = game.deck;
+ int i;
+ for (i=0; i < game.num_players; i++)
+ game.state.players[i] = game.players[i];
+
//hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0])
//deck_print(&game.deck);
G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (G_OBJECT (window), "expose_event",
G_CALLBACK (on_expose_event), &game);
+// g_signal_connect (G_OBJECT (window), "configure_event",
+// G_CALLBACK (on_configure_event), &game);
g_signal_connect (G_OBJECT (window), "key_press_event",
G_CALLBACK (on_key_press_event), &game);
g_signal_connect (G_OBJECT (window), "button_press_event",