shell 打印俄罗斯方块

网络上一直有一个shell写的俄罗斯方块脚本,他定义了一堆方块

box0=(0 0 0 1 1 0 1 1)
box1=(0 2 1 2 2 2 3 2 1 0 1 1 1 2 1 3)
box2=(0 0 0 1 1 1 1 2 0 1 1 0 1 1 2 0)
box3=(0 1 0 2 1 0 1 1 0 0 1 0 1 1 2 1)
box4=(0 1 0 2 1 1 2 1 1 0 1 1 1 2 2 2 0 1 1 1 2 0 2 1 0 0 1 0 1 1 1 2)
box5=(0 1 1 1 2 1 2 2 1 0 1 1 1 2 2 0 0 0 0 1 1 1 2 1 0 2 1 0 1 1 1 2)
box6=(0 1 1 1 1 2 2 1 1 0 1 1 1 2 2 1 0 1 1 0 1 1 2 1 0 1 1 0 1 1 1 2)

使用shell数组,shell数组不能创建二维数组,所以他使用的是(x0 y0 x1 y1 x2 y2....)方式定义了数组。没8个是一组表示一个方块,如box4是32表示一个方块的四种变化。

写了一个脚本来展示一些它的表现方式。

创建一个函数来处理不同的数组,逻辑就是画一个正方形,数组标记的值写入"[]",没写入的用"  "填充。

#!/bin/bash

#filename: coords.sh

# shell 没有二维数组使用一维数组
function display_box()
{
  local coords
  coords=($(echo "$@")) 

  # 设置一个背景板宽度,后用两个空格填人,这里简单处理一下
  max=$(printf "%d\n" "${coords[@]}" | sort -rn | head -1)
  if (( ${#coords[@]}/2 > max )); then
    ((width = ${#coords[@]} / 2))
  else
    width=$max
  fi


  # make box 两个空格填充背景板
  for ((i = 0; i < width * width; i++)); do box[i]=" "; done

  # coords update box 使用坐标填充"[]"到背景板
  for ((n = 0; n < ${#coords[@]}; n += 2)); do
    ((z = coords[n] + coords[n+1] * width))
    box[z]="[]"
  done

  # display box 遇到余数为0是先回车,否则打印“[]”
  for ((f = 0; f < width * width; f++)); do
    if ((f % width == 0)) ; then # if for 进行数字比较用(())
      echo
      echo -n "${box[f]}"
    else 
      echo -n "${box[f]}"
    fi
  done

# end
echo 
}

# main 打印每种方块
echo "0 ----"
box0=(0 0 0 1 1 0 1 1)
display_box ${box0[@]}

echo "1 ----"
box10=(0 2 1 2 2 2 3 2)
box11=(1 0 1 1 1 2 1 3)
display_box ${box10[@]}
display_box ${box11[@]}

echo "2 ----"
box20=(0 0 0 1 1 1 1 2)
box22=(0 1 1 0 1 1 2 0)
display_box ${box20[@]}
display_box ${box22[@]}

echo "3 ----"
box30=(0 1 0 2 1 0 1 1)
box33=(0 0 1 0 1 1 2 1)
display_box ${box30[@]}
display_box ${box33[@]}

echo "4 ----"
box40=(0 1 0 2 1 1 2 1)
box41=(1 0 1 1 1 2 2 2)
box42=(0 1 1 1 2 0 2 1)
box43=(0 0 1 0 1 1 1 2)
display_box ${box40[@]}
display_box ${box41[@]}
display_box ${box42[@]}
display_box ${box43[@]}

echo "5 ----"
box50=(0 1 1 1 2 1 2 2)
box51=(1 0 1 1 1 2 2 0)
box52=(0 0 0 1 1 1 2 1)
box53=(0 2 1 0 1 1 1 2)
display_box ${box50[@]}
display_box ${box51[@]}
display_box ${box52[@]}
display_box ${box53[@]}

echo "6 ----"
box60=(0 1 1 1 1 2 2 1)
box61=(1 0 1 1 1 2 2 1)
box62=(0 1 1 0 1 1 2 1)
box63=(0 1 1 0 1 1 1 2)
display_box ${box60[@]}
display_box ${box61[@]}
display_box ${box62[@]}
display_box ${box63[@]}

box6 拆分为4个部分,输出如:$ bash coords.sh 这里一定要用bash 去执行,或者chmod +x coords.sh 后 ./coords.sh 其他像zsh fish 等shell会报错。


[][][]
  []


  []
  [][]
  []


  []
[][][]



  []
[][]
  []

### 如何在 CUDA 10.2 上安装 PyTorch 为了确保顺利安装适用于 CUDA 10.2 的 PyTorch 版本,建议通过 Anaconda 来管理依赖关系和环境。以下是具体操作指南: #### 创建并激活新的 Conda 环境 推荐先创建一个新的 Python 环境来隔离不同项目的库文件,防止版本冲突。 ```bash conda create -n pytorch_env python=3.7 conda activate pytorch_env ``` #### 配置国内镜像源加快下载速度 考虑到网络因素可能影响包的获取效率,可设置清华大学开源软件镜像站作为默认渠道之一[^5]。 ```bash conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --set show_channel_urls yes conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/ ``` #### 安装指定版本的 PyTorch 及其相关组件 根据需求选择合适的 PyTorch 和其他必要的扩展模块版本进行安装。对于 CUDA 10.2 用户来说,可以选择如下命令完成安装过程[^2]。 ```bash conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=10.2 -c pytorch ``` #### 验证安装情况 最后一步是在 Python 解释器内部验证是否正确加载了带有 GPU 支持功能的 PyTorch 库[^4]。 ```python import torch print(torch.__version__) print(torch.cuda.is_available()) ``` 如果一切正常,则会显示相应的 PyTorch 版本号,并确认存在可用的 CUDA 设备支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值