Exemplo n.º 1
0
/* tries opening a file in a temporary win structure, and displays errors if unable */
int
board_load_popup (window_board_t *win, int append, char *filename)
{
	int i;
	int ret = 0;
	window_board_t *win1 = calloc (1, sizeof (window_board_t));

	if (board_load (win1, filename)) {
		if (append) {
			int n = win->n_boards;
			for (i = 0; i < win1->n_boards; i++)
				board_window_append_board (win, win1->boards[i]);
			if (win->n_boards > n) /* set to first new board */
				win->cur = n;
		} else {
			MOVE_PTR (win->filename, win1->filename);
			MOVE_PTR (win->title, win1->title);
			MOVE_PTR (win->subtitle, win1->subtitle);
			MOVE_PTR (win->team1, win1->team1);
			MOVE_PTR (win->team2, win1->team2);

			for (i = 0; i < win->n_boards; i++)
				if (win->boards[i])
					board_free (win->boards[i]);
			win->boards = win1->boards;
			win->n_boards = win1->n_boards;
			win->n_boards_alloc = win1->n_boards_alloc;
			win->cur = 0;
		}

		card_window_update(win->boards[win->cur]->dealt_cards);
		show_board(win->boards[win->cur], REDRAW_FULL);
		recently_used_add (filename);

		ret = 1;
	} else {
		GtkWidget *error = gtk_message_dialog_new (GTK_WINDOW (win->window),
				GTK_DIALOG_DESTROY_WITH_PARENT,
				GTK_MESSAGE_ERROR,
				GTK_BUTTONS_CLOSE,
				_("Error loading file '%s': %s"),
				filename, g_strerror (errno));
		gtk_dialog_run (GTK_DIALOG (error));
		gtk_widget_destroy (error);
	}

	TRY_FREE (win1->filename);
	TRY_FREE (win1->title);
	TRY_FREE (win1->subtitle);
	TRY_FREE (win1->team1);
	TRY_FREE (win1->team2);
	free (win1);

	return ret;
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
    FILE *f;
    board_t board;
    char *line = malloc(128);
    int verbose = 0;

    if(argc > 1 && strcmp(argv[1], "-v") == 0) {
        verbose = 1;
    }

    if((argc < 2) || (verbose && argc < 3)) {
        fprintf(stderr, "Syntax:\n\tsudooku [-v] <file>\n\n");
        return -1;
    }


    if((f = fopen(argv[verbose ? 2 : 1], "r")) == NULL) {
        fprintf(stderr, "Error opening file '%s'\n", argv[verbose ? 2 : 1]);
        return -1;
    }

    while (!feof(f) && (fgets(line, 128, f) != NULL)) {
        if(strlen(line) == 82) {
            board_init(&board);
            board_load(&board, line);
            if(verbose) {
                board_dump(stdout, &board);
            }
            if(board_solve(&board) != 0) {
                fprintf(stderr, "Error solving\n");
            }
            if(verbose) {
                board_dump(stdout, &board);
            }
        }
    }

    fclose(f);
    free(line);
    exit(0);
}
Exemplo n.º 3
0
int
main (int argc, char *argv[])
{
#ifdef ENABLE_NLS
  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
#endif

  gtk_set_locale ();
  gtk_init (&argc, &argv);

  init_solve();
  srand(time(NULL));

  win = malloc(sizeof(window_board_t));
  char *xml_file = NULL;

  int i;
  for (i = 0; i < sizeof (xml_files); i++) {
	  struct stat buf;
	  if (stat (xml_files[i], &buf) != -1) {
		  xml_file = xml_files[i];
		  g_debug ("Using interface file %s\n", xml_file);
		  break;
	  }
  }
  if (! xml_file) {
	  fprintf (stderr, _("Could not find interface definition file: %s\n"), "tenace.ui");
	  exit (1);
  }

  GError *error = NULL;
  win->builder = gtk_builder_new ();
  if (! gtk_builder_add_from_file (win->builder, xml_file, &error)) {
	  g_warning (_("Could not load builder file: %s"), error->message);
	  g_error_free (error);
	  exit (1);
  }

  gtk_builder_connect_signals (win->builder, NULL);

  board_window_init (win);
  read_config (win);
  board_window_apply_style (win);

  if (argc > 1) {
	char *fname = argv[1];
	if (! g_path_is_absolute (argv[1])) {
		char *cwd = g_get_current_dir ();
		fname = g_build_filename (cwd, argv[1], NULL);
		g_free (cwd);
	}
	if (! board_load (win, fname)) {
		if (errno == EMEDIUMTYPE) {
			perror (fname);
			puts (_("Hint: tenace can only read files in .lin format"));
		} else
			perror (fname);
		exit (1);
	}
	recently_used_add (fname);
	if (! g_path_is_absolute (argv[1]))
		g_free (fname);
  } else {
	board_window_append_board (win, NULL);
	deal_random (win->boards[0]); /* show a random deal on startup */
  }

  show_board(win->boards[0], REDRAW_FULL);
  card_window_update(win->boards[0]->dealt_cards);

  gtk_main ();
  write_config (win);
  return 0;
}
Exemplo n.º 4
0
Arquivo: test.c Projeto: mkghub/pachi
void
unittest(char *filename)
{
	FILE *f = fopen(filename, "r");
	if (!f) {
		perror(filename);
		exit(EXIT_FAILURE);
	}

	int total = 0;
	int passed = 0;
	int skipped = 0;
	
	struct board *b = board_init(NULL);
	b->komi = 7.5;
	char line[256];

	while (fgets(line, sizeof(line), f)) {
		line[strlen(line) - 1] = 0; // chomp
		switch (line[0]) {
			case '%': printf("\n%s\n", line); continue;
			case '!': printf("%s...\tSKIPPED\n", line); skipped++; continue;
			case 0: continue;
		}
		if (!strncmp(line, "boardsize ", 10)) {
			board_load(b, f, atoi(line + 10));  continue;
		}
		if (!strncmp(line, "ko ", 3)) {
			set_ko(b, line + 3);  continue;
		}
		
		total++;
		if (!strncmp(line, "sar ", 4))
			passed += test_sar(b, line + 4); 
		else if (!strncmp(line, "ladder ", 7))
			passed += test_ladder(b, line + 7);
		else if (!strncmp(line, "useful_ladder ", 14))
			passed += test_useful_ladder(b, line + 14);
		else if (!strncmp(line, "can_countercap ", 15))
			passed += test_can_countercapture(b, line + 15); 
		else if (!strncmp(line, "two_eyes ", 9))
			passed += test_two_eyes(b, line + 9); 
		else if (!strncmp(line, "moggy moves ", 12)) 
			passed += test_moggy_moves(b, line + 12);
		else if (!strncmp(line, "moggy status ", 13)) 
			passed += test_moggy_status(b, line + 13);
   		else if (!strncmp(line, "board_undo_stress_test", 22))
			passed += board_undo_stress_test(b, line + 22); 
		else {
			fprintf(stderr, "Syntax error: %s\n", line);
			exit(EXIT_FAILURE);
		}
	}

	fclose(f);
	
	printf("\n\n----------- [  %i/%i tests passed (%i%%)  ] -----------\n\n", passed, total, passed * 100 / total);
 	if (total == passed)
		printf("\nAll tests PASSED");
	else {
		printf("\nSome tests FAILED\n");
		exit(EXIT_FAILURE);
	}
	if (skipped > 0)
		printf(", %d test(s) SKIPPED", skipped);
	printf("\n");
}