题目要求:编写脚本,实现人机石头剪刀布游戏
#!/usr/bin/bash declare -A array_key array_key=(["rock"]=0 ["paper"]=1 ["scissors"]=2) array_=(rock paper scissors) key=1 stty erase '^H' while [ $key -eq 1 ] do let system_key_index=$RANDOM%3 echo -e -n "\e[34mPlease input you gesture( rock or paper or scissors or quit): \e[0m" read self_key # exit if [ "$self_key" == "quit" ];then echo "Good bay!" exit fi # input error self_key_index=${array_key[$self_key]} if [ -z $self_key_index ];then echo "you input error!please input again!" continue fi let right=($system_key_index+1)%3 echo -e "\e[32mcomputer input ${array_[$system_key_index]}" echo -e "you input $self_key\e[0m" if [ $system_key_index -eq $self_key_index ];then echo -e "\e[36myou are draw\e[0m!" elif [ $right -eq $self_key_index ];then echo -e "\e[35myou are win!\e[0m" else echo -e "\e[31myou are failure!\e[0m" fi done
总结:
- echo 的颜色输出的写法:echo -e “\e[33mthis is a text.\e[0m”
- shell中的随机数变量RANDOM,范围:0~32767。