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

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

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

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

多次元配列

1次元配列内にさらに配列を格納している配列を、多次元配列と呼びます。

配列

配列は、各データの要素(値または変数)が連続的に並べられたデータ構造です。各配列は添え字(INDEX)で識別されています。

Q&A

解決済

1回答

909閲覧

配列の値をリセットして繰り返し最初からゲームを始めたい

bafubafu_

総合スコア16

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

多次元配列

1次元配列内にさらに配列を格納している配列を、多次元配列と呼びます。

配列

配列は、各データの要素(値または変数)が連続的に並べられたデータ構造です。各配列は添え字(INDEX)で識別されています。

0グッド

0クリップ

投稿2022/01/13 20:24

編集2022/01/13 20:25

学校の課題で三目並べを作っています。
勝ち負けを決めるところまでは書けました。(1度のみの対戦)
このゲームをnoと入力するまで繰り返しできるようにしたいです。
while文を使ってループ処理を書いてみたのですが、挙動がおかしくなりました。

下のソースコードに新たに

はじめに,対戦者名(Player1, Player2)の名前を入力し,表示する.
初回の対戦は,対戦者名の入力の後に始まる.
2回目以降の対戦については,前回の対戦終了後に続けて対戦するかどうか尋ねるメッセージを表示し,"yes" または "no" の入力を求める.入力された文字列の1文字目が 'y' の場合,次の対戦を行なう.
対戦継続を尋ねる表示形式は,"Continue? (yes/no): " である.
対戦(1回以上)において,対戦数・Player1とPlayer2の勝利数・引き分け数をカウントして,下記のコードに作成したPLAYDATA型の変数のメンバにそれぞれの値を格納する.
"no"が入力され,対戦が終了したときに,Player1とPlayer2の勝率を計算し,下記のコードに作成したPLAYDATA型の変数のメンバにそれぞれの値を格納する.
対戦が終了した後,対戦データを表示する.
対戦データは,対戦数・Player1の勝利数・Player2の勝利数・引き分け数・Player1の勝率・Player2の勝率の順で表示する.ここで,勝率は勝利数/対戦数で求まるとして,小数点以下3桁まで表示することとする.それぞれの表示形式を次に記す.
対戦数: "Plays: %d\n"
Player1の勝利数: "Wins of Player1 %s: %d\n" (%sではPlayer1の名前を表示する)
Player2の勝利数: "Wins of Player2 %s: %d\n" (%sではPlayer2の名前を表示する)
引き分け数: "Draws: %d\n"
Player1の勝率: "%s's winning percentage: %.3lf\n" (%sではPlayer1の名前を表示する)
Player2の勝率: "%s's winning percentage: %.3lf\n" (%sではPlayer2の名前を表示する)
対戦データの表示は,次のプロトタイプ宣言により示される関数print_playdataを用いる.

void print_playdata(PLAYDATA data);
関数print_playdataの仕様は次の通りである.

引数:
PLAYDATA data - 対戦情報
戻り値: なし
機能:
引数dataのメンバを,プログラムの仕様に基づき,順に表示する.

この要素を下記のソースコードに追加したいです。
下に実行例を書きました。

#include <stdio.h> #define LEN_NAME 17 void print_board(int board[3][3]); void mark_board(int board[3][3], int turn); int judge(int board[3][3]); typedef struct { int num_play; /* 対戦数 */ int num_player1_win; /* Player1の勝利数 */ int num_player2_win; /* Player2の勝利数 */ int num_draw; /* 引き分け数 */ double ave_player1_win; /* Player1の勝率 */ double ave_player2_win; /* Player2の勝率 */ char player1[LEN_NAME]; /* Player1の名前 */ char player2[LEN_NAME]; /* Player2の名前 */ }PLAYDATA; int main(void) { int turn, i, count = 0, win; int board[3][3] = { {0,0,0}, {0,0,0}, {0,0,0} }; PLAYDATA game; printf("Enter Player1's name (max. 16 chars): "); scanf("%s", game.player1); printf("Enter Player2's name (max. 16 chars): "); scanf("%s", game.player2); printf("\n%s vs %s\n\n", game.player1, game.player2); while(1) { if (count%2 == 0) { turn = 1; } else { turn = -1; } mark_board(board, turn); print_board(board); count++; win = judge(board); if (win == 1) { if (turn == 1) { printf("Player1 (o), %s wins!", game.player1); break; } if (turn == -1) { printf("Player2 (x), %s wins!", game.player2); break; } } if (count == 9) { break; } } if (win == -2) { printf("Draw."); } return 0; } void print_board(int board[3][3]) { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("|"); switch (board[i][j]) { case -1: printf("x"); break; case 0: printf(" "); break; case 1: printf("o"); break; } } printf("|\n"); } } void mark_board(int board[3][3], int turn) { int i, j; if (turn == 1) { printf("Player1's turn (o)\n"); } else { printf("Player2's turn (x)\n"); } while (1) { printf("What is the vertical position? "); scanf("%d", &i); printf("What is the horizontal position? "); scanf("%d", &j); if (board[i][j] == 0 && i <= 2 && i >= 0 && j <= 2 && j >= 0) { if (turn == 1) { board[i][j] = 1; break; } else { board[i][j] = -1; break; } } else { printf("Cannot put there\n"); } } } int judge(int board[3][3]) { int i, j, result, count1, count2, count3, count4; count1 = 0; count2 = 0; count3 = 0; count4 = 0; result = 0; for (i = 0; i < 3; i++) { count1 = 0; for (j = 0; j < 2; j++) { if (board[i][j] == board[i][j + 1] && board[i][j] != 0) { count1++; if (count1 == 2) { result = 1; } } } } for (j = 0; j < 3; j++) { count2 = 0; for (i = 0; i < 2; i++) { if (board[i][j] == board[i + 1][j] && board[i][j] != 0) { count2++; if (count2 == 2) { result = 1; } } } } for (i = 0; i < 2; i++) { if (board[i][i] == board[i + 1][i + 1] && board[i][i] != 0) { count3++; } } for (i = 0; i < 2; i++) { if (board[2 - i][i] == board[1 - i][i + 1] && board[2 - i][i] != 0) { count4++; } } if (count3 == 2 || count4 == 2 || result == 1) { return 1; } return -2; }

下のソースコードを最後にゲームのループを抜けたあとに最終的な結果として表示させたいです。

void print_playdata(PLAYDATA data) { data.ave_player1_win = (double)data.num_play / data.num_player1_win; data.ave_player2_win = (double)data.num_play / data.num_player2_win; printf("\nPlays: %d\n", data.num_play); printf("Wins of Player1 %s: %d\n", data.player1,data.num_player1_win); printf("Wins of Player2 %s: %d\n", data.player2, data.num_player2_win); printf("Draws: %d\n", data.num_draw); printf("%s's winning percentage: %.3f\n", data.player1, data.ave_player1_win); printf("%s's winning percentage: %.3f\n", data.player2, data.ave_player2_win); }

実行例です。

Enter Player1's name (max. 16 chars): Taro⏎
Enter Player2's name (max. 16 chars): Hanako⏎

Taro vs Hanako

Player1's turn (o)
What is the vertical position? 0⏎
What is the horizontal position? 0⏎
|o| | |
| | | |
| | | |
Player2's turn (x)
What is the vertical position? 1⏎
What is the horizontal position? 1⏎
|o| | |
| |x| |
| | | |
Player1's turn (o)
What is the vertical position? 0⏎
What is the horizontal position? 1⏎
|o|o| |
| |x| |
| | | |
Player2's turn (x)
What is the vertical position? 0⏎
What is the horizontal position? 2⏎
|o|o|x|
| |x| |
| | | |
Player1's turn (o)
What is the vertical position? 3⏎
What is the horizontal position? 3⏎
Cannot put there
What is the vertical position? 1⏎
What is the horizontal position? 0⏎
|o|o|x|
|o|x| |
| | | |
Player2's turn (x)
What is the vertical position? 2⏎
What is the horizontal position? 0⏎
|o|o|x|
|o|x| |
|x| | |
Player2 (x), Hanako wins!

Continue? (yes/no): yes⏎
Player1's turn (o)
What is the vertical position? 0⏎
What is the horizontal position? 0⏎
|o| | |
| | | |
| | | |
Player2's turn (x)
What is the vertical position? 0⏎
What is the horizontal position? 1⏎
|o|x| |
| | | |
| | | |
Player1's turn (o)
What is the vertical position? 1⏎
What is the horizontal position? 0⏎
|o|x| |
|o| | |
| | | |
Player2's turn (x)
What is the vertical position? 0⏎
What is the horizontal position? 0⏎
Cannot put there
What is the vertical position? 1⏎
What is the horizontal position? 1⏎
|o|x| |
|o|x| |
| | | |
Player1's turn (o)
What is the vertical position? 2⏎
What is the horizontal position? 0⏎
|o|x| |
|o|x| |
|o| | |
Player1 (o), Taro wins!

Continue? (yes/no): yes⏎
Player1's turn (o)
What is the vertical position? 0⏎
What is the horizontal position? 0⏎
|o| | |
| | | |
| | | |
Player2's turn (x)
What is the vertical position? 0⏎
What is the horizontal position? 1⏎
|o|x| |
| | | |
| | | |
Player1's turn (o)
What is the vertical position? 0⏎
What is the horizontal position? 2⏎
|o|x|o|
| | | |
| | | |
Player2's turn (x)
What is the vertical position? 1⏎
What is the horizontal position? 1⏎
|o|x|o|
| |x| |
| | | |
Player1's turn (o)
What is the vertical position? 2⏎
What is the horizontal position? 1⏎
|o|x|o|
| |x| |
| |o| |
Player2's turn (x)
What is the vertical position? 1⏎
What is the horizontal position? 0⏎
|o|x|o|
|x|x| |
| |o| |
Player1's turn (o)
What is the vertical position? 1⏎
What is the horizontal position? 2⏎
|o|x|o|
|x|x|o|
| |o| |
Player2's turn (x)
What is the vertical position? 2⏎
What is the horizontal position? 2⏎
|o|x|o|
|x|x|o|
| |o|x|
Player1's turn (o)
What is the vertical position? 2⏎
What is the horizontal position? 0⏎
|o|x|o|
|x|x|o|
|o|o|x|
Draw.

Continue? (yes/no): yes⏎
Player1's turn (o)
What is the vertical position? 0⏎
What is the horizontal position? 0⏎
|o| | |
| | | |
| | | |
Player2's turn (x)
What is the vertical position? 1⏎
What is the horizontal position? 1⏎
|o| | |
| |x| |
| | | |
Player1's turn (o)
What is the vertical position? 2⏎
What is the horizontal position? 1⏎
|o| | |
| |x| |
| |o| |
Player2's turn (x)
What is the vertical position? 2⏎
What is the horizontal position? 0⏎
|o| | |
| |x| |
|x|o| |
Player1's turn (o)
What is the vertical position? 0⏎
What is the horizontal position? 1⏎
|o|o| |
| |x| |
|x|o| |
Player2's turn (x)
What is the vertical position? 0⏎
What is the horizontal position? 2⏎
|o|o|x|
| |x| |
|x|o| |
Player2 (x), Hanako wins!

Continue? (yes/no): no⏎

Plays: 4
Wins of Player1 Taro: 1
Wins of Player2 Hanako: 2
Draws: 1
Taro's winning percentage: 0.250
Hanako's winning percentage: 0.500

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

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

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

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

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

guest

回答1

0

ベストアンサー

フローチャート、習ったかしら。
多分、Cの文一つ一つと箱を1:1に対応させるようなことを習ったと思うけど、それは多分役に立たない。
だけど、いくつかの命令をまとめて名前をつけて箱にするなら、考えをまとめるのに役に立つこともあるでしょう。
処理概略
描画ツールの関係でループとかがきれいに表せていないけれど、言いたいことは伝わるかな?

投稿2022/01/15 01:19

thkana

総合スコア7639

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

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

bafubafu_

2022/01/15 09:01

無事出来ました。 有難うございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問