#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// prototype
void print_board();
int judge();
void mark_board();
void print_message(int result);
int calc(int from, int step);
// global
int board[3][3] = {};
int turn = 1; /*1=maru,-1=batsu*/
int result;
int mode;
int main()
{
printf("0:debug, 1:manual\n");
print_board();
while (1) {
mark_board();
print_board();
if ((result = judge())) break;
turn *= -1;
}
print_message(result);
return 0;
}
void print_board()
{
/*引数で受け取った盤のデータ(board)を縦線(|)や○や×を使い画面に表示する*/
int i, j;
char s[] = {"X O"};
for ( i = 0; i < 3; ++i) {
for ( j = 0; j < 3; ++j) {
printf("|%c", s
[board
[i
][j
] + 1]); }
}
}
int judge()
{
int r, i, *p = *board;
/*引数boardの内容をチェックし、○が勝ちなら1を、×が勝ちなら2を、
引き分けなら3を、まだ勝敗が決まっていないときは0を返す*/
// printf("i am judge\n");
while (1) {
if ((r = calc(0, 3))) break;
if ((r = calc(1, 3))) break;
if ((r = calc(2, 3))) break;
if ((r = calc(0, 1))) break;
if ((r = calc(3, 1))) break;
if ((r = calc(6, 1))) break;
if ((r = calc(0, 4))) break;
if ((r = calc(2, 2))) break;
r = 0;
for ( i = 0; i < 9; ++i)
r += (*p++ == 0);
return r ? 0 : 3;
}
return r == 1 ? 1 : 2;
}
void mark_board()
{
/*盤の縦横の位置の入力を促すメッセージを表示し、キーボードから数値を読み込み,
board配列へturnの値を代入する*/
int i, j;
do {
if (mode == 0) {
} else {
}
} while (i < 0 | i >= 3 | j < 0 | j >= 3 | board[i][j]);
board[i][j] = turn;
}
void print_message(int result)
{
char *s[] = {"", "Oの勝ち", "Xの勝ち", "引き分け"};
/*resultの値に従って勝敗のメッセージを表示する*/
}
int calc(int from, int step)
{
int *p = (*board) + from, r = 0, i;
for ( i = 0; i < 3; ++i) {
r += *p;
p += step;
}
// printf("r = %d %d\n", r, r / 3);
return r / 3;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUua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HJhbmQoKSAlIDM7CiAgICAgIGogPSByYW5kKCkgJSAzOwogICAgfSBlbHNlIHsKICAgICAgcHJpbnRmKCJpIGogPSAiKTsKICAgICAgc2NhbmYoIiVkICVkIiwgJmksICZqKTsKICAgIH0KICB9IHdoaWxlIChpIDwgMCB8IGkgPj0gMyB8IGogPCAwIHwgaiA+PSAzIHwgYm9hcmRbaV1bal0pOwogIGJvYXJkW2ldW2pdID0gdHVybjsKfQoKdm9pZCBwcmludF9tZXNzYWdlKGludCByZXN1bHQpCnsKICBjaGFyICpzW10gPSB7IiIsICJP44Gu5Yud44GhIiwgIljjga7li53jgaEiLCAi5byV44GN5YiG44GRIn07CiAgLypyZXN1bHTjga7lgKTjgavlvpPjgaPjgabli53mlZfjga7jg6Hjg4Pjgrvjg7zjgrjjgpLooajnpLrjgZnjgosqLwogIHByaW50ZigiJXNcbiIsIHNbcmVzdWx0XSk7Cgp9CgppbnQgY2FsYyhpbnQgZnJvbSwgaW50IHN0ZXApCnsKICBpbnQgKnAgPSAoKmJvYXJkKSArIGZyb20sIHIgPSAwLCBpOwogIGZvciAoIGkgPSAwOyBpIDwgMzsgKytpKSB7CiAgICByICs9ICpwOwogICAgcCArPSBzdGVwOwogIH0KICAvLyBwcmludGYoInIgPSAlZCAlZFxuIiwgciwgciAvIDMpOwogIHJldHVybiByIC8gMzsKfQo=