C语言康复训练3
head.c
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include"project.h"
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1 : game(); break;
case 0 : printf("爱玩玩,不玩滚\n"); break;
default: printf("请重新输入\n"); break;
}
} while (input);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
project,c
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include"project.h"
void menu()
{
printf("**********************\n");
printf("**********************\n");
printf("******1.开始游戏******\n");
printf("******0.退出游戏******\n");
printf("**********************\n");
printf("**********************\n");
}
void game()
{
char ret = 0;
char board[hang][lie];
chushihua(board, hang, lie);
xianshi(board, hang, lie);
while (1)
{
wanjiadong(board, hang, lie);
xianshi(board, hang, lie);
ret = panduan(board, hang, lie);
if (ret != 'j')
{
break;
}
diannaodong(board, hang, lie);
xianshi(board, hang, lie);
ret = panduan(board, hang, lie);
if (ret != 'j')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else if (ret == 'p')
{
printf("平局\n");
}
}
void chushihua(char board[hang][lie], int Hang, int Lie)
{
int i = 0;
int j = 0;
for (i = 0;i < Hang;i++)
{
for (j = 0;j < Lie;j++)
{
board[i][j] = ' ';
}
}
}
void xianshi(char board[hang][lie], int Hang, int Lie)
{
int i = 0;
for (i = 0;i < hang;i++)
{
int j = 0;
for (j = 0;j < lie;j++)
{
printf(" %c ", board[i][j]);
if (j < lie - 1)
{
printf("|");
}
}
printf("\n");
if (i < hang - 1)
{
for (j = 0; j < lie; j++)
{
printf("---");
if (j < lie - 1)
printf("|");
}
printf("\n");
}
}
}
void wanjiadong(char board[hang][lie], int Hang, int Lie)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入想要输入的坐标;\n");
scanf("%d""%d", &x, &y);
if (x >= 1 && x <= Hang && y >= 1 && y <= Lie)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该坐标被占用\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
void diannaodong(char board[hang][lie], int Hang, int Lie)
{
int x = 0;
int y = 0;
printf("电脑开始动了\n");
while (1)
{
x = rand() % Hang;
y = rand() % Lie;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
char panduan(char board[hang][lie], int Hang, int Lie)
{
int i = 0;
for (i = 0; i < hang; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][0];
}
}
for (i = 0; i < lie; i++)
{
if (board[0][i] == board[2][i] && board[2][i] == board[1][i] && board[1][i] != ' ')
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[0][2] == board[2][0] && board[2][0] == board[1][1] && board[1][1] != ' ')
{
return board[1][1];
}
if (1 == shibushiman(board, Hang, Lie))
{
return 'p';
}
return 'j';
}
int shibushiman(char board[hang][lie], int Hang, int Lie)
{
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
- 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
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
project.h
#pragma once
#define hang 3
#define lie 3
void menu();
void game();
void chushihua(char board[hang][lie], int Hang, int Lie);
void xianshi(char board[hang][lie], int Hang, int Lie);
void wanjiadong(char board[hang][lie], int Hang, int Lie);
void diannaodong(char board[hang][lie], int Hang, int Lie);
char panduan(char board[hang][lie], int Hang, int Lie);
int shibushiman(char board[hang][lie], int Hang, int Lie);