-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmora.c
130 lines (125 loc) · 2.76 KB
/
mora.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void moramenu(void);
void startgame(int);
void overgame(void);
int same(char *pc,char *ps);
int main(int argc,char **argv){
int player=0;
moramenu();
return 0;
}
void startgame(int times){
srand(time(NULL));
char *ppmora=NULL,*pmora=NULL;
int winner=0,flat=0,lose=0;
int win=0;
int computer=0;
system("CLS");
ppmora=malloc(sizeof(char)*15);
label:
printf("input what you want to mora\n (rock > scissors)\n (cloth > rock)\n (scissors > cloth)\n");
printf("input ==>>> ");
scanf("%s",ppmora);
if (*ppmora!='s'&&*ppmora!='r'&&*ppmora!='c'){
goto label;
}
computer=rand()%3+1;
switch (computer){
case 1:
pmora="scissors";
break;
case 2:
pmora="rock";
break;
case 3:
pmora="cloth";
break;
}
win=same(ppmora,pmora);
if (win==1){
winner++;
}else if (win==0){
flat++;
}else{
lose++;
}
times--;
if (times!=0){
goto label;
}
system("CLS");
printf("you win %d time%c , lose %d time%c , flat %d time%c\n",winner,(winner>1)?'s':' ',lose,(lose>1)?'s':' ',flat,(flat>1)?'s':' ');
free(ppmora);
free(pmora);
}
void overgame(void){
system("CLS");
printf("Thank you\n");
return;
}
int same(char *pc,char *ps){
char *pcheck=pc,*pcomputer=ps;
//printf("%p %p\n",pc,ps);
int n=0;
while (*pcheck!='\0'){
if (*pcheck!=*pcomputer){
n=1;
break;
}
pcheck++;
pcomputer++;
}
if (n==1){
if (*pc=='r'&&*ps=='s'){
printf("user winner\n");
return 1;
}else if (*pc=='s'&&*ps=='c'){
printf("user winner\n");
return 1;
}else if (*pc=='c'&&*ps=='r'){
printf("user winner\n");
return 1;
}else{
printf("computer winner\n");
return 2;
}
}else{
printf("Flat\n");
return 0;
}
//return ;
}
void moramenu(void){
int i=10;
char *pc=malloc(sizeof(char)*5);
char *pc1=malloc(sizeof(char)*10);
if (pc1==NULL){
printf("Sorry have an error\n");
exit(1);
}
label:
printf("Do you want to start Mora game: (Y/y or N/n): ");
scanf("%s",pc1);
if (*pc1=='Y'||*pc1=='y'){
//free(pc1);
printf("How times do you want to play: ");
scanf("%s",pc);
startgame(atoi(pc));
}else if(*pc1=='N'||*pc1=='n'){
//free(pc1);
overgame();
}else{
printf("input is error\n");
fflush(stdin);
i--;
if (i==0){
free(pc1);
return ;
}
goto label;
}
free(pc1);
return ;
}