じゃんけんプログラム

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
r2

じゃんけんプログラム

#1

投稿記事 by r2 » 7年前

C言語に関する質問です。
大学のプログラミングの授業で以下の課題が出されたのですが、まったく手がつけられません、、、
どなたかプログラミングに詳しい方がいらっしゃいましたらソースコードの1例を教えてください(。>﹏<。)

C言語を用いてじゃんけんのプログラムを作成する。プレイヤーとコンピュータがじゃんけんで対戦し,勝ったほうが1点を取得する。どちらかが5点を獲得したら,結果を表示して終了する。以下に,プログラムを示す。main関数はすべてのコードが記載されているが,その他の関数は関数の定義部分のみが記載されている。

<設問のプログラム>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define WINNER_POINT 5
#define MAX_LINE 256

/* 状況を表示する関数 */
int print_situation(int player_point, int computer_point, int turns)
{

}

/* プレイヤーの出し手を取得する関数 */
int get_player_hand(void)
{

}

/* コンピュータの出し手を取得する関数 */
int get_computer_hand(void)
{

}

/* お互いの出し手を表示する関数 */
int print_hand(int player_hand, int computer_hand)
{

}

/* 勝敗を判定する関数 */
int judge(int player_hand, int computer_hand)
{

}

/* 対戦結果を表示する関数 */
void print_result(int player_point, int computer_point)
{

}

/* プログラムの開始点 */
int main(void)
{
int player_hand; /* プレイヤーの出し手 */
int computer_hand; /* コンピュータの出し手 */
int player_point; /* プレイヤーの得点 */
int computer_point; /* コンピュータの得点 */
int turns; /* ターン数 */
int point; /* じゃんけんの勝敗 */

srand(time(NULL)); /* 乱数の初期化 */

player_point = 0; /* プレイヤーの得点を初期化 */
computer_point = 0; /* コンピュータの得点を初期化 */
turns = 1; /* ターン数を初期化 */

printf("じゃんけんで%d点勝ち抜けの対戦を行います。\n\n", WINNER_POINT);

do {
do {
/* 状況を表示 */
print_situation(player_point, computer_point, turns);
/* プレイヤーの出し手を取得 */
player_hand = get_player_hand();
} while (player_hand <= 0 || player_hand >= 4);
/* 正しい出し手が入力されるまで繰り返し */

/* コンピュータの出し手を取得 */
computer_hand = get_computer_hand();
/* お互いの出し手を表示 */
print_hand(player_hand, computer_hand);
/* じゃんけんの勝敗を判定 */
point = judge(player_hand, computer_hand);

if (point == 1) { /* プレイヤーの得点 */
printf("プレイヤーの得点です。\n\n");
player_point++; /* プレイヤーの得点をインクリメント */
turns++; /* ターン数をインクリメント */
}
else if (point == -1) { /* コンピュータの得点 */
printf("コンピュータの得点です。\n\n");
computer_point++; /* コンピュータの得点をインクリメント */
turns++; /* ターン数をインクリメント */
}
else { /* あいこ */
printf("あいこです。\n\n");
}

} while (player_point < WINNER_POINT && computer_point < WINNER_POINT);
/* どちらかがWINNER_POINTに到達するまで繰り返し */

/* 対戦結果の表示 */
print_result(player_point, computer_point);

return 0;
}

かずま

Re: じゃんけんプログラム

#2

投稿記事 by かずま » 7年前

r2 さんが書きました: どなたかプログラミングに詳しい方がいらっしゃいましたらソースコードの1例を教えてください(。>﹏<。)
1例です。これの実行例を[code=text] のタグで
貼り付けてくれたら、2例目を書くかもしれません。

コード:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define WINNER_POINT 5
#define MAX_LINE 256

/* 状況を表示する関数 */
int print_situation(int player_point, int computer_point, int turns)
{
static const char *s[] = { "", "グー", "チョキ", "パー" };
int ph, ch, pp = 0, cp = 0, t = 1, pt;
do { do { printf("[%d] あなた:%d, コンピュータ: %d\n"
"手(1:グー、2:チョキ、3:パー): ", t, pp, cp);
if (scanf("%d", &ph) != 1) exit(1);
} while (ph <= 0 || ph >= 4); ch = rand() % 3 + 1;
printf("あなた: %s, コンピュータ: %s\n", s[ph], s[ch]);
pt = (ch - ph + 4) % 3 - 1;
puts(pt == 1 ? pp++, t++, "プレイヤーの得点です。\n" :
pt == -1 ? cp++, t++, "コンピュータの得点です。\n" : "あいこです。\n");
} while (pp < WINNER_POINT && cp < WINNER_POINT);
if (printf("あなた: %d, コンピュータ: %d\n" "%sの勝ち\n",
pp, cp, pp > cp ?  "あなた" : "コンピュータ")) exit(0); return 0;
}

/* プレイヤーの出し手を取得する関数 */
int get_player_hand(void)
{
return 0;
}

/* コンピュータの出し手を取得する関数 */
int get_computer_hand(void)
{
return 0;
}

/* お互いの出し手を表示する関数 */
int print_hand(int player_hand, int computer_hand)
{
return 0;
}

/* 勝敗を判定する関数 */
int judge(int player_hand, int computer_hand)
{
return 0;
}

/* 対戦結果を表示する関数 */
void print_result(int player_point, int computer_point)
{

}

/* プログラムの開始点 */
int main(void)
{
int player_hand; /* プレイヤーの出し手 */
int computer_hand; /* コンピュータの出し手 */
int player_point; /* プレイヤーの得点 */
int computer_point; /* コンピュータの得点 */
int turns; /* ターン数 */
int point; /* じゃんけんの勝敗 */

srand(time(NULL)); /* 乱数の初期化 */

player_point = 0; /* プレイヤーの得点を初期化 */
computer_point = 0; /* コンピュータの得点を初期化 */
turns = 1; /* ターン数を初期化 */

printf("じゃんけんで%d点勝ち抜けの対戦を行います。\n\n", WINNER_POINT);

do {
do {
/* 状況を表示 */
print_situation(player_point, computer_point, turns);
/* プレイヤーの出し手を取得 */
player_hand = get_player_hand();
} while (player_hand <= 0 || player_hand >= 4);
/* 正しい出し手が入力されるまで繰り返し */

/* コンピュータの出し手を取得 */
computer_hand = get_computer_hand();
/* お互いの出し手を表示 */
print_hand(player_hand, computer_hand);
/* じゃんけんの勝敗を判定 */
point = judge(player_hand, computer_hand);

if (point == 1) { /* プレイヤーの得点 */
printf("プレイヤーの得点です。\n\n");
player_point++; /* プレイヤーの得点をインクリメント */
turns++; /* ターン数をインクリメント */
}
else if (point == -1) { /* コンピュータの得点 */
printf("コンピュータの得点です。\n\n");
computer_point++; /* コンピュータの得点をインクリメント */
turns++; /* ターン数をインクリメント */
}
else { /* あいこ */
printf("あいこです。\n\n");
}

} while (player_point < WINNER_POINT && computer_point < WINNER_POINT);
/* どちらかがWINNER_POINTに到達するまで繰り返し */

/* 対戦結果の表示 */
print_result(player_point, computer_point);

return 0;
}

アバター
asd
記事: 319
登録日時: 14年前

Re: じゃんけんプログラム

#3

投稿記事 by asd » 7年前

► スポイラーを表示
フォーラムルール上、マルチポストする場合は相互リンクを貼ってくださいね。
https://detail.chiebukuro.yahoo.co.jp/q ... 4182638639
Advanced Supporting Developer
無理やりこじつけ(ぉ

返信

“C言語何でも質問掲示板” へ戻る