C language programming course design - mind reading


foreword

This is the first project ((●'◡'●)) designed by my university C language programming course, I am still very happy haha. Although this project is not difficult to make, and the amount of code is not much, but as long as you have enough skills, any simple thing can be complicated.

1. Game rules

Mind reading, as the name suggests, means that I can know what is on your mind. Of course, this thing is not so magical, how could it be possible to know that you are thinking of letting go, right? The mind reading here is just that we have been tricked.
Specific rules: Choose a number from 0 to 7 and meditate in your heart. After the selection, you need to answer three questions. Enter 1 or 0 for each answer. If you enter other numbers or letters, you will be asked to re-enter. Three questions After answering, you will know the number in your mind, and then we have added a user login system on the basis of the original, that is to say, only by entering the correct account number and password can you log in successfully to play the game, but Note that if the account number is entered incorrectly, the account number needs to be re-entered. If the account number is entered correctly but the password is entered incorrectly, the account number also needs to be re-entered. If the account password has been entered incorrectly three times in total, it will not be possible to enter the account, and it must be re-entered when the game is opened next time.

2. Principle

The essence of mind reading is actually the problem of base conversion. First, we convert the eight numbers from 0 to 7 from decimal to binary. The binary number of the largest number 7 is 111, which means that three-digit binary is enough. If it is not enough, fill in 0. 0:000, 1:001, 2:010, 3:011, 4:100, 5:101, 6:110, 7:111. Then we look from the low bit to the high bit of the binary of these eight numbers to see which numbers have the same bit as 1, so the first question is 1, 3, 5, 7, and the second question is 3, 5, 6, 7, the third question is 4, 5, 6, 7. Then we can know the number in your mind based on the 1 or 0 of the answer to the question, that is to say, this number is calculated by us.

3. Code

#include <bits/stdc++.h>
using namespace std;
int main() 
{
    
    	
	int num[3][4] = {
    
    {
    
    1, 3, 5, 7}, {
    
    2, 3, 6, 7}, {
    
    4, 5, 6, 7}};
	int sum=0,t=1;
	printf("\t\t\t\t\t\t欢迎来到读心术游戏\t\n");
	string s="jiruan6ban";
	int password = 123456789;
	for(int i=1;i<=3;i++)
	{
    
    
		cout<<"请输入您的用户名:"<<endl;
		string username;
		cin>>username;
		int password1;
		if(username==s)
		{
    
    
			cout<<"用户名正确,请输入密码:"<<endl; 
			cin>>password1;
			if(password1==password)
			{
    
    
				cout<<"密码正确!登入成功!"<<endl<<endl;
				cout<<"接下来将进行读心术的游戏了哦!"<<endl;
				cout << "请从下面的数字(0~7)中选择一个默念在心中" << endl;
				cout << "0、1、2、3、4、5、6、7"<<endl;
				cout<<"心里有数了叭!接下来我只需要询问您三个问题就能知道您想的那个数是什么,怎么? 不信吗?那么咋们走着瞧吧哈哈"<<endl<<endl;
				for(int i=0;i<=2;i++)
				{
    
    
					printf("第%d个问题~下面的数字是否有你所想的:",i+1);
					for(int j=0;j<=3;j++)
					{
    
    
						cout<<num[i][j]<<" ";
						
					}
					char a ;
					cout<<"请输入1(有) 或 0(没有)"<<endl;
					while(1)
					{
    
    
						cin>>a;
						if(a!='0'&&a!='1')
						{
    
    
							printf("输入非法,请重新输入:\n");
							continue;
						}	
						else
							break;
					}
					if(a =='1')
					{
    
    
						sum+=t;
						t*=2;
					}
					else
						t*=2;
					
				}
				printf("见证奇迹的时刻到啦!你心里想的那个数字是 %d 吧嘿嘿\n",sum);
				return 0;
																																
			}
			else
			{
    
    
				cout<<"密码错误!"<<endl;
				continue; 
			}		 		 
		}
		else
		{
    
    
			cout<<"用户名错误!"<<endl;
			continue;
		}
			
	}
	cout<<"抱歉!您已输入错误三次,无法再次进行输入,为确保账号的安全,请在下次打开时进行尝试!"<<endl;	
	
}	

Code test:
insert image description here
What I thought was indeed 7, damn it guessed it. If you don't believe me, try it!

Summarize

In general, whether it is a mind-reading project or some other projects, there are rules. The rules are user-oriented and help users understand how to operate. The principle of the project requires us to implement it through code. The code itself is written by us, so the principle is equivalent to writing the things we know well into code so that the computer can understand and execute them. Expansion is to add some other functions on the original basis to increase the playability of the game. After drawing the flow chart of the whole program, we can clearly know the framework of this project. When a bug occurs in a certain part of our program, we can quickly find the part with the problem according to the flow chart and prescribe the right medicine. The process of analyzing the code can improve our framework thinking and have a better understanding of the project.

Guess you like

Origin blog.csdn.net/H1727548/article/details/129431970