C语言实现简易五子棋

时间:2022-09-19 13:28:54

本文实例为大家分享了C语言实现简易五子棋的具体代码,供大家参考,具体内容如下

?
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
 
#define ROW 5
#define COL 5
 
char g_broad[ROW][COL];
void menu();
void menu()
{
 printf("-----------------------\n");
 printf("------1.开始游戏-------\n");
 printf("------0.结束游戏-------\n");
 printf("-----------------------\n");
 printf("请输入您的选择:");
 }
void Init(char broad[ROW][COL])
{
  for (int row = 0; row < ROW; ++row)
  {
  for (int col = 0; col < COL; ++col)
   {
   broad[row][col] = ' ';
   }
  }
 }
void print(char broad[ROW][COL])
{
 for (int row = 0; row < ROW; ++row)
 {
  printf("| %c | %c | %c | %c | %c |\n", broad[row][0],
  broad[row][1], broad[row][2], broad[row][3],
  broad[row][4]);
  if (row != ROW - 1)
 {
 printf(" ---|---|---|---|--- \n");
    }
 }
}
void playermove(char broad[ROW][COL])
{
  printf("玩家落子:\n");
  while (1)
  {
  printf("玩家请输入一组坐标:(row col)");
  int row = 0;
  int col = 0;
  scanf("%d %d", &row, &col);
  if (row < 0 || row >= ROW || col < 0 || col >= COL)
  {
  printf("您输入的坐标非法,请重新输入:\n");
  continue;
  }
 else if (broad[row][col] != ' ')
 {
  printf("该位置已经被占用,请重新输入:\n");
  continue;
 }
 else
 {
  broad[row][col] = 'x';
  break;
 }
 }
}
void computermove(char broad[ROW][COL])
{
  printf("电脑落子:\n");
  while (1)
  {
   int row = rand() % ROW;
   int col = rand() % COL;
   if (broad[row][col] != ' ')
   {
   continue;
   }
  else
  {
  broad[row][col] = 'o';
  break;
  }
  }
 }
int Isfull(char broad[ROW][COL])
{
 for (int row = 0; row < ROW; ++row)
 {
  for (int col = 0; col < COL; ++col)
  {
  if (broad[row][col] == ' ')
  return 0;
  }
  }
 return 1;
}
char checkwinner(char broad[ROW][COL])
{
 for (int row = 0; row < ROW; ++row)
 {
 if (broad[row][0] == broad[row][1]
 && broad[row][0] == broad[row][2]
 && broad[row][0] == broad[row][3]
 && broad[row][0] == broad[row][4]
 && broad[row][0] != ' ')
 {
 return broad[row][0];
 }
   }
   for (int col = 0; col < COL; ++col)
   {
   if (broad[0][col] == broad[1][col]
   && broad[0][col] == broad[2][col]
   && broad[0][col] == broad[3][col]
   && broad[0][col] == broad[4][col]
   && broad[0][col] != ' ')
    {
    return broad[0][col];
    }
    }
    if (broad[0][0] == broad[1][1]
    && broad[0][0] == broad[2][2]
    && broad[0][0] == broad[3][3]
    && broad[0][0] == broad[4][4]
    && broad[0][0] != ' ')
    {
    return broad[0][0];
    }
    if (broad[0][4] == broad[1][3]
    && broad[0][4] == broad[2][2]
    && broad[0][4] == broad[3][1]
    && broad[0][4] == broad[4][0]
    && broad[4][0] != ' ')
    {
    return broad[4][0];
    }
    if (Isfull(broad))
     {
     return 'p';
     }
     else
     return ' ';
    }
int main()
{
srand((unsigned int)time(0));
int input = 0;
menu(g_broad); //初始化棋盘,将棋盘初始化成' '
Init(g_broad);
scanf("%d", &input);
char winner = ' ';
while (input)
{
 //第一次打印棋盘
 print(g_broad);
 //玩家输入,提示玩家输入一组坐标,检查游戏是否结束
 playermove(g_broad);
 winner = checkwinner(g_broad);
 if (winner != ' ')
  {
  break;
  }
  //电脑输入,瞎下,检查游戏是否结束
  computermove(g_broad);
  winner = checkwinner(g_broad);
  if (winner != ' ')
   {
   break;
   }
   }
   if (winner == 'x')
   {
   printf("玩家胜利!\n");
   print(g_broad);
   }
   if (winner == 'o')
  {
  printf("电脑胜利!\n");
  print(g_broad);
  }
  if (winner == 'p')
  {
  printf("和棋!\n");
  print(g_broad);
  }
  if (winner == ' ')
  {
  printf("游戏结束!goodbay~\n");
  return 0;
  }
  return 0;
}

游戏运行结果如下:

C语言实现简易五子棋

C语言实现简易五子棋

C语言实现简易五子棋

注:使用宏定义可以扩充棋盘

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_43852903/article/details/85002376