R语言写2048游戏
2048 是一款益智游戏,只需要用方向键让两两相同的数字碰撞就会诞生一个翻倍的数字,初始数字由 2 或者 4 构成,直到游戏界面全部被填满,游戏结束。
编程时并未查看原作者代码,不喜勿喷。
程序结构如下:

R语言代码:
#!/usr/bin/Rscript
#画背景
draw_bg <- function(){
plot(0,0,xlim=c(0,0.8),ylim=c(0,0.8),type='n',xaxs="i", yaxs="i")
for (i in c(1:4)){
for (j in c(1:4)){
points(0.1+0.2*(i-1),0.9-0.2*(j),col="gray",pch=15,cex=16)}}
}
#画数字矩阵
draw_num <- function(){
for (i in c(1:4)){
for (j in c(1:4)){
if (e$m[i,j] != 0){
text(0.1+(j-1)*0.2,0.7-(i-1)*0.2,font=2,family="Arial",label=e$m[i,j],cex=2)
}
}
}
}
#初次运行游戏,生成随机矩阵
init <- function(){
e$stage=1
mt <- matrix(c(sample(c(2,4),1),rep(0,15)),nrow=4)
e$m <- mt[sample(4),sample(4)]
draw_bg()
draw_num()
} #移除矩阵数字间的0
rm_zero <- function(){
if (e$x==0){
if (e$dir=="up"){
for (c in 1:4) e$m[,c] <- c(e$m[,c][which(e$m[,c]!=0)],rep(0,4-length(e$m[,c][which(e$m[,c]!=0)])))
}
if (e$dir=="down"){
for (c in 1:4) e$m[,c] <- c(rep(0,4-length(e$m[,c][which(e$m[,c]!=0)])),e$m[,c][which(e$m[,c]!=0)])
}
if (e$dir=="left"){
for (r in 1:4) e$m[r,] <- c(e$m[r,][which(e$m[r,]!=0)],rep(0,4-length(e$m[r,][which(e$m[r,]!=0)])))
} if (e$dir=="right"){
for (r in 1:4) e$m[r,] <- c(rep(0,4-length(e$m[r,][which(e$m[r,]!=0)])),e$m[r,][which(e$m[r,]!=0)]) }
}
else{
if (e$dir=="up"){
c <- e$x
e$m[,c] <- c(e$m[,c][which(e$m[,c]!=0)],rep(0,4-length(e$m[,c][which(e$m[,c]!=0)])))
}
if (e$dir=="down"){
c <- e$x
e$m[,c] <- c(rep(0,4-length(e$m[,c][which(e$m[,c]!=0)])),e$m[,c][which(e$m[,c]!=0)])
}
if (e$dir=="left"){
r <- e$x
e$m[r,] <- c(e$m[r,][which(e$m[r,]!=0)],rep(0,4-length(e$m[r,][which(e$m[r,]!=0)])))
} if (e$dir=="right"){
r <- e$x
e$m[r,] <- c(rep(0,4-length(e$m[r,][which(e$m[r,]!=0)])),e$m[r,][which(e$m[r,]!=0)]) } }
} #在空白处添加随机数字
new_mt <- function(){
e$m[sample(which(e$m==0),1)] <- sample(c(2,4),1)
} #检查是否游戏失败
fail <- function(){
if (length(e$m[which(e$m==0)])==0){
e$x=0
for (r in 1:3){
for (c in 1:3){
if (e$m[r,c] == e$m[r,c+1] | e$m[r,c] == e$m[r+1,c]){
e$x=1
}
}
}
if (e$x==0){
stage2() }
}
}
#游戏中
stage1 <- function(){
e$stage <- 1
e$x <- 0
rm_zero()
if (e$dir=="left"){
i=1
while (i<=4){
if (e$m[i,1] != 0 & e$m[i,1]==e$m[i,2] & e$m[i,1]==e$m[i,3] & e$m[i,1]==e$m[i,4]){
e$m[i,]=rep(c(2*e$m[i,1],0),each=2)
e$x=1
}
else if (e$m[i,2]!=0 & e$m[i,3] != 0 & e$m[i,2]==e$m[i,1] & e$m[i,3]==e$m[i,4]){
e$m[i,]=c(2*e$m[i,1],0,2*e$m[i,3],0)
e$x=1
}
else if (e$m[i,2] != 0 & e$m[i,2]==e$m[i,1]){
e$m[i,]=c(2*e$m[i,1],0,e$m[i,3],e$m[i,4])
e$x=1
}
else if (e$m[i,3] != 0 & e$m[i,3]==e$m[i,4]){
e$m[i,]=c(e$m[i,1],e$m[i,2],2*e$m[i,3],0)
e$x=1
}
else if (e$m[i,2] != 0 & e$m[i,2]==e$m[i,3]){
e$m[i,]=c(e$m[i,1],2*e$m[i,2],0,e$m[i,4])
e$x=1
}
i=i+1
}
rm_zero()
new_mt()
draw_bg()
draw_num()
fail()
}
if (e$dir=="right"){
i=1
while (i<=4){
if (e$m[i,1] != 0 & e$m[i,1]==e$m[i,2] & e$m[i,1]==e$m[i,3] & e$m[i,1]==e$m[i,4]){
e$m[i,]=rep(c(0,2*e$m[i,1]),each=2)
e$x=1
}
else if (e$m[i,2] != 0 & e$m[i,3] != 0 & e$m[i,2]==e$m[i,1] & e$m[i,3]==e$m[i,4]){
e$m[i,]=c(0,2*e$m[i,1],0,2*e$m[i,3])
e$x=1
}
else if (e$m[i,2] != 0 & e$m[i,2]==e$m[i,1]){
e$m[i,]=c(0,2*e$m[i,1],e$m[i,3],e$m[i,4])
e$x=1
}
else if (e$m[i,3] != 0 & e$m[i,3]==e$m[i,4]){
e$m[i,]=c(e$m[i,1],e$m[i,2],0,2*e$m[i,3])
e$x=1
}
else if (e$m[i,2] != 0 & e$m[i,2]==e$m[i,3]){
e$m[i,]=c(e$m[i,1],0,2*e$m[i,2],e$m[i,4])
e$x=1
}
i=i+1
}
rm_zero()
new_mt()
draw_bg()
draw_num()
fail()
} if (e$dir=="up"){
j=1
while (j<=4){
if (e$m[1,j] != 0 & e$m[1,j]==e$m[2,j] & e$m[1,j]==e$m[3,j] & e$m[1,j]==e$m[4,j]){
e$m[,j]=rep(c(2*e$m[1,j],0),each=2)
e$x=1
}
else if (e$m[2,j] != 0 & e$m[3,j] != 0 & e$m[2,j]==e$m[1,j] & e$m[3,j]==e$m[4,j]){
e$m[,j]=c(2*e$m[1,j],0,2*e$m[3,j],0)
e$x=1
}
else if (e$m[2,j] != 0 & e$m[2,j]==e$m[1,j]){
e$m[,j]=c(2*e$m[1,j],0,e$m[3,j],e$m[4,j])
e$x=1
}
else if (e$m[3,j] != 0 & e$m[3,j]==e$m[4,j]){
e$m[,j]=c(e$m[1,j],e$m[2,j],2*e$m[3,j],0)
e$x=1
}
else if (e$m[2,j] != 0 & e$m[2,j]==e$m[3,j]){
e$m[,j]=c(e$m[1,j],2*e$m[2,j],0,e$m[4,j])
e$x=1
}
j=j+1
}
rm_zero()
new_mt()
draw_bg()
draw_num()
fail()
}
if (e$dir=="down"){
j=1
while (j<=4){
if (e$m[1,j] != 0 & e$m[1,j]==e$m[2,j] & e$m[1,j]==e$m[3,j] & e$m[1,j]==e$m[4,j]){
e$m[,j]=rep(c(0,2*e$m[1,j]),each=2)
e$x=1
}
else if (e$m[2,j] != 0 & e$m[3,j] != 0 & e$m[2,j]==e$m[1,j] & e$m[3,j]==e$m[4,j]){
e$m[,j]=c(0,2*e$m[1,j],0,2*e$m[3,j])
e$x=1
}
else if (e$m[2,j] != 0 & e$m[2,j]==e$m[1,j]){
e$m[,j]=c(0,2*e$m[1,j],e$m[3,j],e$m[4,j])
e$x=1
}
else if (e$m[3,j] != 0 & e$m[3,j]==e$m[4,j]){
e$m[,j]=c(e$m[1,j],e$m[2,j],0,2*e$m[3,j])
e$x=1
}
else if (e$m[2,j] != 0 & e$m[2,j]==e$m[3,j]){
e$m[,j]=c(e$m[1,j],0,2*e$m[2,j],e$m[4,j])
e$x=1
}
j=j+1
} rm_zero()
new_mt()
draw_bg()
draw_num()
fail()
}
}
stage2<-function(){
e$stage<-2
plot(0,0,xlim=c(0,1),ylim=c(0,1),type='n',xaxs="i", yaxs="i")
text(0.5,0.7,label="Game Over",cex=2)
text(0.5,0.4,label="Space to restart, q to quit.",cex=2,col=4)
text(0.2,0.05,label="Author:YwLiao",cex=1)
} # 开机画图
stage0<-function(){
e$stage<-0
plot(0,0,xlim=c(0,1),ylim=c(0,1),type='n',xaxs="i", yaxs="i")
text(0.5,0.7,label="",cex=2)
text(0.5,0.4,label="Any keyboard to start",cex=2,col=4)
text(0.5,0.3,label="Up,Down,Left,Rigth to control direction",cex=2,col=2)
text(0.2,0.05,label="Author:YwLiao",cex=1)
}
#键盘事件
keydown<-function(K){
print(paste("keydown:",K,",stage:",e$stage));
if(e$stage==0){
#开机画面
init()
return(NULL)
}
if(e$stage==2){ #结束画面
if(K=="q") q()
else if(K==' ') stage0()
return(NULL)
}
if(e$stage==1){ #游戏中
if(K == "q") {
stage2()
}else {
if(tolower(K) %in% c("up","down","left","right")){
e$dir<-tolower(K)
stage1()
}
}
}
return(NULL)
} #开始运行游戏
run<-function(){
e<<-new.env()
#X11(type="Xlib") #linux系统需添加此行代码,不过字体受到限制,没有windows下大
stage0()
getGraphicsEvent(prompt="",onKeybd=keydown)
} run()
游戏画面

参考资料
张丹.R的极客理想:http://www.kuqin.com/shuoit/20140704/340987.html
R语言写2048游戏的更多相关文章
- C语言写猜拳游戏中遇到的函数循环小问题
各位可能在初学C语言的时候都有写过猜拳游戏.但在写猜拳的函数时,避免不了会使用循环. 当函数被套在一个循环中的时候,你的计分变量可能就会被重置为函数体里的初始值.那么怎么解决这个问题? 其实很简单,你 ...
- js写2048游戏代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- R语言写简单线性回归
library(MASS) library(ISLR) lm.fit <- lm(medv~lstat,data=Boston) attach(Boston) lm.fit = lm(medv~ ...
- R语言中函数调试
有时候会用R语言写一下简单的脚本处理函数,加入需要调试的话可以按照下面的步骤进行: fun <- function(x , y){ x + y x - y x * y x / y } debug ...
- 决策树ID3原理及R语言python代码实现(西瓜书)
决策树ID3原理及R语言python代码实现(西瓜书) 摘要: 决策树是机器学习中一种非常常见的分类与回归方法,可以认为是if-else结构的规则.分类决策树是由节点和有向边组成的树形结构,节点表示特 ...
- C语言写的2048小游戏
基于"基于C_语言的2048算法设计_颜冠鹏.pdf" 这一篇文献提供的思路 在中国知网上能找到 就不贴具体内容了 [摘 要] 针对2048的游戏规则,分析了该游戏的算法特点,对其 ...
- 一个用 C 语言写的迷你版 2048 游戏,仅仅有 500个字符
Jay Chan 用 C 语言写的一个迷你版 2048 游戏,仅仅有 487 个字符. 来围观吧 M[16],X=16,W,k;main(){T(system("stty cbreak&qu ...
- C++学习(三十九)(C语言部分)之 游戏项目(2048游戏)
/***************************项目 2048**********************c语言编写 图形库制作时间:2019.04.03 准备工具: vs2013 图形库 i ...
- C语言实现2048小游戏
目录 2048 一.设计思路 1.游戏规则 2.思路 二.代码实现 1.存储结构 2.初始化游戏数据 3.向左合并 4.其他方向合并 5.产生新的方块 6.源代码 7.实例演示 三.问题 2048 一 ...
随机推荐
- mfc---CFileFind
使用CFileFind实现在指定路径下,查找指定类型文件 CFileFind.FindFile(FilePath + "\\*.*"),成功返回true,否则返回false CFi ...
- cocos2dx 魔塔项目总结(一)
<魔塔天城>发布已经有半年的时间了,一直想找时间来总结一下这个项目,但总是一拖再拖.如果再这么拖下去,就永远都不会有时间来写这个总结了,时间总是挤出来的. 魔塔天城使用的cocos2dx ...
- 1432: [ZJOI2009]Function
1432: [ZJOI2009]Function Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 710 Solved: 528[Submit][Stat ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(十)SVN搭建
日常啰嗦 前面一篇文章讲了一下版本控制,但其实这一篇并没有打算讲细节的,感觉应该自己去动手弄一下,后来考虑了一下,版本控制真的挺重要的,如果自己实在搭建不好反而不去使用的话,真的有点可惜,当然这些话是 ...
- 深入理解java String 及intern
一.字符串问题 字符串在我们平时的编码工作中其实用的非常多,并且用起来也比较简单,所以很少有人对其做特别深入的研究.倒是面试或者笔试的时候,往往会涉及比较深入和难度大一点的问题.我在招聘的时候也偶尔会 ...
- 一段获取app性能指标的py脚本
#coding:utf-8 import os import timeimport datetimeimport subprocess ActivityManager = 'homepage.Main ...
- C — 对C语言的认识
有趣的C语言代码 看一下这段代码输出的是什么 #include <stdio.h> int main() { ; printf("%d\n", printf(" ...
- MySQL基准测试(benchmark)
基准测试是唯一方便有效的.可以学习系统在给定的工作负载下会发生什么的方法.基准测试可以观察系统在不同压力下的行为,评估系统的容量,掌握哪些是重要的变化,或者观察系统如何处理不同的数据. 验证基于系统的 ...
- 在WPF应用程序中使用Font Awesome图标
Font Awesome 在网站开发中,经常用到.今天介绍如何在WPF应用程序中使用Font Awesome . 如果是自定义的图标字体,使用方法相同. 下载图标字体 在官方网站或github上下载资 ...
- JavaScript高级程序设计(学习)
文档模式是:混杂模式和标准模式. 若在文档开始处没有文档类型声明,则浏览器就会开启混杂模式.这种模式在不同的浏览器下行为差异非常大,如果不使用某些hack技术,跨浏览器的行为根本就没有一致性可言. 局 ...