/*
C/C++の宿題片付けます 161代目
http://t...content-available-to-author-only...h.net/test/read.cgi/tech/1354070278/75
[1] 授業単元:c言語
[2] 問題文(含コード&リンク):三目並べの作成
・先手を○、後手を×として交互に3×3の盤上に印をつけていく
・縦横斜めのいずれかに同じ印が並ぶと終了し、その印の方を勝ちとする
・9箇所すべての箇所に印がついており、勝敗がついていない場合は引き分けとする
・無効な位置が入力されたら再入力させる
http://c...content-available-to-author-only...d.org/2e5lVtcO
mainの関数はできているのでそのほかの関数の作成をお願いいたします
関数の説明はリンク先に書いてあります
[3] 環境
[3.1] OS: Linux
[3.2] コンパイラ名とバージョン: gcc
[3.3] 言語: C
[4] 期限:12/4
よろしくお願いいたします
*/
#include<stdio.h>
#include<stdlib.h>
void print_board(int board[3][3]);
int judge(int board[3][3]);
void mark_board(int board[3][3], int turn);
void print_message(int result);
int main(void)
{
int board[3][3] = { };
int turn = 1; /*1=maru,-1=batsu */
int result;
do {
print_board(board);
mark_board(board, turn);
turn *= -1;
} while ((result = judge(board)) == 0);
print_board(board);
print_message(result);
return 0;
}
void print_board(int board[3][3])
{
/*引数で受け取った盤のデータ(board)を縦線(|)や○や×を使い画面に表示する */
/* | | |
| | |
| | |
盤はこの様な形でお願いします */
int x, y;
for (y = 0; y < 3; y++) {
for (x = 0; x < 3; x++) {
switch (board[y][x]) {
case 1:
break;
case -1:
break;
default:
break;
}
}
}
}
int judge(int board[3][3])
{
/*引数boardの内容をチェックし、○が勝ちなら1を、×が勝ちなら2を、
引き分けなら3を、まだ勝敗が決まっていないときは0を返す */
int x, y;
int ml, bl; // 揃っているライン数
int mc, bc; // 1ライン数える用
int fSp; //空白あるか?フラグ
//
fSp = ml = bl = 0;
// 行
for (y = 0; y < 3; y++) {
mc = bc = 0;
for (x = 0; x < 3; x++) {
switch (board[y][x]) {
case 1:
mc++;
break;
case -1:
bc++;
break;
default:
fSp = 1;
break;
}
}
if (mc == 3) {
ml++;
}
if (bc == 3) {
bl++;
}
}
// 列
for (x = 0; x < 3; x++) {
mc = bc = 0;
for (y = 0; y < 3; y++) {
switch (board[y][x]) {
case 1:
mc++;
break;
case -1:
bc++;
break;
default:
fSp = 1;
break;
}
}
if (mc == 3) {
ml++;
}
if (bc == 3) {
bl++;
}
}
// 斜め (y = x)
mc = bc = 0;
for (x = 0; x < 3; x++) {
switch (board[x][x]) {
case 1:
mc++;
break;
case -1:
bc++;
break;
}
if (mc == 3) {
ml++;
}
if (bc == 3) {
bl++;
}
}
// 斜め (y = 2-x)
mc = bc = 0;
for (x = 0; x < 3; x++) {
switch (board[2 - x][x]) {
case 1:
mc++;
break;
case -1:
bc++;
break;
}
if (mc == 3) {
ml++;
}
if (bc == 3) {
bl++;
}
}
// 揃っているラインの比較
if (ml > bl) {
return 1;
} else if (ml < bl) {
return 2;
}
// 空白があるか
if (fSp) {
return 0;
} else {
return 3;
}
}
void mark_board(int board[3][3], int turn)
{
/*盤の縦横の位置の入力を促すメッセージを表示し、キーボードから数値を読み込み,
board配列へturnの値を代入する */
int x, y;
switch (turn) {
case 1:
printf("「○ 」を置く場所を1~3で指定してください\n"); break;
case -1:
printf("「× 」を置く場所を1~3で指定してください\n"); break;
default:
printf("プログラムエラー\n " __FILE__
" (%d)\n", __LINE__
); }
while (1) {
while (1) {
if (x < 1 || 3 < x) {
} else {
break;
}
}
while (1) {
if (y < 1 || 3 < y) {
} else {
break;
}
}
x--;
y--;
if (board[y][x]) {
printf("(%d,%d)には既に何かあります。再度入力してください\n", x
+ 1, y
+ 1); } else {
board[y][x] = turn;
return;
}
}
}
void print_message(int result)
{
/*resultの値に従って勝敗のメッセージを表示する */
switch (result) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
//
LyoKICAgIEMvQysr44Gu5a6/6aGM54mH5LuY44GR44G+44GZIDE2MeS7o+ebrgogICAgaHR0cDovL3QuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLmgubmV0L3Rlc3QvcmVhZC5jZ2kvdGVjaC8xMzU0MDcwMjc4Lzc1CiAgICAgICAgWzFdIOaOiOalreWNmOWFg++8mmPoqIDoqp4KICAgICAgICBbMl0g5ZWP6aGM5paHKOWQq+OCs+ODvOODiSZhbXA744Oq44Oz44KvKe+8muS4ieebruS4puOBueOBruS9nOaIkAogICAgICAgIOODu+WFiOaJi+OCkuKXi+OAgeW+jOaJi+OCksOX44Go44GX44Gm5Lqk5LqS44GrM8OXM+OBruebpOS4iuOBq+WNsOOCkuOBpOOBkeOBpuOBhOOBjwogICAgICAgIOODu+e4puaoquaWnOOCgeOBruOBhOOBmuOCjOOBi+OBq+WQjOOBmOWNsOOBjOS4puOBtuOBqOe1guS6huOBl+OAgeOBneOBruWNsOOBruaWueOCkuWLneOBoeOBqOOBmeOCiwogICAgICAgIOODuznnrofmiYDjgZnjgbnjgabjga7nrofmiYDjgavljbDjgYzjgaTjgYTjgabjgYrjgorjgIHli53mlZfjgYzjgaTjgYTjgabjgYTjgarjgYTloLTlkIjjga/lvJXjgY3liIbjgZHjgajjgZnjgosKICAgICAgICDjg7vnhKHlirnjgarkvY3nva7jgYzlhaXlipvjgZXjgozjgZ/jgonlho3lhaXlipvjgZXjgZvjgosKICAgICAgICBodHRwOi8vYy4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLWF1dGhvci1vbmx5Li4uZC5vcmcvMmU1bFZ0Y08KICAgICAgICBtYWlu44Gu6Zai5pWw44Gv44Gn44GN44Gm44GE44KL44Gu44Gn44Gd44Gu44G744GL44Gu6Zai5pWw44Gu5L2c5oiQ44KS44GK6aGY44GE44GE44Gf44GX44G+44GZCiAgICAgICAg6Zai5pWw44Gu6Kqs5piO44Gv44Oq44Oz44Kv5YWI44Gr5pu444GE44Gm44GC44KK44G+44GZCiAgICAgICAgWzNdIOeSsOWigwogICAgICAgICAgWzMuMV0gT1PvvJogTGludXgKICAgICAgICAgIFszLjJdIOOCs+ODs+ODkeOCpOODqeWQjeOBqOODkOODvOOCuOODp+ODs++8miBnY2MKICAgICAgICAgIFszLjNdIOiogOiqnu+8miBDCiAgICAgICAgWzRdIOacn+mZkDoxMi80CiAgICAgICAg44KI44KN44GX44GP44GK6aGY44GE44GE44Gf44GX44G+44GZIAoqLwoKI2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+Cgp2b2lkIHByaW50X2JvYXJkKGludCBib2FyZFszXVszXSk7CmludCBqdWRnZShpbnQgYm9hcmRbM11bM10pOwp2b2lkIG1hcmtfYm9hcmQoaW50IGJvYXJkWzNdWzNdLCBpbnQgdHVybik7CnZvaWQgcHJpbnRfbWVzc2FnZShpbnQgcmVzdWx0KTsKCmludCBtYWluKHZvaWQpCnsKICAgIGludCBib2FyZFszXVszXSA9IHsgfTsKICAgIGludCB0dXJuID0gMTsgICAgICAgICAgICAgICAvKjE9bWFydSwtMT1iYXRzdSAqLwogICAgaW50IHJlc3VsdDsKCiAgICBkbyB7CiAgICAgICAgcHJpbnRfYm9hcmQoYm9hcmQpOwogICAgICAgIG1hcmtfYm9hcmQoYm9hcmQsIHR1cm4pOwogICAgICAgIHR1cm4gKj0gLTE7CiAgICB9IHdoaWxlICgocmVzdWx0ID0ganVkZ2UoYm9hcmQpKSA9PSAwKTsKICAgIHByaW50X2JvYXJkKGJvYXJkKTsKICAgIHByaW50X21lc3NhZ2UocmVzdWx0KTsKCiAgICByZXR1cm4gMDsKfQoKdm9pZCBwcmludF9ib2FyZChpbnQgYm9hcmRbM11bM10pCnsKICAgIC8q5byV5pWw44Gn5Y+X44GR5Y+W44Gj44Gf55uk44Gu44OH44O844K/KGJvYXJkKeOCkue4pue3mih8KeOChOKXi+OChMOX44KS5L2/44GE55S76Z2i44Gr6KGo56S644GZ44KLICovCiAgICAvKiB8IHwgfAogICAgICAgfCB8IHwKICAgICAgIHwgfCB8CiAgICAgICDnm6Tjga/jgZPjga7mp5jjgarlvaLjgafjgYrpoZjjgYTjgZfjgb7jgZkgKi8KICAgIGludCB4LCB5OwogICAgZm9yICh5ID0gMDsgeSA8IDM7IHkrKykgewogICAgICAgIHByaW50ZigifCIpOwogICAgICAgIGZvciAoeCA9IDA7IHggPCAzOyB4KyspIHsKICAgICAgICAgICAgc3dpdGNoIChib2FyZFt5XVt4XSkgewogICAgICAgICAgICBjYXNlIDE6CiAgICAgICAgICAgICAgICBwcmludGYoIuKXiyB8Iik7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSAtMToKICAgICAgICAgICAgICAgIHByaW50Zigiw5cgfCIpOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgICBwcmludGYoIiAgfCIpOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcHJpbnRmKCJcbiIpOwogICAgfQp9CgppbnQganVkZ2UoaW50IGJvYXJkWzNdWzNdKQp7CiAgICAvKuW8leaVsGJvYXJk44Gu5YaF5a6544KS44OB44Kn44OD44Kv44GX44CB4peL44GM5Yud44Gh44Gq44KJMeOCkuOAgcOX44GM5Yud44Gh44Gq44KJMuOCkuOAgQogICAgICAg5byV44GN5YiG44GR44Gq44KJM+OCkuOAgeOBvuOBoOWLneaVl+OBjOaxuuOBvuOBo+OBpuOBhOOBquOBhOOBqOOBjeOBrzDjgpLov5TjgZkgKi8KICAgIGludCB4LCB5OwogICAgaW50IG1sLCBibDsgICAgICAgICAgICAgICAgIC8vIOaPg+OBo+OBpuOBhOOCi+ODqeOCpOODs+aVsAogICAgaW50IG1jLCBiYzsgICAgICAgICAgICAgICAgIC8vIDHjg6njgqTjg7PmlbDjgYjjgovnlKgKICAgIGludCBmU3A7ICAgICAgICAgICAgICAgICAgICAvL+epuueZveOBguOCi+OBi++8n+ODleODqeOCsAoKICAgIC8vCiAgICBmU3AgPSBtbCA9IGJsID0gMDsKICAgIC8vIOihjAogICAgZm9yICh5ID0gMDsgeSA8IDM7IHkrKykgewogICAgICAgIG1jID0gYmMgPSAwOwogICAgICAgIGZvciAoeCA9IDA7IHggPCAzOyB4KyspIHsKICAgICAgICAgICAgc3dpdGNoIChib2FyZFt5XVt4XSkgewogICAgICAgICAgICBjYXNlIDE6CiAgICAgICAgICAgICAgICBtYysrOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgLTE6CiAgICAgICAgICAgICAgICBiYysrOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgICBmU3AgPSAxOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKG1jID09IDMpIHsKICAgICAgICAgICAgbWwrKzsKICAgICAgICB9CiAgICAgICAgaWYgKGJjID09IDMpIHsKICAgICAgICAgICAgYmwrKzsKICAgICAgICB9CiAgICB9CgogICAgLy8g5YiXCiAgICBmb3IgKHggPSAwOyB4IDwgMzsgeCsrKSB7CiAgICAgICAgbWMgPSBiYyA9IDA7CiAgICAgICAgZm9yICh5ID0gMDsgeSA8IDM7IHkrKykgewogICAgICAgICAgICBzd2l0Y2ggKGJvYXJkW3ldW3hdKSB7CiAgICAgICAgICAgIGNhc2UgMToKICAgICAgICAgICAgICAgIG1jKys7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSAtMToKICAgICAgICAgICAgICAgIGJjKys7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICAgIGZTcCA9IDE7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAobWMgPT0gMykgewogICAgICAgICAgICBtbCsrOwogICAgICAgIH0KICAgICAgICBpZiAoYmMgPT0gMykgewogICAgICAgICAgICBibCsrOwogICAgICAgIH0KICAgIH0KCiAgICAvLyDmlpzjgoEgKHkgPSB4KQogICAgbWMgPSBiYyA9IDA7CiAgICBmb3IgKHggPSAwOyB4IDwgMzsgeCsrKSB7CiAgICAgICAgc3dpdGNoIChib2FyZFt4XVt4XSkgewogICAgICAgIGNhc2UgMToKICAgICAgICAgICAgbWMrKzsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgY2FzZSAtMToKICAgICAgICAgICAgYmMrKzsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfQogICAgICAgIGlmIChtYyA9PSAzKSB7CiAgICAgICAgICAgIG1sKys7CiAgICAgICAgfQogICAgICAgIGlmIChiYyA9PSAzKSB7CiAgICAgICAgICAgIGJsKys7CiAgICAgICAgfQogICAgfQoKICAgIC8vIOaWnOOCgSAoeSA9IDIteCkKICAgIG1jID0gYmMgPSAwOwogICAgZm9yICh4ID0gMDsgeCA8IDM7IHgrKykgewogICAgICAgIHN3aXRjaCAoYm9hcmRbMiAtIHhdW3hdKSB7CiAgICAgICAgY2FzZSAxOgogICAgICAgICAgICBtYysrOwogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlIC0xOgogICAgICAgICAgICBiYysrOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgICAgaWYgKG1jID09IDMpIHsKICAgICAgICAgICAgbWwrKzsKICAgICAgICB9CiAgICAgICAgaWYgKGJjID09IDMpIHsKICAgICAgICAgICAgYmwrKzsKICAgICAgICB9CiAgICB9CgogICAgLy8g5o+D44Gj44Gm44GE44KL44Op44Kk44Oz44Gu5q+U6LyDCiAgICBpZiAobWwgPiBibCkgewogICAgICAgIHJldHVybiAxOwogICAgfSBlbHNlIGlmIChtbCA8IGJsKSB7CiAgICAgICAgcmV0dXJuIDI7CiAgICB9CiAgICAvLyDnqbrnmb3jgYzjgYLjgovjgYsKICAgIGlmIChmU3ApIHsKICAgICAgICByZXR1cm4gMDsKICAgIH0gZWxzZSB7CiAgICAgICAgcmV0dXJuIDM7CiAgICB9Cn0KCnZvaWQgbWFya19ib2FyZChpbnQgYm9hcmRbM11bM10sIGludCB0dXJuKQp7CiAgICAvKuebpOOBrue4puaoquOBruS9jee9ruOBruWFpeWKm+OCkuS/g+OBmeODoeODg+OCu+ODvOOCuOOCkuihqOekuuOBl+OAgeOCreODvOODnOODvOODieOBi+OCieaVsOWApOOCkuiqreOBv+i+vOOBvywKICAgICAgIGJvYXJk6YWN5YiX44G4dHVybuOBruWApOOCkuS7o+WFpeOBmeOCiyAqLwogICAgaW50IHgsIHk7CiAgICBzd2l0Y2ggKHR1cm4pIHsKICAgIGNhc2UgMToKICAgICAgICBwcmludGYoIuOAjOKXiyDjgI3jgpLnva7jgY/loLTmiYDjgpIx772eM+OBp+aMh+WumuOBl+OBpuOBj+OBoOOBleOBhFxuIik7CiAgICAgICAgYnJlYWs7CiAgICBjYXNlIC0xOgogICAgICAgIHByaW50Zigi44CMw5cg44CN44KS572u44GP5aC05omA44KSMe+9njPjgafmjIflrprjgZfjgabjgY/jgaDjgZXjgYRcbiIpOwogICAgICAgIGJyZWFrOwogICAgZGVmYXVsdDoKICAgICAgICBwcmludGYoIuODl+ODreOCsOODqeODoOOCqOODqeODvFxuICAiIF9fRklMRV9fICIgKCVkKVxuIiwgX19MSU5FX18pOwogICAgICAgIGV4aXQoMSk7CiAgICB9CiAgICB3aGlsZSAoMSkgewogICAgICAgIHdoaWxlICgxKSB7CiAgICAgICAgICAgIHByaW50ZigieCA9ICIpOwogICAgICAgICAgICBzY2FuZigiJWQiLCAmeCk7CiAgICAgICAgICAgIGlmICh4IDwgMSB8fCAzIDwgeCkgewogICAgICAgICAgICAgICAgcHJpbnRmKCIx772eM+OCkuWFpeWKm+OBl+OBpuOBj+OBoOOBleOBhFxuIik7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB3aGlsZSAoMSkgewogICAgICAgICAgICBwcmludGYoInkgPSAiKTsKICAgICAgICAgICAgc2NhbmYoIiVkIiwgJnkpOwogICAgICAgICAgICBpZiAoeSA8IDEgfHwgMyA8IHkpIHsKICAgICAgICAgICAgICAgIHByaW50ZigiMe+9njPjgpLlhaXlipvjgZfjgabjgY/jgaDjgZXjgYRcbiIpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgeC0tOwogICAgICAgIHktLTsKICAgICAgICBpZiAoYm9hcmRbeV1beF0pIHsKICAgICAgICAgICAgcHJpbnRmKCIoJWQsJWQp44Gr44Gv5pei44Gr5L2V44GL44GC44KK44G+44GZ44CC5YaN5bqm5YWl5Yqb44GX44Gm44GP44Gg44GV44GEXG4iLCB4ICsgMSwgeSArIDEpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGJvYXJkW3ldW3hdID0gdHVybjsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgIH0KfQoKdm9pZCBwcmludF9tZXNzYWdlKGludCByZXN1bHQpCnsKICAgIC8qcmVzdWx044Gu5YCk44Gr5b6T44Gj44Gm5Yud5pWX44Gu44Oh44OD44K744O844K444KS6KGo56S644GZ44KLICovCiAgICBzd2l0Y2ggKHJlc3VsdCkgewogICAgY2FzZSAwOgogICAgICAgIHByaW50Zigi5Yud6LKg5LitXG4iKTsKICAgICAgICBicmVhazsKICAgIGNhc2UgMToKICAgICAgICBwcmludGYoIuKXiyDjgYzli53jgaFcbiIpOwogICAgICAgIGJyZWFrOwogICAgY2FzZSAyOgogICAgICAgIHByaW50Zigiw5cg44GM5Yud44GhXG4iKTsKICAgICAgICBicmVhazsKICAgIGNhc2UgMzoKICAgICAgICBwcmludGYoIuW8leOBjeWIhuOBkVxuIik7CiAgICAgICAgYnJlYWs7CiAgICBkZWZhdWx0OgogICAgICAgIGJyZWFrOwogICAgfQp9CgovLw==