Shell脚本实现的猜数字小游戏

   2015-08-30 0
核心提示:这篇文章主要介绍了Shell脚本实现的猜数字小游戏,本文的猜数字规则相对一般规则的猜数字可能难度要大不少,需要的朋友可以参考下

生成的密码和用户输入可以接受重复数字。
所以相对一般规则的猜数字可能难度要大不少。

本版本规则:

A--数字对,位置也对
B--排除A的结果后,数字对,但位置不对

开始后,系统化初始化一个4位可重复数字,如“1223”。假设用户第一次输入“1234”,那么系统将提示“2A1B”,前两位数字“12”相同并且位置也相同,为“2A”。后两位数字中,用户输入的“3”与密文中“3”相同,但两者位置不同,则为“1B”,最终结果为“2A1B”。

再假设用户此时输入“1232”,那么结果则为“2A2B”,计算方法与前次一样。

代码如下:

复制代码 代码如下:

#!/bin/bash
clear
echo
echo "###################################################################"
echo "#  this is a bash-shell game write by Email:breeze7086@gmail.com  #"
echo "# the game called *digits*,and this version have repeated numbers #"
echo "#                           version 1.0                           #"
echo "###################################################################"
echo -e "\n\n"
declare INPUT
declare PASSWORD
declare A
declare B
declare X
declare Y
declare LOOP
#This funtion init the variable PASSWORD that user need to guess
init_password()
{
        PASSWORD=`echo $(($RANDOM%10000))`
        echo $PASSWORD | grep '^[0-9]\{4\}$' >/dev/null 2>&1
        if [ $ != 0 ]
        then
                init_password
        else
                input
        fi
}
#This funtion accept the input from user's keyboard
input()
{
        echo -n "please input a number between 0000-9999:"
        read INPUT
        echo $INPUT | grep '^[0-9]\{4\}$' >/dev/null 2>&1
        if [ $ != 0 ]
        then
                echo "retry a number between 0000-9999 and do not input a char"
                input
        else
                judge
        fi
}
#This funtion is the main funtion
judge()
{
        X=$INPUT
        Y=$PASSWORD
        while [ $INPUT != $PASSWORD ]
        do
                A=0
                B=0
                judge_a
                judge_b
                LOOP=`expr $LOOP + 1`
                echo "****************************"
                echo "*           "$A"A"$B"B           *"
                echo "****************************"
                input
        done
}
#This funtion count the variable A's value
judge_a()
{
                for i in `seq 4`
                do
                        VAR_INPUT=`expr substr "$X" $i 1`
                        for j in `seq 4`
                        do
                                VAR_PASSWORD=`expr substr "$Y" $j 1`
                                if [[ $VAR_INPUT = $VAR_PASSWORD && $VAR_INPUT != "" && $VAR_PASSWORD != "" && $i = $j ]]
                                then
                                        A=`expr $A + 1`
                                        X=`expr substr $X 1 "$[$i-1]"``expr substr $X "$[$i+1]" 4`
                                        Y=`expr substr $Y 1 "$[$i-1]"``expr substr $Y "$[$i+1]" 4`
                                        judge_a
                                fi
                        done
                done
}
#This funtion count the variable B's value
judge_b()
{
                for i in `seq 4`
                do
                        VAR_INPUT=`expr substr "$X" $i 1`
                        for j in `seq 4`
                        do
                                VAR_PASSWORD=`expr substr "$Y" $j 1`
                                if [[ $VAR_INPUT = $VAR_PASSWORD && $VAR_INPUT != "" && $VAR_PASSWORD != "" ]]
                                then
                                        B=`expr $B + 1`
                                        X=`expr substr "$X" 1 "$[$i-1]"``expr substr "$X" "$[$i+1]" 4`
                                        Y=`expr substr "$Y" 1 "$[$j-1]"``expr substr "$Y" "$[$j+1]" 4`
                                        judge_b
                                fi
                        done
                done
}
#This is the begin of script
LOOP=1
init_password
echo "#############################################"
echo "#congratulations!You have tried $LOOP times!    #"
echo "#        The password is $PASSWORD !             #"
echo "#############################################"

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • PHP安全之webshell和后门检测
    PHP安全之webshell和后门检测
    基于PHP的应用面临着各种各样的攻击:XSS:对PHP的Web应用而言,跨站脚本是一个易受攻击的点。攻击者可以利用它盗取用户信息。你可以配置Apache,或是写更安全的PHP代码(验证所有用户输入)来防范XSS攻击SQL注入:这是PHP应用中,数据库层的易受攻击点。防范
    02-09
  • 【译】使用 Rust 构建你自己的 Shell
    Build Your Own Shell using Rust 译文原文地址:https://www.joshmcguigan.com/blog/build-your-own-shell-rust/原文作者:Josh Mcguigan译文出自:https://github.com/suhanyujie/article-transfer-rs本文永久链接: https://github.com/suhanyujie/article
    02-09
  • ShellExecute使用详解(DELPHI版)
    有三个API函数可以运行可执行文件WinExec、ShellExecute和CreateProcess。 1.CreateProcess因为使用复杂,比较少用。 2.WinExec主要运行EXE文件。如:WinExec(’Notepad.exe Readme.txt’, SW_SHOW); 3.ShellExecute不仅可以运行EXE文件,也可以运行已经关联
    02-09
  • shell perl 等
    tail -f xls2csv.sh |perl -pe 's/(csv)/\e[1;31m$1\e[0m/g'数据替换grep -E '_id|PHONE|PROFILE30|PROFILE31|PROFILE32|PROFILE35'LABEL272_VALUE_labelLABEL272_VALUE_label_2统计bug,并根据计数排序$NF最后一列为创建时间 $NF-1为处理人awk -F ","'/^(1|2|
    02-09
  • perl版 Webshell存活检测
    原理:检测url返回状态即可源码: 1 #!c:\\perl\\bin\\perl.exe 2 use warnings; 3 use strict; 4 use LWP::UserAgent; 5 $| = 1; 6 print "---------------------------------------------------------\n"; 7 print "|Webshell online check v1.0 |\n"; 8 pri
    02-09
  • shell执行lua脚本传参数 shell脚本执行时传入参
    #lua test.lua 2 5arg[0]= test.lua arg[1]= 2arg[2]= 5if arg[1] and arg[1] == "2" then  print("arg[1]=2")end 
    02-09
  • lua调用shell 脚本
    Lua中,os.execute可以执行dos命令,但是返回的是系统状态码,默认输出。io.popen()也可以执行dos命令,但是返回一个文件。eg:复制代码 代码如下:local t = io.popen('svn help')local a = t:read("*all")--a返回一个字符串,内容是svn help的内容如果想执
    02-09
  • 脚本语言语言脚本语言:Shell , JavaScript、VBS
    今天一直在研究脚本语言语言之类的问题,上午正好有机会和大家共享一下.     脚本语言:Shell , JavaScript、VBScript、Perl、PHP、Python、Ruby、Lua                  工作控制语言和shell    Shell 脚本    此类脚本用于自动
    02-09
  • VB.net shell、IO.File.Open、Process.Start、S
      打开文件还有很多方法,但我了解到运用较多的是上面几种- -,为了防止以后忘记,先把了解到的写下来。1、Shell  这个看了很多网页,最靠谱的运用方法:Shell("cmd.exe /c "Br_PathLocal) ' 一般用shell调用cmd命令来打开文件但问题是,命令窗口关不了- -
    02-09
  • vb6.0的各种SHELL,CMD内部命令、外部命令、SHEL
    1 Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long
    02-09
点击排行