#include <stdio.h> #include <time.h> #include <stdlib.h> int main () { int player, computer; int p_count = 5; /*プレイヤーの負け数残り*/ int c_count = 5; /*コンピュータの負け数残り*/ printf("[野球拳ゲーム]\n"); srand(time(NULL)); do { printf("\n野球拳するなら\n"); printf("こういう具合にしやしゃんせ\n"); printf("アウト セーフ\n"); do { printf("よよいのよい!\n"); printf("(グー:1 チョキ:2 パー:3を入力) > "); player = 0; scanf("%d", &player); while(getchar() != '\n') { } computer = rand()%3 + 1; printf("コンピュータは"); if(computer == 1) {printf("グー");} else if(computer ==2) {printf("チョキ");} else {printf("パー");} printf("! "); } while((player != 0) && (computer == player)); if((player ==1 && computer == 2) || (player == 2 && computer == 3) || (player == 3 && computer ==1)) { printf("プレイヤーの勝ち\n"); c_count--; } else { printf("コンピュータの勝ち\n"); p_count--; } } while((p_count != 0) && (c_count != 0)); printf("\n勝負あり!\n"); if(p_count == 0) { printf("あなたの負け!"); } else { printf("あなたの勝ち!"); } return 0; }上のプログラムをコンパイルすると
yakyuuken.c:21:23: warning: multi-character character constant
と出るのでコンパイル時に-Wno-multicharでエラーが出ないようにしてますが、
すると起動してもコマンド入力時に入力しても次の結果が出ずに無限ループしてしまいます。
野球拳ゲーム]\n\n野球拳するなら\nこういう具合にしやしゃんせ
\nアウト セーフ\nよよいのよい!\n(グー:1 チョキ:2 パー:3を入力) > 3
2
1
2
3
2
1
2
これは、\nの改行がうまく行かず「\n」がそのまま出てしまうのと何か関係があるのでしょうか?
OS:Mac OS X コンパイル:gcc バージョン4.0.1