int FindWinner (void) { int i; int winner = 0; int total; WinningScore = 0; FreeUndoRedoLists (); for (i = 0; i < NumberOfPlayers; ++i) { total = total_score (i); if (total > WinningScore) { WinningScore = total_score (i); winner = i; } } /* Detect a drawn game. Returning the negative of the score * is a bit of a hack, but it allows us to find out who the winners * were without having to pass around a list. */ for (i = 0; i < NumberOfPlayers; ++i) { total = total_score (i); if ((total == WinningScore) && (i != winner)) return -total; } return winner; }
static void CheerWinner (void) { int winner; int i; gint pos; gchar *message; ShowoffPlayer (ScoreList, CurrentPlayer, 0); winner = FindWinner (); /* draw. The score is returned as a negative value */ if (winner < 0) { for (i = 0; i < NumberOfPlayers; i++) { if (total_score (i) == -winner) { ShowoffPlayer (ScoreList, i, 1); } } say (_("The game is a draw!")); return; } ShowoffPlayer (ScoreList, winner, 1); if (winner < NumberOfHumans) { pos = games_scores_add_plain_score (highscores, (guint32) WinningScore); if (pos > 0) { games_scores_update_score (highscores, players[winner].name); if (dialog) { gtk_window_present (GTK_WINDOW (dialog)); } else { dialog = games_scores_dialog_new (GTK_WINDOW (window), highscores, _("Tali Scores")); message = g_strdup_printf ("<b>%s</b>\n\n%s", _("Congratulations!"), pos == 1 ? _("Your score is the best!") : _("Your score has made the top ten.")); games_scores_dialog_set_message (GAMES_SCORES_DIALOG (dialog), message); g_free (message); } games_scores_dialog_set_hilight (GAMES_SCORES_DIALOG (dialog), pos); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_hide (dialog); } } if (players[winner].name) say (ngettext ("%s wins the game with %d point", "%s wins the game with %d points", WinningScore), players[winner].name, WinningScore); else say (_("Game over!")); }
int main(int argc, char *argv[]) { char **names = (char **)malloc(sizeof(char *) * NAME_COUNT); for (int j = 0; j < NAME_COUNT; j++) names[j] = (char *)malloc(sizeof(char) * MAX_LENGTH); const char *filename = "names.txt"; if (!read_file(filename, names)) return 1; sort_names(names); long score = total_score(names); printf("%ld\n", score); return 0; }
int main (int argc, char *argv[]) { char **player_names; gsize n_player_names; guint i; GOptionContext *context; gboolean retval; GError *error = NULL; setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); application = gtk_application_new ("org.gnome.gtali", 0); g_signal_connect (application, "activate", G_CALLBACK (GyahtzeeCreateMainWindow), NULL); games_scores_startup (); /* Reset all yahtzee variables before parsing args */ YahtzeeInit (); context = g_option_context_new (NULL); g_option_context_add_group (context, gtk_get_option_group (TRUE)); g_option_context_add_main_entries (context, yahtzee_options, GETTEXT_PACKAGE); retval = g_option_context_parse (context, &argc, &argv, &error); g_option_context_free (context); if (!retval) { g_print ("%s", error->message); g_error_free (error); exit (1); } settings = g_settings_new ("org.gnome.gtali"); g_set_application_name (_(appName)); games_stock_init (); /* If we're in computer test mode, just run some tests, no GUI */ if (test_computer_play > 0) { gint ii, jj, kk; gdouble sum_scores = 0.0; game_type = GAME_YAHTZEE; if (game_type_string) game_type = game_type_from_string(game_type_string); g_message("In test computer play section - Using %d trials for simulation", NUM_TRIALS); for (ii = 0; ii < test_computer_play; ii++) { int num_rolls = 0; NumberOfHumans = 0; NumberOfComputers = 1; NewGame (); while (!GameIsOver() && num_rolls < 100) { ComputerRolling (CurrentPlayer); if (NoDiceSelected () || (NumberOfRolls >= NUM_ROLLS)) { ComputerScoring (CurrentPlayer); NumberOfRolls = 0; SelectAllDice (); RollSelectedDice (); } else { RollSelectedDice (); } num_rolls++; } for (kk = NumberOfHumans; kk < NumberOfPlayers; kk++) { printf("Computer score: %d\n", total_score(kk)); sum_scores += total_score(kk); if (num_rolls > 98) { for (jj = 0; jj < NUM_FIELDS; jj++) g_message("Category %d is score %d", jj, players[kk].score[jj]); } } } printf("Computer average: %.2f for %d trials\n", sum_scores / test_computer_play, NUM_TRIALS); exit(0); } highscores = games_scores_new ("gtali", category_array, G_N_ELEMENTS (category_array), "game type", NULL, 0 /* default category */, GAMES_SCORES_STYLE_PLAIN_DESCENDING); gtk_window_set_default_icon_name ("gtali"); if (NumberOfComputers == 0) /* Not set on the command-line. */ NumberOfComputers = g_settings_get_int (settings, "number-of-computer-opponents"); if (NumberOfHumans == 0) /* Not set on the command-line. */ NumberOfHumans = g_settings_get_int (settings, "number-of-human-opponents"); if (NumberOfHumans < 1) NumberOfHumans = 1; if (NumberOfComputers < 0) NumberOfComputers = 0; if (NumberOfHumans > MAX_NUMBER_OF_PLAYERS) NumberOfHumans = MAX_NUMBER_OF_PLAYERS; if ((NumberOfHumans + NumberOfComputers) > MAX_NUMBER_OF_PLAYERS) NumberOfComputers = MAX_NUMBER_OF_PLAYERS - NumberOfHumans; if (game_type_string) game_type = game_type_from_string(game_type_string); else { char *type; type = g_settings_get_string (settings, "game-type"); game_type = game_type_from_string(type); } set_new_game_type(game_type); if (NUM_TRIALS <= 0) NUM_TRIALS = g_settings_get_int (settings, "monte-carlo-trials"); if (DoDelay == 0) /* Not set on the command-line */ DoDelay = g_settings_get_boolean (settings, "delay-between-rolls"); if (DisplayComputerThoughts == 0) /* Not set on the command-line */ DisplayComputerThoughts = g_settings_get_boolean (settings, "display-computer-thoughts"); /* Read in new player names */ player_names = g_settings_get_strv (settings, "player-names"); n_player_names = g_strv_length (player_names); if (player_names) { n_player_names = MIN (n_player_names, MAX_NUMBER_OF_PLAYERS); for (i = 0; i < n_player_names; ++i) { if (i == 0 && strcasecmp (player_names[i], _("Human")) == 0) { const char *realname; realname = g_get_real_name (); if (realname && realname[0] && strcmp (realname, "Unknown") != 0) { players[i].name = g_locale_to_utf8 (realname, -1, NULL, NULL, NULL); } if (!players[i].name) { players[i].name = g_strdup (player_names[i]); } } else { players[i].name = g_strdup (player_names[i]); } } g_strfreev (player_names); } g_application_run (G_APPLICATION (application), argc, argv); exit(0); }