逻辑首先有控制台写了一下。
需要注意的地方不少:
- 进一步,需要考虑3个层面的问题。
- 前面的位置是空地,成功点,墙,还是箱子。
- 前面是箱子的时候,箱子的前面是空地,墙,成功点,还是箱子。
- 当移动的时候,原先人的地点是空地,成功点,需要恢复。
- 需要后退一步的时候,需要考虑两种情况。
- 恢复两个点的节点原先值。
- 由于上面2的情况,需要恢复3个点的原先值。
Source Code:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 struct State 8 { 9 public int posX, posY, mapx; 10 }; 11 12 namespace PushBox 13 { 14 15 16 class Program 17 { 18 // 1 表示 强 19 // 2 表示 人 20 // 3 表示 箱子 21 // 4 表示 成功点 22 static int[,] map = new int[10, 10] { 23 {1,1,1,1,1,1,1,1,1,1}, 24 {1,0,4,0,0,0,0,0,0,1}, 25 {1,0,0,0,0,4,0,0,0,1}, 26 {1,0,0,0,0,4,0,0,0,1}, 27 {1,0,0,0,0,3,0,0,0,1}, 28 {1,0,3,0,0,4,0,4,0,1}, 29 {1,4,0,4,4,2,0,3,0,1}, 30 {1,0,0,0,0,0,0,4,0,1}, 31 {1,0,0,0,0,0,0,0,0,1}, 32 {1,1,1,1,1,1,1,1,1,1} 33 }; 34 static int posX, posY; 35 36 static State[] list = new State[3]; 37 static int list_cnt = 0; 38 //箱子个数 39 static int cube_cnt; 40 static int _cube_cnt; 41 //步数 42 static int cnt; 43 44 45 static void pre_clear() 46 { 47 list_cnt = 0; 48 } 49 static void pre_save(int POSX,int POSY) 50 { 51 list[list_cnt].posX = POSX; 52 list[list_cnt].posY = POSY; 53 list[list_cnt].mapx = map[POSX, POSY]; 54 list_cnt++; 55 } 56 57 static bool victory() 58 { 59 int flag_cube_cnt = 0; 60 for (int i = 0; i < 10; i++) 61 { 62 for (int j = 0; j < 10; j++) 63 { 64 if (map[i, j] == 7) 65 flag_cube_cnt++; 66 } 67 } 68 return flag_cube_cnt == cube_cnt; 69 } 70 71 static void init() 72 { 73 cube_cnt = 0; 74 cnt = 0; 75 for (int i = 0; i < 10; i++) 76 { 77 for (int j = 0; j < 10; j++) 78 { 79 if (map[i, j] == 3) 80 { 81 cube_cnt++; 82 } 83 if (map[i, j] == 2) 84 { 85 posX = i; 86 posY = j; 87 pre_save(i, j); 88 } 89 } 90 } 91 } 92 93 static void draw() 94 { 95 Console.Clear(); 96 for (int i = 0; i < 10; i++) 97 { 98 for (int j = 0; j < 10; j++) 99 { 100 if (map[i, j] == 1) //# 101 { 102 System.Console.Write("■"); 103 } 104 else if(map[i,j]==0) 105 System.Console.Write(" "); 106 else if (map[i, j] == 2) //pepole 107 { 108 System.Console.Write("♀"); 109 } 110 else if(map[i,j] == 3) 111 { 112 System.Console.Write("△"); 113 } 114 else if (map[i, j] == 4) 115 { 116 System.Console.Write("☆"); 117 } 118 else if (map[i,j] == 6) // 人 + 成功点 119 { 120 System.Console.Write("◎"); 121 } 122 else if (map[i, j] == 7) 123 { 124 _cube_cnt++; 125 System.Console.Write("◆"); 126 127 128 } 129 130 } 131 Console.WriteLine(""); 132 133 } 134 /* 135 System.Console.Write("步数:{0}\n保存的链表",cnt); 136 137 for (int i = 0; i < list_cnt; i++) 138 { 139 Console.WriteLine("{0} {1} {2}", list[i].posX, list[i].posY, list[i].mapx); 140 141 }*/ 142 System.Console.WriteLine("按z键退回一步哦!"); 143 } 144 145 //向上移动 146 static void moveUp() 147 { 148 if (posX - 1 >= 0) 149 { 150 if (map[posX - 1, posY] == 0) // 空地 151 { 152 if (map[posX, posY] == 6) 153 { 154 pre_clear(); 155 pre_save(posX, posY); 156 pre_save(posX - 1, posY); 157 158 159 map[posX, posY] = 4; 160 map[posX-1,posY] = 2; 161 posX--; 162 //now_save(); 163 } 164 else if (map[posX, posY] == 2) 165 { 166 pre_clear(); 167 pre_save(posX, posY); 168 pre_save(posX-1, posY); 169 map[posX, posY] = 0; 170 map[posX-1, posY] = 2; 171 posX--; 172 } 173 } 174 else if (map[posX - 1, posY] == 4) //成功点 175 { 176 177 if (map[posX, posY] == 6) 178 { 179 pre_clear(); 180 pre_save(posX, posY); 181 pre_save(posX - 1, posY); 182 map[posX - 1, posY] = 6; 183 map[posX, posY] = 4; 184 posX--; 185 } 186 else if (map[posX, posY] == 2) 187 { 188 pre_clear(); 189 pre_save(posX, posY); 190 pre_save(posX - 1, posY); 191 192 map[posX - 1, posY] = 6; 193 map[posX, posY] = 0; 194 posX--; 195 } 196 } 197 else if (map[posX - 1, posY] == 3) // 箱子 198 { 199 if (posX - 2 >= 0) 200 { 201 if (map[posX - 2, posY] == 0) // 箱子上面是空地 202 { 203 if (map[posX, posY] == 6) 204 { 205 206 // pre_save(); 207 pre_clear(); 208 pre_save(posX, posY); 209 pre_save(posX - 1, posY); 210 pre_save(posX - 2, posY); 211 212 213 map[posX - 2, posY] = 3; 214 map[posX - 1, posY] = 2; 215 map[posX, posY] = 4; 216 posX--; 217 } 218 else if (map[posX, posY] == 2) 219 { 220 pre_clear(); 221 pre_save(posX, posY); 222 pre_save(posX - 1, posY); 223 pre_save(posX - 2, posY); 224 225 map[posX - 2, posY] = 3; 226 map[posX - 1, posY] = 2; 227 map[posX, posY] = 0; 228 posX--; 229 } 230 } 231 else if (map[posX - 2, posY] == 4) //箱子上面是成功点 232 { 233 if (map[posX, posY] == 6) 234 { 235 pre_clear(); 236 pre_save(posX, posY); 237 pre_save(posX - 1, posY); 238 pre_save(posX - 2, posY); 239 240 map[posX - 2, posY] = 7; 241 map[posX - 1, posY] = 2; 242 map[posX, posY] = 4; 243 posX--; 244 } 245 else if (map[posX, posY] == 2) 246 { 247 pre_clear(); 248 pre_save(posX, posY); 249 pre_save(posX - 1, posY); 250 pre_save(posX - 2, posY); 251 252 map[posX - 2, posY] = 7; 253 map[posX - 1, posY] = 2; 254 map[posX, posY] = 0; 255 posX--; 256 } 257 258 } 259 } 260 } 261 else if (map[posX - 1, posY] == 7) // 箱子+成功点 262 { 263 if (posX - 2 >= 0) 264 { 265 if (map[posX - 2, posY] == 0) // 箱子上面是空地 266 { 267 if (map[posX, posY] == 6) 268 { 269 pre_clear(); 270 pre_save(posX, posY); 271 pre_save(posX - 1, posY); 272 pre_save(posX - 2, posY); 273 274 map[posX - 2, posY] = 3; 275 map[posX - 1, posY] = 6; 276 map[posX, posY] = 4; 277 posX--; 278 } 279 else if (map[posX, posY] == 2) 280 { 281 pre_clear(); 282 pre_save(posX, posY); 283 pre_save(posX - 1, posY); 284 pre_save(posX - 2, posY); 285 286 map[posX - 2, posY] = 3; 287 map[posX - 1, posY] = 6; 288 map[posX, posY] = 0; 289 posX--; 290 } 291 } 292 else if (map[posX - 2, posY] == 4) //箱子上面是成功点 293 { 294 if (map[posX, posY] == 6) 295 { 296 pre_clear(); 297 pre_save(posX, posY); 298 pre_save(posX - 1, posY); 299 pre_save(posX - 2, posY); 300 301 map[posX - 2, posY] = 7; 302 map[posX - 1, posY] = 6; 303 map[posX, posY] = 4; 304 posX--; 305 } 306 else if (map[posX, posY] == 2) 307 { 308 pre_clear(); 309 pre_save(posX, posY); 310 pre_save(posX - 1, posY); 311 pre_save(posX - 2, posY); 312 313 map[posX - 2, posY] = 7; 314 map[posX - 1, posY] = 6; 315 map[posX, posY] = 0; 316 posX--; 317 } 318 319 } 320 } 321 } 322 } 323 } 324 325 //向下移动 326 static void moveDown() 327 { 328 if (posX + 1 < 10) 329 { 330 if (map[posX + 1, posY] == 0) // 空地 331 { 332 if (map[posX, posY] == 6) 333 { 334 pre_clear(); 335 pre_save(posX, posY); 336 pre_save(posX + 1, posY); 337 338 map[posX, posY] = 4; 339 map[posX + 1, posY] = 2; 340 posX++; 341 } 342 else if (map[posX, posY] == 2) 343 { 344 pre_clear(); 345 pre_save(posX, posY); 346 pre_save(posX + 1, posY); 347 348 map[posX, posY] = 0; 349 map[posX + 1, posY] = 2; 350 posX++; 351 } 352 } 353 else if (map[posX + 1, posY] == 4) //成功点 354 { 355 356 if (map[posX, posY] == 6) 357 { 358 pre_clear(); 359 pre_save(posX, posY); 360 pre_save(posX + 1, posY); 361 362 map[posX + 1, posY] = 6; 363 map[posX, posY] = 4; 364 posX++; 365 } 366 else if (map[posX, posY] == 2) 367 { 368 pre_clear(); 369 pre_save(posX, posY); 370 pre_save(posX + 1, posY); 371 372 map[posX + 1, posY] = 6; 373 map[posX, posY] = 0; 374 posX++; 375 } 376 } 377 else if (map[posX + 1, posY] == 3) // 箱子 378 { 379 if (posX + 2 < 10) 380 { 381 if (map[posX + 2, posY] == 0) // 箱子上面是空地 382 { 383 if (map[posX, posY] == 6) 384 { 385 pre_clear(); 386 pre_save(posX, posY); 387 pre_save(posX + 1, posY); 388 pre_save(posX + 2, posY); 389 390 391 map[posX + 2, posY] = 3; 392 map[posX + 1, posY] = 2; 393 map[posX, posY] = 4; 394 posX++; 395 } 396 else if (map[posX, posY] == 2) 397 { 398 pre_clear(); 399 pre_save(posX, posY); 400 pre_save(posX + 1, posY); 401 pre_save(posX + 2, posY); 402 403 map[posX + 2, posY] = 3; 404 map[posX + 1, posY] = 2; 405 map[posX, posY] = 0; 406 posX++; 407 } 408 } 409 else if (map[posX + 2, posY] == 4) //箱子上面是成功点 410 { 411 if (map[posX, posY] == 6) 412 { 413 pre_clear(); 414 pre_save(posX, posY); 415 pre_save(posX + 1, posY); 416 pre_save(posX + 2, posY); 417 418 map[posX + 2, posY] = 7; 419 map[posX + 1, posY] = 2; 420 map[posX, posY] = 4; 421 posX++; 422 } 423 else if (map[posX, posY] == 2) 424 { 425 pre_clear(); 426 pre_save(posX, posY); 427 pre_save(posX + 1, posY); 428 pre_save(posX + 2, posY); 429 430 map[posX + 2, posY] = 7; 431 map[posX + 1, posY] = 2; 432 map[posX, posY] = 0; 433 posX++; 434 } 435 436 } 437 } 438 } 439 else if (map[posX + 1, posY] == 7) // 箱子+成功点 440 { 441 if (posX + 2 < 10) 442 { 443 if (map[posX + 2, posY] == 0) // 箱子上面是空地 444 { 445 if (map[posX, posY] == 6) 446 { 447 pre_clear(); 448 pre_save(posX, posY); 449 pre_save(posX + 1, posY); 450 pre_save(posX + 2, posY); 451 452 map[posX + 2, posY] = 3; 453 map[posX + 1, posY] = 6; 454 map[posX, posY] = 4; 455 posX++; 456 } 457 else if (map[posX, posY] == 2) 458 { 459 pre_clear(); 460 pre_save(posX, posY); 461 pre_save(posX + 1, posY); 462 pre_save(posX + 2, posY); 463 464 map[posX + 2, posY] = 3; 465 map[posX + 1, posY] = 6; 466 map[posX, posY] = 0; 467 posX++; 468 } 469 } 470 else if (map[posX + 2, posY] == 4) //箱子上面是成功点 471 { 472 if (map[posX, posY] == 6) 473 { 474 pre_clear(); 475 pre_save(posX, posY); 476 pre_save(posX + 1, posY); 477 pre_save(posX + 2, posY); 478 479 map[posX + 2, posY] = 7; 480 map[posX + 1, posY] = 6; 481 map[posX, posY] = 4; 482 posX++; 483 } 484 else if (map[posX, posY] == 2) 485 { 486 pre_clear(); 487 pre_save(posX, posY); 488 pre_save(posX + 1, posY); 489 pre_save(posX + 2, posY); 490 491 map[posX + 2, posY] = 7; 492 map[posX + 1, posY] = 6; 493 map[posX, posY] = 0; 494 posX++; 495 } 496 497 } 498 } 499 } 500 } 501 } 502 503 static void moveLeft() 504 { 505 if (posY - 1 >= 0) 506 { 507 if (map[posX , posY-1] == 0) // 空地 508 { 509 if (map[posX, posY] == 6) 510 { 511 pre_clear(); 512 pre_save(posX, posY); 513 pre_save(posX, posY-1); 514 515 map[posX, posY] = 4; 516 map[posX, posY-1] = 2; 517 posY--; 518 } 519 else if (map[posX, posY] == 2) 520 { 521 pre_clear(); 522 pre_save(posX, posY); 523 pre_save(posX, posY - 1); 524 525 map[posX, posY] = 0; 526 map[posX, posY-1] = 2; 527 posY--; 528 } 529 } 530 else if (map[posX, posY-1] == 4) //成功点 531 { 532 533 if (map[posX, posY] == 6) 534 { 535 pre_clear(); 536 pre_save(posX, posY); 537 pre_save(posX, posY - 1); 538 539 map[posX, posY - 1] = 6; 540 map[posX, posY] = 4; 541 posY--; 542 } 543 else if (map[posX, posY] == 2) 544 { 545 pre_clear(); 546 pre_save(posX, posY); 547 pre_save(posX, posY - 1); 548 549 map[posX, posY - 1] = 6; 550 map[posX, posY] = 0; 551 posY--; 552 } 553 } 554 else if (map[posX, posY-1] == 3) // 箱子 555 { 556 if (posY - 2 >= 0) 557 { 558 if (map[posX, posY-2] == 0) // 箱子上面是空地 559 { 560 if (map[posX, posY] == 6) 561 { 562 pre_clear(); 563 pre_save(posX, posY); 564 pre_save(posX, posY - 1); 565 pre_save(posX, posY - 2); 566 567 map[posX , posY-2] = 3; 568 map[posX, posY-1] = 2; 569 map[posX, posY] = 4; 570 posY--; 571 } 572 else if (map[posX, posY] == 2) 573 { 574 pre_clear(); 575 pre_save(posX, posY); 576 pre_save(posX, posY - 1); 577 pre_save(posX, posY - 2); 578 579 map[posX , posY-2] = 3; 580 map[posX , posY-1] = 2; 581 map[posX, posY] = 0; 582 posY--; 583 } 584 } 585 else if (map[posX , posY-2] == 4) //箱子上面是成功点 586 { 587 if (map[posX, posY] == 6) 588 { 589 pre_clear(); 590 pre_save(posX, posY); 591 pre_save(posX, posY - 1); 592 pre_save(posX, posY - 2); 593 594 map[posX, posY-2] = 7; 595 map[posX, posY-1] = 2; 596 map[posX, posY] = 4; 597 posY--; 598 } 599 else if (map[posX, posY] == 2) 600 { 601 pre_clear(); 602 pre_save(posX, posY); 603 pre_save(posX, posY - 1); 604 pre_save(posX, posY - 2); 605 606 map[posX , posY-2] = 7; 607 map[posX , posY-1] = 2; 608 map[posX, posY] = 0; 609 posY--; 610 } 611 612 } 613 } 614 } 615 else if (map[posX, posY-1] == 7) // 箱子+成功点 616 { 617 if (posY - 2 >= 0) 618 { 619 if (map[posX, posY-2] == 0) // 箱子上面是空地 620 { 621 if (map[posX, posY] == 6) 622 { 623 pre_clear(); 624 pre_save(posX, posY); 625 pre_save(posX, posY - 1); 626 pre_save(posX, posY - 2); 627 628 map[posX, posY-2] = 3; 629 map[posX, posY-1] = 6; 630 map[posX, posY] = 4; 631 posY--; 632 } 633 else if (map[posX, posY] == 2) 634 { 635 pre_clear(); 636 pre_save(posX, posY); 637 pre_save(posX, posY - 1); 638 pre_save(posX, posY - 2); 639 640 map[posX, posY-2] = 3; 641 map[posX, posY-1] = 6; 642 map[posX, posY] = 0; 643 posY--; 644 } 645 } 646 else if (map[posX, posY-2] == 4) //箱子上面是成功点 647 { 648 if (map[posX, posY] == 6) 649 { 650 pre_clear(); 651 pre_save(posX, posY); 652 pre_save(posX, posY - 1); 653 pre_save(posX, posY - 2); 654 655 map[posX, posY-2] = 7; 656 map[posX, posY-1] = 6; 657 map[posX, posY] = 4; 658 posY--; 659 } 660 else if (map[posX, posY] == 2) 661 { 662 pre_clear(); 663 pre_save(posX, posY); 664 pre_save(posX, posY - 1); 665 pre_save(posX, posY - 2); 666 667 map[posX, posY-2] = 7; 668 map[posX, posY-1] = 6; 669 map[posX, posY] = 0; 670 posY--; 671 } 672 673 } 674 } 675 } 676 } 677 } 678 679 static void moveRight() 680 { 681 if (posY + 1 < 10) 682 { 683 if (map[posX, posY + 1] == 0) // 空地 684 { 685 if (map[posX, posY] == 6) 686 { 687 pre_clear(); 688 pre_save(posX, posY); 689 pre_save(posX, posY + 1); 690 691 map[posX, posY] = 4; 692 map[posX, posY + 1] = 2; 693 posY++; 694 } 695 else if (map[posX, posY] == 2) 696 { 697 pre_clear(); 698 pre_save(posX, posY); 699 pre_save(posX, posY + 1); 700 701 map[posX, posY] = 0; 702 map[posX, posY + 1] = 2; 703 posY++; 704 } 705 } 706 else if (map[posX, posY + 1] == 4) //成功点 707 { 708 709 if (map[posX, posY] == 6) 710 { 711 pre_clear(); 712 pre_save(posX, posY); 713 pre_save(posX, posY + 1); 714 715 map[posX, posY + 1] = 6; 716 map[posX, posY] = 4; 717 posY++; 718 } 719 else if (map[posX, posY] == 2) 720 { 721 pre_clear(); 722 pre_save(posX, posY); 723 pre_save(posX, posY + 1); 724 725 map[posX, posY + 1] = 6; 726 map[posX, posY] = 0; 727 posY++; 728 } 729 } 730 else if (map[posX, posY + 1] == 3) // 箱子 731 { 732 if (posY + 2 < 10) 733 { 734 if (map[posX, posY + 2] == 0) // 箱子上面是空地 735 { 736 if (map[posX, posY] == 6) 737 { 738 pre_clear(); 739 pre_save(posX, posY); 740 pre_save(posX, posY + 1); 741 pre_save(posX, posY + 2); 742 743 map[posX, posY + 2] = 3; 744 map[posX, posY + 1] = 2; 745 map[posX, posY] = 4; 746 posY++; 747 } 748 else if (map[posX, posY] == 2) 749 { 750 pre_clear(); 751 pre_save(posX, posY); 752 pre_save(posX, posY + 1); 753 pre_save(posX, posY + 2); 754 755 map[posX, posY + 2] = 3; 756 map[posX, posY + 1] = 2; 757 map[posX, posY] = 0; 758 posY++; 759 } 760 } 761 else if (map[posX, posY + 2] == 4) //箱子上面是成功点 762 { 763 if (map[posX, posY] == 6) 764 { 765 pre_clear(); 766 pre_save(posX, posY); 767 pre_save(posX, posY + 1); 768 pre_save(posX, posY + 2); 769 770 map[posX, posY + 2] = 7; 771 map[posX, posY + 1] = 2; 772 map[posX, posY] = 4; 773 posY++; 774 } 775 else if (map[posX, posY] == 2) 776 { 777 pre_clear(); 778 pre_save(posX, posY); 779 pre_save(posX, posY + 1); 780 pre_save(posX, posY + 2); 781 782 map[posX, posY + 2] = 7; 783 map[posX, posY + 1] = 2; 784 map[posX, posY] = 0; 785 posY++; 786 } 787 788 } 789 } 790 } 791 else if (map[posX, posY + 1] == 7) // 箱子+成功点 792 { 793 if (posY + 2 < 10) 794 { 795 if (map[posX, posY + 2] == 0) // 箱子上面是空地 796 { 797 if (map[posX, posY] == 6) 798 { 799 pre_clear(); 800 pre_save(posX, posY); 801 pre_save(posX, posY + 1); 802 pre_save(posX, posY + 2); 803 804 map[posX, posY + 2] = 3; 805 map[posX, posY + 1] = 6; 806 map[posX, posY] = 4; 807 posY++; 808 } 809 else if (map[posX, posY] == 2) 810 { 811 pre_clear(); 812 pre_save(posX, posY); 813 pre_save(posX, posY + 1); 814 pre_save(posX, posY + 2); 815 816 map[posX, posY + 2] = 3; 817 map[posX, posY + 1] = 6; 818 map[posX, posY] = 0; 819 posY++; 820 } 821 } 822 else if (map[posX, posY + 2] == 4) //箱子上面是成功点 823 { 824 if (map[posX, posY] == 6) 825 { 826 pre_clear(); 827 pre_save(posX, posY); 828 pre_save(posX, posY + 1); 829 pre_save(posX, posY + 2); 830 831 map[posX, posY + 2] = 7; 832 map[posX, posY + 1] = 6; 833 map[posX, posY] = 4; 834 posY++; 835 } 836 else if (map[posX, posY] == 2) 837 { 838 pre_clear(); 839 pre_save(posX, posY); 840 pre_save(posX, posY + 1); 841 pre_save(posX, posY + 2); 842 843 map[posX, posY + 2] = 7; 844 map[posX, posY + 1] = 6; 845 map[posX, posY] = 0; 846 posY++; 847 } 848 849 } 850 } 851 } 852 } 853 } 854 855 static bool move() 856 { 857 var info = Console.ReadKey(); 858 if (info.Key == ConsoleKey.UpArrow||info.Key == ConsoleKey.W) 859 { 860 cnt++; 861 moveUp(); 862 863 } 864 865 else if (info.Key == ConsoleKey.DownArrow||info.Key == ConsoleKey.S) 866 { 867 cnt++; 868 moveDown(); 869 } 870 871 else if (info.Key == ConsoleKey.LeftArrow || info.Key == ConsoleKey.A) 872 { 873 cnt++; 874 moveLeft(); 875 } 876 877 else if (info.Key == ConsoleKey.RightArrow || info.Key == ConsoleKey.D) 878 { 879 cnt++; 880 moveRight(); 881 } 882 else if (info.Key == ConsoleKey.Z) 883 { 884 if (list_cnt > 0) 885 { 886 cnt--; 887 for (int i = 0; i < list_cnt; i++) 888 { 889 int tx = list[i].posX; 890 int ty = list[i].posY; 891 int tmax = list[i].mapx; 892 893 map[tx, ty] = tmax; 894 895 } 896 posX = list[0].posX; 897 posY = list[0].posY; 898 pre_clear(); 899 } 900 901 902 } 903 return victory(); 904 } 905 906 static void Main(string[] args) 907 { 908 // System.Console.WriteLine("Hello"); 909 for (int i = 0; i < 1; i++) 910 { 911 init(); 912 while (true) 913 { 914 915 draw(); 916 if (move()==true) 917 { 918 break; 919 } 920 } 921 Console.Clear(); 922 923 Console.WriteLine("恭喜!"); 924 Console.ReadKey(); 925 } 926 927 } 928 } 929 }
基本上整个推箱子的逻辑部分就已经完成了,现在只要在Unity下进行实例化就可以了。
Source Code:
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.UI; 4 5 struct State 6 { 7 public int posX, posY, mapx; 8 } 9 public class Map : MonoBehaviour 10 { 11 12 public GameObject wallPre; 13 public GameObject Box; 14 public GameObject Point; 15 public GameObject EndPoint; 16 public GameObject man; 17 18 int posX, posY; 19 20 State[] list = new State[5]; 21 int list_cnt = 0; 22 //箱子个数 23 int cube_cnt; 24 int _cube_cnt; 25 //步数 26 int cnt; 27 28 29 void pre_clear() 30 { 31 list_cnt = 0; 32 } 33 void pre_save(int POSX, int POSY) 34 { 35 list[list_cnt].posX = POSX; 36 list[list_cnt].posY = POSY; 37 list[list_cnt].mapx = map[POSX, POSY]; 38 list_cnt++; 39 } 40 bool victory() 41 { 42 int flag_cube_cnt = 0; 43 for (int i = 0; i < 10; i++) 44 { 45 for (int j = 0; j < 10; j++) 46 { 47 if (map[i, j] == 7) 48 flag_cube_cnt++; 49 } 50 } 51 return flag_cube_cnt == cube_cnt; 52 } 53 54 55 56 57 static int[,] map = new int[10, 10] 58 { 59 {1,1,1,1,1,1,1,1,1,1}, 60 {1,4,0,0,0,0,0,0,0,1}, 61 {1,0,0,0,0,0,0,4,0,1}, 62 {1,0,3,0,0,4,0,0,0,1}, 63 {1,0,0,0,0,3,0,0,0,1}, 64 {1,0,0,2,0,0,0,0,0,1}, 65 {1,4,0,0,0,3,0,0,0,1}, 66 {1,0,0,3,0,0,0,0,0,1}, 67 {1,0,0,0,0,0,0,0,0,1}, 68 {1,1,1,1,1,1,1,1,1,1}, 69 }; 70 static int[,] map_1 = new int[10, 10] { 71 {1,1,1,1,1,1,1,1,1,1}, 72 {1,4,0,0,0,0,0,0,0,1}, 73 {1,0,0,0,0,0,0,4,0,1}, 74 {1,0,3,0,0,4,0,0,0,1}, 75 {1,0,0,0,0,3,0,0,0,1}, 76 {1,0,0,2,0,0,0,0,0,1}, 77 {1,4,0,0,0,3,0,0,0,1}, 78 {1,0,0,3,0,0,0,0,0,1}, 79 {1,0,0,0,0,0,0,0,0,1}, 80 {1,1,1,1,1,1,1,1,1,1}, 81 }; 82 83 static int[,] map_2 = new int[10, 10] { 84 {1,1,1,1,1,1,1,1,1,1}, 85 {1,4,0,0,0,0,0,0,0,1}, 86 {1,4,0,0,0,0,0,4,0,1}, 87 {1,0,3,0,0,4,0,0,0,1}, 88 {1,0,0,3,0,3,0,0,0,1}, 89 {1,0,0,0,0,0,0,0,0,1}, 90 {1,4,0,0,0,3,0,0,0,1}, 91 {1,0,0,3,0,2,0,0,0,1}, 92 {1,0,0,0,0,0,0,0,0,1}, 93 {1,1,1,1,1,1,1,1,1,1}, 94 }; 95 96 GameObject x; 97 // Use this for initialization 98 void Start() 99 { 100 x = GameObject.Find("victory_UI"); 101 x.SetActive(false); 102 init(); 103 draw(); 104 } 105 // Update is called once per frame 106 void Update() 107 { 108 draw(); 109 if (move()) 110 { 111 print("xxxxxxxxxxxxxxxxxxxxx\n"); 112 113 x.SetActive(true); 114 115 //init(); 116 // draw(); 117 } 118 } 119 120 Vector3 getPoint(int x, float y, int z) 121 { 122 return new Vector3(x - 5.5f, y, z - 5.5f); 123 } 124 125 //关卡 126 static int level; 127 void init() 128 { 129 cube_cnt = 0; 130 cnt = 0; 131 132 GameObject x = GameObject.Find("level"); 133 string tmp; 134 tmp = "第" + (level+1).ToString() + "关"; 135 x.GetComponent<Text>().text = tmp; 136 137 if (level == 0) 138 { 139 for (int i = 0; i < 10; i++) 140 { 141 for (int j = 0; j < 10; j++) 142 { 143 if (map[i, j] == 3) 144 { 145 cube_cnt++; 146 } 147 if (map[i, j] == 2) 148 { 149 posX = i; 150 posY = j; 151 pre_save(i, j); 152 } 153 } 154 } 155 } 156 else if (level >= 1) 157 { 158 map = new int[10, 10] { 159 {1,1,1,1,1,1,1,1,1,1}, 160 {1,4,0,0,0,0,0,0,0,1}, 161 {1,4,0,0,0,0,0,4,0,1}, 162 {1,0,3,0,0,4,0,0,0,1}, 163 {1,0,0,3,0,3,0,0,0,1}, 164 {1,0,0,0,0,0,0,0,0,1}, 165 {1,4,0,0,0,3,0,0,0,1}, 166 {1,0,0,3,0,2,0,0,0,1}, 167 {1,0,0,0,0,0,0,0,0,1}, 168 {1,1,1,1,1,1,1,1,1,1}, 169 }; 170 for (int i = 0; i < 10; i++) 171 { 172 for (int j = 0; j < 10; j++) 173 { 174 if (map[i, j] == 3) 175 { 176 cube_cnt++; 177 } 178 if (map[i, j] == 2) 179 { 180 posX = i; 181 posY = j; 182 pre_save(i, j); 183 } 184 } 185 } 186 } 187 level++; 188 Game_UI_Manage.Level++; 189 } 190 191 192 void draw() 193 { 194 destroyMap(); 195 for (int i = 0; i < 10; i++) 196 { 197 for (int j = 0; j < 10; j++) 198 { 199 if (map[i, j] == 1) //# 200 { 201 Instantiate(wallPre, getPoint(i, 0.5f, j), Quaternion.identity); 202 //System.Console.Write("■"); 203 } 204 else if (map[i, j] == 0) 205 { 206 207 } 208 209 //System.Console.Write(" "); 210 else if (map[i, j] == 2) //pepole 211 { 212 Instantiate(man, getPoint(i, 0.5f, j), Quaternion.identity); 213 //System.Console.Write("♀"); 214 } 215 else if (map[i, j] == 3) 216 { 217 Instantiate(Box, getPoint(i, 0.5f, j), Quaternion.identity); 218 //System.Console.Write("△"); 219 } 220 else if (map[i, j] == 4) 221 { 222 Instantiate(Point, getPoint(i, 0.5f, j), Quaternion.identity); 223 //System.Console.Write("☆");f 224 } 225 else if (map[i, j] == 6) // 人 + 成功点 226 { 227 Instantiate(man, getPoint(i, 0.5f, j), Quaternion.identity); 228 } 229 else if (map[i, j] == 7) 230 { 231 Instantiate(EndPoint, getPoint(i, 0.5f, j), Quaternion.identity); 232 233 } 234 235 } 236 237 } 238 239 } 240 241 242 void destroyMap() 243 { 244 GameObject[] gos = GameObject.FindGameObjectsWithTag("brick"); 245 foreach (var go in gos) 246 { 247 Destroy(go); 248 } 249 } 250 251 252 253 254 bool move() 255 { 256 //上 257 if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A)) 258 { 259 cnt++; 260 moveUp(); 261 } 262 //下 263 else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D)) 264 { 265 cnt++; 266 moveDown(); 267 268 } 269 //左 270 else if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S)) 271 { 272 cnt++; 273 moveLeft(); 274 } 275 //右 276 else if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W)) 277 { 278 cnt++; 279 moveRight(); 280 } 281 else if (Input.GetKeyDown(KeyCode.Z)) 282 { 283 if (list_cnt > 0) 284 { 285 cnt--; 286 for (int i = 0; i < list_cnt; i++) 287 { 288 int tx = list[i].posX; 289 int ty = list[i].posY; 290 int tmax = list[i].mapx; 291 292 map[tx, ty] = tmax; 293 294 } 295 posX = list[0].posX; 296 posY = list[0].posY; 297 pre_clear(); 298 } 299 } 300 return victory(); 301 } 302 303 304 305 306 void moveUp() 307 { 308 if (posX - 1 >= 0) 309 { 310 if (map[posX - 1, posY] == 0) // 空地 311 { 312 if (map[posX, posY] == 6) 313 { 314 pre_clear(); 315 pre_save(posX, posY); 316 pre_save(posX - 1, posY); 317 318 319 map[posX, posY] = 4; 320 map[posX - 1, posY] = 2; 321 posX--; 322 //now_save(); 323 } 324 else if (map[posX, posY] == 2) 325 { 326 pre_clear(); 327 pre_save(posX, posY); 328 pre_save(posX - 1, posY); 329 map[posX, posY] = 0; 330 map[posX - 1, posY] = 2; 331 posX--; 332 } 333 } 334 else if (map[posX - 1, posY] == 4) //成功点 335 { 336 337 if (map[posX, posY] == 6) 338 { 339 pre_clear(); 340 pre_save(posX, posY); 341 pre_save(posX - 1, posY); 342 map[posX - 1, posY] = 6; 343 map[posX, posY] = 4; 344 posX--; 345 } 346 else if (map[posX, posY] == 2) 347 { 348 pre_clear(); 349 pre_save(posX, posY); 350 pre_save(posX - 1, posY); 351 352 map[posX - 1, posY] = 6; 353 map[posX, posY] = 0; 354 posX--; 355 } 356 } 357 else if (map[posX - 1, posY] == 3) // 箱子 358 { 359 if (posX - 2 >= 0) 360 { 361 if (map[posX - 2, posY] == 0) // 箱子上面是空地 362 { 363 if (map[posX, posY] == 6) 364 { 365 366 // pre_save(); 367 pre_clear(); 368 pre_save(posX, posY); 369 pre_save(posX - 1, posY); 370 pre_save(posX - 2, posY); 371 372 373 map[posX - 2, posY] = 3; 374 map[posX - 1, posY] = 2; 375 map[posX, posY] = 4; 376 posX--; 377 } 378 else if (map[posX, posY] == 2) 379 { 380 pre_clear(); 381 pre_save(posX, posY); 382 pre_save(posX - 1, posY); 383 pre_save(posX - 2, posY); 384 385 map[posX - 2, posY] = 3; 386 map[posX - 1, posY] = 2; 387 map[posX, posY] = 0; 388 posX--; 389 } 390 } 391 else if (map[posX - 2, posY] == 4) //箱子上面是成功点 392 { 393 if (map[posX, posY] == 6) 394 { 395 pre_clear(); 396 pre_save(posX, posY); 397 pre_save(posX - 1, posY); 398 pre_save(posX - 2, posY); 399 400 map[posX - 2, posY] = 7; 401 map[posX - 1, posY] = 2; 402 map[posX, posY] = 4; 403 posX--; 404 } 405 else if (map[posX, posY] == 2) 406 { 407 pre_clear(); 408 pre_save(posX, posY); 409 pre_save(posX - 1, posY); 410 pre_save(posX - 2, posY); 411 412 map[posX - 2, posY] = 7; 413 map[posX - 1, posY] = 2; 414 map[posX, posY] = 0; 415 posX--; 416 } 417 418 } 419 } 420 } 421 else if (map[posX - 1, posY] == 7) // 箱子+成功点 422 { 423 if (posX - 2 >= 0) 424 { 425 if (map[posX - 2, posY] == 0) // 箱子上面是空地 426 { 427 if (map[posX, posY] == 6) 428 { 429 pre_clear(); 430 pre_save(posX, posY); 431 pre_save(posX - 1, posY); 432 pre_save(posX - 2, posY); 433 434 map[posX - 2, posY] = 3; 435 map[posX - 1, posY] = 6; 436 map[posX, posY] = 4; 437 posX--; 438 } 439 else if (map[posX, posY] == 2) 440 { 441 pre_clear(); 442 pre_save(posX, posY); 443 pre_save(posX - 1, posY); 444 pre_save(posX - 2, posY); 445 446 map[posX - 2, posY] = 3; 447 map[posX - 1, posY] = 6; 448 map[posX, posY] = 0; 449 posX--; 450 } 451 } 452 else if (map[posX - 2, posY] == 4) //箱子上面是成功点 453 { 454 if (map[posX, posY] == 6) 455 { 456 pre_clear(); 457 pre_save(posX, posY); 458 pre_save(posX - 1, posY); 459 pre_save(posX - 2, posY); 460 461 map[posX - 2, posY] = 7; 462 map[posX - 1, posY] = 6; 463 map[posX, posY] = 4; 464 posX--; 465 } 466 else if (map[posX, posY] == 2) 467 { 468 pre_clear(); 469 pre_save(posX, posY); 470 pre_save(posX - 1, posY); 471 pre_save(posX - 2, posY); 472 473 map[posX - 2, posY] = 7; 474 map[posX - 1, posY] = 6; 475 map[posX, posY] = 0; 476 posX--; 477 } 478 479 } 480 } 481 } 482 } 483 } 484 485 486 //向下移动 487 void moveDown() 488 { 489 if (posX + 1 < 10) 490 { 491 if (map[posX + 1, posY] == 0) // 空地 492 { 493 if (map[posX, posY] == 6) 494 { 495 pre_clear(); 496 pre_save(posX, posY); 497 pre_save(posX + 1, posY); 498 499 map[posX, posY] = 4; 500 map[posX + 1, posY] = 2; 501 posX++; 502 } 503 else if (map[posX, posY] == 2) 504 { 505 pre_clear(); 506 pre_save(posX, posY); 507 pre_save(posX + 1, posY); 508 509 map[posX, posY] = 0; 510 map[posX + 1, posY] = 2; 511 posX++; 512 } 513 } 514 else if (map[posX + 1, posY] == 4) //成功点 515 { 516 517 if (map[posX, posY] == 6) 518 { 519 pre_clear(); 520 pre_save(posX, posY); 521 pre_save(posX + 1, posY); 522 523 map[posX + 1, posY] = 6; 524 map[posX, posY] = 4; 525 posX++; 526 } 527 else if (map[posX, posY] == 2) 528 { 529 pre_clear(); 530 pre_save(posX, posY); 531 pre_save(posX + 1, posY); 532 533 map[posX + 1, posY] = 6; 534 map[posX, posY] = 0; 535 posX++; 536 } 537 } 538 else if (map[posX + 1, posY] == 3) // 箱子 539 { 540 if (posX + 2 < 10) 541 { 542 if (map[posX + 2, posY] == 0) // 箱子上面是空地 543 { 544 if (map[posX, posY] == 6) 545 { 546 pre_clear(); 547 pre_save(posX, posY); 548 pre_save(posX + 1, posY); 549 pre_save(posX + 2, posY); 550 551 552 map[posX + 2, posY] = 3; 553 map[posX + 1, posY] = 2; 554 map[posX, posY] = 4; 555 posX++; 556 } 557 else if (map[posX, posY] == 2) 558 { 559 pre_clear(); 560 pre_save(posX, posY); 561 pre_save(posX + 1, posY); 562 pre_save(posX + 2, posY); 563 564 map[posX + 2, posY] = 3; 565 map[posX + 1, posY] = 2; 566 map[posX, posY] = 0; 567 posX++; 568 } 569 } 570 else if (map[posX + 2, posY] == 4) //箱子上面是成功点 571 { 572 if (map[posX, posY] == 6) 573 { 574 pre_clear(); 575 pre_save(posX, posY); 576 pre_save(posX + 1, posY); 577 pre_save(posX + 2, posY); 578 579 map[posX + 2, posY] = 7; 580 map[posX + 1, posY] = 2; 581 map[posX, posY] = 4; 582 posX++; 583 } 584 else if (map[posX, posY] == 2) 585 { 586 pre_clear(); 587 pre_save(posX, posY); 588 pre_save(posX + 1, posY); 589 pre_save(posX + 2, posY); 590 591 map[posX + 2, posY] = 7; 592 map[posX + 1, posY] = 2; 593 map[posX, posY] = 0; 594 posX++; 595 } 596 597 } 598 } 599 } 600 else if (map[posX + 1, posY] == 7) // 箱子+成功点 601 { 602 if (posX + 2 < 10) 603 { 604 if (map[posX + 2, posY] == 0) // 箱子上面是空地 605 { 606 if (map[posX, posY] == 6) 607 { 608 pre_clear(); 609 pre_save(posX, posY); 610 pre_save(posX + 1, posY); 611 pre_save(posX + 2, posY); 612 613 map[posX + 2, posY] = 3; 614 map[posX + 1, posY] = 6; 615 map[posX, posY] = 4; 616 posX++; 617 } 618 else if (map[posX, posY] == 2) 619 { 620 pre_clear(); 621 pre_save(posX, posY); 622 pre_save(posX + 1, posY); 623 pre_save(posX + 2, posY); 624 625 map[posX + 2, posY] = 3; 626 map[posX + 1, posY] = 6; 627 map[posX, posY] = 0; 628 posX++; 629 } 630 } 631 else if (map[posX + 2, posY] == 4) //箱子上面是成功点 632 { 633 if (map[posX, posY] == 6) 634 { 635 pre_clear(); 636 pre_save(posX, posY); 637 pre_save(posX + 1, posY); 638 pre_save(posX + 2, posY); 639 640 map[posX + 2, posY] = 7; 641 map[posX + 1, posY] = 6; 642 map[posX, posY] = 4; 643 posX++; 644 } 645 else if (map[posX, posY] == 2) 646 { 647 pre_clear(); 648 pre_save(posX, posY); 649 pre_save(posX + 1, posY); 650 pre_save(posX + 2, posY); 651 652 map[posX + 2, posY] = 7; 653 map[posX + 1, posY] = 6; 654 map[posX, posY] = 0; 655 posX++; 656 } 657 658 } 659 } 660 } 661 } 662 } 663 664 void moveLeft() 665 { 666 if (posY - 1 >= 0) 667 { 668 if (map[posX, posY - 1] == 0) // 空地 669 { 670 if (map[posX, posY] == 6) 671 { 672 pre_clear(); 673 pre_save(posX, posY); 674 pre_save(posX, posY - 1); 675 676 map[posX, posY] = 4; 677 map[posX, posY - 1] = 2; 678 posY--; 679 } 680 else if (map[posX, posY] == 2) 681 { 682 pre_clear(); 683 pre_save(posX, posY); 684 pre_save(posX, posY - 1); 685 686 map[posX, posY] = 0; 687 map[posX, posY - 1] = 2; 688 posY--; 689 } 690 } 691 else if (map[posX, posY - 1] == 4) //成功点 692 { 693 694 if (map[posX, posY] == 6) 695 { 696 pre_clear(); 697 pre_save(posX, posY); 698 pre_save(posX, posY - 1); 699 700 map[posX, posY - 1] = 6; 701 map[posX, posY] = 4; 702 posY--; 703 } 704 else if (map[posX, posY] == 2) 705 { 706 pre_clear(); 707 pre_save(posX, posY); 708 pre_save(posX, posY - 1); 709 710 map[posX, posY - 1] = 6; 711 map[posX, posY] = 0; 712 posY--; 713 } 714 } 715 else if (map[posX, posY - 1] == 3) // 箱子 716 { 717 if (posY - 2 >= 0) 718 { 719 if (map[posX, posY - 2] == 0) // 箱子上面是空地 720 { 721 if (map[posX, posY] == 6) 722 { 723 pre_clear(); 724 pre_save(posX, posY); 725 pre_save(posX, posY - 1); 726 pre_save(posX, posY - 2); 727 728 map[posX, posY - 2] = 3; 729 map[posX, posY - 1] = 2; 730 map[posX, posY] = 4; 731 posY--; 732 } 733 else if (map[posX, posY] == 2) 734 { 735 pre_clear(); 736 pre_save(posX, posY); 737 pre_save(posX, posY - 1); 738 pre_save(posX, posY - 2); 739 740 map[posX, posY - 2] = 3; 741 map[posX, posY - 1] = 2; 742 map[posX, posY] = 0; 743 posY--; 744 } 745 } 746 else if (map[posX, posY - 2] == 4) //箱子上面是成功点 747 { 748 if (map[posX, posY] == 6) 749 { 750 pre_clear(); 751 pre_save(posX, posY); 752 pre_save(posX, posY - 1); 753 pre_save(posX, posY - 2); 754 755 map[posX, posY - 2] = 7; 756 map[posX, posY - 1] = 2; 757 map[posX, posY] = 4; 758 posY--; 759 } 760 else if (map[posX, posY] == 2) 761 { 762 pre_clear(); 763 pre_save(posX, posY); 764 pre_save(posX, posY - 1); 765 pre_save(posX, posY - 2); 766 767 map[posX, posY - 2] = 7; 768 map[posX, posY - 1] = 2; 769 map[posX, posY] = 0; 770 posY--; 771 } 772 773 } 774 } 775 } 776 else if (map[posX, posY - 1] == 7) // 箱子+成功点 777 { 778 if (posY - 2 >= 0) 779 { 780 if (map[posX, posY - 2] == 0) // 箱子上面是空地 781 { 782 if (map[posX, posY] == 6) 783 { 784 pre_clear(); 785 pre_save(posX, posY); 786 pre_save(posX, posY - 1); 787 pre_save(posX, posY - 2); 788 789 map[posX, posY - 2] = 3; 790 map[posX, posY - 1] = 6; 791 map[posX, posY] = 4; 792 posY--; 793 } 794 else if (map[posX, posY] == 2) 795 { 796 pre_clear(); 797 pre_save(posX, posY); 798 pre_save(posX, posY - 1); 799 pre_save(posX, posY - 2); 800 801 map[posX, posY - 2] = 3; 802 map[posX, posY - 1] = 6; 803 map[posX, posY] = 0; 804 posY--; 805 } 806 } 807 else if (map[posX, posY - 2] == 4) //箱子上面是成功点 808 { 809 if (map[posX, posY] == 6) 810 { 811 pre_clear(); 812 pre_save(posX, posY); 813 pre_save(posX, posY - 1); 814 pre_save(posX, posY - 2); 815 816 map[posX, posY - 2] = 7; 817 map[posX, posY - 1] = 6; 818 map[posX, posY] = 4; 819 posY--; 820 } 821 else if (map[posX, posY] == 2) 822 { 823 pre_clear(); 824 pre_save(posX, posY); 825 pre_save(posX, posY - 1); 826 pre_save(posX, posY - 2); 827 828 map[posX, posY - 2] = 7; 829 map[posX, posY - 1] = 6; 830 map[posX, posY] = 0; 831 posY--; 832 } 833 834 } 835 } 836 } 837 } 838 } 839 840 841 void moveRight() 842 { 843 if (posY + 1 < 10) 844 { 845 if (map[posX, posY + 1] == 0) // 空地 846 { 847 if (map[posX, posY] == 6) 848 { 849 pre_clear(); 850 pre_save(posX, posY); 851 pre_save(posX, posY + 1); 852 853 map[posX, posY] = 4; 854 map[posX, posY + 1] = 2; 855 posY++; 856 } 857 else if (map[posX, posY] == 2) 858 { 859 pre_clear(); 860 pre_save(posX, posY); 861 pre_save(posX, posY + 1); 862 863 map[posX, posY] = 0; 864 map[posX, posY + 1] = 2; 865 posY++; 866 } 867 } 868 else if (map[posX, posY + 1] == 4) //成功点 869 { 870 871 if (map[posX, posY] == 6) 872 { 873 pre_clear(); 874 pre_save(posX, posY); 875 pre_save(posX, posY + 1); 876 877 map[posX, posY + 1] = 6; 878 map[posX, posY] = 4; 879 posY++; 880 } 881 else if (map[posX, posY] == 2) 882 { 883 pre_clear(); 884 pre_save(posX, posY); 885 pre_save(posX, posY + 1); 886 887 map[posX, posY + 1] = 6; 888 map[posX, posY] = 0; 889 posY++; 890 } 891 } 892 else if (map[posX, posY + 1] == 3) // 箱子 893 { 894 if (posY + 2 < 10) 895 { 896 if (map[posX, posY + 2] == 0) // 箱子上面是空地 897 { 898 if (map[posX, posY] == 6) 899 { 900 pre_clear(); 901 pre_save(posX, posY); 902 pre_save(posX, posY + 1); 903 pre_save(posX, posY + 2); 904 905 map[posX, posY + 2] = 3; 906 map[posX, posY + 1] = 2; 907 map[posX, posY] = 4; 908 posY++; 909 } 910 else if (map[posX, posY] == 2) 911 { 912 pre_clear(); 913 pre_save(posX, posY); 914 pre_save(posX, posY + 1); 915 pre_save(posX, posY + 2); 916 917 map[posX, posY + 2] = 3; 918 map[posX, posY + 1] = 2; 919 map[posX, posY] = 0; 920 posY++; 921 } 922 } 923 else if (map[posX, posY + 2] == 4) //箱子上面是成功点 924 { 925 if (map[posX, posY] == 6) 926 { 927 pre_clear(); 928 pre_save(posX, posY); 929 pre_save(posX, posY + 1); 930 pre_save(posX, posY + 2); 931 932 map[posX, posY + 2] = 7; 933 map[posX, posY + 1] = 2; 934 map[posX, posY] = 4; 935 posY++; 936 } 937 else if (map[posX, posY] == 2) 938 { 939 pre_clear(); 940 pre_save(posX, posY); 941 pre_save(posX, posY + 1); 942 pre_save(posX, posY + 2); 943 944 map[posX, posY + 2] = 7; 945 map[posX, posY + 1] = 2; 946 map[posX, posY] = 0; 947 posY++; 948 } 949 950 } 951 } 952 } 953 else if (map[posX, posY + 1] == 7) // 箱子+成功点 954 { 955 if (posY + 2 < 10) 956 { 957 if (map[posX, posY + 2] == 0) // 箱子上面是空地 958 { 959 if (map[posX, posY] == 6) 960 { 961 pre_clear(); 962 pre_save(posX, posY); 963 pre_save(posX, posY + 1); 964 pre_save(posX, posY + 2); 965 966 map[posX, posY + 2] = 3; 967 map[posX, posY + 1] = 6; 968 map[posX, posY] = 4; 969 posY++; 970 } 971 else if (map[posX, posY] == 2) 972 { 973 pre_clear(); 974 pre_save(posX, posY); 975 pre_save(posX, posY + 1); 976 pre_save(posX, posY + 2); 977 978 map[posX, posY + 2] = 3; 979 map[posX, posY + 1] = 6; 980 map[posX, posY] = 0; 981 posY++; 982 } 983 } 984 else if (map[posX, posY + 2] == 4) //箱子上面是成功点 985 { 986 if (map[posX, posY] == 6) 987 { 988 pre_clear(); 989 pre_save(posX, posY); 990 pre_save(posX, posY + 1); 991 pre_save(posX, posY + 2); 992 993 map[posX, posY + 2] = 7; 994 map[posX, posY + 1] = 6; 995 map[posX, posY] = 4; 996 posY++; 997 } 998 else if (map[posX, posY] == 2) 999 { 1000 pre_clear(); 1001 pre_save(posX, posY); 1002 pre_save(posX, posY + 1); 1003 pre_save(posX, posY + 2); 1004 1005 map[posX, posY + 2] = 7; 1006 map[posX, posY + 1] = 6; 1007 map[posX, posY] = 0; 1008 posY++; 1009 } 1010 1011 } 1012 } 1013 } 1014 } 1015 } 1016 1017 1018 static public void Reset(int Level) 1019 { 1020 level = Level; 1021 if (Level == 0) 1022 { 1023 for (int i = 0; i < 10; i++) 1024 for (int j = 0; j < 10; j++) 1025 map[i,j] = map_1[i,j]; 1026 1027 } 1028 else if (Level >= 1) 1029 { 1030 for (int i = 0; i < 10; i++) 1031 for (int j = 0; j < 10; j++) 1032 map[i, j] = map_2[i, j]; 1033 1034 } 1035 1036 } 1037 1038 public static void set_level(int Level) 1039 { 1040 level = Level; 1041 if (Level == 0) 1042 { 1043 for (int i = 0; i < 10; i++) 1044 for (int j = 0; j < 10; j++) 1045 map[i, j] = map_1[i, j]; 1046 1047 } 1048 else if (Level >= 1) 1049 { 1050 for (int i = 0; i < 10; i++) 1051 for (int j = 0; j < 10; j++) 1052 map[i, j] = map_2[i, j]; 1053 1054 } 1055 } 1056 1057 1058 }
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.SceneManagement; 4 5 6 public class UI_Manage : MonoBehaviour { 7 8 // Use this for initialization 9 void Start () { 10 11 } 12 13 // Update is called once per frame 14 void Update () { 15 16 } 17 18 19 public void onStartBtnClick() 20 { 21 //跳转场景 22 23 SceneManager.LoadScene("Game"); 24 //退出游戏 25 //Application.Quit(); 26 //显示与隐藏 27 //游戏物体.setActive(true / false); 28 29 30 } 31 32 public void onEndBtnClick() 33 { 34 Application.Quit(); 35 System.Console.WriteLine("Exit!"); 36 } 37 38 }
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.SceneManagement; 4 5 6 public class Game_UI_Manage : MonoBehaviour { 7 8 // Use this for initialization 9 void Start () { 10 11 } 12 13 // Update is called once per frame 14 void Update () { 15 16 } 17 18 public void onHomeBtnClick() 19 { 20 Level = 0; 21 print(Level); 22 Map.set_level(Level); 23 SceneManager.LoadScene("Start"); 24 25 } 26 27 public static int Level; 28 public void onResetClick() 29 { 30 print(Level); 31 Map.set_level(--Level); 32 SceneManager.LoadScene("Game"); 33 } 34 35 36 public void victory_Home() 37 { 38 Level = 0; 39 Map.set_level(Level); 40 SceneManager.LoadScene("Start"); 41 } 42 43 public void victory_next() 44 { 45 SceneManager.LoadScene("Game"); 46 } 47 48 }
结构:
UI_Manage管理开始界面的所有UI
Map:控制绘图,和行走
Game_UI_Manage:控制游戏界面的所有UI