#include <stdio.h>
#include <windows.h>
#include <time.h>
int decision(char player, char table[3][3]);
int reach(char player, char table[3][3], int *n1, int *n2);
int draw(char table[3][3]);
int main(void)
{
char table[3][3];
int i = 0, j = 0;
char p1 = 'A', p2 = 'B';
int count1 = 0, count2 = 0;
int flag1 = 0, flag2 = 0, flag3 = 0;
/*初期値の設定*/
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
*(table[i] + j) = '-';
}
}
while (1)
{
/*リーチの確認(自分)*/
flag1 = reach(p1, table, &i, &j);
/*リーチの確認(相手)*/
flag2 = reach(p2, table, &i, &j);
/*引き分けの確認*/
flag3 = draw(table);
if (((flag1 == 0) && (flag2 == 0)) && (flag3 == 1))
{
break;
}
else
{
flag1 = flag2 = flag3 = 0;
}
printf("★現在の表★\n");
printf("\n 0 1 2\n");
printf(" ------\n");
for (i = 0; i < 3; i++)
{
printf("%d|", i);
for (j = 0; j < 3; j++)
{
printf("%2c", *(table[i] + j));
}
printf("\n");
}
/*入力の処理*/
while (1)
{
/*入力の判定処理*/
while (1)
{
printf("先攻:%cさんの横(x)座標を入力:", p1);
scanf_s("%d", &j);
if ((0 <= j) && (j <= 2))
{
break;
}
else
{
printf("値が正しくありません。\n\n");
}
}
/*入力の判定処理*/
while (1)
{
printf("先攻:%cさんの縦(y)座標を入力:", p1);
scanf_s("%d", &i);
printf("\n");
if ((0 <= i) && (i <= 2))
{
break;
}
else
{
printf("値が正しくありません。\n\n");
}
}
/*表の中身の判定処理*/
if (*(table[i] + j) == '-')
{
*(table[i] + j) = p1;
break;
}
else
{
printf("指定の位置が正しくありません。\n\n");
}
}
printf("★現在の表★\n");
printf("\n 0 1 2\n");
printf(" ------\n");
for (i = 0; i < 3; i++)
{
printf("%d|", i);
for (j = 0; j < 3; j++)
{
printf("%2c", *(table[i] + j));
}
printf("\n");
}
printf("\n");
/*判定処理*/
count1 = decision(p1, table);
if (count1 == 3)
{
break;
}
/*入力の処理*/
while (1)
{
/*リーチの確認(自分)*/
flag1 = reach(p2, table, &i, &j);
if (flag1 == 1)
{
break;
}
/*リーチの確認(相手)*/
flag2 = reach(p1, table, &i, &j);
if (flag2 == 1)
{
break;
}
/*真ん中の確認(後攻)*/
i = 1;
j = 1;
if (table[i][j] == '-')
{
break;
}
while (1)
{
srand((unsigned int) time(NULL));
j = rand() % 3;
for (i = 0; i < 3; i++)
{
if (table[i][j] == '-')
{
break;
}
}
if (table[i][j] == '-')
{
break;
}
}
/*引き分けの確認*/
flag3 = draw(table);
break;
}
if (flag3 == 1)
{
break;
}
printf("後攻:%cさんの横(x)座標を入力:", p2);
printf("%d\n", j);
printf("後攻:%cさんの縦(y)座標を入力:", p2);
printf("%d\n", i);
printf("\n");
/*表の中身へ挿入*/
table[i][j] = p2;
/*判定処理*/
count2 = decision(p2, table);
if (count2 == 3)
{
break;
}
}
/*先攻の判定処理*/
if (count1 == 3)
{
printf("%cさんの勝利!", p1);
}
/*後攻の判定処理*/
else if (count2 == 3)
{
printf("★現在の表★\n");
printf("\n 0 1 2\n");
printf(" ------\n");
for (i = 0; i < 3; i++)
{
printf("%d|", i);
for (j = 0; j < 3; j++)
{
printf("%2c", table[i][j]);
}
printf("\n");
}
printf("%cさんの勝利!", p2);
}
else if (flag3 == 1)
{
printf("★現在の表★\n");
printf("\n 0 1 2\n");
printf(" ------\n");
for (i = 0; i < 3; i++)
{
printf("%d|", i);
for (j = 0; j < 3; j++)
{
printf("%2c", table[i][j]);
}
printf("\n");
}
printf("引き分け!");
}
return 0;
}
int decision(char player, char table[3][3])
{
int i, j;
int count = 0;
while (1)
{
/*横の判定処理(後攻)*/
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (*(table[i] + j) == player)
{
count++;
}
}
if (count == 3)
{
break;
}
else
{
count = 0;
}
}
if (count == 3)
{
break;
}
/*縦の判定処理(後攻)*/
for (j = 0; j < 3; j++)
{
for (i = 0; i < 3; i++)
{
if (*(table[i] + j) == player)
{
count++;
}
}
if (count == 3)
{
break;
}
else
{
count = 0;
}
}
if (count == 3)
{
break;
}
/*斜め右下がりの判定処理(後攻)*/
for (i = 0, j = 0; i < 3; i++, j++)
{
if (*(table[i] + j) == player)
{
count++;
}
}
if (count == 3)
{
break;
}
else
{
count = 0;
}
/*斜め左下がりの判定処理(後攻)*/
for (i = 2, j = 0; i >= 0; i--, j++)
{
if (*(table[i] + j) == player)
{
count++;
}
}
if (count == 3)
{
break;
}
else
{
count = 0;
}
break;
}
return count;
}
int reach(char player, char table[3][3], int *n1, int *n2)
{
int i, j;
int count1 = 0, count2 = 0;
int flag = 0;
while (1)
{
/*リーチ確認(横)*/
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (*(table[i] + j) == '-')
{
count1++;
}
else if (*(table[i] + j) == player)
{
count2++;
}
}
if ((count1 == 1) && (count2 == 2))
{
flag = 1;
break;
}
else
{
count1 = count2 = 0;
}
}
if (flag == 1)
{
for (j = 0; j < 3; j++)
{
if (*(table[i] + j) == '-')
{
count1 = count2 = 0;
break;
}
}
break;
}
/*リーチ確認(縦)*/
for (j = 0; j < 3; j++)
{
for (i = 0; i < 3; i++)
{
if (*(table[i] + j) == '-')
{
count1++;
}
else if (*(table[i] + j) == player)
{
count2++;
}
}
if ((count1 == 1) && (count2 == 2))
{
flag = 1;
break;
}
else
{
count1 = count2 = 0;
}
}
if (flag == 1)
{
for (i = 0; i < 3; i++)
{
if (*(table[i] + j) == '-')
{
count1 = count2 = 0;
break;
}
}
break;
}
/*リーチ確認(斜め右下がり)*/
for (i = 0, j = 0; i < 3; i++, j++)
{
if (*(table[i] + j) == '-')
{
count1++;
}
else if (*(table[i] + j) == player)
{
count2++;
}
}
if ((count1 == 1) && (count2 == 2))
{
flag = 1;
}
else
{
count1 = count2 = 0;
}
if (flag == 1)
{
for (i = 0, j = 0; i < 3; i++, j++)
{
if (*(table[i] + j) == '-')
{
count1 = count2 = 0;
break;
}
}
break;
}
/*リーチ確認(斜め左下がり)*/
for (i = 2, j = 0; i >= 0; i--, j++)
{
if (*(table[i] + j) == '-')
{
count1++;
}
else if (*(table[i] + j) == player)
{
count2++;
}
}
if ((count1 == 1) && (count2 == 2))
{
flag = 1;
}
else
{
count1 = count2 = 0;
}
if (flag == 1)
{
for (i = 2, j = 0; i >= 0; i--, j++)
{
if (*(table[i] + j) == '-')
{
count1 = count2 = 0;
break;
}
}
break;
}
break;
}
if (flag == 1)
{
*n1 = i;
*n2 = j;
return flag;
}
else
{
return flag;
}
}
int draw(char table[3][3])
{
int i, j;
int count = 0;
int flag = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (*(table[i] + j) == '-')
{
count++;
}
}
}
if (count <= 2)
{
flag = 1;
}
return flag;
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme