#!/bin/bash
# Tetris Game
# $Id$
#
# Copyright (C) xhchen<xhchen@winbond.com.tw>
# All rights reserved.
#
# $Log$
#
#颜色定义
cRed=1
cGreen=2
cYellow=3
cBlue=4
cFuchsia=5
cCyan=6
cWhite=7
colorTable=($cRed $cGreen $cYellow $cBlue $cFuchsia $cCyan $cWhite)
#位置和大小
iLeft=3
iTop=2
((iTrayLeft = iLeft + 2))
((iTrayTop = iTop + 1))
((iTrayWidth = 10))
((iTrayHeight = 15))
#颜色设置
cBorder=$cGreen
cScore=$cFuchsia
cScoreValue=$cCyan
#控制信号
#改游戏使用两个进程,一个用于接收输入,一个用于游戏流程和显示界面;
#当前者接收到上下左右等按键时,通过向后者发送signal的方式通知后者。
sigRotate=25
sigLeft=26
sigRight=27
sigDown=28
sigAllDown=29
sigExit=30
#七中不同的方块的定义
#通过旋转,每种方块的显示的样式可能有几种
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)
#所有其中方块的定义都放到box变量中
box=(${box0[@]} ${box1[@]} ${box2[@]} ${box3[@]} ${box4[@]} ${box5[@]} ${box6[@]})
#各种方块旋转后可能的样式数目
countBox=(1 2 2 2 4 4 4)
#各种方块再box数组中的偏移
offsetBox=(0 1 3 5 7 11 15)
#每提高一个速度级需要积累的分数
iScoreEachLevel=50 #be greater than 7
#运行时数据
sig=0 #接收到的signal
iScore=0 #总分
iLevel=0 #速度级
boxNew=() #新下落的方块的位置定义
cBoxNew=0 #新下落的方块的颜色
iBoxNewType=0 #新下落的方块的种类
iBoxNewRotate=0 #新下落的方块的旋转角度
boxCur=() #当前方块的位置定义
cBoxCur=0 #当前方块的颜色
iBoxCurType=0 #当前方块的种类
iBoxCurRotate=0 #当前方块的旋转角度
boxCurX=-1 #当前方块的x坐标位置
boxCurY=-1 #当前方块的y坐标位置
iMap=() #背景方块图表
#初始化所有背景方块为-1, 表示没有方块
for ((i = 0; i < iTrayHeight * iTrayWidth; i++)); do iMap[$i]=-1; done
#接收输入的进程的主函数
function RunAsKeyReceiver()
{
local pidDisplayer key aKey sig cESC sTTY
pidDisplayer=$1
aKey=(0 0 0)
cESC=`echo -ne "/33"`
cSpace=`echo -ne "/40"`
#保存终端属性。在read -s读取终端键时,终端的属性会被暂时改变。
#如果在read -s时程序被不幸杀掉,可能会导致终端混乱,
#需要在程序退出时恢复终端属性。
sTTY=`stty -g`
#捕捉退出信号
trap "MyExit;" INT TERM
trap "MyExitNoSub;" $sigExit
#隐藏光标
echo -ne "/33[?25l"
while [[ 1 ]]
do
#读取输入。注-s不回显,-n读到一个字符立即返回
read -s -n 1 key
aKey[0]=${aKey[1]}
aKey[1]=${aKey[2]}
aKey[2]=$key
sig=0
#判断输入了何种键
if [[ $key == $cESC && ${aKey[1]} == $cESC ]]
then
#ESC键
MyExit
elif [[ ${aKey[0]} == $cESC && ${aKey[1]} == "[" ]]
then
if [[ $key == "A" ]]; then sig=$sigRotate #<向上键>
elif [[ $key == "B" ]]; then sig=$sigDown #<向下键>
elif [[ $key == "D" ]]; then sig=$sigLeft #<向左键>
elif [[ $key == "C" ]]; then sig=$sigRight #<向右键>
fi
elif [[ $key == "W" || $key == "w" ]]; then sig=$sigRotate #W, w
elif [[ $key == "S" || $key == "s" ]]; then sig=$sigDown #S, s
elif [[ $key == "A" || $key == "a" ]]; then sig=$sigLeft #A, a
elif [[ $key == "D" || $key == "d" ]]; then sig=$sigRight #D, d
elif [[ "[$key]" == "[]" ]]; then sig=$sigAllDown #空格键
elif [[ $key == "Q" || $key == "q" ]] #Q, q
then
MyExit
fi
if [[ $sig != 0 ]]
then
#向另一进程发送消息
kill -$sig $pidDisplayer
fi
done
}
#退出前的恢复
function MyExitNoSub()
{
local y
#恢复终端属性
stty $sTTY
((y = iTop + iTrayHeight + 4))
#显示光标
echo -e "/33[?25h/33[${y};0H"
exit
}
function MyExit()
{
#通知显示进程需要退出
kill -$sigExit $pidDisplayer
MyExitNoSub
}
#处理显示和游戏流程的主函数
function RunAsDisplayer()
{
local sigThis
InitDraw
#挂载各种信号的处理函数
trap "sig=$sigRotate;" $sigRotate
trap "sig=$sigLeft;" $sigLeft
trap "sig=$sigRight;" $sigRight
trap "sig=$sigDown;" $sigDown
trap "sig=$sigAllDown;" $sigAllDown
trap "ShowExit;" $sigExit
while [[ 1 ]]
do
#根据当前的速度级iLevel不同,设定相应的循环的次数
for ((i = 0; i < 21 - iLevel; i++))
do
usleep 20000
sigThis=$sig
sig=0
#根据sig变量判断是否接受到相应的信号
if ((sigThis == sigRotate)); then BoxRotate; #旋转
elif ((sigThis == sigLeft)); then BoxLeft; #左移一列
elif ((sigThis == sigRight)); then BoxRight; #右移一列
elif ((sigThis == sigDown)); then BoxDown; #下落一行
elif ((sigThis == sigAllDown)); then BoxAllDown; #下落到底
fi
done
#kill -$sigDown $$
BoxDown #下落一行
done
}
#BoxMove(y, x), 测试是否可以把移动中的方块移到(x, y)的位置, 返回0则可以, 1不可以
function BoxMove()
{
local j i x y xTest yTest
yTest=$1
xTest=$2
for ((j = 0; j < 8; j += 2))
do
((i = j + 1))
((y = ${boxCur[$j]} + yTest))
((x = ${boxCur[$i]} + xTest))
if (( y < 0 || y >= iTrayHeight || x < 0 || x >= iTrayWidth))
then
#撞到墙壁了
return 1
fi
if ((${iMap[y * iTrayWidth + x]} != -1 ))
then
#撞到其他已经存在的方块了
return 1
fi
done
return 0;
}
#将当前移动中的方块放到背景方块中去,
#并计算新的分数和速度级。(即一次方块落到底部)
function Box2Map()
{
local j i x y xp yp line
#将当前移动中的方块放到背景方块中去
for ((j = 0; j < 8; j += 2))
do
((i = j + 1))
((y = ${boxCur[$j]} + boxCurY))
((x = ${boxCur[$i]} + boxCurX))
((i = y * iTrayWidth + x))
iMap[$i]=$cBoxCur
done
#消去可被消去的行
line=0
for ((j = 0; j < iTrayWidth * iTrayHeight; j += iTrayWidth))
do
for ((i = j + iTrayWidth - 1; i >= j; i--))
do
if ((${iMap[$i]} == -1)); then break; fi
done
if ((i >= j)); then continue; fi
((line++))
for ((i = j - 1; i >= 0; i--))
do
((x = i + iTrayWidth))
iMap[$x]=${iMap[$i]}
done
for ((i = 0; i < iTrayWidth; i++))
do
iMap[$i]=-1
done
done
if ((line == 0)); then return; fi
#根据消去的行数line计算分数和速度级
((x = iLeft + iTrayWidth * 2 + 7))
((y = iTop + 11))
((iScore += line * 2 - 1))
#显示新的分数
echo -ne "/33[1m/33[3${cScoreValue}m/33[${y};${x}H${iScore} "
if ((iScore % iScoreEachLevel < line * 2 - 1))
then
if ((iLevel < 20))
then
((iLevel++))
((y = iTop + 14))
#显示新的速度级
echo -ne "/33[3${cScoreValue}m/33[${y};${x}H${iLevel} "
fi
fi
echo -ne "/33[0m"
#重新显示背景方块
for ((y = 0; y < iTrayHeight; y++))
do
((yp = y + iTrayTop + 1))
((xp = iTrayLeft + 1))
((i = y * iTrayWidth))
echo -ne "/33[${yp};${xp}H"
for ((x = 0; x < iTrayWidth; x++))
do
((j = i + x))
if ((${iMap[$j]} == -1))
then
echo -ne " "
else
echo -ne "/33[1m/33[7m/33[3${iMap[$j]}m/33[4${iMap[$j]}m[]/33[0m"
fi
done
done
}
#下落一行
function BoxDown()
{
local y s
((y = boxCurY + 1)) #新的y坐标
if BoxMove $y $boxCurX #测试是否可以下落一行
then
s="`DrawCurBox 0`" #将旧的方块抹去
((boxCurY = y))
s="$s`DrawCurBox 1`" #显示新的下落后方块
echo -ne $s
else
#走到这儿, 如果不能下落了
Box2Map #将当前移动中的方块贴到背景方块中
RandomBox #产生新的方块
fi
}
#左移一列
function BoxLeft()
{
local x s
((x = boxCurX - 1))
if BoxMove $boxCurY $x
then
s=`DrawCurBox 0`
((boxCurX = x))
s=$s`DrawCurBox 1`
echo -ne $s
fi
}
#右移一列
function BoxRight()
{
local x s
((x = boxCurX + 1))
if BoxMove $boxCurY $x
then
s=`DrawCurBox 0`
((boxCurX = x))
s=$s`DrawCurBox 1`
echo -ne $s
fi
}
#下落到底
function BoxAllDown()
{
local k j i x y iDown s
iDown=$iTrayHeight
#计算一共需要下落多少行
for ((j = 0; j < 8; j += 2))
do
((i = j + 1))
((y = ${boxCur[$j]} + boxCurY))
((x = ${boxCur[$i]} + boxCurX))
for ((k = y + 1; k < iTrayHeight; k++))
do
((i = k * iTrayWidth + x))
if (( ${iMap[$i]} != -1)); then break; fi
done
((k -= y + 1))
if (( $iDown > $k )); then iDown=$k; fi
done
s=`DrawCurBox 0` #将旧的方块抹去
((boxCurY += iDown))
s=$s`DrawCurBox 1` #显示新的下落后的方块
echo -ne $s
Box2Map #将当前移动中的方块贴到背景方块中
RandomBox #产生新的方块
}
#旋转方块
function BoxRotate()
{
local iCount iTestRotate boxTest j i s
iCount=${countBox[$iBoxCurType]} #当前的方块经旋转可以产生的样式的数目
#计算旋转后的新的样式
((iTestRotate = iBoxCurRotate + 1))
if ((iTestRotate >= iCount))
then
((iTestRotate = 0))
fi
#更新到新的样式, 保存老的样式(但不显示)
for ((j = 0, i = (${offsetBox[$iBoxCurType]} + $iTestRotate) * 8; j < 8; j++, i++))
do
boxTest[$j]=${boxCur[$j]}
boxCur[$j]=${box[$i]}
done
if BoxMove $boxCurY $boxCurX #测试旋转后是否有空间放的下
then
#抹去旧的方块
for ((j = 0; j < 8; j++))
do
boxCur[$j]=${boxTest[$j]}
done
s=`DrawCurBox 0`
#画上新的方块
for ((j = 0, i = (${offsetBox[$iBoxCurType]} + $iTestRotate) * 8; j < 8; j++, i++))
do
boxCur[$j]=${box[$i]}
done
s=$s`DrawCurBox 1`
echo -ne $s
iBoxCurRotate=$iTestRotate
else
#不能旋转,还是继续使用老的样式
for ((j = 0; j < 8; j++))
do
boxCur[$j]=${boxTest[$j]}
done
fi
}
#DrawCurBox(bDraw), 绘制当前移动中的方块, bDraw为1, 画上, bDraw为0, 抹去方块。
function DrawCurBox()
{
local i j t bDraw sBox s
bDraw=$1
s=""
if (( bDraw == 0 ))
then
sBox="/40/40"
else
sBox="[]"
s=$s"/33[1m/33[7m/33[3${cBoxCur}m/33[4${cBoxCur}m"
fi
for ((j = 0; j < 8; j += 2))
do
((i = iTrayTop + 1 + ${boxCur[$j]} + boxCurY))
((t = iTrayLeft + 1 + 2 * (boxCurX + ${boxCur[$j + 1]})))
#/33[y;xH, 光标到(x, y)处
s=$s"/33[${i};${t}H${sBox}"
done
s=$s"/33[0m"
echo -n $s
}
#更新新的方块
function RandomBox()
{
local i j t
#更新当前移动的方块
iBoxCurType=${iBoxNewType}
iBoxCurRotate=${iBoxNewRotate}
cBoxCur=${cBoxNew}
for ((j = 0; j < ${#boxNew[@]}; j++))
do
boxCur[$j]=${boxNew[$j]}
done
#显示当前移动的方块
if (( ${#boxCur[@]} == 8 ))
then
#计算当前方块该从顶端哪一行"冒"出来
for ((j = 0, t = 4; j < 8; j += 2))
do
if ((${boxCur[$j]} < t)); then t=${boxCur[$j]}; fi
done
((boxCurY = -t))
for ((j = 1, i = -4, t = 20; j < 8; j += 2))
do
if ((${boxCur[$j]} > i)); then i=${boxCur[$j]}; fi
if ((${boxCur[$j]} < t)); then t=${boxCur[$j]}; fi
done
((boxCurX = (iTrayWidth - 1 - i - t) / 2))
#显示当前移动的方块
echo -ne `DrawCurBox 1`
#如果方块一出来就没处放,Game over!
if ! BoxMove $boxCurY $boxCurX
then
kill -$sigExit ${PPID}
ShowExit
fi
fi
#清除右边预显示的方块
for ((j = 0; j < 4; j++))
do
((i = iTop + 1 + j))
((t = iLeft + 2 * iTrayWidth + 7))
echo -ne "/33[${i};${t}H "
done
#随机产生新的方块
((iBoxNewType = RANDOM % ${#offsetBox[@]}))
((iBoxNewRotate = RANDOM % ${countBox[$iBoxNewType]}))
for ((j = 0, i = (${offsetBox[$iBoxNewType]} + $iBoxNewRotate) * 8; j < 8; j++, i++))
do
boxNew[$j]=${box[$i]};
done
((cBoxNew = ${colorTable[RANDOM % ${#colorTable[@]}]}))
#显示右边预显示的方块
echo -ne "/33[1m/33[7m/33[3${cBoxNew}m/33[4${cBoxNew}m"
for ((j = 0; j < 8; j += 2))
do
((i = iTop + 1 + ${boxNew[$j]}))
((t = iLeft + 2 * iTrayWidth + 7 + 2 * ${boxNew[$j + 1]}))
echo -ne "/33[${i};${t}H[]"
done
echo -ne "/33[0m"
}
#初始绘制
function InitDraw()
{
clear
RandomBox #随机产生方块,这时右边预显示窗口中有方快了
RandomBox #再随机产生方块,右边预显示窗口中的方块被更新,原先的方块将开始下落
local i t1 t2 t3
#显示边框
echo -ne "/33[1m"
echo -ne "/33[3${cBorder}m/33[4${cBorder}m"
((t2 = iLeft + 1))
((t3 = iLeft + iTrayWidth * 2 + 3))
for ((i = 0; i < iTrayHeight; i++))
do
((t1 = i + iTop + 2))
echo -ne "/33[${t1};${t2}H||"
echo -ne "/33[${t1};${t3}H||"
done
((t2 = iTop + iTrayHeight + 2))
for ((i = 0; i < iTrayWidth + 2; i++))
do
((t1 = i * 2 + iLeft + 1))
echo -ne "/33[${iTrayTop};${t1}H=="
echo -ne "/33[${t2};${t1}H=="
done
echo -ne "/33[0m"
#显示"Score"和"Level"字样
echo -ne "/33[1m"
((t1 = iLeft + iTrayWidth * 2 + 7))
((t2 = iTop + 10))
echo -ne "/33[3${cScore}m/33[${t2};${t1}HScore"
((t2 = iTop + 11))
echo -ne "/33[3${cScoreValue}m/33[${t2};${t1}H${iScore}"
((t2 = iTop + 13))
echo -ne "/33[3${cScore}m/33[${t2};${t1}HLevel"
((t2 = iTop + 14))
echo -ne "/33[3${cScoreValue}m/33[${t2};${t1}H${iLevel}"
echo -ne "/33[0m"
}
#退出时显示GameOVer!
function ShowExit()
{
local y
((y = iTrayHeight + iTrayTop + 3))
echo -e "/33[${y};0HGameOver!/33[0m"
exit
}
#游戏主程序在这儿开始.
if [[ $1 != "--show" ]]
then
bash $0 --show& #以参数--show将本程序再运行一遍
RunAsKeyReceiver $! #以上一行产生的进程的进程号作为参数
exit
else
#当发现具有参数--show时,运行显示函数
RunAsDisplayer
exit
fi
相关推荐
shell脚本案例-俄罗斯方块
该脚本需要在Linux-centos系统中运行,需要安装格式转换工具 dos2unix; 改脚本对于shell初学者来说是不错案例选择,仅用619行脚本代码,写出了有趣的小游戏; 环境:Centos7.x 1.dos2unix安装 yum -y install dos2unix 2.格式转换: dos2unix square.sh 3.执行脚本 sh square.sh 备注:该脚本本人已经验证过。如有疑问请关注微信公众号:“IT菜籽U”搜索"(八)Linux系统-shell基础--脚本执行方式",有详细的介绍;
一个简单的俄罗斯方块脚本
这是一个简单的俄罗斯方块的源码,用JavaScript编写。它是我以前在网上偶尔发现的,觉得很有价值,于是收藏起来。遗憾的是,现在已经不知其出处了。该脚本最大的特点(优点)是用数组存储所需数据,简化了编程,值得学习。虽没有注释,但看起来应该不难理解作者的思路。俄罗斯方块body {margin:0;background:black;}td {height:20;width:2
别再手动建模了!用Simscape Multibody Link一键导入SolidWorks装配体到Simulink
本文介绍了如何利用Simscape Multibody Link插件将SolidWorks装配体一键导入Simulink,实现CAD到仿真的无缝衔接。该技术显著提升机械系统设计与控制效率,特别适用于工业机器人、车辆悬架等复杂机构的动力学分析。文章详细解析了插件安装、模型导出优化及仿真验证的全流程,帮助工程师摆脱手动建模的低效工作模式。
在centos7上用shell编写一个猜拳游戏
1.首先这个centos7上写shell脚本 必须得规范,不然机器不识别 2.准备一台电脑 安装centos7系统 3.创建一个.sh脚本 touch caiquan.sh 4.编写程序 vi caiquan.sh #按 i 进入编辑模式 #将下面的代码输入 #!/bin/bash #猜拳游戏 num=$(date +%N); a=`expr $num % 100 ` b=`expr $((a+1))` #echo $b echo "随机数已获得 猜拳游戏开始" c=0 while
开关电源PCB安规设计避坑指南:从光耦开槽到变压器挡墙,这些细节决定认证成败
本文深入解析开关电源PCB安规设计中的关键细节,包括光耦开槽、变压器挡墙工艺等12个致命要点。通过实际案例和工艺参数表,揭示如何避免认证失败,特别强调爬电距离和电气间隙的精确控制,助力工程师提升产品安全性和认证通过率。
群晖存储空间损毁不等于硬盘损坏:排查与修复实战
NAS存储系统中,物理硬盘、存储池与文件系统空间构成层级结构,任何一层出现异常都可能在管理界面汇总为“存储空间损毁”。事实上,异常断电、卷未正常挂载等软性问题也会触发这一红色告警,而存储池和硬盘SMART状态往往完全正常。理解这一机制后,用户无需急于格式化重建,通过事件日志审查、SSH确认挂载状态,再借助btrfs scrub等文件系统自愈工具,即可在数据不丢失的前提下恢复存储空间正常。该方案对家庭及小型办公环境中的群晖NAS尤其适用,特别是在供电不稳或硬盘休眠频繁的场景下,能有效规避误判风险。本文完整记录
shell实现的俄罗斯方块游戏
该脚本测试可以运行起来,但是实际使用发现功能不太正常。 主要可以学习一下里面的一些语法。 #!/bin/bash # Tetris Game # 10.21.2003 xhchen<[email]xhchen@winbond.com.tw[/email]> #APP declaration APP_NAME="${0##*[\\/]}" #print exec name APP_VERSION="1.0" #颜色定义 cRed=1 cGreen=2 cYellow=3 cBlue=4
OpenClaw 开机自启配置:Windows/macOS/Linux 全平台技术指南
OpenClaw 开机自启全平台配置指南 本文提供了在Windows、macOS和Linux系统上配置OpenClaw开机自启的完整技术方案。主要内容包括: Windows配置:提供三种方法(Windows服务、任务计划程序、启动文件夹),详细说明服务脚本创建、服务注册和测试流程 macOS配置:使用LaunchAgent实现开机自启,包含配置文件创建和加载方法 Linux配置:通过systemd服务实现,包括服务单元文件创建和启用步骤 系统要求:明确硬件(64位CPU/4GB内存)和软件(各平台操作系统版
linux编写俄罗斯方块程序
效果图: #!/bin/bash # Tetris Game # 10.21.2003 xhchen<[email]xhchen@winbond.com.tw[/email]> #APP declaration APP_NAME="${0##*[\\/]}" APP_VERSION="1.0" #颜色定义 cRed=1 cGreen=2 cYellow=3 c...
运行codex时出现登录失败:failed to start login server: 以一种访问权限不允许的方式做了一个访问套接字的尝试。 (os error 10013) 解决方案
的本质是Windows 系统端口保留策略而非代码 bug。推荐使用方案一(调整动态端口范围),一劳永逸解决。如果你安装了 Docker Desktop 或启用了 WSL2,调整端口范围后记得检查相关服务是否正常运行。
【论文汇总】Diffusion Models视频生成/视频编辑/可控视频生成/跨模态视频生成
本文总结了Diffusion Models视频生成领域相关的工作,目前共收录142篇,持续更新中。
Shell脚本实现俄罗斯方块
#!/bin/bash #Pargram tetris game #History Walker 2015-07-27 version:first APP_NAME="${0##*[\\/]}" APP_VERSION="1.0" #颜色定义 iSumColor=7 #颜色总数 cRed=1 #红色 cGreen=2 cYellow=3 cBlue=4 cFuchSi
多物理场耦合仿真-主题032-电磁-流体耦合
电磁-流体耦合,即磁流体动力学(MHD, Magnetohydrodynamics),是研究导电流体在电磁场中运动规律的学科,涉及电磁场与流体力学的深度耦合。本主题系统阐述磁流体动力学的基本理论、控制方程和数值方法。首先介绍MHD的基本概念和物理基础,包括导电流体的性质、电磁场基本方程;然后详细推导MHD控制方程组,包括连续性方程、动量方程、能量方程和电磁场方程;接着讨论MHD流动的特征参数,如Hartmann数、磁雷诺数、相互作用参数等;最后通过六个典型工程案例展示MHD分析的实际应用,包括电磁泵、磁流体
shell 俄罗斯方块
源码: #!/bin/bash #颜色定义 cRed=1 cGreen=2 cYellow=3 cBlue=4 cFuchsia=5 cCyan=6 cWhite=7 colorTable=($cRed $cGreen $cYellow $cBlue $cFuchsia $cCyan $cWhite) #位置和大小 iLeft=3
TMS_WebGMaps_1.9.8.0_Full_Source
TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder
OV9734 DATASHEET,完整规格书
OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等
俄罗斯方块(未实验版)
<br />#!/bin/bash<br /># Tetris Game<br /># 10.21.2003 xhchen<[email]xhchen@winbond.com.tw[/email]><br />#APP declaration<br />APP_NAME="${0##*[///]}"<br />APP_VERSION="1.0"<br /><br />#颜色定义<br />cRed=1<br />cGreen=2<br />cYellow=3<br />cBlue=4<br />cFuchs
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
用shell写俄罗斯方块
http://bbs.chinaunix.net/thread-184858-1-1.html 把注释加进去了,可能不是很详尽, 只希望能把问题表现的清楚一点 感谢各位兄弟的捧场, 属于旧瓶装新酒了,呵呵 如果你的终端可以显示出颜色,效果会好点 运行于GNU bash, version 2.05a.0(1)-release (i686-pc-linux-gnu) --
shell脚本的俄罗斯方块 : )
#!/bin/bash # Tetris Game # 10.21.2003 xhchen<[email]xhchen@winbond .com.tw[/email]> #APP declaration APP_NAME="${0##*[\\/]}" APP_VERSION="1.0" #颜色定义 cRed=1 cGreen=2 cYellow=3 cBlue=4 cFuch...
java俄罗斯方块七中图形类_shell脚本编写的俄罗斯方块游戏源码
粘贴以下代码到一个空的Shell脚此文件中,并在Bash 中运行即可!复制代码 代码如下:#!/bin/bash# Tetris Game# 10.21.2003 xhchen#APP declarationAPP_NAME="${0##*[\\/]}"APP_VERSION="1.0"#颜色定义cRed=1cGreen=2cYellow=3cBlue=4cFuchsia=5cCyan=6cWhi...
shell脚本实现俄罗斯方块
脚本内容:#!/bin/bash# Tetris Game# 10.21.2003 xhchen< [email]xhchen@winbond.com.tw[/email]>#APP declarationAPP_NAME="${0##*[\\/]}"APP_VERSION="1.0"#颜色定义cRed=1cGreen=2cYellow=3cBlue=4c...
俄罗斯方块游戏shell编程代码
#!/bin/bash # Tetris Game # mr.Young #位置和大小 iLeft=3 iTop=2 ((iTrayLeft = iLeft + 2)) ((iTrayTop = iTop + 1)) ((iTrayWidth = 10)) ((iTrayHeight = 15)) #APP declaration APP_NAME="${0##*[\\/]}" ...
bash shell写俄罗斯方块
#!/bin/bash # Tetris Game # 10.21.2003 xhchen<[email]xhchen@winbond.com.tw[/email]> #APP declaration APP_NAME="${0##*[\\/]}" APP_VERSION="1.0" #颜色定义 cRed=1 cGreen=2 cYello...
工作的时候在终端画画、炒股、玩游戏
工作是为了啥啊,不就是生活;生活是为了啥啊,不就是为了乐子;乐子从哪来?不就是自以为是的欲望得到满足么! 如果要加个形容词,那就是优越感。 ???? 一个shell终端,黑漆漆的,总觉得缺少生机。其实除了盯下日志,也可以用来玩游戏,甚至可以用来炒股盯盘。可怜的老板还以为你在工作呢,呵呵。 在命令行版星球大战 # 挖坟会不会被打? (opens new window)中,我们看到有通过telnet看电影的这种神器。 有意思的东西还有很多。我们统一把无功效、无回报、小众的这种东西,称做艺术。 字符
java俄罗斯方块七中图形类_shell脚本编写的俄罗斯方块游戏代码
粘贴以下代码到一个空的Shell脚本文件中,并在Bash 中运行即可!#!/bin/bash# Tetris Game# 10.21.2003 xhchen#APP declarationAPP_NAME="${0##*[\\/]}"APP_VERSION="1.0"#颜色定义cRed=1cGreen=2cYellow=3cBlue=4cFuchsia=5cCyan=6cWhite=7colorT...
健康肠道菌群中的“分子注射器”:T3SS效应蛋白与宿主免疫互作图谱
肠道微生物组与宿主健康的关联是当前生命科学的热点,而细菌分泌系统在菌群与宿主细胞通讯中扮演着关键角色。其中III型分泌系统(T3SS)如同一支“分子注射器”,可将效应蛋白直接递送至宿主细胞内部。传统观点认为T3SS是病原菌的核心毒力因子,但最新研究表明,健康肠道中的共生菌同样携带并利用这一系统,向宿主免疫系统发送信号。通过构建效应蛋白-宿主相互作用组图谱,研究人员大规模鉴定了共生菌效应蛋白的宿主靶点,发现它们广泛富集于NF-κB、自噬等免疫调节通路。这一发现改变了我们对T3SS功能的认知,揭示了“分子注射器
用shell写的俄罗斯方块游戏
把这个代码挨着看一遍,就学到不少。#!/bin/bash # Tetris Game # 10.21.2003 xhchen #APP declaration APP_NAME="${0##*[\\/]}" APP_VERSION="1.0" #颜色定义 cRed=1 cGreen=2 cYellow=3 cBlue=4 cFuchsia=5 cCyan=6 cWhite=7 color
898




被折叠的 条评论
为什么被折叠?



