实例介绍
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; using System.Drawing.Drawing2D; using System.Net; using System.Net.Sockets; using System.Threading; namespace DTTest { public partial class Form1 : Form { private string localPath = Application.StartupPath; private Image deskImg; private CHIChess chi; private ArrayList chessList; private ChiDesk desk; private GraphicsPath oldPath; private GraphicsPath newPath; private bool isMove = false ; private ArrayList history; private int [][] curMap; private CHIChess eatedChess; private UdpClient client; private bool canMove = false ; public string remoteIP; public bool isOwner = true ; private Abouts bout; private IAsyncResult result; private ListenMsg lmsg; public Form1( string remoteip, bool isOwner) { InitializeComponent(); this .remoteIP = remoteip; this .isOwner = isOwner; deskImg=Image.FromFile(localPath "\\imgs\\desk.bmp" ); this .deskBox.Size = deskImg.Size; this .Size = new Size(deskImg.Width 9, deskImg.Height 60); this .MaximumSize = this .Size; this .MinimumSize = this .Size; desk = new ChiDesk(); chessList = new ArrayList(); #region //chi = new CHIChess(); //chi.location = new Point(68,79); //chi.ChiType = CHITypes.JU; //chi.curImg = this.getCHiChessImg(chi.ChiType); #endregion curMap = desk.GetMaps(); ; this .LoadChesses(curMap); client = new UdpClient( new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], 1234)); lmsg = new ListenMsg( this .listenMsg); this .result = lmsg.BeginInvoke( null , null ); } private void LoadChesses( int [][] map) { if (history != null ) history= null ; history = new ArrayList(); if (chessList != null ) chessList.Clear(); oldPath = null ; newPath = null ; for ( int row = 0; row < 10; row ) { for ( int col = 0; col < 9; col ) { if (map[row][col] != 0) { CHIChess chiche = new CHIChess(); chiche.ChiType = desk.GetTypeByID(map[row][col]); chiche.SetIndexs( new Indexs(col,row)); chiche.curImg = this .getCHiChessImg(chiche.ChiType); chiche.CreatePath(); chessList.Add(chiche); } } } this .canMove = this .isOwner; this .deskBox.Invalidate(); } private void deskBox_Paint( object sender,PaintEventArgs e) { e.Graphics.DrawImageUnscaled(deskImg, new Point(0,0)); foreach (CHIChess onechess in chessList) { if (onechess != chi) { onechess.CreateSelf(e.Graphics); //e.Graphics.DrawPath(Pens.Red, onechess.chiPath); } } if (newPath != null &&oldPath != null ) { e.Graphics.DrawPath( new Pen(Color.Red, 2), newPath); e.Graphics.DrawPath( new Pen(Color.Green,2), oldPath); } if (chi != null ) { chi.CreateSelf(e.Graphics); //e.Graphics.DrawPath(Pens.Red,chi.chiPath); } } public Image getCHiChessImg(CHITypes chitype) { Image chiImg = null ; if (isOwner == true ) //正方 { switch (chitype) { case CHITypes.JIANG: chiImg = Image.FromFile(localPath "\\imgs\\111.png" ); break ; case CHITypes.SHI: chiImg = Image.FromFile(localPath "\\imgs\\222.png" ); break ; case CHITypes.XIANG: chiImg = Image.FromFile(localPath "\\imgs\\333.png" ); break ; case CHITypes.MA: chiImg = Image.FromFile(localPath "\\imgs\\444.png" ); break ; case CHITypes.JU: chiImg = Image.FromFile(localPath "\\imgs\\555.png" ); break ; case CHITypes.PAO: chiImg = Image.FromFile(localPath "\\imgs\\666.png" ); break ; case CHITypes.ZU: chiImg = Image.FromFile(localPath "\\imgs\\777.png" ); break ; case CHITypes.JIANG2: chiImg = Image.FromFile(localPath "\\imgs\\1.png" ); break ; case CHITypes.SHI2: chiImg = Image.FromFile(localPath "\\imgs\\2.png" ); break ; case CHITypes.XIANG2: chiImg = Image.FromFile(localPath "\\imgs\\3.png" ); break ; case CHITypes.MA2: chiImg = Image.FromFile(localPath "\\imgs\\4.png" ); break ; case CHITypes.JU2: chiImg = Image.FromFile(localPath "\\imgs\\5.png" ); break ; case CHITypes.PAO2: chiImg = Image.FromFile(localPath "\\imgs\\6.png" ); break ; case CHITypes.ZU2: chiImg = Image.FromFile(localPath "\\imgs\\7.png" ); break ; } } else { switch (chitype) { case CHITypes.JIANG: chiImg = Image.FromFile(localPath "\\imgs\\1.png" ); break ; case CHITypes.SHI: chiImg = Image.FromFile(localPath "\\imgs\\2.png" ); break ; case CHITypes.XIANG: chiImg = Image.FromFile(localPath "\\imgs\\3.png" ); break ; case CHITypes.MA: chiImg = Image.FromFile(localPath "\\imgs\\4.png" ); break ; case CHITypes.JU: chiImg = Image.FromFile(localPath "\\imgs\\5.png" ); break ; case CHITypes.PAO: chiImg = Image.FromFile(localPath "\\imgs\\6.png" ); break ; case CHITypes.ZU: chiImg = Image.FromFile(localPath "\\imgs\\7.png" ); break ; case CHITypes.JIANG2: chiImg = Image.FromFile(localPath "\\imgs\\111.png" ); break ; case CHITypes.SHI2: chiImg = Image.FromFile(localPath "\\imgs\\222.png" ); break ; case CHITypes.XIANG2: chiImg = Image.FromFile(localPath "\\imgs\\333.png" ); break ; case CHITypes.MA2: chiImg = Image.FromFile(localPath "\\imgs\\444.png" ); break ; case CHITypes.JU2: chiImg = Image.FromFile(localPath "\\imgs\\555.png" ); break ; case CHITypes.PAO2: chiImg = Image.FromFile(localPath "\\imgs\\666.png" ); break ; case CHITypes.ZU2: chiImg = Image.FromFile(localPath "\\imgs\\777.png" ); break ; } } return chiImg; } private void deskBox_MouseDown( object sender,MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point curpoint = e.Location; findSel fs = new findSel( this .findSel); fs.Invoke(e.Location); this .deskBox.Invalidate(); } } private void findSel(Point e) { bool isfind = false ; foreach (CHIChess onechess in chessList) { if (onechess.chiPath.IsVisible(e)&&Convert.ToInt16(onechess.ChiType)>=8&&canMove== true ) { chi = onechess; isfind = true ; break ; } } } private void deskBox_MouseMove( object sender,MouseEventArgs e) { if (e.Button == MouseButtons.Left&&chi!= null ) { chi.location = e.Location; isMove = true ; this .deskBox.Invalidate(); } } private void deskBox_MouseUp( object sender,MouseEventArgs e) { this .MoveChess(e.Location); } private void MoveChess(Point p) { if (chi != null ) { bool isnew = this .findNewPath(p,curMap); chi.CreatePath(); if (isnew == true ) { newPath = (GraphicsPath)chi.chiPath.Clone(); this .SendMoveInfo(); } chi = null ; this .deskBox.Invalidate(); } } private bool findNewPath(Point p, int [][] map) { Indexs oldIndex = chi.MyIndex; bool findnew = false ; for ( int row = 0; row < 10; row ) { for ( int col = 0; col < 9; col ) { //if(map[row][col]==0) //{ GraphicsPath gpath = new GraphicsPath(); if (row < 5) { gpath.AddRectangle( new Rectangle(68 col* 39-20,38 row* 39-20,40,40)); } else { gpath.AddRectangle( new Rectangle(68 col* 39-20,43 row* 39-20,40,40)); } if (gpath.IsVisible(chi.location)) { if (checkCanPass(chi.MyIndex, new Indexs(col, row), chi.ChiType, curMap)) //能通过 { if (chi.ChiType != CHITypes.PAO2 && chi.ChiType != CHITypes.PAO) //吃子 { if (map[row][col]!=0&&!EatChess( new Indexs(col,row))) { break ; } } CHIChess chiclone1=chi.cloneSelf(); HistoryRecorder recoder = new HistoryRecorder(chiclone1,chiclone1.MyIndex, new Indexs(col, row)); if (eatedChess != null ) { recoder.eatCHI = this .eatedChess.cloneSelf(); } eatedChess = null ; history.Add(recoder); oldPath =(GraphicsPath)chi.chiPath.Clone(); map[chi.MyIndex.Y][chi.MyIndex.X] = 0; chi.SetIndexs( new Indexs(col, row)); map[row][col] = desk.GetIDByType(chi.ChiType); findnew = true ; } break ; } //} } } if (!findnew) chi.SetIndexs(oldIndex); return findnew; } private bool checkCanPass(Indexs oldIndex, Indexs newIndexs,CHITypes types, int [][] map) { bool passed = true ; //判断两边的将是否碰面了 if (oldIndex.X >= 3 && oldIndex.X <= 5) { bool haslaotou = false ; Indexs laotou1 = new Indexs(-1, -1); Indexs laotou2 = new Indexs(-1, -1); for ( int y = 0; y <= 2; y ) { if (map[y][oldIndex.X] == 5) { laotou1.X = oldIndex.X; laotou1.Y = y; break ; } } for ( int y = 7; y <= 9; y ) { if (map[y][oldIndex.X] == 12) { laotou2.X = oldIndex.X; laotou2.Y = y; } } if (laotou1.Y != -1 && laotou2.Y != -1 && laotou1.X==laotou2.X) //两个老头在同一条线上面 { int zicount = 0; if (laotou2.Y > laotou1.Y) { for ( int y = laotou1.Y 1; y <laotou2.Y; y ) { if ((map[y][oldIndex.X] != 0 && map[y][oldIndex.X] != map[oldIndex.Y][oldIndex.X]) ||oldIndex.X==newIndexs.X) //找到了隔点 { zicount ; break ; } } } if (zicount<=0) { return false ; } } } if (types == CHITypes.JU || types == CHITypes.JU2) { if (newIndexs.X == oldIndex.X&&newIndexs.Y!=oldIndex.Y) //纵向移动 { if (oldIndex.Y < newIndexs.Y) { for ( int y = oldIndex.Y 1;y<newIndexs.Y; y ) { if (map[y][oldIndex.X] != 0) //路径中间有障碍物 { passed = false ; break ; } } } else { for ( int y=newIndexs.Y 1; y <oldIndex.Y; y ) { if (map[y][oldIndex.X] != 0) //路径中间有障碍物 { passed = false ; break ; } } } return passed; } else if (newIndexs.Y == oldIndex.Y&&newIndexs.X!=oldIndex.X) //横向移动 { if (oldIndex.X<newIndexs.X) { for ( int x = oldIndex.X 1; x < newIndexs.X; x ) { if (map[oldIndex.Y][x] != 0) { passed = false ; break ; } } } else { for ( int x = newIndexs.X 1; x < oldIndex.X; x ) { if (map[oldIndex.Y][x] != 0) { passed = false ; break ; } } } } else { return false ; } } if (types == CHITypes.MA || types == CHITypes.MA2) { passed = false ; if (newIndexs.X == oldIndex.X - 1 && newIndexs.Y == oldIndex.Y - 2&&map[oldIndex.Y-1][oldIndex.X]==0) { return true ; } if (newIndexs.X == oldIndex.X 1 && newIndexs.Y == oldIndex.Y - 2 && map[oldIndex.Y - 1][oldIndex.X] == 0) { return true ; } if (newIndexs.X == oldIndex.X - 1 && newIndexs.Y == oldIndex.Y 2 && map[oldIndex.Y 1][oldIndex.X] == 0) { return true ; } if (newIndexs.X == oldIndex.X 1 && newIndexs.Y==oldIndex.Y 2 && map[oldIndex.Y 1][oldIndex.X] == 0) { return true ; } if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y - 1 && map[oldIndex.Y][oldIndex.X - 1] == 0) { return true ; } if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y 1 && map[oldIndex.Y][oldIndex.X - 1] == 0) { return true ; } if (newIndexs.X == oldIndex.X 2 && newIndexs.Y==oldIndex.Y - 1 && map[oldIndex.Y][oldIndex.X 1] == 0) { return true ; } if (newIndexs.X == oldIndex.X 2 && newIndexs.Y == oldIndex.Y 1 && map[oldIndex.Y][oldIndex.X 1] == 0) { return true ; } return passed; } if (types == CHITypes.XIANG || types == CHITypes.XIANG2) { passed = false ; if (types == CHITypes.XIANG&&newIndexs.Y<=4) { if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y - 2 && map[oldIndex.Y - 1][oldIndex.X - 1] == 0) { return true ; } if (newIndexs.X == oldIndex.X 2 && newIndexs.Y == oldIndex.Y - 2 && map[oldIndex.Y - 1][oldIndex.X 1] == 0) { return true ; } if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y 2 && map[oldIndex.Y 1][oldIndex.X - 1] == 0) { return true ; } if (newIndexs.X == oldIndex.X 2 && newIndexs.Y == oldIndex.Y 2 && map[oldIndex.Y 1][oldIndex.X 1] == 0) { return true ; } return passed; } if (types==CHITypes.XIANG2&&newIndexs.Y>=5) { if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y - 2 && map[oldIndex.Y - 1][oldIndex.X - 1] == 0) { return true ; } if (newIndexs.X == oldIndex.X 2 && newIndexs.Y == oldIndex.Y - 2 && map[oldIndex.Y - 1][oldIndex.X 1] == 0) { return true ; } if (newIndexs.X == oldIndex.X - 2 && newIndexs.Y == oldIndex.Y 2 && map[oldIndex.Y 1][oldIndex.X - 1] == 0) { return true ; } if (newIndexs.X == oldIndex.X 2 && newIndexs.Y == oldIndex.Y 2 && map[oldIndex.Y 1][oldIndex.X 1] == 0) { return true ; } } return passed; } if (types == CHITypes.SHI || types == CHITypes.SHI2) { passed = false ; if (Math.Abs(newIndexs.X-oldIndex.X)==1&&Math.Abs(newIndexs.Y-oldIndex.Y)==1) { if (newIndexs.X >= 3 && newIndexs.X <= 5) { if ((newIndexs.Y>=0&&newIndexs.Y<=2)||(newIndexs.Y>=7&&newIndexs.Y<=9)) { return true ; } } } return passed; } if (types == CHITypes.JIANG || types == CHITypes.JIANG2) { passed = false ; if ((Math.Abs(newIndexs.X - oldIndex.X) == 1 && newIndexs.Y== oldIndex.Y)|| Math.Abs(newIndexs.Y - oldIndex.Y) == 1 && newIndexs.X == oldIndex.X) { if (newIndexs.X >= 3 && newIndexs.X <= 5) { if ((newIndexs.Y >= 0 && newIndexs.Y <= 2) || (newIndexs.Y >= 7 && newIndexs.Y <= 9)) { return true ; } } } return passed; } if (types == CHITypes.PAO || types == CHITypes.PAO2) { int chessCount = 0; if (newIndexs.X == oldIndex.X && newIndexs.Y != oldIndex.Y) //纵向移动 { if (oldIndex.Y < newIndexs.Y) { for ( int y = oldIndex.Y 1; y < newIndexs.Y; y ) { if (map[y][oldIndex.X] != 0) //路径中间有障碍物 { chessCount ; } } } else { for ( int y = newIndexs.Y 1; y < oldIndex.Y; y ) { if (map[y][oldIndex.X] != 0) //路径中间有障碍物 { chessCount ; } } } if (chessCount == 0 && map[newIndexs.Y][newIndexs.X] == 0) //空走 { return true ; } if (chessCount == 1 && map[newIndexs.Y][newIndexs.X] != 0) //吃子走 { if (EatChess(newIndexs)) { return true ; } } return false ; } else if (newIndexs.Y == oldIndex.Y && newIndexs.X != oldIndex.X) //横向移动 { if (oldIndex.X < newIndexs.X) { for ( int x = oldIndex.X 1; x < newIndexs.X; x ) { if (map[oldIndex.Y][x] != 0) { chessCount ; } } } else { for ( int x = newIndexs.X 1; x < oldIndex.X; x ) { if (map[oldIndex.Y][x] != 0) { chessCount ; } } } if (chessCount == 0&&map[newIndexs.Y][newIndexs.X]==0) //空走 { return true ; } if (chessCount == 1 && map[newIndexs.Y][newIndexs.X] != 0) //吃子走 { if (EatChess(newIndexs)) { return true ; } } return false ; } else { return false ; } } if (types == CHITypes.ZU || types == CHITypes.ZU2) { passed = false ; if (types == CHITypes.ZU) { if (oldIndex.Y <= 4) { if (newIndexs.Y - oldIndex.Y ==1&&newIndexs.X==oldIndex.X) { return true ; } } else { if (!(newIndexs.X != oldIndex.X && newIndexs.Y != oldIndex.Y)) { if (newIndexs.Y - oldIndex.Y==1) { return true ; } if (Math.Abs(newIndexs.X-oldIndex.X)==1) { return true ; } } } return passed; } if (types == CHITypes.ZU2) { if (oldIndex.Y>=5) { if (newIndexs.Y - oldIndex.Y ==-1 && newIndexs.X == oldIndex.X) { return true ; } } else { if (!(newIndexs.X != oldIndex.X && newIndexs.Y != oldIndex.Y)) { if (newIndexs.Y - oldIndex.Y == -1) { return true ; } if (Math.Abs(newIndexs.X - oldIndex.X) == 1) { return true ; } } } } return passed; } return passed; } private bool EatChess(Indexs newchessIndex) { bool eatOk = false ; object [] objs = chessList.ToArray(); for ( int i = 0; i < objs.Length; i ) { CHIChess onechess = (CHIChess)objs[i]; if (onechess.MyIndex.X == newchessIndex.X && onechess.MyIndex.Y == newchessIndex.Y && onechess != chi) //找到了吃的子 { if ((chi.ChiType.ToString().IndexOf( "2" )!=-1&&onechess.ChiType.ToString().IndexOf( "2" )==-1)|| (chi.ChiType.ToString().IndexOf( "2" )==-1 && onechess.ChiType.ToString().IndexOf( "2" )!=-1)) //只能吃对方的子 { eatedChess = onechess.cloneSelf(); chessList.Remove(onechess); eatOk = true ; } break ; } } return eatOk; } private void button1_Click( object sender, EventArgs e) { //if (history != null && history.Count > 0) //{ // UdpClient uclient = new UdpClient(); // uclient.Connect(IPAddress.Parse(this.textBox1.Text),2345); // HistoryRecorder hisrecoder = (HistoryRecorder)history.ToArray()[history.Count - 1]; // byte[] bts = null; // if (hisrecoder.eatCHI != null) // { // bts = Encoding.UTF8.GetBytes(desk.GetIDByType(hisrecoder.hisCHI.ChiType) // "|" hisrecoder.fromIndex.X "," hisrecoder.fromIndex.Y "|" // hisrecoder.toIndex.X "," hisrecoder.toIndex.Y "|" desk.GetIDByType(hisrecoder.eatCHI.ChiType)); // } // else // { // bts = Encoding.UTF8.GetBytes(desk.GetIDByType(hisrecoder.hisCHI.ChiType) // "|" hisrecoder.fromIndex.X "," hisrecoder.fromIndex.Y "|" // hisrecoder.toIndex.X "," hisrecoder.toIndex.Y "|0"); // } // uclient.Send(bts, bts.Length); //} } private void SendMsg( byte [] msg) { UdpClient uclient = new UdpClient(); uclient.Connect(IPAddress.Parse(remoteIP), 1234); uclient.Send(msg,msg.Length); } private void SendMoveInfo() { if (history != null && history.Count > 0) { HistoryRecorder hisrecoder = (HistoryRecorder)history.ToArray()[history.Count - 1]; byte [] bts = null ; if (hisrecoder.eatCHI != null ) { bts = Encoding.UTF8.GetBytes(desk.GetIDByType(hisrecoder.hisCHI.ChiType) "|" hisrecoder.fromIndex.X "," hisrecoder.fromIndex.Y "|" hisrecoder.toIndex.X "," hisrecoder.toIndex.Y "|" desk.GetIDByType(hisrecoder.eatCHI.ChiType)); } else { bts = Encoding.UTF8.GetBytes(desk.GetIDByType(hisrecoder.hisCHI.ChiType) "|" hisrecoder.fromIndex.X "," hisrecoder.fromIndex.Y "|" hisrecoder.toIndex.X "," hisrecoder.toIndex.Y "|0" ); } this .canMove = false ; this .SendMsg(bts); } } private void button2_Click( object sender, EventArgs e) { } private void listenMsg() { while ( true ) { IPEndPoint remote = new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], 1234); byte [] bts = this .client.Receive( ref remote); if (bts.Length >=10&&Encoding.UTF8.GetString(bts).Contains( "|" )) { TransferObject msgobj = DecodeMsg(Encoding.UTF8.GetString(bts)); foreach (CHIChess ochess in chessList) { if (ochess.MyIndex.X == msgobj.fromIndex.X && ochess.MyIndex.Y == msgobj.fromIndex.Y && ochess.ChiType == desk.GetTypeByID(msgobj.hischiID)) { chi = ochess; break ; } } if (chi != null ) { chi.SetIndexs(msgobj.fromIndex); if (msgobj.toIndexs.Y < 5) { chi.location = new Point(68 msgobj.toIndexs.X * 39, 38 msgobj.toIndexs.Y * 39); } else { chi.location = new Point(68 msgobj.toIndexs.X * 39, 43 msgobj.toIndexs.Y * 39); } this .MoveChess(chi.location); this .canMove = true ; } continue ; } if (Encoding.UTF8.GetString(bts).Trim() == "hui" .Trim()) { this .deskBox.Enabled = false ; if (MessageBox.Show( "对方要求悔棋,你是否同意?" , "悔棋" , MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ToHistory(); this .SendMsg(Encoding.UTF8.GetBytes( "huiyes" )); //同意了 } else { this .SendMsg(Encoding.UTF8.GetBytes( "huino" )); //不同意 } this .deskBox.Enabled = true ; continue ; } if (Encoding.UTF8.GetString(bts).Trim() == "new" .Trim()) { this .deskBox.Enabled = false ; if (MessageBox.Show( "对方要求重新开始,你是否同意?" , "重新开始" , MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this .SendMsg(Encoding.UTF8.GetBytes( "newyes" .Trim())); //同意重新开始 curMap = desk.GetMaps(); this .LoadChesses(curMap); } else { this .SendMsg(Encoding.UTF8.GetBytes( "newno" .Trim())); //不同意重新开始 } this .deskBox.Enabled = true ; continue ; } if (Encoding.UTF8.GetString(bts).Trim() == "newyes" .Trim()) //对方同意了重新开始 { curMap = desk.GetMaps(); this .LoadChesses(curMap); continue ; } if (Encoding.UTF8.GetString(bts).Trim() == "newno" .Trim()) //对方不同意重新开始 { this .deskBox.Enabled = false ; MessageBox.Show( "对方不同意重新开始!" , "信息" , MessageBoxButtons.OK, MessageBoxIcon.Information); this .deskBox.Enabled = true ; continue ; } if (Encoding.UTF8.GetString(bts).Trim() == "renshu" .Trim()) //对方已经认输了, 重新开始 { this .deskBox.Enabled = false ; MessageBox.Show( "恭喜你,对方已经认输了!" , "信息" , MessageBoxButtons.OK, MessageBoxIcon.Information); curMap = desk.GetMaps(); this .LoadChesses(curMap); this .deskBox.Enabled = true ; continue ; } if (Encoding.UTF8.GetString(bts).Trim() == "qiuhe" .Trim()) //对方求和了 { this .deskBox.Enabled = false ; if (MessageBox.Show( "对方求和,你是否同意?" , "求和" , MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) //同意求和,重新开始 { curMap = desk.GetMaps(); this .LoadChesses(curMap); this .SendMsg(Encoding.UTF8.GetBytes( "qiuheyes" .Trim())); //同意求和 } else { this .SendMsg(Encoding.UTF8.GetBytes( "qiuheno" .Trim())); //不同意求和 } this .deskBox.Enabled = true ; continue ; } if (Encoding.UTF8.GetString(bts).Trim() == "qiuheyes" .Trim()) //对方同意了你的求和,重新开始 { curMap = desk.GetMaps(); this .LoadChesses(curMap); continue ; } if (Encoding.UTF8.GetString(bts).Trim() == "qiuheno" .Trim()) //对方同意了你的求和,重新开始 { this .deskBox.Enabled = false ; MessageBox.Show( "对方不同意你的求和!" , "信息" , MessageBoxButtons.OK, MessageBoxIcon.Information); this .deskBox.Enabled = true ; continue ; } if (Encoding.UTF8.GetString(bts).Trim() == "huiyes" .Trim()) { ToHistory(); //this.deskBox.Enabled = true; continue ; } if (Encoding.UTF8.GetString(bts).Trim() == "huino" .Trim()) { this .deskBox.Enabled = false ; MessageBox.Show( "对方不同意你的悔棋!" , "信息" ,MessageBoxButtons.OK,MessageBoxIcon.Information); this .deskBox.Enabled = true ; continue ; } if (Encoding.UTF8.GetString(bts).Trim() == "fclose" .Trim()) { this .deskBox.Enabled = false ; this .Close(); MessageBox.Show( "对方已经下线!" , "信息" , MessageBoxButtons.OK, MessageBoxIcon.Information); continue ; } } } private TransferObject DecodeMsg( string msgstr) { string [] str = msgstr.Split( new char [] { '|' }); TransferObject tobj = new TransferObject(); if (str.Length > 0) { tobj.hischiID = Math.Abs(Convert.ToInt16(str[0]) - 7); string [] fromStr = str[1].Split( new char [] { ',' }); tobj.fromIndex.X = Convert.ToInt16(fromStr[0]); tobj.fromIndex.Y = Math.Abs(9 - Convert.ToInt16(fromStr[1])); string [] toStr = str[2].Split( new char [] { ',' }); tobj.toIndexs.X = Convert.ToInt16(toStr[0]); tobj.toIndexs.Y = Math.Abs(9 - Convert.ToInt16(toStr[1])); tobj.eatchiID = Math.Abs(Convert.ToInt16(str[3]) - 7); } return tobj; } private void lastToolStripMenuItem_Click( object sender, EventArgs e) //还原历史记录 { if (history != null && history.Count > 0) { this .SendMsg(Encoding.UTF8.GetBytes( "hui" )); //发送悔棋的信息 this .deskBox.Enabled = false ; } else { MessageBox.Show( "没有棋可悔!" , "信息" , MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void ToHistory() { if (history != null && history.Count > 0) { bool ishisOk = false ; object [] hisobjs = history.ToArray(); HistoryRecorder hisrecorder = (HistoryRecorder)hisobjs[hisobjs.Length - 1]; //查找链表最后一条记录 object [] objs = chessList.ToArray(); for ( int i = 0; i < objs.Length; i ) { CHIChess chichess = (CHIChess)objs[i]; if (chichess.MyIndex.X == hisrecorder.toIndex.X && chichess.MyIndex.Y == hisrecorder.toIndex.Y) { chessList.Remove(chichess); //删除现在的 chessList.Add(hisrecorder.hisCHI.cloneSelf()); //添加原来的 if (hisobjs.Length - 2 >= 0) { CHIChess chichess1 = (CHIChess)((HistoryRecorder)hisobjs[hisobjs.Length - 2]).hisCHI; oldPath = (GraphicsPath)chichess1.chiPath.Clone(); } newPath = (GraphicsPath)hisrecorder.hisCHI.chiPath.Clone(); curMap[hisrecorder.fromIndex.Y][hisrecorder.fromIndex.X] = desk.GetIDByType(chichess.ChiType); curMap[hisrecorder.toIndex.Y][hisrecorder.toIndex.X] = 0; ishisOk = true ; break ; } } if (hisrecorder.eatCHI != null ) { CHIChess cloneCHI = hisrecorder.eatCHI.cloneSelf(); chessList.Add(cloneCHI); curMap[hisrecorder.eatCHI.MyIndex.Y][hisrecorder.eatCHI.MyIndex.X] = desk.GetIDByType(cloneCHI.ChiType); } if (ishisOk) { if ( this .canMove == true ) { this .canMove = false ; } else { this .canMove = true ; } history.Remove(hisrecorder); } this .deskBox.Invalidate(); } } private void newStartToolStripMenuItem_Click( object sender, EventArgs e) { SendMsg(Encoding.UTF8.GetBytes( "new" )); } private void exitToolStripMenuItem_Click( object sender, EventArgs e) { this .Close(); } private void remoteToolStripMenuItem_Click( object sender, EventArgs e) { } private void Form1_FormClosed( object sender, FormClosedEventArgs e) { SendMsg(Encoding.UTF8.GetBytes( "fclose" )); client.Close(); client = null ; this .DialogResult = DialogResult.OK; //try //{ // lmsg.EndInvoke(this.result); //} //catch //{ //} } private void aboutgame_Click( object sender, EventArgs e) { if (bout == null ) { bout = new Abouts(); bout.Show(); } } private void 认输ToolStripMenuItem_Click( object sender, EventArgs e) //认输了 { SendMsg(Encoding.UTF8.GetBytes( "renshu" )); curMap = desk.GetMaps(); this .LoadChesses(curMap); } private void 求和ToolStripMenuItem_Click( object sender, EventArgs e) //求和了 { SendMsg(Encoding.UTF8.GetBytes( "qiuhe" )); } } public delegate void ListenMsg(); public delegate void findSel(Point e); } |
标签: 象棋
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论