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
|
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<Windows.h> int pcinput; //电脑输入 int userinput; //用户输入 #define echo ">_ " //俺觉得好看 char case_win( int left, int right); //判断哪边赢了;left是玩家,right是电脑 int echoinput( int inputnum); //根据两个全局变量输出对应进行的操作 int main() { printf ( "规则\n\n" ); puts ( "1.石头\n" ); puts ( "2.剪刀\n" ); puts ( "3.布 \n" ); printf ( "%s你的输入:" , echo); scanf_s( "%d" , &userinput); printf ( "\n" ); if (echoinput(userinput) == 1) { Sleep(1000); system ( "cls" ); main(); } else { printf ( "\n" ); printf ( "\n" ); printf ( "%s等待电脑结果.\0" , echo); Sleep(500); printf ( "." ); Sleep(500); printf ( ".\n" ); srand ( time (NULL)); pcinput = rand () % 3 + 1; printf ( "\n" ); echoinput(pcinput); printf ( "\n" ); printf ( "\n" ); if (case_win(userinput,pcinput) == 'L' ) { printf ( "玩家胜利" ); } else if (case_win(userinput,pcinput)== 'R' ){ printf ( "电脑胜利" ); } else { printf ( "双方平局" ); } printf ( "\n" ); printf ( "\n" ); printf ( "--------------------" ); printf ( "\n" ); printf ( "\n" ); printf ( "三秒后继续..." ); Sleep(1000); printf ( "二秒后继续..." ); Sleep(1000); printf ( "一秒后继续..." ); Sleep(1000); system ( "cls" ); main(); } return 0; } char case_win( int left, int right) { char reslut; /* *声明 *石头->1 *剪刀->2 * _布->3 */ /* * 规则: * 石头->剪刀 * 剪刀->布 * 布->石头 */ /*判断,左边是玩家*/ if (left == right) { reslut = 'P' ; } else { if (left == 1) { if (right == 2) { reslut = 'L' ; } else if (right == 3) { reslut = 'R' ; } } else if (left == 2) { if (right == 1) { reslut = 'R' ; } else if (right == 3) { reslut = 'L' ; } } else if (left == 3) { if (right == 1) { reslut = 'L' ; } else if (right == 2) { reslut = 'R' ; } } } return reslut; } int echoinput( int inputnum) { int reslut = 0; switch (inputnum) { case 1: printf ( "(石头)" ); break ; case 2: printf ( "(剪刀)" ); break ; case 3: printf ( "(布)" ); break ; default : printf ( "(输入有误)" ); reslut = 1; break ; } return reslut; } |
全局变量
1
2
|
int pcinput; //电脑输入 int userinput; //用户输入 |
函数定义
1
2
|
char case_win( int left, int right); //判断哪边赢了;left是玩家,right是电脑 int echoinput( int inputnum); //根据两个全局变量输出对应进行的操作 |
打印 规则和输入提示
1
2
3
4
5
6
7
|
printf ( "规则\n\n" ); puts ( "1.石头\n" ); puts ( "2.剪刀\n" ); puts ( "3.布 \n" ); printf ( "%s你的输入:" , echo); scanf_s( "%d" , &userinput); printf ( "\n" ); |
Main方法中的主要逻辑
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
|
int main() { printf ( "规则\n\n" ); puts ( "1.石头\n" ); puts ( "2.剪刀\n" ); puts ( "3.布 \n" ); printf ( "%s你的输入:" , echo); scanf_s( "%d" , &userinput); printf ( "\n" ); if (echoinput(userinput) == 1) { Sleep(1000); system ( "cls" ); main(); } else { printf ( "\n" ); printf ( "\n" ); printf ( "%s等待电脑结果.\0" , echo); Sleep(500); printf ( "." ); Sleep(500); printf ( ".\n" ); srand ( time (NULL)); pcinput = rand () % 3 + 1; printf ( "\n" ); echoinput(pcinput); printf ( "\n" ); printf ( "\n" ); if (case_win(userinput,pcinput) == 'L' ) { printf ( "玩家胜利" ); } else if (case_win(userinput,pcinput)== 'R' ){ printf ( "电脑胜利" ); } else { printf ( "双方平局" ); } printf ( "\n" ); printf ( "\n" ); printf ( "--------------------" ); printf ( "\n" ); printf ( "\n" ); printf ( "三秒后继续..." ); Sleep(1000); printf ( "二秒后继续..." ); Sleep(1000); printf ( "一秒后继续..." ); Sleep(1000); system ( "cls" ); main(); } return 0; } |
函数实现-判断输入
当参数inputnum为1,2,3时,进行对应的打印操作,如果有误在Main方法中清屏递归
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
int echoinput( int inputnum) { int reslut = 0; switch (inputnum) { case 1: printf ( "(石头)" ); break ; case 2: printf ( "(剪刀)" ); break ; case 3: printf ( "(布)" ); break ; default : printf ( "(输入有误)" ); reslut = 1; break ; } return reslut; } |
逻辑函数-判断输赢
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
|
char case_win( int left, int right) { char reslut; /* *声明 *石头->1 *剪刀->2 * _布->3 */ /* * 规则: * 石头->剪刀 * 剪刀->布 * 布->石头 */ /*判断,左边是玩家*/ if (left == right) { reslut = 'P' ; } else { if (left == 1) { if (right == 2) { reslut = 'L' ; } else if (right == 3) { reslut = 'R' ; } } else if (left == 2) { if (right == 1) { reslut = 'R' ; } else if (right == 3) { reslut = 'L' ; } } else if (left == 3) { if (right == 1) { reslut = 'L' ; } else if (right == 2) { reslut = 'R' ; } } } return reslut; } |
实现逻辑函数的返回值
1
2
3
4
5
|
/* 当返回值reslut为'L'时,玩家胜利 当返回值reslut为'R'时,电脑胜利 否则双方平局 */ |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/HONKER_MENGSHANG/article/details/114175701