代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
两人联机版俄罗斯方块
作者:
黑白2013
/ 发布于
2014/4/8
/
529
public class TetrisOption { private static final int ROW=24,LINE=12; private static final int UP=1,DOWN=2,LEFT=3,RIGHT=4,TRANSFORMATION=5,QUICK_DOWN=6; /*0表示不显示,非0显示*/ private int [][] nextmovegroup=new int[ROW][LINE]; private int [][] movegroup=new int[ROW][LINE]; private int [][] regulargroup=new int[ROW][LINE]; //得分 private int score=0; //模式种类 private int modelnumber=19; //当前方块型号:model private int model,nextmodel; //构造函数 public TetrisOption(){ for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ nextmovegroup[i][j]=movegroup[i][j]=regulargroup[i][j]=0; } } score=0; makeNextMoveGroup(); makeMovegroup(); } public void init(){ for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ nextmovegroup[i][j]=movegroup[i][j]=regulargroup[i][j]=0; } } makeNextMoveGroup(); makeMovegroup(); } public int getScore(){ return score; } //***************** //外部接口,接受操作类型,普通下DOWN=2,左LEFT=3,右RIGHT=4,变形TRANSFORMATION=5,快速下QUICK_DOWN=6 public int Run(int option,int [][]mg,int [][]rg,int [][]nmg){ updateMovegroup(option); //print(); for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ mg[i][j]=movegroup[i][j]; rg[i][j]=regulargroup[i][j]; nmg[i][j]=nextmovegroup[i][j]; } } // print(); return 0; } //操作函数区,行列为坐标 public boolean moveUp(){ return true; } //DOWN******** public boolean moveDown(){ //print(); for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ //如果向下的一步将会碰到下边界或者碰到固定组,则刷新固定组,产生下一个方块 if((movegroup[i][j]==1&®ulargroup[i+1][j]==1)||movegroup[ROW-1][j]==1){ updateRegulargroup(); makeMovegroup(); return false; } } } for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ if(movegroup[i][j]==1){//获取定点坐标 // cleanrMoveGroup(); makePositon(model,i+1,j,'c');//实施下移 return true; } } } return true; } //LEFT******** public boolean moveLeft(){ int mark1=0; for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ //如果向左的一步将会碰到左边界或者碰到固定组则取消操作 if(movegroup[j][0]==1||movegroup[i][j]==1&®ulargroup[i][j-1]==1){ mark1=1; } } } if(mark1==1)return false; // System.out.println("可以左移!"); for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ if(movegroup[i][j]==1){//获取定点坐标 //cleanrMoveGroup(); makePositon(model,i,j-1,'c');//实施左移 return true; } } } return false; } //RIGHT******** public boolean moveRight(){ int mark1=0; for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ //如果向左的一步将会碰到右边界或者碰到固定组则取消操作 if(movegroup[j][LINE-1]==1||movegroup[i][j]==1&®ulargroup[i][j+1]==1){ mark1=1; } } } if(mark1==1)return false; // System.out.println("可以右移!"); for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ if(movegroup[i][j]==1){//获取定点坐标 //cleanrMoveGroup(); makePositon(model,i,j+1,'c');//实施右移 return true; } } } return false; } //TRANSFORMATION********sig=0顺时针旋转,sig=1,逆时针旋转 public boolean moveTransformation(){ int mark=1; int x=0,y=5; for(int i=0;i<ROW&&mark==1;i++){ for(int j=0;j<LINE;j++){ if(movegroup[i][j]==1){//获取定点坐标 x=i;y=j;mark=0; break; } } } //////////////判断能否旋转////不能则返回假,表示取消操作////// switch(model){ case 1:break; case 2:if(x-1<0||x+2>ROW-1)return false;break; case 3:if(y-1<0||y+2>ROW-1)return false;break; case 4:if(y+2>LINE-1)return false;break; case 5:if(x+2>ROW-1)return false;break; case 6:if(y-1<0)return false;break; case 7:if(x+2>ROW-1)return false;break; case 8:if(x-1<0)return false;break; case 9:if(y+1>LINE-1)return false;break; case 10:if(x+2>ROW-1)return false;break; case 11:if(y-1<0)return false;break; case 12:if(x-1<0)return false;break; case 13:if(y+1>LINE-1)return false;break; case 14:if(x-1<0)return false;break; case 15:if(y-1<0)return false;break; case 16:if(x+2>ROW-1)return false;break; case 17:if(y-1<0)return false;break; case 18:if(x-1<0)return false;break; case 19:if(y+1>LINE-1)return false;break; default:break; } ////////////////////////// try { switch (model) { case 1: case 10: case 5: case 16: case 19: x = x; y = y; break; case 2: case 12: case 18: x = x - 1; y = y + 1; break; case 3: case 9: case 17: case 13: x = x + 1; y = y - 1; break; case 4: case 15: x = x + 1; y = y; break; case 11: x = x; y = y - 1; break; case 8: x = x - 1; y = y + 2; break; case 7: x = x - 1; y = y - 1; break; case 14: x = x - 1; y = y; break; case 6: x = x; y = y + 1; break; default: break; } model = getNextModel(model); // 如果不会碰到边界,再判断是否会碰到固定组 for (int i = 0; i < ROW; i++) { for (int j = 0; j < LINE; j++) { if (movegroup[i][j] == 1 && regulargroup[i][j] == 1) {//会碰到, // 则还原 return false; } } } // 实施变换 //cleanrMoveGroup(); return makePositon(model, x, y,'c'); } catch (Exception e) {// 如果发生异常说明不能旋转 throw new RuntimeException(e); } } //QUICK_DOWN******** public boolean moveQuickDown(){ while(moveDown()){ } return true; } /*函数:产生一个新的移动组*************** * */ public void makeMovegroup(){ copy(); model=nextmodel; //产生下一个 nextmodel=(int)(Math.random()*100)%modelnumber+1; makePositon(nextmodel,0,5,'n'); } /*函数:在游戏开始时的方块组产生*/ public void makeNextMoveGroup(){ nextmodel=(int)(Math.random()*100)%modelnumber+1; makePositon(nextmodel,0,5,'n'); // print(); } /*复制*/ public void copy(){ for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ movegroup[i][j]=nextmovegroup[i][j]; } } } /*加分分析函数,规则满一层+10,满二层+30,满三层+50,满四层+100,返回有无满层 * */ public boolean countScore(){ int n=0; int sig=0; for(int i=0;i<ROW;i++){ sig=0; for(int j=0;j<LINE;j++){ if(regulargroup[i][j]==1){ sig++; } } if(sig==LINE)n++; } if(n==0)return false; if(n==1)score+=100; if(n==2)score+=300; if(n==3)score+=500; if(n==4)score+=1000; return true; } /*固定组更新函数: * 1.将移动组添加到固定组 * 2.判断满层,有->计算得分,消除满城,无->判断空间满->满结束; * 消除满层 * */ public void updateRegulargroup(){ //1. for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ if(movegroup[i][j]==1){ regulargroup[i][j]=1; } } } //2. int mark; if (countScore()) { int [][]tem=new int[ROW][LINE]; for (int i = 0; i <ROW; i++) { for (int j = 0; j < LINE; j++) { tem[i][j]=0; } } int m=ROW-1; // System.out.println("需要消除"); for (int i = ROW - 1; i >= 0; i--) { mark = 1; for (int j = 0; j < LINE; j++) { if (regulargroup[i][j] == 0) { mark = 0; break; } } if (mark == 0) { for (int j = 0; j < LINE; j++) { tem[m][j]=regulargroup[i][j]; } m--; } } for (int i = 0; i <ROW; i++) { for (int j = 0; j < LINE; j++) { regulargroup[i][j]=tem[i][j]; } } } if(isFull())gameOver(); } /*移动组更新,需要参数:变换操作,若果即将进行的操作会越过边界,则操作取消,返回假 * */ public boolean updateMovegroup(int option){ try{ if(option==UP) return moveUp(); else if(option==DOWN) return moveDown(); else if(option==QUICK_DOWN) return moveQuickDown(); else if(option==LEFT) return moveLeft(); else if(option==TRANSFORMATION) return moveTransformation(); else if(option==RIGHT) return moveRight(); }catch(Exception e){ return false; } return false; } /*移动空间满判断(最高一层不能有方块),满返回true,否则返回false * 该函数由碰撞检测函数调用 * */ public boolean isFull(){ for(int i=0;i<LINE;i++){ if(regulargroup[0][i]==1){ return true; } } return false; } /*游戏结束函数 * */ public void gameOver(){ // System.out.println("GAME OVER !"); // System.exit(0); } /*方块位置规则 * */ public boolean makePositon(int model,int x,int y,char next){ int[] row=new int[4]; int[] line=new int[4]; for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ if(next=='n')nextmovegroup[i][j]=0; else if(next=='c')movegroup[i][j]=0; } } /*nextmovegroup已经清零*/ switch(model){ case 1: row[0]=x; line[0]=y; row[1]=x; line[1]=y+1; row[2]=x+1; line[2]=y; row[3]=x+1; line[3]=y+1; break; case 2: row[0]=x; line[0]=y; row[1]=x; line[1]=y+1; row[2]=x; line[2]=y+2; row[3]=x; line[3]=y+3; break; case 3: row[0]=x; line[0]=y; row[1]=x+1; line[1]=y; row[2]=x+2; line[2]=y; row[3]=x+3; line[3]=y; break; case 4: row[0]=x; line[0]=y; row[1]=x+1; line[1]=y; row[2]=x+2; line[2]=y; row[3]=x+2; line[3]=y+1; break; case 5: row[0]=x; line[0]=y; row[1]=x; line[1]=y+1; row[2]=x; line[2]=y+2; row[3]=x+1; line[3]=y; break; case 6: row[0]=x; line[0]=y; row[1]=x; line[1]=y+1; row[2]=x+1; line[2]=y+1; row[3]=x+2; line[3]=y+1; break; case 7: row[0]=x; line[0]=y; row[1]=x+1; line[1]=y-2; row[2]=x+1; line[2]=y-1; row[3]=x+1; line[3]=y; break; case 8: row[0]=x; line[0]=y; row[1]=x; line[1]=y+1; row[2]=x; line[2]=y+2; row[3]=x+1; line[3]=y+2; break; case 9: row[0]=x; line[0]=y; row[1]=x+1; line[1]=y; row[2]=x+2; line[2]=y; row[3]=x+2; line[3]=y-1; break; case 10: row[0]=x; line[0]=y; row[1]=x+1; line[1]=y; row[2]=x+1; line[2]=y+1; row[3]=x+1; line[3]=y+2; break; case 11: row[0]=x; line[0]=y; row[1]=x; line[1]=y+1; row[2]=x+1; line[2]=y; row[3]=x+2; line[3]=y; break; case 12: row[0]=x; line[0]=y; row[1]=x; line[1]=y+1; row[2]=x+1; line[2]=y+1; row[3]=x+1; line[3]=y+2; break; case 13: row[0]=x; line[0]=y; row[1]=x+1; line[1]=y-1; row[2]=x+1; line[2]=y; row[3]=x+2; line[3]=y-1; break; case 14: row[0]=x; line[0]=y; row[1]=x; line[1]=y+1; row[2]=x+1; line[2]=y; row[3]=x+1; line[3]=y-1; break; case 15: row[0]=x; line[0]=y; row[1]=x+1; line[1]=y; row[2]=x+1; line[2]=y+1; row[3]=x+2; line[3]=y+1; break; case 16: row[0]=x; line[0]=y; row[1]=x+1; line[1]=y-1; row[2]=x+1; line[2]=y; row[3]=x+1; line[3]=y+1; break; case 17: row[0]=x; line[0]=y; row[1]=x+1; line[1]=y; row[2]=x+1; line[2]=y+1; row[3]=x+2; line[3]=y; break; case 18: row[0]=x; line[0]=y; row[1]=x; line[1]=y+1; row[2]=x; line[2]=y+2; row[3]=x+1; line[3]=y+1; break; case 19: row[0]=x; line[0]=y; row[1]=x+1; line[1]=y-1; row[2]=x+1; line[2]=y; row[3]=x+2; line[3]=y; break; default :break; } try{ for(int i=0;i<4;i++){ if(next=='n')nextmovegroup[row[i]][line[i]]=1; else movegroup[row[i]][line[i]]=1; } return true; }catch(Exception e){ throw new RuntimeException(e); } } /*获取下一个模型*/ public int getNextModel(int model){ if(model>=1&&model<=modelnumber){ if(model==1)return 1; if(model==2)return 3; if(model==3)return 2; if(model==4)return 5; if(model==5)return 6; if(model==6)return 7; if(model==7)return 4; if(model==8)return 9; if(model==9)return 10; if(model==10)return 11; if(model==11)return 8; if(model==12)return 13; if(model==13)return 12; if(model==14)return 15; if(model==15)return 14; if(model==16)return 17; if(model==17)return 18; if(model==18)return 19; if(model==19)return 16; } return 1; } /*清空移动组*/ public void cleanrMoveGroup(){ for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ movegroup[i][j]=0; } } } public void print(){ System.out.println("==========================="); for(int i=0;i<ROW;i++){ for(int j=0;j<LINE;j++){ System.out.print(movegroup[i][j]+" "); } System.out.println(); } System.out.println("==========================="); } }
试试其它关键字
俄罗斯方块
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
黑白2013
贡献的其它代码
(
1
)
.
两人联机版俄罗斯方块
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3