Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Number guessing Mini Game source code sharing realized by linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

本篇内容主要讲解"linux实现的猜数字小游戏源码分享",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"linux实现的猜数字小游戏源码分享"吧!

一个简单的linux猜数字小游戏源码

游戏规则:

猜数字游戏通常由两个人玩,一方出数字,一方猜。出数字的人要想好一个没有重复数字的 4 个数,不能让猜的人知道。猜的人就可以开始猜。每猜一个数字,出数者就要根据这个数字给出几 A 几 B,其中 A 前面的数字表示位置正确的数的个数,而 B 前的数字表示数字正确而位置不对的数的个数。如正确答案为 5234,而猜的人猜 5346,则是 1A2B,其中有一个 5 的位置对了,记为 1A,而 3 和 4 这两个数字对了,而位置没对,因此记为 2B,合起来就是 1A2B。接着猜的人再根据出题者的几 A 几 B 继续猜,直到猜中(即 4A0B)为止。

猜的人有 8 次机会。

例如:

乙出一个数字,甲猜。

甲 乙

1234 1A0B

5678 2A1B

5674 1A1B

5638 1A1B

2678 2A2B

6278 4A0B(猜中)

源码:

#!/bin/bashclearechoecho "###################################################################"echo "# this is a bash-shell game write by lee #"echo "# this game is infinite frequency 猜数字 #"echo "# version 2.1.1.20200421 #"echo "###################################################################"echo -e "\n\n"declare INPUTdeclare PASSWORDdeclare LEN_PWDdeclare Adeclare Bdeclare LOOP #this function is create random numberrandom_number(){ PASSWORD=$RANDOM LEN_PWD=`echo $PASSWORD | wc -L` if [[ $LEN_PWD -ne 4 ]] then random_number else #输出标准值,测试需要,开发完成注释掉 echo $PASSWORD input fi} #this function is accept the input from user's keyboardinput(){ read -n4 -p "please input a number between 0000-9999:" input# 10#${input} 进制转换 if [[ 10#${input} -eq 10#${PASSWORD} ]] then echo -e "\n" echo "#############################################" echo "#congratulations!You have tried $LOOP times!#" echo "# The password is $PASSWORD ! #" echo "#############################################" exit elif [[ $LOOP -eq 6 ]] then echo -e "\n" echo "You have tried $LOOP times!Game over!" exit else A=0 B=0 count_a count_b echo -e "\n" echo "****************************" echo "* "$A"A"$B"B *" echo "****************************" echo "You have tried $LOOP times! You left `expr 6 - $LOOP` times!" LOOP=`expr $LOOP + 1` input fi} #this function is count the variable A's valuecount_a(){ for i in `seq 4` do VAR_INPUT=`expr substr $input $i 1` for j in `seq 4` do VAR_PASSWORD=`expr substr $PASSWORD $j 1` if [[ $VAR_INPUT -eq $VAR_PASSWORD ]] && [[ $i -eq $j ]] then A=`expr $A + 1` fi done done} #this function is count the variable B's valuecount_b(){ for i in `seq 4` do VAR_INPUT=`expr substr $input $i 1` for j in `seq 4` do VAR_PASSWORD=`expr substr $PASSWORD $j 1` if [[ $VAR_INPUT -eq $VAR_PASSWORD ]] && [[ $i -ne $j ]] then B=`expr $B + 1` fi done done} LOOP=1random_number到此,相信大家对"linux实现的猜数字小游戏源码分享"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report