本文实例为大家分享了C语言实现三子棋的具体代码,供大家参考,具体内容如下
代码内有详细注释
源.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
|
#define _CRT_SECURE_NO_WARNINGS
#include"Game.h"
void Game()
{
char result = 0;
//创建棋盘
char board[ROW][COL] = { 0 };
//初始化棋盘
Init_board(board, ROW, COL);
//打印棋盘
Print_board(board, ROW, COL);
while (1)
{
//玩家走棋
printf ( "玩家走棋: " );
Player_move(board, ROW, COL);
Print_board(board, ROW, COL);
//判断游戏状态 继续 平局 玩家胜 电脑胜
result = Game_state(board, ROW, COL);
if (result != 'C' )
{
break ;
}
//电脑走棋
printf ( "电脑走棋: " );
Computer_move(board, ROW, COL);
Print_board(board, ROW, COL);
//判断游戏状态 继续 平局 玩家胜 电脑胜
result = Game_state(board, ROW, COL);
if (result != 'C' )
{
break ;
}
}
if (result == 'X' )
printf ( "玩家胜 " );
else if (result == 'O' )
printf ( "电脑胜 " );
else
printf ( "平局 " );
printf ( "再来一局请输入1,退出请输入0 " );
}
void Menu()
{
int input = 0;
printf ( "***************************** " );
printf ( "**** 1.play 2.exit **** " );
printf ( "***************************** " );
do
{
printf ( "请选择: " );
scanf ( "%d" , &input);
switch (input)
{
case 1:
Game();
break ;
case 0:
printf ( "退出游戏 " );
break ;
default :
printf ( "请输入1或0 " );
break ;
}
} while (input);
}
int main()
{
//设置随机数起点
srand ( time (NULL));
//写一个游戏
//打印菜单
Menu();
return 0;
}
|
Game.h代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include<stdio.h>//NULL
#include<time.h>//time
#include<stdlib.h>//rand srand
#define ROW 3
#define COL 3
void Game();
void Init_board( char board[ROW][COL], int row, int col);
void Print_board( char board[ROW][COL], int row, int col);
void Player_move( char board[ROW][COL], int row, int col);
void Computer_move( char board[ROW][COL], int row, int col);
char Game_state( char board[ROW][COL], int row, int col);
|
Game.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
|
#define _CRT_SECURE_NO_WARNINGS
#include"Game.h"
void Init_board( char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ' ;
}
}
}
void Print_board( char board[ROW][COL], int row, int col)
{
// | |
//---|---|---
// | |
//---|---|---
// | |
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (j == col - 1)
printf ( " %c " , board[i][j]);
else
printf ( " %c |" , board[i][j]);
}
if (i < row -1)
printf ( "---|---|--- " );
}
}
void Player_move( char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
//判断是否可以落子 1.可以落子 2.已占用 3.坐标非法
while (1)
{
printf ( "请输入要落子的坐标: " );
scanf ( "%d%d" , &x, &y);
printf ( " " );
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ' )
{
board[x - 1][y - 1] = 'X' ;
break ;
}
else
printf ( "坐标已占用,请重新输入 " );
}
else
printf ( "坐标非法,请重新输入 " );
}
}
void Computer_move( char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
while (1)
{
x = rand () % row;
y = rand () % col;
if (board[x][y] == ' ' )
{
board[x][y] = 'O' ;
break ;
}
}
printf ( " " );
}
int Full_board( char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ' )
return 0;
}
}
return 1;
}
char Game_state( char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
//判断行是否成立
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ' )
return board[i][0];
}
//判断列是否成立
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ' )
return board[0][i];
} //判断对角线是否成立
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ' )
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ' )
return board[0][2];
//判断是否平局
if (Full_board(board, ROW, COL) == 1)
return 'D' ;
//上述情况都不成立,游戏继续
return 'C' ;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/YouMMo/article/details/112725435