-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path五子棋.cpp
126 lines (105 loc) · 2.5 KB
/
五子棋.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//棋子 ● ○
#include <stdio.h>
#include <stdlib.h>
#define N 15
int chessboard[N + 1][N + 1] = { 0 };
//用来记录轮到玩家1还是玩家2
int whoseTurn = 0;
void initGame(void);
void printChessboard(void);
void playChess(void);
int judge(int, int);
int main(void)
{
//初始化游戏
initGame();
while (1)
{
//每次循环自增1,实现玩家轮流下子
whoseTurn++;
playChess();
}
return 0;
}
void initGame(void)
{
char c;
printf("欢迎^_^请输入y进入游戏:");
c = getchar();
if ('y' != c && 'Y' != c)
exit(0);
system("cls");
printChessboard();
}
void printChessboard(void)
{
int i, j;
for (i = 0; i <= N; i++)
{
for (j = 0; j <= N; j++)
{
if (0 == i)
printf("%3d", j);
else if (j == 0)
printf("%3d", i);
else if (1 == chessboard[i][j])
printf(" ●");
else if (2 == chessboard[i][j])
printf(" ○");
else
printf(" *");
}
printf("\n");
}
}
void playChess(void)
{
int i, j, winner;
if (1 == whoseTurn % 2)
{
printf("轮到玩家1,请输入棋子的位置,格式为行号+空格+列号:");
scanf("%d %d", &i, &j);
chessboard[i][j] = 1;
}
else
{
printf("轮到玩家2,请输入棋子的位置,格式为行号+空格+列号:");
scanf("%d %d", &i, &j);
chessboard[i][j] = 2;
}
system("cls");
printChessboard();
if (judge(i, j))
{
if (1 == whoseTurn % 2)
printf("玩家1胜!\n");
else
printf("玩家2胜!\n");
}
}
int judge(int x, int y)
{
int i, j;
int t = 2 - whoseTurn % 2;
for (i = x - 4, j = y; i <= x; i++)
{
if (i >= 1 && i <= N - 4 && t == chessboard[i][j] && t == chessboard[i + 1][j] && t == chessboard[i + 2][j] && t == chessboard[i + 3][j] && t == chessboard[i + 4][j])
return 1;
}
for (i = x, j = y - 4; j <= y; j++)
{
if (j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i][j + 1] && t == chessboard[i][j + 1] && t == chessboard[i][j + 3] && t == chessboard[i][j + 4])
return 1;
}
for (i = x - 4, j = y - 4; i <= x, j <= y; i++, j++)
{
if (i >= 1 && i <= N - 4 && j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i + 1][j + 1] && t == chessboard[i + 2][j + 2] && t == chessboard[i + 3][j + 3] && t == chessboard[i + 4][j + 4])
return 1;
}
for (i = x + 4, j = y - 4; i >= 1, j <= y; i--, j++)
{
if (i >= 1 && i <= N - 4 && j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i - 1][j + 1] && t == chessboard[i - 2][j + 2] && t == chessboard[i - 3][j + 3] && t == chessboard[i - 4][j + 4])
return 1;
}
return 0;
}