java 魔方小程序。只拧红、蓝、白三色。大概8秒内复原。拧1亿次左右





菜鸟娱乐的。

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;


public class Cube {
		long operationTimes=0;
		Calendar c = Calendar.getInstance();
		long t1 = System.currentTimeMillis();
		long t2,tmpTime;
		int intervalTime = 10000;//log展示间隔时间
		String startTime = new SimpleDateFormat("yyyy.MM.dd_HH:mm:ss z").format(new Date());
		Random rdm = new Random();
		String tmp1[] = {"tmp","tmp","tmp"};
		String tmp2[] = {"t","t","t"};
		String w[][] = {{"white","white","white"},{"white","white","white"},{"white","white","white"},{"white","white","white"}};
		String y[][] = {{"yellow","yellow","yellow"},{"yellow","yellow","yellow"},{"yellow","yellow","yellow"},{"yellow","yellow","yellow"}};
		String b[][] = {{"blue","blue","blue"},{"blue","blue","blue"},{"blue","blue","blue"},{"blue","blue","blue"}};
		String g[][] = {{"green","green","green"},{"green","green","green"},{"green","green","green"},{"green","green","green"}};
		String r[][] = {{"red","red","red"},{"red","red","red"},{"red","red","red"},{"red","red","red"}};
		String o[][] = {{"orange","orange","orange"},{"orange","orange","orange"},{"orange","orange","orange"},{"orange","orange","orange"}};
		String cube[][][] = {w,y,b,g,r,o};
		public boolean isInterval(){
			t2 = System.currentTimeMillis();
			if(t2-tmpTime >intervalTime){
				tmpTime =t2;
				return true;
			}else return false;
		}
		public void  showCostTime(){
			t2  = System.currentTimeMillis();
			c.setTimeInMillis(t2-t1);
//			String endTime = new SimpleDateFormat("yyyy.MM.dd_HH:mm:ss z").format(c.getTime());
			String endTime = new SimpleDateFormat("yyyy.MM.dd_HH:mm:ss z").format(new Date());
			System.out.println("从"+ startTime  + "开始,到"+ endTime + "结束,共消耗"+ c.get((c.HOUR_OF_DAY)-9)   + "小时"+c.get(c.MINUTE)   +"分"+ c.get(c.SECOND)  +"秒");
		}

		public void log(long i,String action, int k){
			if(isInterval()){
			System.out.println("第"+i+"次随机操作,动作是"+action+",执行"+(k+1)+"次");
			}
		}
		public void showCube(){
			for(int i=0;i<cube.length;i++){
				for(int j=0;j<cube[i].length;j++){
					for(int k=0;k<cube[i][j].length;k++){
						System.out.print(cube[i][j][k]);
						System.out.print(",");
					}
					System.out.println();
				}
				System.out.println();
			}
		}
	public void wF(int n){
		for(int j=0;j<=n;j++){
			tmp1 = w[0];
			tmp2 = r[0];
			for(int i=1;i<w.length;i++){
				w[i-1] = w[i];
			}
			w[w.length-1] = tmp1;
			r[0] = b[1];
			b[1] = o[2];
			o[2] = g[3];
			g[3] = tmp2;
		}
	}
	public void wB(int n){
		if(n==2){
			n = 0;
		}else if(n == 1){
			n =1;
		}else if(n == 0){
			n =2;
		}

		for(int j=0;j<n;j++){
			tmp1 = w[0];
			tmp2 = r[0];
			for(int i=1;i<w.length;i++){
				w[i-1] = w[i];
			}
			w[w.length-1] = tmp1;
			r[0] = b[1];
			b[1] = o[2];
			o[2] = g[3];
			g[3] = tmp2;
		}
	}
	//其他几个颜色转动的方法还没有补充。有兴趣可以自己补充。
	public void yF(int n){
	}
	public void yB(int n){
	}
	public void bF(int n){
		tmp1 = b[1];
		tmp2 = w[1];
		b[1] = b[2];
		b[2] = b[3];
		b[3] = b[0];
		b[0] = tmp1;
		
		w[1] = r[3];
		r[3] = y[1];
		y[1] = o[3];
		o[3] = tmp2;
	}

	public void bB(int n){
		if(n==0){
			n = 2;
		}else if(n == 1){
			n = 1;
		}else if(n == 2){
			n = 0;
		}

		for(int j=0;j<=n;j++){
			tmp1 = b[1];
			tmp2 = w[1];
			b[1] = b[2];
			b[2] = b[3];
			b[3] = b[0];
			b[0] = tmp1;
			
			w[1] = r[3];
			r[3] = y[1];
			y[1] = o[3];
			o[3] = tmp2;
			
		}
	}

	public void gF(int n){
		
	}

	public void gB(int n){
		
	}
	public void rF(int n){
		for(int j=0;j<=n;j++){
			tmp1 = r[0];
			tmp2 = w[0];
			r[0] = r[3];
			r[3] = r[2];
			r[2] = r[1];
			r[1] = r[0];
			r[0] = tmp1;
		}
		w[0] = g[2];
		g[2] = y[0];
		y[0] = b[2];
		b[2] = w[0];
		w[0] = tmp2;
	}
	public void rB(int n){
		if(n==0){
			n = 2;
		}else if(n == 1){
			n = 1;
		}else if(n == 2){
			n = 0;
		}
		for(int j=0;j<=n;j++){
			tmp1 = r[0];
			tmp2 = w[0];
			r[0] = r[3];
			r[3] = r[2];
			r[2] = r[1];
			r[1] = r[0];
			r[0] = tmp1;
		}
		w[0] = g[2];
		g[2] = y[0];
		y[0] = b[2];
		b[2] = w[0];
		w[0] = tmp2;
	}

	public void oF(int n){
		
	}

	public void oB(int n){
		
	}

	public boolean isSixFaceRecover(){
			for(int i=0;i<cube.length;i++){
				for(int j=0;j<cube[i].length;j++){
					for(int k=0;k<cube[i][j].length;k++){
						
						try{
							if(cube[i][j][k]!=cube[i][0][0]){
//								if(isInterval()){
//									System.out.println(cube[i][j][k]+"->"+cube[i][0][0]);
//									System.out.println("cube[i][j][k]:"+i+","+j+","+k+"与第一个颜色不一致");
//								}
								return false;
							}
						}catch(ArrayIndexOutOfBoundsException e){
//							System.out.println("cube[i][j][k]:"+i+","+j+","+k);
							e.printStackTrace();
								
						}
					}
				}
			}
			return true;
	}
	public void rangePlay(){
		while(!isSixFaceRecover()){
			int rMethod = rdm.nextInt(12);
			int rTurn = rdm.nextInt(3);
			if(rMethod==0){
				this.wF(rTurn);
				operationTimes++;
				log(operationTimes,"wF",rTurn);
			}else if(rMethod==1){
				this.wB(rTurn);
				operationTimes++;
				log(operationTimes,"wB",rTurn);
			}else if(rMethod==2){
				this.yF(rTurn);
				operationTimes++;
				log(operationTimes,"yF",rTurn);
			}else if(rMethod==3){
				this.yB(rTurn);
				operationTimes++;
				log(operationTimes,"yB",rTurn);
			}else if(rMethod==4){
				this.bF(rTurn);
				operationTimes++;
				log(operationTimes,"bF",rTurn);
			}else if(rMethod==5){
				this.bB(rTurn);
				operationTimes++;
				log(operationTimes,"bB",rTurn);
			}else if(rMethod==6){
				this.gF(rTurn);
				operationTimes++;
				log(operationTimes,"gF",rTurn);
			}else if(rMethod==7){
				this.gB(rTurn);
				operationTimes++;
				log(operationTimes,"gB",rTurn);
			}
			else if(rMethod==8){
				this.rF(rTurn);
				operationTimes++;
				log(operationTimes,"rF",rTurn);
			}else if(rMethod==9){
				this.rB(rTurn);
				operationTimes++;
				log(operationTimes,"rB",rTurn);
			}else if(rMethod==10){
				this.oF(rTurn);
				operationTimes++;
				log(operationTimes,"oF",rTurn);
			}else if(rMethod==11){
				this.oB(rTurn);
				operationTimes++;
				log(operationTimes,"oB",rTurn);
			}
		}
		showCube();
		System.out.println("cube经过"+operationTimes+"次随机操作后复原");
	}
	public void rangePlay(int i){
		for(int m=0;m<i;m++){
			int rMethod = rdm.nextInt(12);
			int rTurn = rdm.nextInt(3);
			if(rMethod==0){
				this.wF(rTurn);
				operationTimes++;
				log(operationTimes,"wF",rTurn);
			}else if(rMethod==1){
				this.wB(rTurn);
				operationTimes++;
				log(operationTimes,"wB",rTurn);
			}else if(rMethod==2){
				this.yF(rTurn);
				operationTimes++;
				log(operationTimes,"yF",rTurn);
			}else if(rMethod==3){
				this.yB(rTurn);
				operationTimes++;
				log(operationTimes,"yB",rTurn);
			}else if(rMethod==4){
				this.bF(rTurn);
				operationTimes++;
				log(operationTimes,"bF",rTurn);
			}else if(rMethod==5){
				this.bB(rTurn);
				operationTimes++;
				log(operationTimes,"bB",rTurn);
			}else if(rMethod==6){
				this.gF(rTurn);
				operationTimes++;
				log(operationTimes,"gF",rTurn);
			}else if(rMethod==7){
				this.gB(rTurn);
				operationTimes++;
				log(operationTimes,"gB",rTurn);
			}
			else if(rMethod==8){
				this.rF(rTurn);
				operationTimes++;
				log(operationTimes,"rF",rTurn);
			}else if(rMethod==9){
				this.rB(rTurn);
				operationTimes++;
				log(operationTimes,"rB",rTurn);
			}else if(rMethod==10){
				this.oF(rTurn);
				operationTimes++;
				log(operationTimes,"oF",rTurn);
			}else if(rMethod==11){
				this.oB(rTurn);
				operationTimes++;
				log(operationTimes,"oB",rTurn);
			}
		}
		System.out.println("cube已经被操作"+i+"次");
	}
	public static void main(String[] args){
		Cube c = new Cube();
		c.rangePlay(10);
		c.showCube();
		c.rangePlay();
		c.showCostTime();
		
	}
}

更多详细信息请查看 java教程网 http://www.itchm.com/forum-59-1.html
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(重要!!!其中使用了easyx图形库,easyx的外部文件需自己另外下载并配置好,本资源未包括该扩展库的文件,即把几个文件放进VC6.0的Lib文件夹里就好了) 作品名称:魔方还原 作品功能:进行三阶魔方还原。 界面介绍:打开程序首先进入一个有着发光魔方背景的欢迎界面,有“Rubik”和“Help”两个按钮。按“Help”按钮进入帮助界面,显示用键盘操作魔方方法。按“Rubik”进入魔方还原的主界面,左边是一个可以看到三面的立体魔方,右边是与立体魔方对应的六面展开图。魔方初始状态是未被打乱的状态。另外搭配主界面的还有一个控制台窗口,用于显示操作步骤。 操作说明:从键盘直接输入字符控制,各字符含义如下。 0: 按数字零退出程序。 *: 按星号键执行逆序法还原魔方。 +: 按加号键执行魔方传统的“层先法”进行还原。 R: 顺时针90°转右面。 r: 逆时针90°转右面。 U: 顺时针90°转顶面。 u: 逆时针90°转顶面。 L: 顺时针90°转左面。 l: 逆时针90°转左面。 D: 顺时针90°转底面。 d: 逆时针90°转底面。 F: 顺时针90°转前面。 f: 逆时针90°转前面。 B: 顺时针90°转背面。 b: 逆时针90°转背面。 M: 向下转中层。 m: 向上转中层。 X: 顺着x轴转90°。 x: 逆着x轴转90°。 Y: 顺着y轴转90°。 y: 逆着y轴转90°。 Z: 顺着z轴转90°。 z: 逆着z轴转90°。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值