学了一点皮毛……想找点事情练练码力。
正好手边有一个魔方,我一激动就决定:写一个魔方模拟器。
费了好大功夫终于写出来了——最大的难点就是如何合理地存储六个面的信息,避免因旋转等因素导致各种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 <= 3; i++) 71 for (int j = 1; j <= 3; j++) 72 { 73 F[i, j] = Red; B[i, j] = Orange; 74 L[i, j] = Blue; R[i, j] = Green; 75 U[i, j] = Yellow; D[i, j] = White; 76 } 77 78 nowFace = Front; //位置初始化 79 nowUnder = Down; 80 81 CountOptions = 0; //操作数初始化 82 } 83 84 public void Reposition() //复位 85 { 86 for (int i = 1; i <= 3; i++) 87 for (int j = 1; j <= 3; j++) 88 { 89 F[i, j] = Red; B[i, j] = Orange; 90 L[i, j] = Blue; R[i, j] = Green; 91 U[i, j] = Yellow; D[i, j] = White; 92 } 93 94 nowFace = Front; 95 nowUnder = Down; 96 97 CountOptions = 0; 98 } 99 100 private void EnsureFront(ref int[,] Play) 101 { 102 if (nowFace == Front) Play = F; 103 if (nowFace == Back) Play = B; 104 if (nowFace == Left) Play = L; 105 if (nowFace == Right) Play = R; 106 if (nowFace == Up) Play = U; 107 if (nowFace == Down) Play = D; 108 } 109 110 private void EnsureBack(ref int[,] Play) 111 { 112 if (Opposite[nowFace] == Front) Play = F; 113 if (Opposite[nowFace] == Back) Play = B; 114 if (Opposite[nowFace] == Left) Play = L; 115 if (Opposite[nowFace] == Right) Play = R; 116 if (Opposite[nowFace] == Up) Play = U; 117 if (Opposite[nowFace] == Down) Play = D; 118 } 119 120 private void EnsureLeft(ref int[,] Play) 121 { 122 if (GetLeft[nowFace, nowUnder] == Front) Play = F; 123 if (GetLeft[nowFace, nowUnder] == Back) Play = B; 124 if (GetLeft[nowFace, nowUnder] == Left) Play = L; 125 if (GetLeft[nowFace, nowUnder] == Right) Play = R; 126 if (GetLeft[nowFace, nowUnder] == Up) Play = U; 127 if (GetLeft[nowFace, nowUnder] == Down) Play = D; 128 } 129 130 private void EnsureRight(ref int[,] Play) 131 { 132 if (Opposite[GetLeft[nowFace, nowUnder]] == Front) Play = F; 133 if (Opposite[GetLeft[nowFace, nowUnder]] == Back) Play = B; 134 if (Opposite[GetLeft[nowFace, nowUnder]] == Left) Play = L; 135 if (Opposite[GetLeft[nowFace, nowUnder]] == Right) Play = R; 136 if (Opposite[GetLeft[nowFace, nowUnder]] == Up) Play = U; 137 if (Opposite[GetLeft[nowFace, nowUnder]] == Down) Play = D; 138 } 139 140 private void EnsureUp(ref int[,] Play) 141 { 142 if (Opposite[nowUnder] == Front) Play = F; 143 if (Opposite[nowUnder] == Back) Play = B; 144 if (Opposite[nowUnder] == Left) Play = L; 145 if (Opposite[nowUnder] == Right) Play = R; 146 if (Opposite[nowUnder] == Up) Play = U; 147 if (Opposite[nowUnder] == Down) Play = D; 148 } 149 150 private void EnsureDown(ref int[,] Play) 151 { 152 if (nowUnder == Front) Play = F; 153 if (nowUnder == Back) Play = B; 154 if (nowUnder == Left) Play = L; 155 if (nowUnder == Right) Play = R; 156 if (nowUnder == Up) Play = U; 157 if (nowUnder == Down) Play = D; 158 } 159 160 public void SpinRight_UpFace() 161 { 162 int[,] refer = new int[4, 4]; 163 EnsureUp(ref refer); 164 int[,] temporary = new int[4, 4]; 165 for (int i = 1; i <= 3; i++) 166 for (int j = 1; j <= 3; j++) 167 temporary[i, j] = refer[i, j]; 168 for (int i = 1; i <= 3; i++) 169 for (int j = 1; j <= 3; j++) 170 refer[3 - j + 1, i] = temporary[i, j]; 171 } 172 173 public void SpinRight_DownFace() 174 { 175 int[,] refer = new int[4, 4]; 176 EnsureDown(ref refer); 177 int[,] temporary = new int[4, 4]; 178 for (int i = 1; i <= 3; i++) 179 for (int j = 1; j <= 3; j++) 180 temporary[i, j] = refer[i, j]; 181 for (int i = 1; i <= 3; i++) 182 for (int j = 1; j <= 3; j++) 183 refer[j, 3 - i + 1] = temporary[i, j]; 184 } 185 186 public void TurnToLeft() 187 { 188 SpinRight_UpFace(); 189 SpinRight_DownFace(); 190 nowFace = GetLeft[nowFace, nowUnder]; 191 } 192 193 public void SpinLeft_UpFace() 194 { 195 int[,] refer = new int[4, 4]; 196 EnsureUp(ref refer); 197 int[,] temporary = new int[4, 4]; 198 for (int i = 1; i <= 3; i++) 199 for (int j = 1; j <= 3; j++) 200 temporary[i, j] = refer[i, j]; 201 for (int i = 1; i <= 3; i++) 202 for (int j = 1; j <= 3; j++) 203 refer[j, 3 - i + 1] = temporary[i, j]; 204 } 205 206 public void SpinLeft_DownFace() 207 { 208 int[,] refer = new int[4, 4]; 209 EnsureDown(ref refer); 210 int[,] temporary = new int[4, 4]; 211 for (int i = 1; i <= 3; i++) 212 for (int j = 1; j <= 3; j++) 213 temporary[i, j] = refer[i, j]; 214 for (int i = 1; i <= 3; i++) 215 for (int j = 1; j <= 3; j++) 216 refer[3 - j + 1, i] = temporary[i, j]; 217 } 218 219 public void TurnToRight() 220 { 221 SpinLeft_UpFace(); 222 SpinLeft_DownFace(); 223 nowFace = Opposite[GetLeft[nowFace, nowUnder]]; 224 } 225 226 public void SpinDown_LeftFace() 227 { 228 int[,] refer = new int[4, 4]; 229 EnsureLeft(ref refer); 230 int[,] temporary = new int[4, 4]; 231 for (int i = 1; i <= 3; i++) 232 for (int j = 1; j <= 3; j++) 233 temporary[i, j] = refer[i, j]; 234 for (int i = 1; i <= 3; i++) 235 for (int j = 1; j <= 3; j++) 236 refer[j, 3 - i + 1] = temporary[i, j]; 237 } 238 239 public void SpinDown_RightFace() 240 { 241 int[,] refer = new int[4, 4]; 242 EnsureRight(ref refer); 243 int[,] temporary = new int[4, 4]; 244 for (int i = 1; i <= 3; i++) 245 for (int j = 1; j <= 3; j++) 246 temporary[i, j] = refer[i, j]; 247 for (int i = 1; i <= 3; i++) 248 for (int j = 1; j <= 3; j++) 249 refer[3 - j + 1, i] = temporary[i, j]; 250 } 251 252 public void TurnToUp() 253 { 254 SpinDown_LeftFace(); 255 SpinDown_RightFace(); 256 257 int[,] refer = new int[4, 4]; 258 EnsureBack(ref refer); 259 int[,] temporary = new int[4, 4]; 260 for (int i = 1; i <= 3; i++) 261 for (int j = 1; j <= 3; j++) 262 temporary[i, j] = refer[i, j]; 263 for (int i = 1; i <= 3; i++) 264 for (int j = 1; j <= 3; j++) 265 refer[i, j] = temporary[3 - i + 1, 3 - j + 1]; 266 267 EnsureDown(ref refer); 268 for (int i = 1; i <= 3; i++) 269 for (int j = 1; j <= 3; j++) 270 temporary[i, j] = refer[i, j]; 271 for (int i = 1; i <= 3; i++) 272 for (int j = 1; j <= 3; j++) 273 refer[i, j] = temporary[3 - i + 1, 3 - j + 1]; 274 275 int temporaryFace = nowUnder; 276 nowUnder = nowFace; 277 nowFace = Opposite[temporaryFace]; 278 } 279 280 public void SpinUp_LeftFace() 281 { 282 int[,] refer = new int[4, 4]; 283 EnsureLeft(ref refer); 284 int[,] temporary = new int[4, 4]; 285 for (int i = 1; i <= 3; i++) 286 for (int j = 1; j <= 3; j++) 287 temporary[i, j] = refer[i, j]; 288 for (int i = 1; i <= 3; i++) 289 for (int j = 1; j <= 3; j++) 290 refer[3 - j + 1, i] = temporary[i, j]; 291 } 292 293 public void SpinUp_RightFace() 294 { 295 int[,] refer = new int[4, 4]; 296 EnsureRight(ref refer); 297 int[,] temporary = new int[4, 4]; 298 for (int i = 1; i <= 3; i++) 299 for (int j = 1; j <= 3; j++) 300 temporary[i, j] = refer[i, j]; 301 for (int i = 1; i <= 3; i++) 302 for (int j = 1; j <= 3; j++) 303 refer[j, 3 - i + 1] = temporary[i, j]; 304 } 305 306 public void TurnToDown() 307 { 308 SpinUp_LeftFace(); 309 SpinUp_RightFace(); 310 311 int[,] refer = new int[4, 4]; 312 EnsureBack(ref refer); 313 int[,] temporary = new int[4, 4]; 314 for (int i = 1; i <= 3; i++) 315 for (int j = 1; j <= 3; j++) 316 temporary[i, j] = refer[i, j]; 317 for (int i = 1; i <= 3; i++) 318 for (int j = 1; j <= 3; j++) 319 refer[i, j] = temporary[3 - i + 1, 3 - j + 1]; 320 321 EnsureUp(ref refer); 322 for (int i = 1; i <= 3; i++) 323 for (int j = 1; j <= 3; j++) 324 temporary[i, j] = refer[i, j]; 325 for (int i = 1; i <= 3; i++) 326 for (int j = 1; j <= 3; j++) 327 refer[i, j] = temporary[3 - i + 1, 3 - j + 1]; 328 329 int temporaryFace = nowFace; 330 nowFace = nowUnder; 331 nowUnder = Opposite[temporaryFace]; 332 } 333 334 public void SpinCounterclockwise_FrontFace() 335 { 336 int[,] refer = new int[4, 4]; 337 EnsureFront(ref refer); 338 int[,] temporary = new int[4, 4]; 339 for (int i = 1; i <= 3; i++) 340 for (int j = 1; j <= 3; j++) 341 temporary[i, j] = refer[i, j]; 342 for (int i = 1; i <= 3; i++) 343 for (int j = 1; j <= 3; j++) 344 refer[3 - j + 1, i] = temporary[i, j]; 345 } 346 347 public void SpinCounterclockwise_BackFace() 348 { 349 int[,] refer = new int[4, 4]; 350 EnsureBack(ref refer); 351 int[,] temporary = new int[4, 4]; 352 for (int i = 1; i <= 3; i++) 353 for (int j = 1; j <= 3; j++) 354 temporary[i, j] = refer[i, j]; 355 for (int i = 1; i <= 3; i++) 356 for (int j = 1; j <= 3; j++) 357 refer[j, 3 - i + 1] = temporary[i, j]; 358 } 359 360 public void SpinClockwise_FrontFace() 361 { 362 int[,] refer = new int[4, 4]; 363 EnsureFront(ref refer); 364 int[,] temporary = new int[4, 4]; 365 for (int i = 1; i <= 3; i++) 366 for (int j = 1; j <= 3; j++) 367 temporary[i, j] = refer[i, j]; 368 for (int i = 1; i <= 3; i++) 369 for (int j = 1; j <= 3; j++) 370 refer[j, 3 - i + 1] = temporary[i, j]; 371 } 372 373 public void SpinClockwise_BackFace() 374 { 375 int[,] refer = new int[4, 4]; 376 EnsureBack(ref refer); 377 int[,] temporary = new int[4, 4]; 378 for (int i = 1; i <= 3; i++) 379 for (int j = 1; j <= 3; j++) 380 temporary[i, j] = refer[i, j]; 381 for (int i = 1; i <= 3; i++) 382 for (int j = 1; j <= 3; j++) 383 refer[3 - j + 1, i] = temporary[i, j]; 384 } 385 386 public void TurnFunction(int rowOrColumn, int number, int direction) 387 { 388 int[,] refer = new int[4, 4]; 389 390 int[,] temporary_Front = new int[4, 4]; 391 EnsureFront(ref refer); 392 for (int i = 1; i <= 3; i++) 393 for (int j = 1; j <= 3; j++) 394 temporary_Front[i, j] = refer[i, j]; 395 396 int[,] temporary_Back = new int[4, 4]; 397 EnsureBack(ref refer); 398 for (int i = 1; i <= 3; i++) 399 for (int j = 1; j <= 3; j++) 400 temporary_Back[i, j] = refer[i, j]; 401 402 if (rowOrColumn == Row) 403 { 404 int[,] temporary_Left = new int[4, 4]; 405 EnsureLeft(ref refer); 406 for (int i = 1; i <= 3; i++) 407 for (int j = 1; j <= 3; j++) 408 temporary_Left[i, j] = refer[i, j]; 409 410 int[,] temporary_Right = new int[4, 4]; 411 EnsureRight(ref refer); 412 for (int i = 1; i <= 3; i++) 413 for (int j = 1; j <= 3; j++) 414 temporary_Right[i, j] = refer[i, j]; 415 416 if (direction == LEFT) 417 { 418 EnsureFront(ref refer); 419 for (int i = 1; i <= 3; i++) 420 refer[number, i] = temporary_Right[number, i]; 421 EnsureLeft(ref refer); 422 for (int i = 1; i <= 3; i++) 423 refer[number, i] = temporary_Front[number, i]; 424 EnsureBack(ref refer); 425 for (int i = 1; i <= 3; i++) 426 refer[number, i] = temporary_Left[number, i]; 427 EnsureRight(ref refer); 428 for (int i = 1; i <= 3; i++) 429 refer[number, i] = temporary_Back[number, i]; 430 431 if (number == 1) SpinLeft_UpFace(); 432 if (number == 3) SpinLeft_DownFace(); 433 } 434 435 else if (direction == RIGHT) 436 { 437 EnsureFront(ref refer); 438 for (int i = 1; i <= 3; i++) 439 refer[number, i] = temporary_Left[number, i]; 440 EnsureLeft(ref refer); 441 for (int i = 1; i <= 3; i++) 442 refer[number, i] = temporary_Back[number, i]; 443 EnsureBack(ref refer); 444 for (int i = 1; i <= 3; i++) 445 refer[number, i] = temporary_Right[number, i]; 446 EnsureRight(ref refer); 447 for (int i = 1; i <= 3; i++) 448 refer[number, i] = temporary_Front[number, i]; 449 450 if (number == 1) SpinRight_UpFace(); 451 if (number == 3) SpinRight_DownFace(); 452 } 453 } 454 455 else if (rowOrColumn == Column) 456 { 457 int[,] temporary_Up = new int[4, 4]; 458 EnsureUp(ref refer); 459 for (int i = 1; i <= 3; i++) 460 for (int j = 1; j <= 3; j++) 461 temporary_Up[i, j] = refer[i, j]; 462 463 int[,] temporary_Down = new int[4, 4]; 464 EnsureDown(ref refer); 465 for (int i = 1; i <= 3; i++) 466 for (int j = 1; j <= 3; j++) 467 temporary_Down[i, j] = refer[i, j]; 468 469 if (direction == UP) 470 { 471 EnsureBack(ref refer); 472 for (int i = 1; i <= 3; i++) 473 refer[3 - i + 1, 3 - number + 1] = temporary_Up[i, number]; 474 EnsureUp(ref refer); 475 for (int i = 1; i <= 3; i++) 476 refer[i, number] = temporary_Front[i, number]; 477 EnsureFront(ref refer); 478 for (int i = 1; i <= 3; i++) 479 refer[i, number] = temporary_Down[i, number]; 480 EnsureDown(ref refer); 481 for (int i = 1; i <= 3; i++) 482 refer[i, number] = temporary_Back[3 - i + 1, 3 - number + 1]; 483 484 if (number == 1) SpinUp_LeftFace(); 485 if (number == 3) SpinUp_RightFace(); 486 } 487 488 else if (direction == DOWN) 489 { 490 EnsureBack(ref refer); 491 for (int i = 1; i <= 3; i++) 492 refer[3 - i + 1, 3 - number + 1] = temporary_Down[i, number]; 493 EnsureDown(ref refer); 494 for (int i = 1; i <= 3; i++) 495 refer[i, number] = temporary_Front[i, number]; 496 EnsureFront(ref refer); 497 for (int i = 1; i <= 3; i++) 498 refer[i, number] = temporary_Up[i, number]; 499 EnsureUp(ref refer); 500 for (int i = 1; i <= 3; i++) 501 refer[i, number] = temporary_Back[3 - i + 1, 3 - number + 1]; 502 503 if (number == 1) SpinDown_LeftFace(); 504 if (number == 3) SpinDown_RightFace(); 505 } 506 } 507 } 508 509 public void SpinFunction(int direction, int number) 510 { 511 int[,] refer = new int[4, 4]; 512 513 int[,] temporary_Up = new int[4, 4]; 514 EnsureUp(ref refer); 515 for (int i = 1; i <= 3; i++) 516 for (int j = 1; j <= 3; j++) 517 temporary_Up[i, j] = refer[i, j]; 518 519 int[,] temporary_Down = new int[4, 4]; 520 EnsureDown(ref refer); 521 for (int i = 1; i <= 3; i++) 522 for (int j = 1; j <= 3; j++) 523 temporary_Down[i, j] = refer[i, j]; 524 525 int[,] temporary_Left = new int[4, 4]; 526 EnsureLeft(ref refer); 527 for (int i = 1; i <= 3; i++) 528 for (int j = 1; j <= 3; j++) 529 temporary_Left[i, j] = refer[i, j]; 530 531 int[,] temporary_Right = new int[4, 4]; 532 EnsureRight(ref refer); 533 for (int i = 1; i <= 3; i++) 534 for (int j = 1; j <= 3; j++) 535 temporary_Right[i, j] = refer[i, j]; 536 537 if (direction == Clockwise) 538 { 539 EnsureUp(ref refer); 540 for (int i = 1; i <= 3; i++) 541 refer[3 - number + 1, i] = temporary_Left[3 - i + 1, 3 - number + 1]; 542 543 EnsureLeft(ref refer); 544 for (int i = 1; i <= 3; i++) 545 refer[i, 3 - number + 1] = temporary_Down[number, i]; 546 547 EnsureDown(ref refer); 548 for (int i = 1; i <= 3; i++) 549 refer[number, i] = temporary_Right[3 - i + 1, number]; 550 551 EnsureRight(ref refer); 552 for (int i = 1; i <= 3; i++) 553 refer[i, number] = temporary_Up[3 - number + 1, i]; 554 555 if (number == 1) SpinClockwise_FrontFace(); 556 if (number == 3) SpinClockwise_BackFace(); 557 } 558 559 else if (direction == Counterclockwise) 560 { 561 EnsureUp(ref refer); 562 for (int i = 1; i <= 3; i++) 563 refer[3 - number + 1, i] = temporary_Right[i, number]; 564 565 EnsureLeft(ref refer); 566 for (int i = 1; i <= 3; i++) 567 refer[i, 3 - number + 1] = temporary_Up[3 - number + 1, 3 - i + 1]; 568 569 EnsureDown(ref refer); 570 for (int i = 1; i <= 3; i++) 571 refer[number, i] = temporary_Left[i, 3 - number + 1]; 572 573 EnsureRight(ref refer); 574 for (int i = 1; i <= 3; i++) 575 refer[i, number] = temporary_Down[number, 3 - i + 1]; 576 577 if (number == 1) SpinCounterclockwise_FrontFace(); 578 if (number == 3) SpinCounterclockwise_BackFace(); 579 } 580 } 581 582 public void PrintNowFace() 583 { 584 int[,] Play = new int[4, 4]; 585 586 EnsureFront(ref Play); 587 Console.WriteLine("\n"); 588 589 //print the megic cube 590 for (int i = 1; i <= 3; i++) 591 { 592 Console.Write(" "); 593 for (int j = 1; j <= 3; j++) 594 { 595 if (Play[i, j] == Red) Console.BackgroundColor = ConsoleColor.Red; 596 if (Play[i, j] == Orange) Console.BackgroundColor = ConsoleColor.Magenta; 597 if (Play[i, j] == Blue) Console.BackgroundColor = ConsoleColor.Blue; 598 if (Play[i, j] == Green) Console.BackgroundColor = ConsoleColor.Green; 599 if (Play[i, j] == Yellow) Console.BackgroundColor = ConsoleColor.Yellow; 600 if (Play[i, j] == White) Console.BackgroundColor = ConsoleColor.White; 601 Console.Write(" "); 602 } 603 Console.WriteLine(); 604 Console.ResetColor(); 605 } 606 } 607 608 public bool PrintScreen_WelcomeFunction() 609 { 610 Console.Clear(); 611 Console.WriteLine(); 612 Console.WriteLine(" * Rubik‘s Cube Simulator *\n"); 613 614 Random random = new Random(); 615 616 for (int i = 1; i <= 3; i++) 617 { 618 Console.Write(" "); 619 for (int j = 1; j <= 3; j++) 620 { 621 int temporary = random.Next(1, 6); 622 if (temporary == Red) Console.BackgroundColor = ConsoleColor.Red; 623 if (temporary == Orange) Console.BackgroundColor = ConsoleColor.Magenta; 624 if (temporary == Blue) Console.BackgroundColor = ConsoleColor.Blue; 625 if (temporary == Green) Console.BackgroundColor = ConsoleColor.Green; 626 if (temporary == Yellow) Console.BackgroundColor = ConsoleColor.Yellow; 627 if (temporary == White) Console.BackgroundColor = ConsoleColor.White; 628 Console.Write(" "); 629 } 630 Console.WriteLine(); 631 Console.ResetColor(); 632 } 633 634 Console.WriteLine(); 635 Console.WriteLine(" · Press \"S\" to create a new magic cube."); 636 Console.WriteLine(" · Press \"E\" to exit this application."); 637 Console.WriteLine("\n · Case-insensitive"); 638 639 string odd = Console.ReadKey().Key.ToString(); 640 if (odd == "E") return false; 641 else if (odd == "S") return true; 642 643 Console.WriteLine("Your input is incorrect! Please press any key to try again."); 644 Console.ReadKey(); 645 return PrintScreen_WelcomeFunction(); 646 } 647 648 public void PrintScreen_GoodbyeFunction() 649 { 650 Console.Clear(); 651 Console.WriteLine(); 652 Console.WriteLine(" * Rubik‘s Cube Simulator *\n"); 653 654 Random random = new Random(); 655 656 for (int i = 1; i <= 3; i++) 657 { 658 Console.Write(" "); 659 for (int j = 1; j <= 3; j++) 660 { 661 int temporary = random.Next(1, 6); 662 if (temporary == Red) Console.BackgroundColor = ConsoleColor.Red; 663 if (temporary == Orange) Console.BackgroundColor = ConsoleColor.Magenta; 664 if (temporary == Blue) Console.BackgroundColor = ConsoleColor.Blue; 665 if (temporary == Green) Console.BackgroundColor = ConsoleColor.Green; 666 if (temporary == Yellow) Console.BackgroundColor = ConsoleColor.Yellow; 667 if (temporary == White) Console.BackgroundColor = ConsoleColor.White; 668 Console.Write(" "); 669 } 670 Console.WriteLine(); 671 Console.ResetColor(); 672 } 673 Console.WriteLine(); 674 Console.WriteLine(" · Thanks to use my product. Have a nice day!\n\n"); 675 Console.ReadKey(); 676 } 677 678 public void InitialRandomOptions() 679 { 680 Random random = new Random(); 681 for (int i = 1; i <= 100; i++) //随机打乱100次 682 { 683 int changeFaceCounter = random.Next(1, 10); 684 while (changeFaceCounter-- != 0) 685 { 686 int changeFace = random.Next(1, 4); 687 if (changeFace == 1) TurnToUp(); 688 else if (changeFace == 2) TurnToDown(); 689 else if (changeFace == 3) TurnToLeft(); 690 else if (changeFace == 4) TurnToRight(); 691 } 692 693 int turnOrSpin = random.Next(1, 4); 694 if (turnOrSpin == Row || turnOrSpin == Column) 695 { 696 int rowOrColumnNumber = random.Next(1, 3); 697 int direction = random.Next(1, 2); 698 TurnFunction(turnOrSpin, rowOrColumnNumber, direction); 699 } 700 else if (turnOrSpin == Clockwise || turnOrSpin == Counterclockwise) 701 { 702 int spinNumber = random.Next(1, 3); 703 SpinFunction(turnOrSpin, spinNumber); 704 } 705 Console.Clear(); 706 Console.WriteLine("\n"); 707 PrintNowFace(); 708 } 709 } 710 711 public void CheckNowFace() 712 { 713 Console.Clear(); 714 Console.Write("\n Now this is your magic cube.\n Of course, this is just one face of it."); 715 PrintNowFace(); 716 } 717 718 public void CheckModel() 719 { 720 while (true) 721 { 722 CheckNowFace(); 723 //guidance 724 Console.WriteLine("\nCheck Model:"); 725 Console.WriteLine(" · Press \"A\" or [left arrow] to turn to the LEFT face and check it."); 726 Console.WriteLine(" · Press \"D\" or [right arrow] to turn to the RIGHT face and check it."); 727 Console.WriteLine(" · Press \"W\" or [up arrow] to turn to the UP face and check it."); 728 Console.WriteLine(" · Press \"S\" or [down arrow] to turn to the DOWN face and check it."); 729 Console.WriteLine(" · Press \"Enter\" to quit Check Model."); 730 731 //option that can change the current face or quit 732 ConsoleKey oddKey = Console.ReadKey().Key; 733 if (oddKey.ToString() == "Enter") break; 734 string odd = oddKey.ToString(); 735 if (odd == "A" || odd == "LeftArrow") TurnToLeft(); 736 else if (odd == "D" || odd == "RightArrow") TurnToRight(); 737 else if (odd == "W" || odd == "UpArrow") TurnToUp(); 738 else if (odd == "S" || odd == "DownArrow") TurnToDown(); 739 else 740 { 741 Console.WriteLine("Your input has something wrong!"); 742 Console.WriteLine("Press any key to continue..."); 743 Console.ReadKey(); 744 } 745 } 746 } 747 748 public int Query() 749 { 750 Console.WriteLine(); 751 Console.WriteLine("You can do things like:"); 752 Console.WriteLine(" · Press \"D\" to go into the Check Model."); 753 Console.WriteLine(" -> In this model, you can change the observation surface."); 754 Console.WriteLine(" · Press \"R\" to shuffle your magic cube randomly."); 755 Console.WriteLine(" · Press \"O\" to operate your magic cube."); 756 Console.WriteLine(" · Press \"U\" to undo your last option."); 757 Console.WriteLine(" · Press \"C\" to reset your magic cube."); 758 Console.WriteLine(" · Press \"E\" to end your play."); 759 760 string odd = Console.ReadKey().Key.ToString(); 761 if (odd == "D") return 1; 762 else if (odd == "R") return 2; 763 else if (odd == "O") return 3; 764 else if (odd == "U") return 4; 765 else if (odd == "C") return 5; 766 else if (odd == "E") return 6; 767 768 Console.WriteLine("Your input is incorrect! Please press any key to try again."); 769 Console.ReadKey(); 770 Console.Clear(); 771 CheckNowFace(); 772 return Query(); 773 } 774 775 public void Operation() 776 { 777 CheckNowFace(); 778 Console.WriteLine(); 779 Console.WriteLine("You can do things like:"); 780 Console.WriteLine(" · Press \"T\" to make cube turn."); 781 Console.WriteLine(" · Press \"S\" to make cube spin."); 782 783 string turnOrSpin = Console.ReadKey().Key.ToString(); 784 785 if (turnOrSpin == "T") 786 { 787 RorC: CheckNowFace(); 788 Console.WriteLine(); 789 Console.WriteLine("You can do things like:"); 790 Console.WriteLine(" · Press \"R\" to make ROW turn."); 791 Console.WriteLine(" · Press \"C\" to make COLUMN turn."); 792 string odd = Console.ReadKey().Key.ToString(); 793 int rowOrColumn = 0, number = 0, direction = 0; 794 795 if (odd == "R") 796 { 797 rowOrColumn = Row; 798 SelR: CheckNowFace(); 799 Console.WriteLine(); 800 Console.WriteLine("You can do things like:"); 801 Console.WriteLine(" · Press \"1\" to make the top row turn."); 802 Console.WriteLine(" · Press \"2\" to make the middle row turn."); 803 Console.WriteLine(" · Press \"3\" to make the bottom row turn."); 804 string rowNumber = Console.ReadKey().Key.ToString(); 805 if (rowNumber == "NumPad1" || rowNumber == "D1") number = 1; 806 else if (rowNumber == "NumPad2" || rowNumber == "D2") number = 2; 807 else if (rowNumber == "NumPad3" || rowNumber == "D3") number = 3; 808 else 809 { 810 Console.WriteLine("Your input is incorrect! Please press any key to try again."); 811 Console.ReadKey(); 812 goto SelR; 813 } 814 LorR: CheckNowFace(); 815 Console.WriteLine(); 816 Console.WriteLine("You can do things like:"); 817 Console.WriteLine(" · Press \"L\" to make the row turn to left."); 818 Console.WriteLine(" · Press \"R\" to make the row turn to right."); 819 string leftOrRight = Console.ReadKey().Key.ToString(); 820 if (leftOrRight == "L") direction = 1; 821 else if (leftOrRight == "R") direction = 2; 822 else 823 { 824 Console.WriteLine("Your input is incorrect! Please press any key to try again."); 825 Console.ReadKey(); 826 goto LorR; 827 } 828 } 829 830 else if (odd == "C") 831 { 832 rowOrColumn = Column; 833 SelC: CheckNowFace(); 834 Console.WriteLine(); 835 Console.WriteLine("You can do things like:"); 836 Console.WriteLine(" · Press \"1\" to make the left column turn."); 837 Console.WriteLine(" · Press \"2\" to make the middle column turn."); 838 Console.WriteLine(" · Press \"3\" to make the right column turn."); 839 string rowNumber = Console.ReadKey().Key.ToString(); 840 if (rowNumber == "NumPad1" || rowNumber == "D1") number = 1; 841 else if (rowNumber == "NumPad2" || rowNumber == "D2") number = 2; 842 else if (rowNumber == "NumPad3" || rowNumber == "D3") number = 3; 843 else 844 { 845 Console.WriteLine("Your input is incorrect! Please press any key to try again."); 846 Console.ReadKey(); 847 goto SelC; 848 } 849 UorD: CheckNowFace(); 850 Console.WriteLine(); 851 Console.WriteLine("You can do things like:"); 852 Console.WriteLine(" · Press \"U\" to make the column turn to up."); 853 Console.WriteLine(" · Press \"D\" to make the column turn to down."); 854 string leftOrRight = Console.ReadKey().Key.ToString(); 855 if (leftOrRight == "U") direction = 1; 856 else if (leftOrRight == "D") direction = 2; 857 else 858 { 859 Console.WriteLine("Your input is incorrect! Please press any key to try again."); 860 Console.ReadKey(); 861 goto UorD; 862 } 863 } 864 865 else 866 { 867 Console.WriteLine("Your input is incorrect! Please press any key to try again."); 868 Console.ReadKey(); 869 goto RorC; 870 } 871 872 TurnFunction(rowOrColumn, number, direction); 873 theLastOptions[++CountOptions].turnOrSpin = Turn; 874 theLastOptions[CountOptions].rowOrColumn = rowOrColumn; 875 theLastOptions[CountOptions].number = number; 876 theLastOptions[CountOptions].direction = direction; 877 } 878 879 else if (turnOrSpin == "S") 880 { 881 QorE: CheckNowFace(); 882 Console.WriteLine(); 883 Console.WriteLine("You can do things like:"); 884 Console.WriteLine(" · Press \"Q\" to make counterclockwise turn."); 885 Console.WriteLine(" · Press \"E\" to make clockwise turn."); 886 string odd = Console.ReadKey().Key.ToString(); 887 int direction = 0, number = 0; 888 889 if (odd == "Q") 890 { 891 direction = Counterclockwise; 892 SelL_1: CheckNowFace(); 893 Console.WriteLine(); 894 Console.WriteLine("You can do things like:"); 895 Console.WriteLine(" · Press \"1\" to make the front layer spin."); 896 Console.WriteLine(" · Press \"2\" to make the middle layer spin."); 897 Console.WriteLine(" · Press \"3\" to make the back layer spin."); 898 string layNumber = Console.ReadKey().Key.ToString(); 899 if (layNumber == "NumPad1" || layNumber == "D1") number = 1; 900 else if (layNumber == "NumPad2" || layNumber == "D2") number = 2; 901 else if (layNumber == "NumPad3" || layNumber == "D3") number = 3; 902 else 903 { 904 Console.WriteLine("Your input is incorrect! Please press any key to try again."); 905 Console.ReadKey(); 906 goto SelL_1; 907 } 908 } 909 910 else if (odd == "E") 911 { 912 direction = Clockwise; 913 SelL_2: CheckNowFace(); 914 Console.WriteLine(); 915 Console.WriteLine("You can do things like:"); 916 Console.WriteLine(" · Press \"1\" to make the front layer spin."); 917 Console.WriteLine(" · Press \"2\" to make the middle layer spin."); 918 Console.WriteLine(" · Press \"3\" to make the back layer spin."); 919 string layNumber = Console.ReadKey().Key.ToString(); 920 if (layNumber == "NumPad1" || layNumber == "D1") number = 1; 921 else if (layNumber == "NumPad2" || layNumber == "D2") number = 2; 922 else if (layNumber == "NumPad3" || layNumber == "D3") number = 3; 923 else 924 { 925 Console.WriteLine("Your input is incorrect! Please press any key to try again."); 926 Console.ReadKey(); 927 goto SelL_2; 928 } 929 } 930 931 else 932 { 933 Console.WriteLine("Your input is incorrect! Please press any key to try again."); 934 Console.ReadKey(); 935 goto QorE; 936 } 937 938 SpinFunction(direction, number); 939 theLastOptions[++CountOptions].turnOrSpin = Spin; 940 theLastOptions[CountOptions].clockwiseOrCounterclockwise = direction; 941 theLastOptions[CountOptions].number = number; 942 } 943 944 else 945 { 946 Console.WriteLine("Your input is incorrect! Please press any key to try again."); 947 Console.ReadKey(); 948 Operation(); 949 } 950 } 951 952 private int CounterNumber(int number) 953 { 954 if (number > 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 }
原文:https://www.cnblogs.com/dypblog/p/12337272.html