[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

More....



Ok, the game can be compiled using the attached command in the previous
message.  Attached here is the source code game.c.  Let me know what you
think!
#include <gtk/gtk.h> 
#include <unistd.h>
#include <stdio.h>

#define XMAX 6
#define YMAX 5

GtkWidget *boxes[XMAX][YMAX];
GtkWidget *labels[XMAX][YMAX];

int owners[XMAX][YMAX];
int bombs[XMAX][YMAX];

int players = 2;
int turn;
int ingame;

void set_box(int x, int y, int owner, int bombsnew) {
  char buffer[20];
  owners[x][y] = owner;
  bombs[x][y] = bombsnew;
  sprintf (buffer, "%d:%d", owner, bombsnew);
  gtk_label_set (GTK_LABEL (labels[x][y]), buffer);
}

int do_explosions(void) {
  int i, j, y, z, a, b, count;
  int box;    /* 1=corner 2=edge 3=center */
  int explode;
  int explosions = 0;
  for (i=0; i<XMAX; i++)
    for (j=0; j<YMAX; j++) {
      explode = 0;
      if ( (i==0 || i==(XMAX-1)) && (j==0 || j==(YMAX-1)) )
        box = 1;
      else if (i==0 || i==(XMAX-1) || j==0 || j==(YMAX-1))
        box = 2;
      else
        box = 3;
      if (bombs[i][j] > box) {  /* Bang! */
        explosions++;
        set_box(i, j, 0, 0);
        for (count=0; count<4; count++) {
          switch (count) {
            case 0 : y=-1; z=0; break;
            case 1 : y=1; z=0; break;
            case 2 : y=0; z=1; break;
            case 3 : y=0; z=-1; break;
          }
          a = i + y;
          b = j + z;
          if (a>=0 && a<XMAX && b>=0 && b<YMAX)
            set_box(a, b, turn, bombs[a][b]+1);            
        }
        gtk_widget_grab_focus(GTK_WIDGET(boxes[i][j]));
        while (gtk_events_pending())
          gtk_main_iteration();
        sleep(1);
/*
        if (gameover()) {
          ingame = 0;  
          return 0;
        ]
*/
      }  /* Bang! */      
    }
  return explosions;
}

int gameover(void) {
  int i, j;
  int last=0;
  for (i=0; i<XMAX; i++)
    for (j=0; j<YMAX; j++) {
      if (!owners[i][j])
        continue;
      if (last)
        if (last != owners[i][j])
          return 0;
      last = owners[i][j];
    }
  return last;
}

void newgame(void) {
  int i, j;
  for (i = 0; i<XMAX; i++)
    for (j = 0; j<YMAX; j++)
      set_box(i, j, 0, 0);
  turn = 1;
  ingame = 1;
}


void delete_event (GtkWidget *widget, GdkEvent *event, gpointer *data)
{
    gtk_main_quit ();
}

static void
clicked_box (GtkWidget *widget,
               GtkWidget *button)      
{
  int i, j, x, y;
/*  if (!ingame)
    return; */
  for (x=0; x<XMAX; x++)
    for (y=0; y<YMAX; y++)
      if (button == boxes[x][y]) {
        i=x; j=y;
      }
  if (owners[i][j] == 0)
    set_box (i, j, turn, 1);
  else if (owners[i][j] == turn)
    set_box (i, j, turn, bombs[i][j] + 1);
  else  /* if (owners[i][j] > 0 && owners[i][j] != turn) */
    return;
  while (do_explosions()) { }
  turn++;
  if (turn > players)
    turn = 1;
}

int main (int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *table;
    GtkWidget *box;
    int i, j;
    char buffer[30];

    gtk_init (&argc, &argv);   
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);  
    gtk_window_set_title (GTK_WINDOW (window), "Chain Reaction");  
    gtk_signal_connect (GTK_OBJECT (window), "delete_event",
                        GTK_SIGNAL_FUNC (delete_event), NULL);
    gtk_container_border_width (GTK_CONTAINER (window), 10);   

    box = gtk_vbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (window), box);
    gtk_widget_show (box);

    table = gtk_table_new (XMAX, YMAX, FALSE);
    gtk_table_set_row_spacings (GTK_TABLE (table), 5);
    gtk_table_set_col_spacings (GTK_TABLE (table), 5);
    gtk_container_border_width (GTK_CONTAINER (table), 10);
    gtk_box_pack_start (GTK_BOX (box), table, TRUE, TRUE, 0);
    gtk_widget_show (table);

    for (i=0; i<XMAX; i++)
      for (j=0; j<YMAX; j++) {
        sprintf (buffer, "(%d,%d)", i, j);
        boxes[i][j] = gtk_button_new();
        labels[i][j] = gtk_label_new (buffer);
        gtk_signal_connect (GTK_OBJECT (boxes[i][j]), "clicked",
                GTK_SIGNAL_FUNC (clicked_box), boxes[i][j] );
        gtk_table_attach (GTK_TABLE (table), boxes[i][j],
                i, i + 1, j, j + 1,
                GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
        gtk_container_add (GTK_CONTAINER (boxes[i][j]), labels[i][j]);
        gtk_widget_show ( labels[i][j] );
        gtk_widget_show ( boxes[i][j] );
      }

    gtk_widget_show (window); 
    newgame();
    gtk_main ();   
    return 0;
}