c言語でじゃんけんゲームを作っています。最初に500円所持していて、じゃんけんに勝つと300円もらえて、負けると200円払い、所持金が1000円または0円になるまで続けるというルールにしたいです。以下のようなコードを書いたのですが、勝つ度、負ける度に、所持金が変化していきません。対処法を教えていただきたいです。(可能でしたら、どこが間違っているかも指摘していただきたいです。)よろしくお願いいたします。
c
1#include <stdio.h> 2#include <stdlib.h> 3 4int get_number(void); 5int money(int a); 6 7int get_number(void) { 8 return (int)(((((double)rand()) / (double)(RAND_MAX + 1)) * 3) + 1); 9} 10 11int money(int a) { 12 13 14 if (a = 1) { 15 int m = 0; 16 return m = m + 300; 17 18 } 19 else if (a = 2) { 20 int m = 0; 21 return m = m - 200; 22 23 24 } 25 else if (a = 0) { 26 return 500; 27 } 28 29 30} 31int main(void) { 32 printf("Let's play rock,paper,sissors!!\n"); 33 printf("This is the rule.\n"); 34 printf("You have 500 yen.\n"); 35 printf("You have to pay 200 yen for one play.\n"); 36 printf("If you loose, you lost 200yen.\n"); 37 printf("If you win, you can get 300 yen.\n"); 38 printf("If you have no money or you get 1,000 yen, this game is end.\n"); 39 printf("Let's begin!\n"); 40 41 printf("You have 500 yen.\n"); 42 int num = 0; 43 //所持金が0円または1000円になるまで繰り返します。 44 for (int i = 1; 0 < money(num) < 1000; i++) { 45 printf("Please choose your hand from 1 to 3\n"); 46 printf("1 rock\n"); 47 printf("2 paper\n"); 48 printf("3 sissors\n"); 49 50 int h = 0; 51 scanf("%d", &h); 52 //もしもユーザーが1~3以外を選んでしまった時の対応。 53 if (h < 1 || h>3) { 54 printf("Error! Please choose from 1 to 3\n"); 55 } 56 //正常に1~3をえらんだ時のゲームの進み。 57 else { 58 int com = 0; 59 com = get_number(); 60 //あいこ。 61 for (; com == h;) { 62 printf("You are Even!!\n"); 63 printf("Please choose your hand from 1 to 3\n"); 64 printf("1 rock\n"); 65 printf("2 paper\n"); 66 printf("3 sissors\n"); 67 68 int h = 0; 69 scanf("%d", &h); 70 //勝敗がつく。 71 int com = 0; 72 com = get_number(); 73 if (com != h) { 74 if (h == 1 && com == 2) { 75 printf("You are loose!\n"); 76 } 77 else if (h == 1 && com == 3) { 78 printf("You are win!!\n"); 79 } 80 else if (h == 2 && com == 1) { 81 printf("You are win!!\n"); 82 } 83 else if (h == 2 && com == 3) { 84 printf("You are loose!\n"); 85 } 86 else if (h == 3 && com == 1) { 87 printf("You are loose!\n"); 88 } 89 else if (h == 3 && com == 2) { 90 printf("You are win!!\n"); 91 } 92 break; 93 } 94 else { 95 //NR 96 } 97 } 98 99 if (h == 1 && com == 2) { 100 printf("You are loose!\n"); 101 } 102 else if (h == 1 && com == 3) { 103 printf("You are win!!\n"); 104 } 105 else if (h == 2 && com == 1) { 106 printf("You are win!!\n"); 107 } 108 else if (h == 2 && com == 3) { 109 printf("You are loose!\n"); 110 } 111 else if (h == 3 && com == 1) { 112 printf("You are loose!\n"); 113 } 114 else if (h == 3 && com == 2) { 115 printf("You are win!!\n"); 116 } 117 118 } 119 //ゲーム終了後の所持金の計算。 120 printf("If you win,please input 1.And if you loose, please input 2.\n"); 121 int num = 0; 122 scanf("%d", &num); 123 printf("As of %d stage end, you have %d yen.\n", i, money(num)); 124 if ( money(num) > 0 && money(num) < 1000) { 125 printf("Continue your game.\n"); 126 127 } 128 else { 129 printf("GAME IS END! See You!\n"); 130 break; 131 } 132 } 133 134 return 0; 135}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/31 10:34
2018/12/31 10:49
2018/12/31 14:28