1. Project content
The project is to make a game that human do scissor-stone-cloth with computer.
2. Project requirement
Computer is able to make all scissor stone and cloth
Human is able to input his choice of scissor stone or cloth
The game is just to tell the choice of both Computer and human
The game is just to tell the result of the competition
3. Project achievement
3.1 The game is just to tell the result of the competition
3.1.1 Choice Type
Scissor
Stone
Cloth
3.1.2 Choice Type Defination
char game_type[3][8]={"scissor", "stone", "cloth"};
Scissor = 0
Stone = 1
Cloth = 2
3.1.3 Result type:
Computer win
Human win
Draw
3.1.4 Relationship between choice type and result type
Assume the former is computer choice, and the later human choice. Relationship
cloth–cloth = 0 (draw) cloth–stone = 1 (computer win) cloth–scittor =2 (human win)
stone–cloth = -1(human win) stone-stone =0 (draw) stone–scittor = 1 (computer win)
scisser-cloth = -2 (computer win)scissor – stone = -1(human win) scissor – scissor = 0 (draw)
Analyse the relationship
2 human win => +3%3 =2
1 computer win =>+3%3 =1
0 draw => +3 %3 =0
-1 human win => +3 %3=2
-2 computerwin => +3%3=1
3.1.5 Result type:
Computer win: 1
Human win: 2
Draw: 0
char result_type[3][13]={"Draw", "Computer win", "Human win"};
3.2 Computer is able to make all scissor stone and cloth
Using rand()%3
3.3 Human is able to input his choice of scissor stone or cloth
scanf
3.4 The game is just to tell the choice of both Computer and human
printf
4. Project Coding
5. #include <stdio.h>
6. #include <stdlib.h>
7.
8. int main(int argc, char *argv[])
9. {
10.
11. char game_type[3][8]={"scissor", "stone", "cloth"};
12. char result_type[3][13]={"Draw", "Computer win", "Human win"};
13. int computer_choice,human_choice;
14. int ret,result;
15.
16. while(1){
17. //computer choice
18. computer_choice = rand()%3;
19. //human choice
20. do{
21. printf("Please input your choice.0 for scissor, 1 for stone, 2 for cloth /n");
22. ret = scanf("%d",&human_choice);
23. }while(human_choice >2||human_choice<0);
24.
25. //Judgment of choise
26. printf("Computer choose:%s/n",game_type[computer_choice]);
27. printf("human choose: %s/n",game_type[human_choice]);
28.
29. //Judgment of result
30. result= ((computer_choice - human_choice) + 3) % 3;
31. printf("Game result:%s/n",result_type[result]);
32.
33. }
34.
35. return 0;
36. } Project testing
zhoudongyong@ubuntu:~/work/linux_c/game$ ./game_ssc
Please input your choice.0 for scissor, 1 for stone, 2 for cloth
0
Computer choose:stone
human choose: scissor
Game result:Computer win
Please input your choice.0 for scissor, 1 for stone, 2 for cloth
1
Computer choose:stone
human choose: stone
Game result:Draw
Please input your choice.0 for scissor, 1 for stone, 2 for cloth
2
Computer choose:scissor
human choose: cloth
Game result:Computer win
37. Project summarization
The game is a very simple game.
The project not just aims to achieve the goal, but follows and form a good developing flow, just as is show below.
Also, The project is using a very good way to solve problem like the game`s judgments.
A good date struct is well solving problem replacing codings. It is Data-driven Programming
The game_type[3][8] and result_type[3][13] can replace of if-elseis-else or switch-case coding.