#include <stdio.h>
#include <stdlib.h>
/*******************************************************************************/
//函数功能:初始化棋盘
/*******************************************************************************/
void
init_chess(
char
(*chess_arr)[
3
])
{
int
i,j;
printf(
"游戏开始:\n"
);
for
(i =
0
;i<
3
;i++)
{
for
(j =
0
;j<
3
;j++)
{
*(*(chess_arr+i)+j) =
' '
;
}
}
for
(i =
0
;i<
3
;i++)
{
printf(
" %c | %c | %c"
,chess_arr[i][
0
],chess_arr[i][
1
],chess_arr[i][
2
]);
if
(i!=
2
)
{
printf(
"\n---|---|---\n"
);
}
}
printf(
"\n"
);
}
/*******************************************************************************/
//函数功能:玩家输入下棋的坐标
/*******************************************************************************/
void
input_player_xy(
char
(*chess_arr)[
3
],
int
x,
int
y)
{
int
i =
0
;
if
(chess_arr[x][y]==
' '
)
{
chess_arr[x][y] =
'Y'
;
}
else
{
printf(
"该步以走过\n"
);
}
for
(i =
0
;i<
3
;i++)
{
printf(
" %c | %c | %c"
,chess_arr[i][
0
],chess_arr[i][
1
],chess_arr[i][
2
]);
if
(i!=
2
)
{
printf(
"\n---|---|---\n"
);
}
}
printf(
"\n"
);
}
/*******************************************************************************/
//函数功能:打印电脑下棋的坐标
/*******************************************************************************/
void
input_computer_xy(
char
(*chess_arr)[
3
])
{
int
i =
0
;
for
(i =
0
;i<
3
;i++)
{
printf(
" %c | %c | %c"
,chess_arr[i][
0
],chess_arr[i][
1
],chess_arr[i][
2
]);
if
(i!=
2
)
{
printf(
"\n---|---|---\n"
);
}
}
printf(
"\n"
);
}
/*******************************************************************************/
//函数功能:确定玩家下棋的坐标
/*******************************************************************************/
void
input_player(
char
(*chess_arr)[
3
])
{
int
x =
0
,y =
0
;
printf(
"请下您的棋:"
);
scanf(
"%d%d"
,&x,&y);
input_player_xy(chess_arr,x,y);
}
/*******************************************************************************/
//函数功能:确定电脑下棋的坐标
/*******************************************************************************/
void
input_computer(
char
(*chess_arr)[
3
])
{
int
i =
0
,j =
0
;
for
(i =
0
;i<
3
;i++)
{
for
(j =
0
;j<
3
;j++)
{
if
(chess_arr[i][j]==
' '
)
{
chess_arr[i][j] =
'O'
;
goto
flag;
}
}
}
flag:
input_computer_xy(chess_arr);
}
/*******************************************************************************/
//函数功能:主函数判断电脑还是玩家获胜
/*******************************************************************************/
int
main()
{
char
chess_arr[
3
][
3
];
int
flag =
1
;
init_chess(chess_arr);
while
(flag)
{
int
i =
0
,j =
0
;
int
diag_flag_player =
0
,diag_flag_computer =
0
,count =
0
;
input_player(chess_arr);
input_computer(chess_arr);
for
(i =
0
;i<
3
;i++)
{
if
(((chess_arr[i][
0
]==
'Y'
)&&(chess_arr[i][
1
]==
'Y'
)&&(chess_arr[i][
2
]==
'Y'
))||
((chess_arr[
0
][i]==
'Y'
)&&(chess_arr[
1
][i]==
'Y'
)&&(chess_arr[
2
][i]==
'Y'
))
)
{
printf(
"player win!\n"
);
flag =
0
;
break
;
}
if
(((chess_arr[i][
0
]==
'O'
)&&(chess_arr[i][
1
]==
'O'
)&&(chess_arr[i][
2
]==
'O'
))||
((chess_arr[
0
][i]==
'O'
)&&(chess_arr[
1
][i]==
'O'
)&&(chess_arr[
2
][i]==
'O'
))
)
{
printf(
"computer win!\n"
);
flag =
0
;
break
;
}
for
(j =
0
;j<
3
;j++)
{
if
(chess_arr[i][j]!=
' '
)
{
count++;
}
}
if
(chess_arr[i][i]==
'Y'
)
{
diag_flag_player++;
}
if
(chess_arr[i][
2
-i]==
'O'
)
{
diag_flag_computer++;
}
}
if
(diag_flag_player==
3
)
{
flag =
0
;
printf(
"player win!\n"
);
}
if
(diag_flag_computer==
3
)
{
flag =
0
;
printf(
"computer win!\n"
);
}
if
(count==
9
)
{
flag =
0
;
printf(
"player equal computer!\n"
);
}
}
system(
"pause"
);
return
0
;
}