1

CloudPebble で C を使用して、Pebble で単純なサイコロを振るアプリケーションを作成しようとしています。Up/Down を使用してスクロールできるさまざまなサイコロのサイズの配列があり、中央のボタンを使用して転がします (現在は乱数を生成するだけで、後でより洗練されたものになります)。上部には、現在のダイを表示するラベルもあります。

それはほとんど機能していますが、私の人生では理解できないバグがあります:

  • サイコロをスクロールすると、サイコロの少なくとも 1 つにサイコロの番号が表示されない
  • 壊れたサイコロはまだ転がりますが、rand() 関数には上限がないようです (または、おそらく 1000 の上限があります。テストのために壊れたサイコロで転がした最高値は 880 でした)。

最初に起動したとき、D6 は壊れていました。バックグラウンドで何が起こっているかを確認するためにコードをいじくり回して微調整し、追加しましたが、突然 D6 が動作し始め (理由はわかりません)、D20 が壊れました。配列の "20" 項目を "35" に変更してみました。現在の D35 は機能しましたが、D2 と D4 は壊れていました。私はそれを元に戻し、別のサイコロが壊れました。

添付されているのは私の現在のコードで、現在 D20 と D4 が壊れていると表示されています。役に立たなかったので、トラブルシューティングに使用していたがらくた、再加工、およびサイド コードをすべて取り除きました。

サイコロが一見ランダムに割れる理由と、それを修正する方法を説明できる人はいますか? ありがとうございました!

#include <pebble.h>

static Window *s_main_window;
static TextLayer *s_label_layer;
static TextLayer *s_output_layer;

static int dice_select = 6;
static char *die_label = "D";
static char *dice_array[] = {"2", "4", "6", "8", "10", "12", "20", "100", NULL};

// === Separate Processes ===

    static void update_label(char *die) {
      die_label[1] = '\0';
      strcat(die_label, die);
      text_layer_set_text(s_label_layer, die_label);
    }

// === Main Window Processes ===

static void main_window_load(Window *window) {
  Layer *window_layer = window_get_root_layer(window);
  GRect window_bounds = layer_get_bounds(window_layer);

  // Create label TextLayer
  s_label_layer = text_layer_create(GRect(5, 0, window_bounds.size.w - 10, 24));
  text_layer_set_font(s_label_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
  text_layer_set_text_alignment(s_label_layer, GTextAlignmentCenter);
  text_layer_set_text(s_label_layer, "D20");
  text_layer_set_overflow_mode(s_label_layer, GTextOverflowModeWordWrap);
  layer_add_child(window_layer, text_layer_get_layer(s_label_layer));

  // Create output TextLayer
  s_output_layer = text_layer_create(GRect(5, 24, window_bounds.size.w - 10, window_bounds.size.h - 24));
  text_layer_set_font(s_output_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28));
  text_layer_set_text(s_output_layer, "No button pressed yet.");
  text_layer_set_overflow_mode(s_output_layer, GTextOverflowModeWordWrap);
  layer_add_child(window_layer, text_layer_get_layer(s_output_layer));
}

static void main_window_unload(Window *window) {
  // Destroy output TextLayer
  text_layer_destroy(s_label_layer);
  text_layer_destroy(s_output_layer);
}


// === Button Handlers ===

static void up_click_handler(ClickRecognizerRef recognizer, void *context) {
  // Increment dice selection
  if(dice_select < 7) {
    dice_select += 1;
  } 
  update_label(dice_array[dice_select]);
}

static void select_click_handler(ClickRecognizerRef recognizer, void *context) {
  // Establish die selection
  char *die = dice_array[dice_select];     
  int die_sides = atoi(die);

  // Roll die
  int result = rand() % die_sides + 1;
  char *result_str = "";
  snprintf(result_str, sizeof(result_str), "%d", result);

  // Print result
  text_layer_set_text(s_output_layer, result_str);
}

static void down_click_handler(ClickRecognizerRef recognizer, void *context) {
  // Increment dice selection
  if(dice_select > 0) {
    dice_select -= 1;
  }
  update_label(dice_array[dice_select]);
}

static void click_config_provider(void *context) {
  // Register the ClickHandlers
  window_single_click_subscribe(BUTTON_ID_UP, up_click_handler);
  window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
  window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler);
}



// === Initialize/Deinitialize App ===

static void init() {
  // Create main Window
  s_main_window = window_create();
  window_set_window_handlers(s_main_window, (WindowHandlers) {
    .load = main_window_load,
    .unload = main_window_unload,
  });
  window_set_click_config_provider(s_main_window, click_config_provider);
  window_stack_push(s_main_window, true);
}

static void deinit() {
  // Destroy main Window
  window_destroy(s_main_window);
}

int main(void) {
  init();
  app_event_loop();
  deinit();
}

(注: 私は訓練を受けていない独学の初心者なので、「なぜそのようにしたのですか?」などのすべての質問に対する答えは、「よくわからなかったからです。」)

編集: コメントごとに追加された新しいコード。

この部分はうまく機能します:

static char die_label[32] = "D";

static void update_label(char *die) {
  snprintf(die_label, sizeof(die_label), "D%s", die);
  text_layer_set_text(s_label_layer, die_label);
}

ただし、この部分ではロール結果が出力されなくなります。

  // Roll die
  int result = rand() % die_sides + 1;
  char result_str[32];
  snprintf(result_str, sizeof(result_str), "%d", result);

  // Print result
  text_layer_set_text(s_output_layer, result_str);
4

1 に答える 1