魔方模拟器 您所在的位置:网站首页 虚拟魔方在线玩二阶 魔方模拟器

魔方模拟器

2024-07-16 17:21| 来源: 网络整理| 查看: 265

        学了一点皮毛……想找点事情练练码力。

        正好手边有一个魔方,我一激动就决定:写一个魔方模拟器。

        费了好大功夫终于写出来了——最大的难点就是如何合理地存储六个面的信息,避免因旋转等因素导致各种BUG。

        这个模拟器支持以下操作:

    (1)查看魔方六个面的信息;

    (2)对魔方进行行操作:左转/右转;

    (3)对魔方进行列操作:上转/下转;

    (4)对魔方进行面旋转:顺时针/逆时针;

    (5)撤销上一步操作;

    (6)随机打乱魔方;

    (7)将魔方复位。

  源码如下:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication 7 { 8 //记录操作历史 9 struct Option 10 { 11 public int turnOrSpin; 12 public int rowOrColumn, direction; 13 public int clockwiseOrCounterclockwise; 14 public int number; 15 } 16 17 class MagicCube 18 { 19 const int maxn = 20009; //句柄最大量 20 21 readonly int Red = 1; //颜色系统 22 readonly int Orange = 2; //实际颜色为粉色……系统颜色中无橙色 23 readonly int Blue = 3; 24 readonly int Green = 4; 25 readonly int Yellow = 5; 26 readonly int White = 6; 27 28 readonly int Front = 1; //朝向系统 29 readonly int Back = 2; 30 readonly int Left = 3; 31 readonly int Right = 4; 32 readonly int Up = 5; 33 readonly int Down = 6; 34 readonly int[] Opposite = { 0, 2, 1, 4, 3, 6, 5 }; //对立面 35 36 public int[,] F = new int[4, 4]; //存储对象 37 public int[,] B = new int[4, 4]; 38 public int[,] L = new int[4, 4]; 39 public int[,] R = new int[4, 4]; 40 public int[,] U = new int[4, 4]; 41 public int[,] D = new int[4, 4]; 42 43 public Option[] theLastOptions = new Option[maxn]; //撤销操作 44 public int CountOptions; 45 46 public int nowFace; //定位系统 47 public int nowUnder; 48 public int[,] GetLeft = {{0,0,0,0,0,0,0}, 49 {0,0,0,5,6,4,3}, 50 {0,0,0,6,5,3,4}, 51 {0,6,5,0,0,1,2}, 52 {0,5,6,0,0,2,1}, 53 {0,3,4,2,1,0,0}, 54 {0,4,3,1,2,0,0}}; 55 56 readonly int Turn = 1; //操作参数 57 readonly int Spin = 2; 58 readonly int Row = 1; 59 readonly int Column = 2; 60 readonly int LEFT = 1; 61 readonly int RIGHT = 2; 62 readonly int UP = 1; 63 readonly int DOWN = 2; 64 readonly int Clockwise = 3; 65 readonly int Counterclockwise = 4; 66 67 public MagicCube() 68 { 69 //颜色初始化 70 for (int i = 1; i 2) return 4 - number + 3; 955 return 2 - number + 1; 956 } 957 958 public void Undo() 959 { 960 if (CountOptions == 0) 961 { 962 Console.WriteLine("There is no option that you can undo!"); 963 Console.WriteLine("Press any key to continue."); 964 Console.ReadKey(); 965 return; 966 } 967 968 if (theLastOptions[CountOptions].turnOrSpin == Turn) 969 { 970 int rowOrColumn = theLastOptions[CountOptions].rowOrColumn; 971 int number = theLastOptions[CountOptions].number; 972 int direction = CounterNumber(theLastOptions[CountOptions].direction); 973 TurnFunction(rowOrColumn, number, direction); 974 } 975 976 else if (theLastOptions[CountOptions].turnOrSpin == Spin) 977 { 978 int ClockwiseOrCounterClockwise = CounterNumber(theLastOptions[CountOptions].clockwiseOrCounterclockwise); 979 int number = theLastOptions[CountOptions].number; 980 SpinFunction(ClockwiseOrCounterClockwise, number); 981 } 982 983 --CountOptions; 984 } 985 } 986 987 class Program 988 { 989 static void Main(string[] args) 990 { 991 MagicCube Cube = new MagicCube(); 992 if (Cube.PrintScreen_WelcomeFunction() == true) 993 while (true) 994 { 995 Cube.CheckNowFace(); 996 int odd = Cube.Query(); 997 if (odd == 1) Cube.CheckModel(); 998 else if (odd == 2) Cube.InitialRandomOptions(); 999 else if (odd == 3) Cube.Operation(); 1000 else if (odd == 4) Cube.Undo(); 1001 else if (odd == 5) Cube.Reposition(); 1002 else if (odd == 6) break; 1003 } 1004 Cube.PrintScreen_GoodbyeFunction(); 1005 } 1006 } 1007 }

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有