[C language] Novices realize simple rock-paper-scissors man-machine battle

1. Knowledge points to be used

1. Understanding of C language loop while, select structure switch and conditional judgment if.
2. The use of logical operators && and ||, ternary operators.
3. Random function and tabulation method

2. Personal learning and understanding when typing code

1. Before writing the code, you should first conceive the framework, what kind of effect you expect to achieve, determine the research object, and the method you need to use, otherwise you will feel unable to start when you write the code.

For example, in this man-machine rock-paper-scissors game, we can think of assigning rock-paper-scissors with numbers, and use the size of the number to judge whether you win or lose;

Secondly, the objects are people and machines. The gestures that people want to make are received by scanf, and the switch realizes different functions according to the received values.

As for computers, they are completely separated from people, their behaviors are not related, and they are random and have no purpose. So I thought of using random functions to randomize computer gestures. In this way, we can judge by comparing the numbers represented by human and computer gestures. Win or lose.

2. Think more and think more, some places have a certain logic, you can try to achieve it from multiple aspects, although sometimes it is just wishful thinking.
E.g:

if (human == computer)
   {
    
    
    printf("平局!\n");    
   }
   else if ((human != 0 && human < computer) || (human == 2 && computer == 0))
   {
    
    
    printf("电脑赢了!\n");
   }
   else
   {
    
    
    printf("你赢了!\n");
    wincount++;
   }
   break;   
   其中我用0代表布,1代表剪刀,2代表石头,以上是可以判断的。

But when I was writing, I had a small idea: Can we avoid the verbosity of the conditions through the feature of taking the remainder?
So I tried it: 1 representative of the cloth, on behalf of scissors 2 and 3 for the stone
which changed else if (human <computer% 3 ) I feel almost
3% 3 = 0 and human comparing to merge cloth <scissors <stone and I think the condition of cloth>stone can be realized more simply. I found that when human takes 1, it is satisfied, but 2 is not enough, because when the computer is random to 3, the result is that the person wins, but the person is Scissors and computers are rocks. Looking at it this way, you need to add the condition of human==1, which is more complicated. Therefore, practice is the only criterion for testing truth. However, the teacher said that there is no harm in thinking about it.

3. When writing, be optimistic about the position of the while(1) loop, whether the defined variable will be reset in the next loop (for example, the definition of global variable count cannot be in while(1).
4. Made for learning, hope Big readers can give valuable suggestions and opinions, and I am grateful for them, and hope to learn and make progress together.

Attach code

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    
    
 //规则如下
 //布<剪刀<石头   并且 布>石头
 //撒下随机函数种子
 srand((unsigned int)time(NULL));
 int wincount = 0;
 int allcount = 0;
 while (1)
    {
    
    
        int human = 0; 
        int computer = 0;
        //制表
     printf("----------------------------\n");
     printf("\t0.退出游戏\n");
     printf("\t1.人机对战\n");
     printf("----------------------------\n");
        int userKey = 0;  
     printf("请输入0~1:\n");
     scanf("%d", &userKey);
  switch (userKey)
  {
    
    
  case 0:
   printf("%d", wincount);
   break;
   
  case 1:
   //制表
   printf("----------------------------\n");
   printf("\t0.布\n");
   printf("\t1.剪刀\n");
   printf("\t2.石头\n");
   printf("----------------------------\n");
   scanf("%d", &human);
   computer = rand() % 3;//范围0~2
   //使用三目运算符打印双方出手的结果
   printf("玩家出的是:%s\n电脑出的是:%s\n",
    (human == 0) ? "布" : (human == 1) ? "剪刀" : "石头", 
    (computer == 0) ? "布" : (computer == 1) ? "剪刀" : "石头");

   if (human == computer)
   {
    
    
    printf("平局!\n");    
   }
   else if ((human != 0 && human < computer) || (human == 2 && computer == 0))
   {
    
    
    printf("电脑赢了!\n");
   }
   else
   {
    
    
    printf("你赢了!\n");
    wincount++;
   }
   break;   
  }
  if (userKey == 0)
		{
    
    
			printf("总次数为:%d,你赢的次数为:%d", allcount,count);
			break;//直接跳出while循环
		}
	    allcount++;
		system("pause");
	    system("cls");//清屏
    }
	system("pause");
	return 0;

}

Guess you like

Origin blog.csdn.net/weixin_50067564/article/details/108304510