じゃんけんプログラム
Posted: 2017年11月28日(火) 22:08
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;
}
大学のプログラミングの授業で以下の課題が出されたのですが、まったく手がつけられません、、、
どなたかプログラミングに詳しい方がいらっしゃいましたらソースコードの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;
}