質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.31%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

651閲覧

ポケモンのタイプ相性を返すプログラムを作りたいが、関数が目的の条件に引っかからない

taka0331

総合スコア1

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2023/10/07 18:33

実現したいこと

ポケモンのタイプ相性結果を返すプログラム

前提

プログラミング初学者です。
構造体や関数の勉強として、C++でポケモンのタイプ相性を判定するプログラムを作成しています。
攻撃技のタイプ、防御側のタイプ1,2の3つを構造体に設定し、
そこから参照して各関数に渡すことによって、相性を判断します。

発生している問題・エラーメッセージ

コンパイルエラーはなくプログラムは最後まで走るのですが、
type_compatibility()において、どのswitch条件にも引っかからずに終了してしまいます。
例えば
・攻撃技のタイプ:ノーマル
・防御側のタイプ1:いわ
・防御側のタイプ2:ゴースト
の場合、「こうかがないようだ…」のメッセージが出力されるのが理想ですが
どのswitch条件にも引っかからないため、こうかふつう(メッセージなし)となってしまいます。

該当のソースコード

C++

1#include <iostream> 2#include <stdio.h> 3#include <map> 4 5using std::map; 6 7////////////////////////////// 8//////////使用データ////////// 9////////////////////////////// 10 11enum Type //タイプ一覧 12{ 13 Null, //なし ※単タイプ用 14 Normal, //ノーマル 15 Fire, //ほのお 16 Water, //みず 17 Electric, //でんき 18 Grass, //くさ 19 Ice, //こおり 20 Psychic, //エスパー 21 Fighting, //かくとう 22 Poison, //どく 23 Ground, //じめん 24 Flying, //ひこう 25 Bug, //むし 26 Rock, //いわ 27 Ghost, //ゴースト 28 Dragon, //ドラゴン 29 Dark, //あく 30 Steel, //はがね 31 Fairy //フェアリー 32}; 33 34//攻撃技のタイプと防御側のタイプを保持する構造体 35struct info 36{ 37 int atk_type = Normal; 38 int def_type1 = Rock; 39 int def_type2 = Ghost; 40}; 41 42//相性メッセージ 43const char* msg_super_effective = "こうかはばつぐんだ!"; 44const char* msg_not_effective = "こうかはいまひとつのようだ"; 45const char* msg_not_affect = "こうかがないようだ…"; 46 47//タイプごとの「こうかはばつぐん」の配列 48const int fire_super_effective[4] = { Grass, Ice, Bug, Steel }; 49const int water_super_effective[3] = { Fire, Ground, Rock }; 50const int electric_super_effective[2] = { Water, Flying }; 51const int grass_super_effective[3] = { Water, Ground, Rock }; 52const int ice_super_effective[4] = { Grass, Ground, Flying, Dragon }; 53const int psychic_super_effective[2] = { Fighting, Poison }; 54const int fighting_super_effective[4] = { Normal, Ice, Rock, Steel }; 55const int poison_super_effective[2] = { Grass, Fairy }; 56const int ground_super_effective[5] = { Fire, Electric, Poison, Rock, Steel }; 57const int flying_super_effective[3] = { Grass, Fighting, Bug }; 58const int bug_super_effective[3] = { Grass, Psychic, Dark }; 59const int rock_super_effective[4] = { Fire, Ice, Flying, Bug }; 60const int ghost_super_effective[2] = { Psychic, Ghost }; 61const int dragon_super_effective[1] = { Dragon }; 62const int dark_super_effective[2] = { Psychic, Ghost }; 63const int steel_super_effective[3] = { Ice, Rock, Fairy }; 64const int fairy_super_effective[3] = { Fighting, Dragon, Dark }; 65 66//タイプごとの「こうかはいまひとつ」の配列 67int normal_not_effective[2] = { Rock, Steel }; 68const int fire_not_effective[4] = { Fire, Water, Rock, Dragon }; 69const int water_not_effective[3] = { Water, Grass, Dragon }; 70const int electric_not_effective[3] = { Electric, Grass, Dragon }; 71const int grass_not_effective[7] = { Fire, Grass, Poison, Flying, Bug, Dragon, Steel }; 72const int ice_not_effective[4] = { Fire, Water, Ice, Steel }; 73const int psychic_not_effective[2] = { Psychic, Steel }; 74const int fighting_not_effective[5] = { Psychic, Poison, Flying, Bug, Fairy }; 75const int poison_not_effective[4] = { Poison, Ground, Rock, Ghost }; 76const int ground_not_effective[2] = { Grass, Bug }; 77const int flying_not_effective[3] = { Electric, Rock, Steel }; 78const int bug_not_effective[7] = { Fire, Fighting, Poison, Flying, Ghost, Steel, Fairy }; 79const int rock_not_effective[3] = { Fighting, Ground, Steel }; 80const int ghost_not_effective[1] = { Dark }; 81const int dragon_not_effective[1] = { Steel }; 82const int dark_not_effective[3] = { Fighting, Dark, Fairy }; 83const int steel_not_effective[4] = { Fire, Water, Electric, Steel }; 84const int fairy_not_effective[3] = { Fire, Poison, Steel }; 85 86//Typeとタイプ名を対応付ける辞書 87std::map <int, const char*> type_dic = { 88 {1, "ノーマル"}, 89 {2, "ほのお"}, 90 {3, "みず"}, 91 {4, "でんき"}, 92 {5, "くさ"}, 93 {6, "こおり"}, 94 {7, "エスパー"}, 95 {8, "かくとう"}, 96 {9, "どく"}, 97 {10, "じめん"}, 98 {11, "ひこう"}, 99 {12, "むし"}, 100 {13, "いわ"}, 101 {14, "ゴースト"}, 102 {15, "ドラゴン"}, 103 {16, "あく"}, 104 {17, "はがね"}, 105 {18, "フェアリー"} 106}; 107 108 109 110////////////////////////////// 111////////// 関数 ////////// 112////////////////////////////// 113 114/// <summary> 115/// タイプ相性を返す 116/// </summary> 117/// <param name="atk_type">攻撃技のタイプ</param> 118/// <param name="def_type">防御側のタイプ</param> 119/// <returns> 120/// -2以下:こうかがないようだ・・・ 121/// -1:こうかはいまひとつのようだ 122/// 0:こうかふつう 123/// 1:こうかはばつぐんだ! 124/// </returns> 125int type_compatibility(int atk_type, int def_type) 126{ 127 int result = 0; 128 129 switch (atk_type) 130 { 131 Normal: 132 if (def_type == Ghost){ result = -3; break; } 133 for (int ary : normal_not_effective) 134 { 135 if (ary == def_type) { result--; break; } 136 } 137 break; 138 139 Fire: 140 for (int ary : fire_super_effective) 141 { 142 if (ary == def_type) { result++; break; } 143 } 144 for (int ary : fire_not_effective) 145 { 146 if (ary == def_type) { result--; break; } 147 } 148 break; 149 150 Water: 151 for (int ary : water_super_effective) 152 { 153 if (ary == def_type) { result++; break; } 154 } 155 for (int ary : water_not_effective) 156 { 157 if (ary == def_type) { result--; break; } 158 } 159 break; 160 161 Electric: 162 if (def_type == Ground) { result = -3; break; } 163 for (int ary : electric_super_effective) 164 { 165 if (ary == def_type) { result++; break; } 166 } 167 for (int ary : electric_not_effective) 168 { 169 if (ary == def_type) { result--; break; } 170 } 171 break; 172 173 Grass: 174 for (int ary : grass_super_effective) 175 { 176 if (ary == def_type) { result++; break; } 177 } 178 for (int ary : grass_not_effective) 179 { 180 if (ary == def_type) { result--; break; } 181 } 182 break; 183 184 Ice: 185 for (int ary : ice_super_effective) 186 { 187 if (ary == def_type) { result++; break; } 188 } 189 for (int ary : ice_not_effective) 190 { 191 if (ary == def_type) { result--; break; } 192 } 193 break; 194 195 Psychic: 196 if (def_type == Dark) { result = -3; break; } 197 for (int ary : psychic_super_effective) 198 { 199 if (ary == def_type) { result++; break; } 200 } 201 for (int ary : psychic_not_effective) 202 { 203 if (ary == def_type) { result--; break; } 204 } 205 break; 206 207 Fighting: 208 if (def_type == Ghost) { result = -3; break; } 209 for (int ary : fighting_super_effective) 210 { 211 if (ary == def_type) { result++; break; } 212 } 213 for (int ary : fighting_not_effective) 214 { 215 if (ary == def_type) { result--; break; } 216 } 217 break; 218 219 Poison: 220 if (def_type == Steel) { result = -3; break; } 221 for (int ary : poison_super_effective) 222 { 223 if (ary == def_type) { result++; break; } 224 } 225 for (int ary : poison_not_effective) 226 { 227 if (ary == def_type) { result--; break; } 228 } 229 break; 230 231 Ground: 232 if (def_type == Flying) { result = -3; break; } 233 for (int ary : ground_super_effective) 234 { 235 if (ary == def_type) { result++; break; } 236 } 237 for (int ary : ground_not_effective) 238 { 239 if (ary == def_type) { result--; break; } 240 } 241 break; 242 243 Flying: 244 for (int ary : flying_super_effective) 245 { 246 if (ary == def_type) { result++; break; } 247 } 248 for (int ary : flying_not_effective) 249 { 250 if (ary == def_type) { result--; break; } 251 } 252 break; 253 254 Bug: 255 for (int ary : bug_super_effective) 256 { 257 if (ary == def_type) { result++; break; } 258 } 259 for (int ary : bug_not_effective) 260 { 261 if (ary == def_type) { result--; break; } 262 } 263 break; 264 265 Rock: 266 for (int ary : rock_super_effective) 267 { 268 if (ary == def_type) { result++; break; } 269 } 270 for (int ary : rock_not_effective) 271 { 272 if (ary == def_type) { result--; break; } 273 } 274 break; 275 276 Ghost: 277 if (def_type == Normal) { result = -3; break; } 278 for (int ary : ghost_super_effective) 279 { 280 if (ary == def_type) { result++; break; } 281 } 282 for (int ary : ghost_not_effective) 283 { 284 if (ary == def_type) { result--; break; } 285 } 286 break; 287 288 Dragon: 289 if (def_type == Fairy) { result = -3; break; } 290 for (int ary : dragon_super_effective) 291 { 292 if (ary == def_type) { result++; break; } 293 } 294 for (int ary : dragon_not_effective) 295 { 296 if (ary == def_type) { result--; break; } 297 } 298 break; 299 300 Dark: 301 for (int ary : dark_super_effective) 302 { 303 if (ary == def_type) { result++; break; } 304 } 305 for (int ary : dark_not_effective) 306 { 307 if (ary == def_type) { result--; break; } 308 } 309 break; 310 311 Steel: 312 for (int ary : steel_super_effective) 313 { 314 if (ary == def_type) { result++; break; } 315 } 316 for (int ary : steel_not_effective) 317 { 318 if (ary == def_type) { result--; break; } 319 } 320 break; 321 322 Fairy: 323 for (int ary : fairy_super_effective) 324 { 325 if (ary == def_type) { result++; break; } 326 } 327 for (int ary : fairy_not_effective) 328 { 329 if (ary == def_type) { result--; break; } 330 } 331 break; 332 } 333 334 return result; 335 336} 337 338/// <summary> 339/// タイプ相性判断結果を返す 340/// </summary> 341/// <param name="atk_type">攻撃技のタイプ</param> 342/// <param name="def_type1">防御側のタイプ1</param> 343/// <param name="def_type2">防御側のタイプ2</param> 344/// <returns> 345/// -2以下:こうかがないようだ・・・ 346/// -1:こうかはいまひとつのようだ 347/// 0:こうかふつう 348/// 1:こうかはばつぐんだ! 349/// </returns> 350int type_compatibility_main(int atk_type, int def_type1, int def_type2) { 351 int result = 0; 352 353 //防御側が2タイプか確認する 354 if (def_type2 == 0) 355 { 356 //単タイプ用 357 result += type_compatibility(atk_type, def_type1); 358 return result; 359 } 360 else 361 { 362 //2タイプ用 363 result += type_compatibility(atk_type, def_type1); 364 result += type_compatibility(atk_type, def_type2); 365 return result; 366 } 367} 368 369/// <summary> 370/// タイプ相性の結果を表示する 371/// </summary> 372/// <param name="result">type_compatibility()で判断した相性結果</param> 373void dsp_compatibility_msg(int result) { 374 struct info info; 375 376 printf("攻撃技のタイプ:%s\n", type_dic[info.atk_type]); 377 378 if (info.def_type2 != 0) 379 { 380 printf("防御側のタイプ1:%s\n", type_dic[info.def_type1]); 381 printf("防御側のタイプ2:%s\n", type_dic[info.def_type2]); 382 } 383 else 384 { 385 printf("防御側のタイプ:%s\n", type_dic[info.def_type1]); 386 } 387 388 if (result <= -2) { printf("%s", msg_not_affect); } 389 else if (result == -1) { printf("%s", msg_not_effective); } 390 else if (result == 1) { printf("%s", msg_super_effective); } 391 else if (result == 2) { printf("%s(4倍弱点)", msg_super_effective); } 392} 393 394 395 396////////////////////////////// 397//////////メイン処理 ///////// 398////////////////////////////// 399int main() 400{ 401 struct info info; 402 403 int result = type_compatibility_main(info.atk_type, info.def_type1, info.def_type2); 404 dsp_compatibility_msg(result); 405} 406 407

お願い

勉強中のため、構造体や列挙型の使い方などで
「この書き方の方が、きれいになるよ」
などのご指摘がありましたらぜひお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

bsdfan

2023/10/08 00:39 編集

(関係なさそうな内容なので削除)
int32_t

2023/10/08 03:21

> コンパイルエラーはなく コンパイル時に警告が出てたかもしれないですね。 どんな環境で開発しているか不明ですが、警告レベルは最高にしておくのがおすすめです。
guest

回答2

0

ベストアンサー

「case」を書き忘れています。これだとgoto文で飛ぶためのラベルになってしまいます。

修正前一部抜粋

C++

1switch (atk_type) 2{ 3Normal:

修正後一部抜粋

C++

1switch (atk_type) 2{ 3case Normal:

投稿2023/10/07 23:19

naitou

総合スコア141

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

switch (atk_type)

のところでブレークポイントを設定して実行を止め、atk_typeになにが入ってるのか見てみよう。

投稿2023/10/07 22:53

y_waiwai

総合スコア88163

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.31%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問