├── .gitignore ├── README.md ├── build.xml ├── build ├── built-jar.properties └── classes │ ├── .netbeans_automatic_build │ ├── bbishop.png │ ├── bking.png │ ├── bknight.png │ ├── bpawn.png │ ├── bqueen.png │ ├── brook.png │ ├── chessboard │ ├── Chessboard.rs │ └── ChessboardBuilder.rs │ ├── wbishop.png │ ├── wking.png │ ├── wknight.png │ ├── wpawn.png │ ├── wqueen.png │ └── wrook.png ├── dist ├── README.TXT └── images │ ├── bbishop.png │ ├── bking.png │ ├── bknight.png │ ├── bpawn.png │ ├── bqueen.png │ ├── brook.png │ ├── wbishop.png │ ├── wking.png │ ├── wknight.png │ ├── wpawn.png │ ├── wqueen.png │ └── wrook.png ├── images ├── bbishop.png ├── bking.png ├── bknight.png ├── bpawn.png ├── bqueen.png ├── brook.png ├── wbishop.png ├── wking.png ├── wknight.png ├── wpawn.png ├── wqueen.png └── wrook.png ├── manifest.mf ├── nbproject ├── build-impl.xml ├── genfiles.properties ├── private │ ├── config.properties │ ├── private.properties │ └── private.xml ├── project.properties └── project.xml ├── src ├── ai │ ├── BoardRater.java │ ├── BruteChessAI.java │ ├── Node.java │ ├── RandomAI.java │ └── Root.java ├── chessboard │ ├── Chessboard.java │ └── ChessboardBuilder.java ├── enums │ └── Player.java ├── pieces │ ├── Bishop.java │ ├── King.java │ ├── Knight.java │ ├── Pawn.java │ ├── Piece.java │ ├── Queen.java │ └── Tower.java ├── sjakk │ ├── Chess.java │ ├── Controller.java │ ├── Game.java │ ├── Move.java │ └── Position.java └── view │ ├── BoardItem.java │ ├── ChessFrame.java │ ├── ChessSquare.java │ ├── Config.java │ ├── MouseClickParser.java │ ├── NavigationPanel.java │ └── chessboardPanel │ └── ChessboardPanel.java └── test ├── Pieces └── TowerTest.java └── sjakk └── ChessboardTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Package Files # 4 | *.jar 5 | *.war 6 | *.ear 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Chess 2 | ===== 3 | 4 | Simple java chess GUI with a even more simple AI. 5 | -------------------------------------------------------------------------------- /build.xml: -------------------------------------------------------------------------------- 1 | 2 | Builds, tests, and runs the project Chess. 3 | 4 | 65 | 66 | -------------------------------------------------------------------------------- /build/built-jar.properties: -------------------------------------------------------------------------------- 1 | #Thu, 18 Apr 2013 18:54:53 +0200 2 | 3 | 4 | /home/quist/github/Chess= 5 | -------------------------------------------------------------------------------- /build/classes/.netbeans_automatic_build: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/.netbeans_automatic_build -------------------------------------------------------------------------------- /build/classes/bbishop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/bbishop.png -------------------------------------------------------------------------------- /build/classes/bking.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/bking.png -------------------------------------------------------------------------------- /build/classes/bknight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/bknight.png -------------------------------------------------------------------------------- /build/classes/bpawn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/bpawn.png -------------------------------------------------------------------------------- /build/classes/bqueen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/bqueen.png -------------------------------------------------------------------------------- /build/classes/brook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/brook.png -------------------------------------------------------------------------------- /build/classes/chessboard/Chessboard.rs: -------------------------------------------------------------------------------- 1 | Chessboard.Chessboard 2 | -------------------------------------------------------------------------------- /build/classes/chessboard/ChessboardBuilder.rs: -------------------------------------------------------------------------------- 1 | Chessboard.ChessboardBuilder 2 | -------------------------------------------------------------------------------- /build/classes/wbishop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/wbishop.png -------------------------------------------------------------------------------- /build/classes/wking.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/wking.png -------------------------------------------------------------------------------- /build/classes/wknight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/wknight.png -------------------------------------------------------------------------------- /build/classes/wpawn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/wpawn.png -------------------------------------------------------------------------------- /build/classes/wqueen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/wqueen.png -------------------------------------------------------------------------------- /build/classes/wrook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/build/classes/wrook.png -------------------------------------------------------------------------------- /dist/README.TXT: -------------------------------------------------------------------------------- 1 | ======================== 2 | BUILD OUTPUT DESCRIPTION 3 | ======================== 4 | 5 | When you build an Java application project that has a main class, the IDE 6 | automatically copies all of the JAR 7 | files on the projects classpath to your projects dist/lib folder. The IDE 8 | also adds each of the JAR files to the Class-Path element in the application 9 | JAR files manifest file (MANIFEST.MF). 10 | 11 | To run the project from the command line, go to the dist folder and 12 | type the following: 13 | 14 | java -jar "Chess.jar" 15 | 16 | To distribute this project, zip up the dist folder (including the lib folder) 17 | and distribute the ZIP file. 18 | 19 | Notes: 20 | 21 | * If two JAR files on the project classpath have the same name, only the first 22 | JAR file is copied to the lib folder. 23 | * Only JAR files are copied to the lib folder. 24 | If the classpath contains other types of files or folders, these files (folders) 25 | are not copied. 26 | * If a library on the projects classpath also has a Class-Path element 27 | specified in the manifest,the content of the Class-Path element has to be on 28 | the projects runtime path. 29 | * To set a main class in a standard Java project, right-click the project node 30 | in the Projects window and choose Properties. Then click Run and enter the 31 | class name in the Main Class field. Alternatively, you can manually type the 32 | class name in the manifest Main-Class element. 33 | -------------------------------------------------------------------------------- /dist/images/bbishop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/bbishop.png -------------------------------------------------------------------------------- /dist/images/bking.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/bking.png -------------------------------------------------------------------------------- /dist/images/bknight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/bknight.png -------------------------------------------------------------------------------- /dist/images/bpawn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/bpawn.png -------------------------------------------------------------------------------- /dist/images/bqueen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/bqueen.png -------------------------------------------------------------------------------- /dist/images/brook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/brook.png -------------------------------------------------------------------------------- /dist/images/wbishop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/wbishop.png -------------------------------------------------------------------------------- /dist/images/wking.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/wking.png -------------------------------------------------------------------------------- /dist/images/wknight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/wknight.png -------------------------------------------------------------------------------- /dist/images/wpawn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/wpawn.png -------------------------------------------------------------------------------- /dist/images/wqueen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/wqueen.png -------------------------------------------------------------------------------- /dist/images/wrook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/dist/images/wrook.png -------------------------------------------------------------------------------- /images/bbishop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/bbishop.png -------------------------------------------------------------------------------- /images/bking.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/bking.png -------------------------------------------------------------------------------- /images/bknight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/bknight.png -------------------------------------------------------------------------------- /images/bpawn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/bpawn.png -------------------------------------------------------------------------------- /images/bqueen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/bqueen.png -------------------------------------------------------------------------------- /images/brook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/brook.png -------------------------------------------------------------------------------- /images/wbishop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/wbishop.png -------------------------------------------------------------------------------- /images/wking.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/wking.png -------------------------------------------------------------------------------- /images/wknight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/wknight.png -------------------------------------------------------------------------------- /images/wpawn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/wpawn.png -------------------------------------------------------------------------------- /images/wqueen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/wqueen.png -------------------------------------------------------------------------------- /images/wrook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/images/wrook.png -------------------------------------------------------------------------------- /manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /nbproject/build-impl.xml: -------------------------------------------------------------------------------- 1 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 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 | Must set src.images.dir 208 | Must set src.dir 209 | Must set test.src.dir 210 | Must set build.dir 211 | Must set dist.dir 212 | Must set build.classes.dir 213 | Must set dist.javadoc.dir 214 | Must set build.test.classes.dir 215 | Must set build.test.results.dir 216 | Must set build.classes.excludes 217 | Must set dist.jar 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 | Must set javac.includes 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 | Must set JVM to use for profiling in profiler.info.jvm 405 | Must set profiler agent JVM arguments in profiler.info.jvmargs.agent 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 | 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 | Must select some files in the IDE or set javac.includes 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | To run this application from the command line without Ant, try: 660 | 661 | 662 | 663 | 664 | 665 | 666 | java -cp "${run.classpath.with.dist.jar}" ${main.class} 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 | To run this application from the command line without Ant, try: 692 | 693 | java -jar "${dist.jar.resolved}" 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | Must select one file in the IDE or set run.class 723 | 724 | 725 | 726 | Must select one file in the IDE or set run.class 727 | 728 | 729 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | Must select one file in the IDE or set debug.class 754 | 755 | 756 | 757 | 758 | Must select one file in the IDE or set debug.class 759 | 760 | 761 | 762 | 763 | Must set fix.includes 764 | 765 | 766 | 767 | 768 | 769 | 770 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | Must select one file in the IDE or set profile.class 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 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 | 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 | Must select some files in the IDE or set javac.includes 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | Some tests failed; see details above. 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | Must select some files in the IDE or set test.includes 939 | 940 | 941 | 942 | Some tests failed; see details above. 943 | 944 | 945 | 950 | 951 | Must select one file in the IDE or set test.class 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 | 981 | 982 | Must select one file in the IDE or set applet.url 983 | 984 | 985 | 986 | 987 | 988 | 989 | 994 | 995 | Must select one file in the IDE or set applet.url 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | 1029 | 1030 | 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | 1044 | 1045 | 1046 | 1047 | 1048 | 1049 | 1050 | -------------------------------------------------------------------------------- /nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=b682d35f 2 | build.xml.script.CRC32=f70757d4 3 | build.xml.stylesheet.CRC32=28e38971@1.44.1.45 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=b682d35f 7 | nbproject/build-impl.xml.script.CRC32=e4816766 8 | nbproject/build-impl.xml.stylesheet.CRC32=0ae3a408@1.44.1.45 9 | -------------------------------------------------------------------------------- /nbproject/private/config.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Quist/Chess/f3b0a247abddd3c2cb42afe342ca58ed91ba0425/nbproject/private/config.properties -------------------------------------------------------------------------------- /nbproject/private/private.properties: -------------------------------------------------------------------------------- 1 | compile.on.save=true 2 | do.depend=false 3 | do.jar=true 4 | javac.debug=true 5 | javadoc.preview=true 6 | user.properties.file=/home/quist/.netbeans/7.0/build.properties 7 | -------------------------------------------------------------------------------- /nbproject/private/private.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.run.all.processors=true 4 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output 5 | application.title=Chess 6 | application.vendor=quist 7 | build.classes.dir=${build.dir}/classes 8 | build.classes.excludes=**/*.java,**/*.form 9 | # This directory is removed when the project is cleaned: 10 | build.dir=build 11 | build.generated.dir=${build.dir}/generated 12 | build.generated.sources.dir=${build.dir}/generated-sources 13 | # Only compile against the classpath explicitly listed here: 14 | build.sysclasspath=ignore 15 | build.test.classes.dir=${build.dir}/test/classes 16 | build.test.results.dir=${build.dir}/test/results 17 | # Uncomment to specify the preferred debugger connection transport: 18 | #debug.transport=dt_socket 19 | debug.classpath=\ 20 | ${run.classpath} 21 | debug.test.classpath=\ 22 | ${run.test.classpath} 23 | # This directory is removed when the project is cleaned: 24 | dist.dir=dist 25 | dist.jar=${dist.dir}/Chess.jar 26 | dist.javadoc.dir=${dist.dir}/javadoc 27 | endorsed.classpath= 28 | excludes= 29 | includes=** 30 | jar.archive.disabled=${jnlp.enabled} 31 | jar.compress=false 32 | jar.index=${jnlp.enabled} 33 | javac.classpath= 34 | # Space-separated list of extra javac options 35 | javac.compilerargs= 36 | javac.deprecation=false 37 | javac.processorpath=\ 38 | ${javac.classpath} 39 | javac.source=1.6 40 | javac.target=1.6 41 | javac.test.classpath=\ 42 | ${javac.classpath}:\ 43 | ${build.classes.dir} 44 | javac.test.processorpath=\ 45 | ${javac.test.classpath} 46 | javadoc.additionalparam= 47 | javadoc.author=false 48 | javadoc.encoding=${source.encoding} 49 | javadoc.noindex=false 50 | javadoc.nonavbar=false 51 | javadoc.notree=false 52 | javadoc.private=false 53 | javadoc.splitindex=true 54 | javadoc.use=true 55 | javadoc.version=false 56 | javadoc.windowtitle= 57 | jnlp.codebase.type=no.codebase 58 | jnlp.descriptor=application 59 | jnlp.enabled=false 60 | jnlp.mixed.code=default 61 | jnlp.offline-allowed=false 62 | jnlp.signed=false 63 | jnlp.signing= 64 | jnlp.signing.alias= 65 | jnlp.signing.keystore= 66 | main.class=sjakk.Chess 67 | manifest.file=manifest.mf 68 | meta.inf.dir=${src.dir}/META-INF 69 | mkdist.disabled=false 70 | platform.active=default_platform 71 | run.classpath=\ 72 | ${javac.classpath}:\ 73 | ${build.classes.dir} 74 | # Space-separated list of JVM arguments used when running the project 75 | # (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value 76 | # or test-sys-prop.name=value to set system properties for unit tests): 77 | run.jvmargs= 78 | run.test.classpath=\ 79 | ${javac.test.classpath}:\ 80 | ${build.test.classes.dir} 81 | source.encoding=UTF-8 82 | src.dir=src 83 | src.images.dir=images 84 | test.src.dir=test 85 | -------------------------------------------------------------------------------- /nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | org.netbeans.modules.java.j2seproject 3 | 4 | 5 | Chess 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/ai/BoardRater.java: -------------------------------------------------------------------------------- 1 | 2 | package ai; 3 | 4 | import enums.Player; 5 | import pieces.Bishop; 6 | import pieces.King; 7 | import pieces.Knight; 8 | import pieces.Pawn; 9 | import pieces.Piece; 10 | import pieces.Queen; 11 | import pieces.Tower; 12 | import Chessboard.Chessboard; 13 | 14 | /** 15 | * 16 | * @author Joakim 17 | */ 18 | public class BoardRater { 19 | private static final int pawnValue = 10; 20 | private static final int rookValue = 50; 21 | private static final int knightValue = 30; 22 | private static final int bishopValue = 30; 23 | private static final int queenValue = 90; 24 | private static final int kingValue = 5000; 25 | 26 | 27 | static public int rateBoard(Chessboard board, Player playingAs){ 28 | int whitePoints = 0; 29 | int blackPoints = 0; 30 | for(Piece p: board.getPieceList()){ 31 | if(p.getPieceColor() == Player.WHITE){ 32 | whitePoints += ratePiece(p); 33 | } else { 34 | blackPoints += ratePiece(p); 35 | } 36 | } 37 | 38 | if(playingAs == Player.WHITE){ 39 | return whitePoints - blackPoints; 40 | } else { 41 | return blackPoints - whitePoints; 42 | } 43 | } 44 | 45 | static private int ratePiece(Piece piece){ 46 | if(piece instanceof Pawn){ 47 | return pawnValue; 48 | } 49 | if(piece instanceof Tower){ 50 | return rookValue + piece.getMoves().size(); 51 | } 52 | if(piece instanceof Knight){ 53 | return knightValue + piece.getMoves().size(); 54 | } 55 | if(piece instanceof Bishop){ 56 | return bishopValue + piece.getMoves().size(); 57 | } 58 | if(piece instanceof Queen){ 59 | return queenValue + piece.getMoves().size(); 60 | } 61 | if(piece instanceof King){ 62 | return kingValue; 63 | } 64 | return 0; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/ai/BruteChessAI.java: -------------------------------------------------------------------------------- 1 | package ai; 2 | 3 | import Chessboard.Chessboard; 4 | import enums.Player; 5 | import sjakk.Move; 6 | 7 | /** 8 | * 9 | * @author joakimlindquister 10 | */ 11 | public class BruteChessAI { 12 | public static int depth = 4; 13 | private Player playingAs; 14 | 15 | 16 | public BruteChessAI(Player playingAs){ 17 | this.playingAs = playingAs; 18 | } 19 | 20 | public Move getChessMove(Chessboard board){ 21 | Root root = new Root(playingAs); 22 | root.explore(board); 23 | return root.bestMove; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/ai/Node.java: -------------------------------------------------------------------------------- 1 | package ai; 2 | 3 | import Chessboard.Chessboard; 4 | import Chessboard.ChessboardBuilder; 5 | import enums.Player; 6 | import java.util.ArrayList; 7 | import sjakk.Chess; 8 | import sjakk.Move; 9 | 10 | /** 11 | * 12 | * @author Joakim 13 | */ 14 | public class Node { 15 | private Player playingAs; 16 | 17 | private int rating; 18 | 19 | public Node(Player playingAs){ 20 | this.playingAs = playingAs; 21 | } 22 | 23 | public int getRating(){ 24 | return rating; 25 | } 26 | 27 | public void explore(int depth, Chessboard board, Player turn){ 28 | if(depth == 0){ 29 | rating = BoardRater.rateBoard(board, playingAs); 30 | return; 31 | } 32 | ArrayList moves = board.getLegalMoves(turn); 33 | if (moves.isEmpty()) { 34 | rating = BoardRater.rateBoard(board, playingAs); 35 | } 36 | 37 | initMaxMin(turn); 38 | 39 | for (Move move : moves) { 40 | Chessboard newBoard = ChessboardBuilder.copy(board); 41 | newBoard.updateMove(move); 42 | Node child = new Node(playingAs); 43 | child.explore(depth - 1, newBoard, Chess.switchPlayer(turn)); 44 | 45 | if (turn == playingAs) { 46 | if (child.rating > rating) { 47 | rating = child.rating; 48 | } 49 | } else { 50 | if(child.rating < rating){ 51 | rating = child.rating; 52 | } 53 | } 54 | } 55 | } 56 | 57 | private void initMaxMin(Player turn) { 58 | if(playingAs == turn){ 59 | rating = Integer.MIN_VALUE; 60 | } else { 61 | rating = Integer.MAX_VALUE; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/ai/RandomAI.java: -------------------------------------------------------------------------------- 1 | 2 | package ai; 3 | 4 | import enums.Player; 5 | import pieces.Piece; 6 | import java.util.ArrayList; 7 | import java.util.Random; 8 | import Chessboard.Chessboard; 9 | import sjakk.Move; 10 | 11 | /** 12 | * 13 | * @author Joakim 14 | */ 15 | public class RandomAI { 16 | private Player playingAs; 17 | 18 | public RandomAI(Player playingAs){ 19 | this.playingAs = playingAs; 20 | 21 | } 22 | 23 | public Move getChessMove(Chessboard board){ 24 | ArrayList posMoves = getPosMoves(board); 25 | return getRandomMove(posMoves); 26 | } 27 | 28 | 29 | 30 | private ArrayList getPosMoves(Chessboard board) { 31 | 32 | ArrayList possibleMoves = new ArrayList(); 33 | 34 | for (Piece piece : board.getPieceList(playingAs)) { 35 | possibleMoves.addAll(board.getLegalMovesFrom(piece.getPosition())); 36 | } 37 | return possibleMoves; 38 | 39 | } 40 | 41 | private Move getRandomMove(ArrayList possibleMoves) { 42 | System.out.println("Number of pos moves: " + possibleMoves.size()); 43 | Random rgen = new Random(); 44 | int randomMove = rgen.nextInt(possibleMoves.size()); 45 | return possibleMoves.get(randomMove); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/ai/Root.java: -------------------------------------------------------------------------------- 1 | package ai; 2 | 3 | import Chessboard.Chessboard; 4 | import Chessboard.ChessboardBuilder; 5 | import enums.Player; 6 | import java.util.ArrayList; 7 | import sjakk.Chess; 8 | import sjakk.Move; 9 | 10 | /** 11 | * 12 | * @author joakimlindquister 13 | */ 14 | public class Root { 15 | public Move bestMove; 16 | private Player playingAs; 17 | 18 | public Root(Player playingAs){ 19 | this.playingAs = playingAs; 20 | } 21 | 22 | public Move explore(Chessboard board){ 23 | ArrayList moves = board.getLegalMoves(playingAs); 24 | if(moves.isEmpty()){ 25 | throw new IllegalStateException(); 26 | } 27 | 28 | int currentBest = Integer.MIN_VALUE; 29 | for(Move move: moves){ 30 | Chessboard newBoard = ChessboardBuilder.copy(board); 31 | newBoard.updateMove(move); 32 | Node node = new Node(playingAs); 33 | node.explore(BruteChessAI.depth -1, newBoard, Chess.switchPlayer(playingAs)); 34 | if(node.getRating() > currentBest){ 35 | bestMove = move; 36 | currentBest = node.getRating(); 37 | } 38 | } 39 | return bestMove; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/chessboard/Chessboard.java: -------------------------------------------------------------------------------- 1 | package Chessboard; 2 | 3 | import enums.Player; 4 | import pieces.*; 5 | import java.util.ArrayList; 6 | import sjakk.Chess; 7 | import sjakk.Move; 8 | import sjakk.Position; 9 | 10 | /** 11 | * 12 | * @author Joakim 13 | */ 14 | public class Chessboard { 15 | 16 | private Piece pieces[][]; 17 | 18 | private Position whiteKingPosition; 19 | private Position blackKingPosition; 20 | 21 | private ArrayList blackPieces; 22 | private ArrayList whitePieces; 23 | public Chessboard(){ 24 | 25 | } 26 | 27 | protected void setPieces(Piece pieces[][]){ 28 | this.pieces = pieces; 29 | } 30 | 31 | protected void setBlackPieces(ArrayList bPieces){ 32 | this.blackPieces = bPieces; 33 | } 34 | 35 | protected void setWhitePieces(ArrayList wPieces){ 36 | this.blackPieces = wPieces; 37 | } 38 | 39 | protected void setKingPosition(Position white, Position black){ 40 | whiteKingPosition = white; 41 | blackKingPosition = black; 42 | } 43 | 44 | protected Piece[][] getPieces(){ 45 | return pieces; 46 | } 47 | 48 | public ArrayList getPieceList() { 49 | ArrayList all = getPieceList(Player.BLACK); 50 | all.addAll(getPieceList(Player.WHITE)); 51 | return all; 52 | } 53 | 54 | public ArrayList getPieceList(Player player) { 55 | ArrayList pieceList = new ArrayList(); 56 | for (int x = 0; x < 8; x++) { 57 | for (int y = 0; y < 8; y++) { 58 | if (pieces[x][y] != null && pieces[x][y].getPieceColor() 59 | == player) { 60 | pieceList.add(pieces[x][y]); 61 | } 62 | } 63 | } 64 | return pieceList; 65 | } 66 | 67 | public Piece getPiece(Position pos) { 68 | return pieces[pos.getRow()][pos.getColumn()]; 69 | } 70 | 71 | public ArrayList getLegalMovesFrom(Position pos){ 72 | ArrayList moves = new ArrayList(); 73 | if(isOutOfBounds(pos)){ 74 | return moves; 75 | } 76 | Piece piece = pieces[pos.getRow()][pos.getColumn()]; 77 | if(piece == null){ 78 | return moves; 79 | } 80 | 81 | for(Move move: piece.getMoves()){ 82 | if(!movesPlayerIntoCheck(move)){ 83 | moves.add(move); 84 | } 85 | } 86 | return moves; 87 | } 88 | 89 | private ArrayList getLegalMoves(Piece piece){ 90 | ArrayList moves = new ArrayList(); 91 | 92 | if(piece == null){ 93 | return moves; 94 | } 95 | 96 | for(Move move: piece.getMoves()){ 97 | if(!movesPlayerIntoCheck(move)){ 98 | moves.add(move); 99 | } 100 | } 101 | return moves; 102 | } 103 | 104 | 105 | public ArrayList getLegalMoves(Player player){ 106 | ArrayList moves = new ArrayList(); 107 | for(Piece piece: getPieceList(player)){ 108 | moves.addAll(getLegalMoves(piece)); 109 | } 110 | return moves; 111 | } 112 | 113 | /** 114 | * Updates a move on the chessboard piece array. Does not enforce any rules! 115 | * 116 | * @param move 117 | */ 118 | public void updateMove(Move move) { 119 | Piece movedPiece = pieces[move.getStartPos().getRow()][move.getStartPos().getColumn()]; 120 | movedPiece.updateCurrentPos(move.getEndPos()); 121 | 122 | pieces[move.getStartPos().getRow()][move.getStartPos().getColumn()] = null; 123 | 124 | pieces[move.getEndPos().getRow()][move.getEndPos().getColumn()] = movedPiece; 125 | if(movedPiece instanceof King){ 126 | updateKingPosition(move); 127 | } 128 | if(movedPiece instanceof Pawn){ 129 | checkPawnPromotion(move); 130 | } 131 | } 132 | 133 | private void updateCastlingMove(Move move){ 134 | int kingX = move.getStartPos().getRow(); 135 | int kingY = move.getEndPos().getColumn(); 136 | Piece king = pieces[kingX][kingY]; 137 | int towerX = move.getEndPos().getRow(); 138 | int towerY = move.getEndPos().getColumn(); 139 | Piece tower = pieces[towerX][towerY]; 140 | 141 | pieces[kingX][kingY] = null; 142 | pieces[towerX][towerY] = null; 143 | 144 | if(towerY > kingY){ //Queenside 145 | Position kPos = new Position(kingX,kingY-2 ); 146 | king.updateCurrentPos(kPos); 147 | } 148 | 149 | } 150 | 151 | private void checkPawnPromotion(Move move) { 152 | int row = move.getEndPos().getRow(); 153 | 154 | if (row == 7 || row == 0) { 155 | 156 | 157 | Player pieceColor = move.getMovingPiece().getPieceColor(); 158 | Queen queen = new Queen(pieceColor, move.getEndPos(), this); 159 | int column = move.getEndPos().getColumn(); 160 | pieces[row][column] = queen; 161 | move.setMovingPiece(queen); 162 | } 163 | } 164 | 165 | private void updateKingPosition(Move move){ 166 | Piece piece = move.getMovingPiece(); 167 | if(piece.getPieceColor() == Player.WHITE){ 168 | whiteKingPosition = move.getEndPos(); 169 | } else { 170 | blackKingPosition = move.getEndPos(); 171 | } 172 | } 173 | 174 | public boolean containsEnemy(Position startPos, Position endPos) { 175 | if (isOutOfBounds(endPos)) { 176 | return false; 177 | } 178 | if (isFree(endPos)) { 179 | return false; 180 | } 181 | Player movingPieceColor = pieces[startPos.getRow()][startPos.getColumn()].getPieceColor(); 182 | 183 | Player recivingPieceColor = pieces[endPos.getRow()][endPos.getColumn()].getPieceColor(); 184 | 185 | return (movingPieceColor != recivingPieceColor); 186 | 187 | } 188 | 189 | public boolean isFree(Position endPos) { 190 | if (isOutOfBounds(endPos)) { 191 | return false; 192 | } 193 | return (pieces[endPos.getRow()][endPos.getColumn()] == null); 194 | } 195 | 196 | public boolean isOutOfBounds(Position endPos) { 197 | return endPos.getRow() >= 8 || endPos.getRow() < 0 198 | || endPos.getColumn() >= 8 || endPos.getColumn() < 0; 199 | } 200 | 201 | /** 202 | * Retrives the other players possible moves and checks if he can hit the king 203 | * @param color 204 | * @return 205 | */ 206 | public boolean isInCheck(Player player) { 207 | Position kingPos = kingPosition(player); 208 | 209 | Player opponent = Chess.switchPlayer(player); 210 | ArrayList pieceList = getPieceList(opponent); 211 | for (Piece p : pieceList) { 212 | for (Move m : p.getMoves()) { 213 | if (m.getEndPos().equals(kingPos)) { 214 | return true; 215 | } 216 | } 217 | } 218 | return false; 219 | } 220 | 221 | public Position kingPosition(Player player) { 222 | if(player == Player.WHITE){ 223 | return whiteKingPosition; 224 | } else { 225 | return blackKingPosition; 226 | } 227 | } 228 | 229 | public boolean isUnmovedTower(Position pos){ 230 | Piece piece = pieces[pos.getRow()][pos.getColumn()]; 231 | if(!(piece instanceof Tower)){ 232 | return false; 233 | } 234 | Tower tower = (Tower) piece; 235 | 236 | 237 | if(tower.isMoved()){ 238 | return false; 239 | } 240 | 241 | return true; 242 | } 243 | 244 | public boolean movesPlayerIntoCheck(Move move) { 245 | Player player = move.getMovingPiece().getPieceColor(); 246 | 247 | Chessboard newBoard = ChessboardBuilder.copy(this); 248 | newBoard.updateMove(move); 249 | if (newBoard.isInCheck(player)) { 250 | return true; 251 | } 252 | 253 | return false; 254 | 255 | } 256 | 257 | public boolean isValidMove(Move move) { 258 | 259 | if (!isIsolatedValidMove(move)) { 260 | return false; 261 | } 262 | 263 | if (movesPlayerIntoCheck(move)) { 264 | return false; 265 | } 266 | return true; 267 | } 268 | 269 | private boolean isIsolatedValidMove(Move move) { 270 | Piece piece = move.getMovingPiece(); 271 | Position candidate = move.getEndPos(); 272 | //Check that the move is in the possible moves list 273 | for (Move m : piece.getMoves()) { 274 | Position endPos = m.getEndPos(); 275 | if (candidate.equals(endPos)) { 276 | return true; 277 | } 278 | } 279 | return false; 280 | } 281 | 282 | 283 | } 284 | -------------------------------------------------------------------------------- /src/chessboard/ChessboardBuilder.java: -------------------------------------------------------------------------------- 1 | package Chessboard; 2 | 3 | import enums.Player; 4 | import pieces.Bishop; 5 | import pieces.King; 6 | import pieces.Knight; 7 | import pieces.Pawn; 8 | import pieces.Piece; 9 | import pieces.Queen; 10 | import pieces.Tower; 11 | import sjakk.Position; 12 | 13 | /** 14 | * 15 | * @author Joakim 16 | */ 17 | public abstract class ChessboardBuilder { 18 | 19 | public static Chessboard build() { 20 | Piece pieces[][] = new Piece[8][8]; 21 | Chessboard chessboard = new Chessboard(); 22 | 23 | addWhitePieces(pieces, chessboard); 24 | addBlackPieces(pieces, chessboard); 25 | chessboard.setPieces(pieces); 26 | chessboard.setKingPosition(new Position(0,3), new Position(7,3)); 27 | return chessboard; 28 | } 29 | 30 | public static Chessboard copy(Chessboard old){ 31 | Chessboard newBoard = new Chessboard(); 32 | Piece pieces[][] = new Piece[8][8]; 33 | for(Piece piece: old.getPieceList()){ 34 | pieces[piece.getPosition().getRow()] 35 | [piece.getPosition().getColumn()] = clonePiece(newBoard,piece); 36 | } 37 | newBoard.setPieces(pieces); 38 | newBoard.setKingPosition(old.kingPosition(Player.WHITE), 39 | old.kingPosition(Player.BLACK)); 40 | return newBoard; 41 | } 42 | 43 | private static Piece clonePiece(Chessboard board, Piece piece) { 44 | if(piece == null){ 45 | return null; 46 | } 47 | 48 | if (piece instanceof Pawn) { 49 | return new Pawn(board, piece); 50 | } 51 | if (piece instanceof Tower) { 52 | return new Tower(board, piece); 53 | } 54 | if (piece instanceof Knight) { 55 | return new Knight(board, piece); 56 | } 57 | if (piece instanceof Queen) { 58 | return new Queen(board, piece); 59 | } 60 | if (piece instanceof King) { 61 | return new King(board, piece); 62 | } 63 | if (piece instanceof Bishop) { 64 | return new Bishop(board, piece); 65 | } 66 | 67 | 68 | throw new IllegalStateException("Piece instance not found"); 69 | } 70 | 71 | private static void addWhitePieces(Piece pieces[][], Chessboard board) { 72 | Position pos = new Position(0, 0); 73 | pieces[0][0] = new Tower(Player.WHITE, pos, board); 74 | 75 | pos = new Position(0, 1); 76 | pieces[0][1] = new Knight(Player.WHITE, pos, board); 77 | 78 | pos = new Position(0, 2); 79 | pieces[0][2] = new Bishop(Player.WHITE, pos, board); 80 | 81 | pos = new Position(0, 3); 82 | pieces[0][3] = new King(Player.WHITE, pos, board); 83 | 84 | pos = new Position(0, 4); 85 | pieces[0][4] = new Queen(Player.WHITE, pos, board); 86 | 87 | pos = new Position(0, 5); 88 | pieces[0][5] = new Bishop(Player.WHITE, pos, board); 89 | 90 | pos = new Position(0, 6); 91 | pieces[0][6] = new Knight(Player.WHITE, pos, board); 92 | 93 | pos = new Position(0, 7); 94 | pieces[0][7] = new Tower(Player.WHITE, pos, board); 95 | 96 | 97 | for (int i = 0; i < 8; i++) { 98 | pos = new Position(1, i); 99 | pieces[1][i] = new Pawn(Player.WHITE, pos, board); 100 | } 101 | } 102 | 103 | private static void addBlackPieces(Piece pieces[][], Chessboard board) { 104 | Position pos = new Position(7, 0); 105 | pieces[7][0] = new Tower(Player.BLACK, pos, board); 106 | 107 | pos = new Position(7, 1); 108 | pieces[7][1] = new Knight(Player.BLACK, pos, board); 109 | 110 | pos = new Position(7, 2); 111 | pieces[7][2] = new Bishop(Player.BLACK, pos, board); 112 | 113 | pos = new Position(7, 3); 114 | pieces[7][3] = new King(Player.BLACK, pos, board); 115 | 116 | pos = new Position(7, 4); 117 | pieces[7][4] = new Queen(Player.BLACK, pos, board); 118 | 119 | pos = new Position(7, 5); 120 | pieces[7][5] = new Bishop(Player.BLACK, pos, board); 121 | 122 | pos = new Position(7, 6); 123 | pieces[7][6] = new Knight(Player.BLACK, pos, board); 124 | 125 | pos = new Position(7, 7); 126 | pieces[7][7] = new Tower(Player.BLACK, pos, board); 127 | 128 | 129 | for (int i = 0; i < 8; i++) { 130 | pos = new Position(6, i); 131 | pieces[6][i] = new Pawn(Player.BLACK, pos, board); 132 | } 133 | } 134 | 135 | } 136 | -------------------------------------------------------------------------------- /src/enums/Player.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package enums; 6 | 7 | /** 8 | * 9 | * @author Joakim 10 | */ 11 | public enum Player { 12 | WHITE, BLACK 13 | } 14 | -------------------------------------------------------------------------------- /src/pieces/Bishop.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package pieces; 6 | 7 | import enums.Player; 8 | import Chessboard.Chessboard; 9 | import sjakk.Position; 10 | 11 | /** 12 | * 13 | * @author Joakim 14 | */ 15 | public class Bishop extends Piece { 16 | private final String imagePathWhite = "images/wbishop.png"; 17 | private final String imagePathBlack = "images/bbishop.png"; 18 | 19 | public Bishop(Player color, Position position,Chessboard board) { 20 | super(color, position, board); 21 | } 22 | 23 | public Bishop(Chessboard board, Piece other){ 24 | super(board,other); 25 | } 26 | 27 | @Override 28 | public void addAllPossibleMoves() { 29 | posMoves.clear(); 30 | checkNorthWest(); 31 | checkNorthEast(); 32 | checkSouthWest(); 33 | checkSouthEast(); 34 | } 35 | 36 | @Override 37 | public String getPieceImagePath(){ 38 | if(pieceColor == Player.WHITE){ 39 | return imagePathWhite; 40 | } 41 | 42 | return imagePathBlack; 43 | } 44 | 45 | private void checkNorthWest() { 46 | int row = currentPos.getRow() +1; 47 | int column = currentPos.getColumn() -1; 48 | 49 | while (row < 8 && column >= 0) { 50 | Position pos = new Position(row,column); 51 | 52 | if (!board.isFree(pos)) { 53 | checkEncounter(pos); 54 | return; 55 | } else { 56 | addMove(pos); 57 | } 58 | row++; 59 | column--; 60 | } 61 | } 62 | 63 | private void checkNorthEast() { 64 | int row = currentPos.getRow() + 1; 65 | int column = currentPos.getColumn() + 1; 66 | 67 | while (row < 8 && column < 8) { 68 | Position pos = new Position(row,column); 69 | if (!board.isFree(pos)) { 70 | checkEncounter(pos); 71 | return; 72 | } else { 73 | addMove(pos); 74 | } 75 | row++; 76 | column++; 77 | } 78 | } 79 | 80 | private void checkSouthWest() { 81 | int row = currentPos.getRow() - 1; 82 | int column = currentPos.getColumn() - 1; 83 | while (row >= 0 && column >= 0) { 84 | Position pos = new Position(row, column); 85 | if (!board.isFree(pos)) { 86 | checkEncounter(pos); 87 | return; 88 | } else { 89 | addMove(pos); 90 | } 91 | row--; 92 | column--; 93 | } 94 | } 95 | 96 | private void checkSouthEast() { 97 | int row = currentPos.getRow() - 1; 98 | int column = currentPos.getColumn() + 1; 99 | while (row >= 0 && column < 8) { 100 | Position pos = new Position(row,column); 101 | if (!board.isFree(pos)) { 102 | checkEncounter(pos); 103 | return; 104 | } else { 105 | addMove(pos); 106 | } 107 | row--; 108 | column++; 109 | } 110 | } 111 | 112 | private void checkEncounter(Position pos) { 113 | if (board.containsEnemy(currentPos,pos)) { 114 | addMove(pos); 115 | } 116 | } 117 | 118 | 119 | 120 | } 121 | -------------------------------------------------------------------------------- /src/pieces/King.java: -------------------------------------------------------------------------------- 1 | package pieces; 2 | 3 | import enums.Player; 4 | import Chessboard.Chessboard; 5 | import sjakk.Position; 6 | 7 | /** 8 | * 9 | * @author Joakim 10 | */ 11 | public class King extends Piece { 12 | private final String imagePathWhite = "images/wking.png"; 13 | private final String imagePathBlack = "images/bking.png"; 14 | 15 | private boolean moved; 16 | 17 | public King(Player color, Position pos, Chessboard board) { 18 | super(color, pos, board); 19 | } 20 | 21 | public King(Chessboard board, Piece other) { 22 | super(board, other); 23 | } 24 | 25 | @Override 26 | public void updateCurrentPos(Position pos) { 27 | this.currentPos = pos; 28 | moved = true; 29 | } 30 | 31 | @Override 32 | public String getPieceImagePath(){ 33 | if(pieceColor == Player.WHITE){ 34 | return imagePathWhite; 35 | } 36 | 37 | return imagePathBlack; 38 | } 39 | 40 | @Override 41 | protected void addAllPossibleMoves() { 42 | posMoves.clear(); 43 | Position pos = new Position(currentPos.getRow() -1, 44 | currentPos.getColumn() - 1); 45 | 46 | checkSpace(pos); 47 | 48 | pos = new Position(currentPos.getRow() - 1,currentPos.getColumn()); 49 | checkSpace(pos); 50 | 51 | pos = new Position(currentPos.getRow() - 1, currentPos.getColumn() +1 ); 52 | checkSpace(pos); 53 | 54 | pos = new Position(currentPos.getRow(), currentPos.getColumn() -1); 55 | checkSpace(pos); 56 | 57 | pos = new Position(currentPos.getRow(), currentPos.getColumn() +1); 58 | checkSpace(pos); 59 | 60 | pos = new Position(currentPos.getRow() +1, currentPos.getColumn() -1); 61 | checkSpace(pos); 62 | 63 | pos = new Position(currentPos.getRow() +1, currentPos.getColumn()); 64 | checkSpace(pos); 65 | 66 | pos = new Position(currentPos.getRow() +1, currentPos.getColumn() +1); 67 | checkSpace(pos); 68 | 69 | } 70 | 71 | private void checkCastling(){ 72 | queenSideCastling(); 73 | kingSideCastling(); 74 | 75 | } 76 | 77 | private void queenSideCastling() { 78 | if (moved) { 79 | return; 80 | } 81 | for(int column = 4; column<7; column++){ 82 | Position pos = new Position(currentPos.getRow(),column); 83 | if(!board.isFree(pos)){ 84 | return; 85 | } 86 | } 87 | 88 | Position queenSideTower = new Position(currentPos.getRow(), 7); 89 | if (board.containsEnemy(currentPos, queenSideTower)) { 90 | return; 91 | } 92 | 93 | if (!board.isUnmovedTower(queenSideTower)) { 94 | return; 95 | } 96 | addMove(queenSideTower); 97 | } 98 | 99 | private void kingSideCastling(){ 100 | if (moved) { 101 | return; 102 | } 103 | for(int column = 2; column > 0; column--){ 104 | Position pos = new Position(currentPos.getRow(),column); 105 | if(!board.isFree(pos)){ 106 | return; 107 | } 108 | } 109 | 110 | Position kingSideTower = new Position(currentPos.getRow(), 0); 111 | if (board.containsEnemy(currentPos, kingSideTower)) { 112 | return; 113 | } 114 | 115 | if (!board.isUnmovedTower(kingSideTower)) { 116 | return; 117 | } 118 | addMove(kingSideTower); 119 | } 120 | 121 | private void checkSpace(Position pos){ 122 | if(board.containsEnemy(currentPos,pos)){ 123 | addMove(pos); 124 | }else if(board.isFree(pos)){ 125 | addMove(pos); 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/pieces/Knight.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package pieces; 6 | 7 | import enums.Player; 8 | import Chessboard.Chessboard; 9 | import sjakk.Position; 10 | 11 | /** 12 | * 13 | * @author Joakim 14 | */ 15 | public class Knight extends Piece { 16 | private final String imagePathWhite = "images/wknight.png"; 17 | private final String imagePathBlack = "images/bknight.png"; 18 | 19 | public Knight(Player color, Position pos, Chessboard board) { 20 | super(color, pos, board); 21 | } 22 | 23 | public Knight(Chessboard board, Piece other) { 24 | super(board, other); 25 | } 26 | 27 | @Override 28 | public String getPieceImagePath(){ 29 | if(pieceColor == Player.WHITE){ 30 | return imagePathWhite; 31 | } 32 | 33 | return imagePathBlack; 34 | } 35 | 36 | @Override 37 | public void addAllPossibleMoves() { 38 | posMoves.clear(); 39 | 40 | //1 north, 2 west: 41 | Position pos = new Position(currentPos.getRow() +1, currentPos.getColumn() -2); 42 | checkEncounter(pos); 43 | 44 | //1 north, 2 east: 45 | pos = new Position(currentPos.getRow() + 1, currentPos.getColumn() +2); 46 | checkEncounter(pos); 47 | 48 | //2 north, 1 west: 49 | pos = new Position(currentPos.getRow()+2, currentPos.getColumn() -1); 50 | checkEncounter(pos); 51 | 52 | //2 north, 1 east: 53 | pos = new Position(currentPos.getRow()+2, currentPos.getColumn()+1); 54 | checkEncounter(pos); 55 | 56 | //2 south, 1 west: 57 | pos = new Position(currentPos.getRow()-2, currentPos.getColumn() -1); 58 | checkEncounter(pos); 59 | 60 | //2 south, 1 east: 61 | pos = new Position(currentPos.getRow()-2, currentPos.getColumn()+1); 62 | checkEncounter(pos); 63 | 64 | //1 south, 2 east: 65 | pos = new Position(currentPos.getRow()-1, currentPos.getColumn()+2); 66 | checkEncounter(pos); 67 | 68 | //1 south, 2 west: 69 | pos = new Position(currentPos.getRow()-1, currentPos.getColumn()-2); 70 | checkEncounter(pos); 71 | } 72 | 73 | private void checkEncounter(Position pos) { 74 | if (board.containsEnemy(currentPos,pos)) { 75 | addMove(pos); 76 | return; 77 | } 78 | if(board.isFree(pos)){ 79 | addMove(pos); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/pieces/Pawn.java: -------------------------------------------------------------------------------- 1 | package pieces; 2 | 3 | import enums.Player; 4 | import Chessboard.Chessboard; 5 | import sjakk.Position; 6 | 7 | 8 | /** 9 | * 10 | * @author Joakim 11 | */ 12 | public class Pawn extends Piece { 13 | 14 | private final String imagePathWhite = "images/wpawn.png"; 15 | private final String imagePathBlack = "images/bpawn.png"; 16 | 17 | public Pawn(Player color, Position pos, Chessboard board) { 18 | super(color, pos, board); 19 | } 20 | 21 | public Pawn(Chessboard board, Piece other){ 22 | super(board,other); 23 | } 24 | 25 | @Override 26 | public String getPieceImagePath(){ 27 | if(pieceColor == Player.WHITE){ 28 | return imagePathWhite; 29 | } 30 | 31 | return imagePathBlack; 32 | } 33 | 34 | @Override 35 | public void addAllPossibleMoves() { 36 | posMoves.clear(); 37 | if (pieceColor == Player.BLACK) { 38 | moveBlackForward(); 39 | attackBlack(); 40 | } else { 41 | moveWhiteForward(); 42 | attackWhite(); 43 | } 44 | } 45 | 46 | private void moveBlackForward() { 47 | Position pos = new Position(currentPos.getRow()-1, currentPos.getColumn()); 48 | if(board.isFree(pos)){ 49 | addMove(pos); 50 | } else { 51 | return; 52 | } 53 | 54 | if (isInStartPos()) { 55 | pos = new Position(currentPos.getRow()-2, currentPos.getColumn()); 56 | if (board.isFree(pos)) { 57 | addMove(pos); 58 | } 59 | } 60 | } 61 | 62 | private void attackBlack() { 63 | //Check to the left 64 | Position pos = new Position(currentPos.getRow()-1, currentPos.getColumn()-1); 65 | if (board.containsEnemy(currentPos,pos)) { 66 | addMove(pos); 67 | } 68 | 69 | //Check to the right: 70 | pos = new Position(currentPos.getRow()-1, currentPos.getColumn()+1); 71 | if (board.containsEnemy(currentPos,pos)) { 72 | addMove(pos); 73 | } 74 | } 75 | 76 | private void moveWhiteForward() { 77 | Position pos = new Position(currentPos.getRow() +1, currentPos.getColumn()); 78 | if (board.isFree(pos)) { 79 | addMove(pos); 80 | } else { 81 | return; 82 | } 83 | 84 | //If in startpos 85 | if (isInStartPos()) { 86 | pos = new Position(currentPos.getRow()+2, currentPos.getColumn()); 87 | if (board.isFree(pos)) { 88 | addMove(pos); 89 | } 90 | } 91 | } 92 | 93 | private void attackWhite() { 94 | //Check to the left 95 | Position pos = new Position(currentPos.getRow() +1, currentPos.getColumn() -1); 96 | if (board.containsEnemy(currentPos,pos)) { 97 | addMove(pos); 98 | } 99 | 100 | //Check to the right: 101 | pos = new Position(currentPos.getRow() + 1, currentPos.getColumn() +1); 102 | if (board.containsEnemy(currentPos,pos)) { 103 | addMove(pos); 104 | } 105 | } 106 | 107 | private boolean isInStartPos() { 108 | if (pieceColor == Player.BLACK) { 109 | return currentPos.getRow() == 6; 110 | } else { 111 | return currentPos.getRow() == 1; 112 | } 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /src/pieces/Piece.java: -------------------------------------------------------------------------------- 1 | 2 | package pieces; 3 | 4 | import enums.Player; 5 | import java.util.ArrayList; 6 | import Chessboard.Chessboard; 7 | import sjakk.Move; 8 | import sjakk.Position; 9 | 10 | /** 11 | * 12 | * @author Joakim 13 | */ 14 | public abstract class Piece{ 15 | protected ArrayList posMoves = new ArrayList(); 16 | protected Player pieceColor; 17 | protected Position currentPos; 18 | protected Chessboard board; 19 | 20 | public Piece(Player color, Position position, Chessboard board) { 21 | currentPos = position; 22 | pieceColor = color; 23 | this.board = board; 24 | } 25 | 26 | //Cloning 27 | public Piece(Chessboard board, Piece other) { 28 | pieceColor = other.getPieceColor(); 29 | this.board = board; 30 | currentPos = new Position(other.getPosition()); 31 | } 32 | 33 | abstract protected void addAllPossibleMoves(); 34 | 35 | abstract public String getPieceImagePath(); 36 | 37 | 38 | public Player getPieceColor(){ 39 | return pieceColor; 40 | } 41 | 42 | 43 | 44 | public void updateCurrentPos(Position pos){ 45 | this.currentPos = pos; 46 | } 47 | 48 | public Position getPosition(){ 49 | return currentPos; 50 | } 51 | 52 | protected void addMove(Position destination) { 53 | Move move = new Move(currentPos, destination, this); 54 | posMoves.add(move); 55 | } 56 | 57 | public ArrayList getMoves(){ 58 | addAllPossibleMoves(); 59 | return posMoves; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/pieces/Queen.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package pieces; 6 | 7 | import enums.Player; 8 | import java.util.ArrayList; 9 | import Chessboard.Chessboard; 10 | import sjakk.Move; 11 | import sjakk.Position; 12 | 13 | /** 14 | * 15 | * @author Joakim 16 | */ 17 | public class Queen extends Piece { 18 | 19 | private final String imagePathWhite = "images/wqueen.png"; 20 | private final String imagePathBlack = "images/bqueen.png"; 21 | 22 | public Queen(Player color, Position pos, Chessboard board) { 23 | super(color, pos, board); 24 | } 25 | 26 | public Queen(Chessboard board, Piece other){ 27 | super(board,other); 28 | } 29 | 30 | @Override 31 | public String getPieceImagePath() { 32 | if (pieceColor == Player.WHITE) { 33 | return imagePathWhite; 34 | } 35 | 36 | return imagePathBlack; 37 | } 38 | 39 | @Override 40 | public void addAllPossibleMoves() { 41 | posMoves.clear(); 42 | 43 | Bishop bishop = new Bishop(pieceColor, currentPos, board); 44 | Tower tower = new Tower(pieceColor, currentPos, board); 45 | ArrayList bishopMoves = bishop.getMoves(); 46 | ArrayList towerMoves = tower.getMoves(); 47 | 48 | bishopMoves.addAll(towerMoves); 49 | for (Move m : bishopMoves) { 50 | addMove(m.getEndPos()); 51 | } 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/pieces/Tower.java: -------------------------------------------------------------------------------- 1 | 2 | package pieces; 3 | 4 | import enums.Player; 5 | import Chessboard.Chessboard; 6 | import sjakk.Position; 7 | 8 | /** 9 | * 10 | * @author Joakim 11 | */ 12 | public class Tower extends Piece { 13 | 14 | private final String imagePathWhite = "images/wrook.png"; 15 | private final String imagePathBlack = "images/brook.png"; 16 | 17 | private boolean moved; 18 | 19 | public Tower(Player color, Position pos, Chessboard board) { 20 | super(color, pos, board); 21 | } 22 | 23 | public Tower(Chessboard board, Piece other){ 24 | super(board,other); 25 | } 26 | 27 | @Override 28 | public String getPieceImagePath() { 29 | if (pieceColor == Player.WHITE) { 30 | return imagePathWhite; 31 | } 32 | 33 | return imagePathBlack; 34 | } 35 | 36 | @Override 37 | public void updateCurrentPos(Position pos) { 38 | this.currentPos = pos; 39 | moved = true; 40 | } 41 | 42 | public boolean isMoved(){ 43 | return moved; 44 | } 45 | 46 | @Override 47 | public void addAllPossibleMoves() { 48 | posMoves.clear(); 49 | goNorth(); 50 | goSouth(); 51 | goWest(); 52 | goEast(); 53 | } 54 | 55 | private void goNorth() { 56 | int column = currentPos.getColumn(); 57 | for (int i = currentPos.getRow() - 1; i >= 0; i--) { 58 | Position pos = new Position(i, column); 59 | if (board.isFree(pos)) { 60 | addMove(pos); 61 | } else { 62 | checkEncounter(pos); 63 | return; 64 | } 65 | } 66 | } 67 | 68 | private void goSouth() { 69 | int column = currentPos.getColumn(); 70 | for (int i = currentPos.getRow() + 1; i < 8; i++) { 71 | Position pos = new Position(i, column); 72 | if (board.isFree(pos)) { 73 | addMove(pos); 74 | } else { 75 | checkEncounter(pos); 76 | return; 77 | } 78 | 79 | } 80 | } 81 | 82 | private void goWest() { 83 | int row = currentPos.getRow(); 84 | for (int i = currentPos.getColumn() - 1; i >= 0; i--) { 85 | Position pos = new Position(row, i); 86 | if (board.isFree(pos)) { 87 | addMove(pos); 88 | } else { 89 | checkEncounter(pos); 90 | return; 91 | } 92 | } 93 | } 94 | 95 | private void goEast() { 96 | int row = currentPos.getRow(); 97 | for (int i = currentPos.getColumn() + 1; i < 8; i++) { 98 | Position pos = new Position(row, i); 99 | if (board.isFree(pos)) { 100 | addMove(pos); 101 | } else { 102 | checkEncounter(pos); 103 | return; 104 | } 105 | } 106 | } 107 | 108 | private void checkEncounter(Position pos) { 109 | if (board.containsEnemy(currentPos, pos)) { 110 | addMove(pos); 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/sjakk/Chess.java: -------------------------------------------------------------------------------- 1 | 2 | package sjakk; 3 | 4 | import enums.Player; 5 | 6 | /** 7 | * 8 | * @author Joakim 9 | */ 10 | public class Chess { 11 | 12 | /** 13 | * @param args the command line arguments 14 | */ 15 | public static void main(String[] args) { 16 | Game game = new Game(); 17 | game.startGame(); 18 | } 19 | 20 | public static Player switchPlayer(Player player){ 21 | if(player == Player.WHITE){ 22 | return Player.BLACK; 23 | } else { 24 | return Player.WHITE; 25 | } 26 | } 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/sjakk/Controller.java: -------------------------------------------------------------------------------- 1 | package sjakk; 2 | 3 | import enums.Player; 4 | import pieces.Piece; 5 | import java.util.ArrayList; 6 | import Chessboard.Chessboard; 7 | import view.ChessFrame; 8 | import view.chessboardPanel.ChessboardPanel; 9 | import view.NavigationPanel; 10 | 11 | /** 12 | * 13 | * @author Joakim 14 | */ 15 | public class Controller { 16 | 17 | private ChessFrame frame; 18 | private ChessboardPanel boardPanel; 19 | 20 | private Game game; 21 | private Chessboard chessboard; 22 | 23 | private Player playerTurn = Player.WHITE; 24 | private final Player playingAs = Player.BLACK; 25 | 26 | 27 | public void prepareUI(Game game) { 28 | this.game = game; 29 | chessboard = game.getChessboard(); 30 | frame = new ChessFrame(); 31 | 32 | NavigationPanel navPanel = new NavigationPanel(); 33 | boardPanel = new ChessboardPanel(this); 34 | 35 | frame.addPanelCenter(boardPanel); 36 | frame.addPanelNorth(navPanel); 37 | } 38 | 39 | public void showStartupBoard() { 40 | 41 | for (Piece piece : chessboard.getPieceList()) { 42 | Position p = mapToUI(piece.getPosition()); 43 | 44 | boardPanel.setSquareImgPath(piece.getPieceImagePath(), p); 45 | } 46 | try { 47 | //Waits for ui to load images 48 | Thread.sleep(200); 49 | } catch (InterruptedException ex) { 50 | System.out.println("Error waiting for pictures to load"); 51 | } 52 | 53 | 54 | boardPanel.refreshUI(); 55 | } 56 | 57 | public void startGame() { 58 | if (playerTurn != playingAs) { 59 | Move move = game.getAIMove(playerTurn); 60 | updateMoveToUI(move); 61 | playerTurn = playingAs; 62 | boardPanel.refreshUI(); 63 | } 64 | } 65 | 66 | public void updateMoveToUI(Move move) { 67 | Move mappedMove = mapToUI(move); 68 | 69 | boardPanel.updateMoveFromAI(mappedMove); 70 | } 71 | 72 | public ArrayList possibleMovesRequestFromUI(Position pos){ 73 | 74 | Position mappedPos = mapFromUI(pos); 75 | return mapMovesToUI(chessboard.getLegalMovesFrom(mappedPos)); 76 | } 77 | 78 | private Move requestMoveFromAI() { 79 | return game.getAIMove(playerTurn); 80 | } 81 | 82 | public boolean processMoveFromUI(Move move) { 83 | Move mappedMove = mapFromUI(move); 84 | Position startPos = mappedMove.getStartPos(); 85 | Piece piece = chessboard.getPiece(startPos); 86 | mappedMove.setMovingPiece(piece); 87 | 88 | if (isValidMove(mappedMove)) { 89 | chessboard.updateMove(mappedMove); 90 | updateMoveToUI(mappedMove); 91 | playerTurn = Chess.switchPlayer(playerTurn); 92 | updateMoveToUI(requestMoveFromAI()); 93 | playerTurn = Chess.switchPlayer(playerTurn); 94 | 95 | return true; 96 | } 97 | return false; 98 | } 99 | 100 | private boolean isValidMove(Move move) { 101 | Piece piece = move.getMovingPiece(); 102 | if (piece == null || piece.getPieceColor() != playerTurn) { 103 | return false; 104 | } 105 | return chessboard.isValidMove(move); 106 | } 107 | 108 | 109 | 110 | /** 111 | * Converts a position in the datastructure to the corrosponding square in 112 | * the user interface 113 | * 114 | * @param x The representation of a position in the datastructure 115 | * @return The corrosponding position for the UI 116 | */ 117 | private Position mapToUI(Position pos){ 118 | Position mappedPos = new Position(pos.getColumn(), pos.getRow()); 119 | return mappedPos; 120 | } 121 | 122 | private Move mapToUI(Move move){ 123 | Position startPos = mapToUI(move.getStartPos()); 124 | Position endPos = mapToUI(move.getEndPos()); 125 | Move mappedMove = new Move(startPos, endPos, move.getMovingPiece()); 126 | return mappedMove; 127 | } 128 | 129 | 130 | private ArrayList mapMovesToUI(ArrayList moves){ 131 | ArrayList positions = new ArrayList(); 132 | 133 | for(Move move: moves ){ 134 | Position pos = move.getEndPos(); 135 | Position mappedPos = new Position(pos.getColumn(),pos.getRow()); 136 | positions.add(mappedPos); 137 | } 138 | return positions; 139 | } 140 | 141 | private int[] mapFromUI(int x, int y){ 142 | int result[] = new int[2]; 143 | result[0] = y; 144 | result[1] = x; 145 | return result; 146 | } 147 | 148 | private Move mapFromUI(Move move){ 149 | Position startPos = mapFromUI(move.getStartPos()); 150 | Position endPos = mapFromUI(move.getEndPos()); 151 | Move mappedMove = new Move(startPos,endPos, move.getMovingPiece()); 152 | return mappedMove; 153 | } 154 | 155 | private Position mapFromUI(Position p){ 156 | Position mappedPos = new Position(p.getColumn(),p.getRow()); 157 | return mappedPos; 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /src/sjakk/Game.java: -------------------------------------------------------------------------------- 1 | package sjakk; 2 | 3 | import Chessboard.Chessboard; 4 | import Chessboard.ChessboardBuilder; 5 | import ai.BruteChessAI; 6 | import enums.Player; 7 | 8 | /** 9 | * 10 | * @author Joakim 11 | */ 12 | public class Game { 13 | private Chessboard chessboard; 14 | private Controller controller; 15 | 16 | public Game(){ 17 | chessboard = ChessboardBuilder.build(); 18 | controller = new Controller(); 19 | } 20 | 21 | public void startGame(){ 22 | controller.prepareUI(this); 23 | controller.showStartupBoard(); 24 | controller.startGame(); 25 | 26 | } 27 | 28 | public Chessboard getChessboard(){ 29 | return chessboard; 30 | } 31 | 32 | public Move getAIMove(Player player){ 33 | Move move; 34 | 35 | if (player == Player.WHITE) { 36 | BruteChessAI aiWhite = new BruteChessAI(Player.WHITE); 37 | move = aiWhite.getChessMove(chessboard); 38 | } else { 39 | BruteChessAI aiBlack = new BruteChessAI(Player.BLACK); 40 | move = aiBlack.getChessMove(chessboard); 41 | } 42 | 43 | chessboard.updateMove(move); 44 | return move; 45 | } 46 | 47 | public void gameLoop(){ 48 | 49 | } 50 | 51 | private Move parseUserMove(String userInput){ 52 | userInput = userInput.toUpperCase(); 53 | String tokens[] = userInput.split(""); 54 | 55 | //Start position 56 | char c = tokens[1].charAt(0); 57 | int startPos[] = new int[2]; 58 | startPos[1] = c - 65; 59 | startPos[0] = Integer.parseInt(tokens[2]) - 1; 60 | 61 | //End position 62 | c = tokens[4].charAt(0); 63 | int endPos[] = new int[2]; 64 | endPos[1] = c - 65; 65 | endPos[0] = Integer.parseInt(tokens[5]) - 1; 66 | //Move move = new Move(startPos, endPos); 67 | return null; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/sjakk/Move.java: -------------------------------------------------------------------------------- 1 | package sjakk; 2 | 3 | import pieces.Piece; 4 | 5 | /** 6 | * 7 | * @author Joakim 8 | */ 9 | public class Move { 10 | private Position startPos; 11 | private Position endPos; 12 | private Piece movingPiece; 13 | private boolean castling; 14 | 15 | public Move(Position startPos, Position endPos, Piece movingPiece){ 16 | this.startPos = startPos; 17 | this.endPos = endPos; 18 | this.movingPiece = movingPiece; 19 | } 20 | 21 | //For castling 22 | public Move(Position startPos, Position endPos){ 23 | this.startPos = startPos; 24 | this.endPos = endPos; 25 | castling = true; 26 | } 27 | 28 | public Position getEndPos(){ 29 | return endPos; 30 | } 31 | public Position getStartPos(){ 32 | return startPos; 33 | } 34 | 35 | public Piece getMovingPiece(){ 36 | return movingPiece; 37 | } 38 | 39 | public void setMovingPiece(Piece piece){ 40 | movingPiece = piece; 41 | } 42 | 43 | @Override 44 | public String toString(){ 45 | String result = positionToString(startPos) + "->" + positionToString(endPos); 46 | return result; 47 | } 48 | 49 | private String positionToString(Position pos){ 50 | char letter = (char) (pos.getColumn() + 65); 51 | String result = letter + "" + (pos.getRow()+1); 52 | return result; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/sjakk/Position.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Represents a piece position on the chessboard 3 | */ 4 | package sjakk; 5 | 6 | /** 7 | * 8 | * @author Joakim 9 | */ 10 | public class Position { 11 | private int row; 12 | private int column; 13 | 14 | public Position(int row, int column){ 15 | this.row = row; 16 | this.column = column; 17 | } 18 | 19 | public Position(Position other){ 20 | this.row = other.row; 21 | this.column = other.column; 22 | } 23 | 24 | public int getColumn() { 25 | return column; 26 | } 27 | 28 | public int getRow() { 29 | return row; 30 | } 31 | 32 | public boolean equals(Position other){ 33 | return (this.row == other.row) && (this.column == other.column); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/view/BoardItem.java: -------------------------------------------------------------------------------- 1 | 2 | package view; 3 | 4 | import java.awt.Color; 5 | import java.awt.Graphics; 6 | 7 | /** 8 | * 9 | * @author Joakim 10 | */ 11 | public abstract class BoardItem { 12 | protected Color color; 13 | 14 | public BoardItem(Color color){ 15 | this.color = color; 16 | } 17 | 18 | public abstract void draw(Graphics g); 19 | } 20 | -------------------------------------------------------------------------------- /src/view/ChessFrame.java: -------------------------------------------------------------------------------- 1 | package view; 2 | 3 | import java.awt.BorderLayout; 4 | import javax.swing.JFrame; 5 | import javax.swing.JPanel; 6 | 7 | /** 8 | * 9 | * @author Joakim 10 | */ 11 | public class ChessFrame extends JFrame { 12 | public ChessFrame(){ 13 | setTitle("King Chess"); 14 | setDefaultCloseOperation(EXIT_ON_CLOSE); 15 | setVisible(true); 16 | setLayout(new BorderLayout()); 17 | } 18 | 19 | public void addPanelNorth(JPanel panel) { 20 | add(panel, BorderLayout.NORTH); 21 | pack(); 22 | } 23 | 24 | public void addPanelCenter(JPanel panel) { 25 | getContentPane().add(panel, BorderLayout.CENTER); 26 | pack(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/view/ChessSquare.java: -------------------------------------------------------------------------------- 1 | package view; 2 | 3 | import java.awt.BasicStroke; 4 | import java.awt.Color; 5 | import java.awt.Graphics; 6 | import java.awt.Graphics2D; 7 | import java.awt.Image; 8 | import java.awt.Toolkit; 9 | import java.awt.geom.Ellipse2D; 10 | import java.awt.geom.Rectangle2D; 11 | 12 | /** 13 | * 14 | * @author Joakim 15 | */ 16 | public class ChessSquare extends BoardItem { 17 | 18 | public final static int SQUARE_SIZE = 60; 19 | 20 | private Color highlightColor = Color.GREEN; 21 | private Color posMoveColor = Color.BLUE; 22 | private int posMoveOvalWidth = 10; 23 | private int posMoveOvalHeight = 10; 24 | 25 | 26 | private String pieceIMGPath; 27 | private int x; 28 | private int y; 29 | 30 | private boolean highlighted; 31 | private boolean posMoveOval; 32 | 33 | 34 | public ChessSquare(Color color, int x, int y) { 35 | super(color); 36 | this.x = x; 37 | this.y = y; 38 | } 39 | 40 | public void setPieceIMGPath(String path) { 41 | this.pieceIMGPath = path; 42 | } 43 | 44 | public void setHighlighted(Color color) { 45 | highlighted = true; 46 | highlightColor = color; 47 | } 48 | 49 | public void disableHighlighted(){ 50 | highlighted = false; 51 | } 52 | 53 | public void setPosMoveOval(){ 54 | posMoveOval = true; 55 | } 56 | public void disablePosMoveOval(){ 57 | posMoveOval = false; 58 | } 59 | 60 | public int getRowID(){ 61 | return x/SQUARE_SIZE; 62 | } 63 | 64 | public int getColumnID(){ 65 | return y/SQUARE_SIZE; 66 | } 67 | 68 | @Override 69 | public void draw(Graphics g) { 70 | Graphics2D g2 = (Graphics2D) g; 71 | 72 | Rectangle2D r = new Rectangle2D.Double(x, y, SQUARE_SIZE, SQUARE_SIZE); 73 | g2.setColor(color); 74 | g2.fill(r); 75 | 76 | 77 | //Draw piece 78 | if (pieceIMGPath != null) { 79 | Image img = readImage(); 80 | g.drawImage(img, x, y, null); 81 | 82 | } 83 | 84 | if (highlighted) { 85 | Rectangle2D outline = new Rectangle2D.Double(x-1, y-1, SQUARE_SIZE, SQUARE_SIZE); 86 | g2.setColor(highlightColor); 87 | BasicStroke stroke = new BasicStroke(2); 88 | 89 | g2.setStroke(stroke); 90 | g2.draw(outline); 91 | } 92 | 93 | if(posMoveOval){ 94 | drawPosibleMoveOval(g2); 95 | } 96 | 97 | 98 | } 99 | 100 | private void drawPosibleMoveOval(Graphics2D g2){ 101 | Ellipse2D e = new Ellipse2D.Double(x,y,posMoveOvalWidth,posMoveOvalHeight); 102 | g2.setColor(posMoveColor); 103 | g2.fill(e); 104 | g2.draw(e); 105 | } 106 | 107 | private Image readImage() { 108 | Image image = Toolkit.getDefaultToolkit().getImage(pieceIMGPath); 109 | return image; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/view/Config.java: -------------------------------------------------------------------------------- 1 | package view; 2 | 3 | /** 4 | * 5 | * @author quist 6 | */ 7 | public class Config { 8 | //COLORS AS STATIC COLORS 9 | } 10 | -------------------------------------------------------------------------------- /src/view/MouseClickParser.java: -------------------------------------------------------------------------------- 1 | package view; 2 | 3 | import java.awt.event.MouseEvent; 4 | 5 | /** 6 | * 7 | * @author Joakim 8 | */ 9 | public class MouseClickParser { 10 | 11 | 12 | public static int[] parseMouseClick(MouseEvent e){ 13 | int result[] = new int[2]; 14 | 15 | int xPoint = e.getX(); 16 | int yPoint = e.getY(); 17 | 18 | result[0] = xPoint/ChessSquare.SQUARE_SIZE; 19 | result[1] = yPoint/ChessSquare.SQUARE_SIZE; 20 | 21 | return result; 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/view/NavigationPanel.java: -------------------------------------------------------------------------------- 1 | 2 | package view; 3 | 4 | import javax.swing.JButton; 5 | import javax.swing.JPanel; 6 | 7 | /** 8 | * 9 | * @author quist 10 | */ 11 | public class NavigationPanel extends JPanel { 12 | 13 | public NavigationPanel(){ 14 | init(); 15 | } 16 | 17 | private void init(){ 18 | JButton newGameBtn = new JButton("New game"); 19 | JButton regretBtn = new JButton("Regret move"); 20 | add(newGameBtn); 21 | add(regretBtn); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/view/chessboardPanel/ChessboardPanel.java: -------------------------------------------------------------------------------- 1 | package view.chessboardPanel; 2 | 3 | import sjakk.Controller; 4 | import java.awt.Color; 5 | import java.awt.Dimension; 6 | import java.awt.Graphics; 7 | import java.awt.event.MouseEvent; 8 | import java.awt.event.MouseListener; 9 | import java.util.ArrayList; 10 | import javax.swing.JPanel; 11 | import sjakk.Move; 12 | import sjakk.Position; 13 | import view.ChessSquare; 14 | import view.MouseClickParser; 15 | 16 | /** 17 | * 18 | * @author Joakim 19 | */ 20 | public class ChessboardPanel extends JPanel implements MouseListener { 21 | 22 | public final static Color CHESSWHITE = Color.WHITE; 23 | public final static Color CHESSBLACK = Color.GRAY; 24 | public final static Color SELECTEDCOLOR = Color.GREEN; 25 | public final static Color RECENTMOVECOLOR = Color.BLUE; 26 | 27 | private ChessSquare squares[][]; 28 | private Controller controller; 29 | 30 | //State varibles 31 | private ChessSquare selectedSquare; 32 | private ArrayList taggedSquares; 33 | 34 | public ChessboardPanel(Controller controller) { 35 | this.controller = controller; 36 | int prefSize = 8 * ChessSquare.SQUARE_SIZE; 37 | setPreferredSize(new Dimension(prefSize, prefSize)); 38 | 39 | squares = new ChessSquare[8][8]; 40 | createAndAddSquares(); 41 | addMouseListener(this); 42 | } 43 | 44 | @Override 45 | protected void paintComponent(Graphics g) { 46 | //super.paintComponent(g); 47 | draw(g); 48 | } 49 | 50 | private void draw(Graphics g) { 51 | for (int x = 0; x < 8; x++) { 52 | for (int y = 0; y < 8; y++) { 53 | squares[x][y].draw(g); 54 | } 55 | } 56 | } 57 | 58 | public void refreshUI() { 59 | paintComponent(this.getGraphics()); 60 | } 61 | 62 | private void createAndAddSquares() { 63 | Color color = CHESSBLACK; 64 | for (int x = 0; x < 8; x++) { 65 | for (int y = 0; y < 8; y++) { 66 | int xPos = x * ChessSquare.SQUARE_SIZE; 67 | int yPos = y * ChessSquare.SQUARE_SIZE; 68 | ChessSquare square = new ChessSquare(color, xPos, yPos); 69 | squares[x][y] = square; 70 | color = switchBlackWhite(color); 71 | } 72 | color = switchBlackWhite(color); 73 | } 74 | } 75 | 76 | private Color switchBlackWhite(Color color) { 77 | if (color == CHESSBLACK) { 78 | return CHESSWHITE; 79 | } else { 80 | return CHESSBLACK; 81 | } 82 | } 83 | 84 | public void setSquareImgPath(String path, Position pos) { 85 | squares[pos.getRow()][pos.getColumn()].setPieceIMGPath(path); 86 | } 87 | 88 | private void tagPossibleMoves(ArrayList pos) { 89 | taggedSquares = new ArrayList(); 90 | for (Position p : pos) { 91 | ChessSquare square = squares[p.getRow()][p.getColumn()]; 92 | square.setPosMoveOval(); 93 | taggedSquares.add(square); 94 | } 95 | } 96 | 97 | private void untagSquares() { 98 | for (ChessSquare c : taggedSquares) { 99 | c.disablePosMoveOval(); 100 | } 101 | taggedSquares = null; 102 | } 103 | 104 | private void selectSquare(ChessSquare square) { 105 | selectedSquare = square; 106 | selectedSquare.setHighlighted(SELECTEDCOLOR); 107 | Position pos = new Position(square.getRowID(), square.getColumnID()); 108 | tagPossibleMoves(controller.possibleMovesRequestFromUI(pos)); 109 | refreshUI(); 110 | } 111 | 112 | private void unselectSquare() { 113 | untagSquares(); 114 | selectedSquare.disableHighlighted(); 115 | selectedSquare = null; 116 | refreshUI(); 117 | } 118 | 119 | private void clickEvent(int square[]) { 120 | int x = square[0]; 121 | int y = square[1]; 122 | ChessSquare clickedSquare = squares[x][y]; 123 | 124 | //If no square currently selected 125 | if (selectedSquare == null) { 126 | selectSquare(clickedSquare); 127 | return; 128 | } 129 | 130 | //Tries to process a move 131 | Move move = squaresToMove(selectedSquare,clickedSquare); 132 | if (processMove(move)) { 133 | unselectSquare(); 134 | } else { 135 | unselectSquare(); 136 | selectSquare(clickedSquare); 137 | } 138 | } 139 | 140 | private Move squaresToMove(ChessSquare a, ChessSquare b){ 141 | Position startPos = new Position(a.getRowID(), 142 | a.getColumnID()); 143 | Position endPos = new Position(b.getRowID(), b.getColumnID()); 144 | 145 | Move move = new Move(startPos,endPos, null); 146 | return move; 147 | } 148 | 149 | private boolean processMove(Move move) { 150 | 151 | return controller.processMoveFromUI(move); 152 | } 153 | 154 | public void updateMoveFromAI(Move move){ 155 | setSquareImgPath(null,move.getStartPos()); 156 | setSquareImgPath(move.getMovingPiece().getPieceImagePath(), 157 | move.getEndPos()); 158 | refreshUI(); 159 | } 160 | 161 | @Override 162 | public void mouseClicked(MouseEvent e) { 163 | } 164 | 165 | @Override 166 | public void mousePressed(MouseEvent e) { 167 | clickEvent(MouseClickParser.parseMouseClick(e)); 168 | } 169 | 170 | @Override 171 | public void mouseReleased(MouseEvent e) { 172 | } 173 | 174 | @Override 175 | public void mouseEntered(MouseEvent e) { 176 | } 177 | 178 | @Override 179 | public void mouseExited(MouseEvent e) { 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /test/Pieces/TowerTest.java: -------------------------------------------------------------------------------- 1 | 2 | package Pieces; 3 | 4 | import pieces.Tower; 5 | import enums.Player; 6 | import org.junit.*; 7 | import static org.junit.Assert.*; 8 | import Chessboard.Chessboard; 9 | import sjakk.Position; 10 | 11 | /** 12 | * 13 | * @author Joakim 14 | */ 15 | public class TowerTest { 16 | Tower tower; 17 | 18 | @Before 19 | public void setUp() { 20 | Chessboard board = new Chessboard(); 21 | Position pos = new Position(0,0); 22 | tower = new Tower(Player.WHITE, pos, board); 23 | } 24 | 25 | @After 26 | public void tearDown() { 27 | } 28 | 29 | @Test 30 | public void testAllMovesFirstPlay() { 31 | assertEquals(0,tower.getMoves().size()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /test/sjakk/ChessboardTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package sjakk; 6 | 7 | import Chessboard.Chessboard; 8 | import enums.Player; 9 | import pieces.*; 10 | import org.junit.*; 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * 15 | * @author Joakim 16 | */ 17 | public class ChessboardTest { 18 | 19 | private Chessboard chessboard; 20 | 21 | public ChessboardTest() { 22 | } 23 | 24 | @Before 25 | public void setUp() { 26 | chessboard = new Chessboard(); 27 | } 28 | 29 | @After 30 | public void tearDown() { 31 | chessboard = null; 32 | } 33 | 34 | @Test 35 | public void testBoardDimensions() { 36 | Piece board[][] = chessboard.getBoard(); 37 | assertEquals("Dimension is not 8x8", 8, board.length); 38 | assertEquals("Dimension is not 8x8", 8, board[0].length); 39 | } 40 | 41 | @Test 42 | public void testPrepareBoardPieceColors() { 43 | Piece board[][] = chessboard.getBoard(); 44 | //Check colors 45 | for (int x = 0; x < 2; x++) { 46 | for (int y = 0; y < 8; y++) { 47 | assertEquals("Wrong piece color", Player.WHITE, 48 | board[x][y].getPieceColor()); 49 | } 50 | } 51 | 52 | for (int x = 7; x < 5; x--) { 53 | for (int y = 0; y < 8; y++) { 54 | assertEquals("Wrong piece color", Player.BLACK, 55 | board[x][y].getPieceColor()); 56 | } 57 | } 58 | } 59 | 60 | @Test 61 | public void testPrepareBoardPieceType() { 62 | Piece board[][] = chessboard.getBoard(); 63 | assertTrue(board[0][0] instanceof Tower); 64 | assertTrue(board[0][1] instanceof Knight); 65 | assertTrue(board[0][2] instanceof Bishop); 66 | assertTrue(board[0][3] instanceof Queen); 67 | assertTrue(board[0][4] instanceof King); 68 | assertTrue(board[0][5] instanceof Bishop); 69 | assertTrue(board[0][6] instanceof Knight); 70 | assertTrue(board[0][7] instanceof Tower); 71 | 72 | assertTrue(board[7][0] instanceof Tower); 73 | assertTrue(board[7][1] instanceof Knight); 74 | assertTrue(board[7][2] instanceof Bishop); 75 | assertTrue(board[7][3] instanceof Queen); 76 | assertTrue(board[7][4] instanceof King); 77 | assertTrue(board[7][5] instanceof Bishop); 78 | assertTrue(board[7][6] instanceof Knight); 79 | assertTrue(board[7][7] instanceof Tower); 80 | 81 | //Test pawns 82 | for (int y = 0; y < 8; y++) { 83 | assertTrue(board[1][y] instanceof Pawn); 84 | assertTrue(board[6][y] instanceof Pawn); 85 | } 86 | } 87 | 88 | @Test 89 | public void testPrepareBoardRightNumberOfPieces() { 90 | Piece[][] board = chessboard.getBoard(); 91 | //Checks middleground for pieces 92 | for (int x = 2; x < 6; x++) { 93 | for (int y = 0; y < 8; y++) { 94 | assertNull(board[x][y]); 95 | } 96 | } 97 | } 98 | 99 | @Test 100 | public void testUpdateMoveSimple() { 101 | Piece oldBoard[][] = chessboard.getBoard(); 102 | //Moves pawn at e2 to e 4. 103 | Position pos = new Position(1, 4); 104 | Position endPos = new Position(3, 4); 105 | Move move = new Move(pos, endPos); 106 | chessboard.updateMove(move); 107 | Piece newBoard[][] = chessboard.getBoard(); 108 | 109 | //Assert board not changes apart from move 110 | for (int x = 0; x < 8; x++) { 111 | for (int y = 0; y < 8; y++) { 112 | if ((x != 1 && y != 4) || (x != 3 && y != 4)) { 113 | assertEquals(oldBoard[x][y], newBoard[x][y]); 114 | } 115 | } 116 | } 117 | 118 | //Check that move has been updated. 119 | assertNull(newBoard[1][4]); 120 | assertTrue(newBoard[3][4] instanceof Pawn); 121 | } 122 | 123 | @Test 124 | public void testUpdateAttackingMove() { 125 | Piece oldBoard[][] = chessboard.getBoard(); 126 | 127 | //Attacks A7 with pawn at a2.. Illegal moves works here 128 | 129 | Position pos = new Position(1, 0); 130 | Position endPos = new Position(6, 0); 131 | Move move = new Move(pos, endPos); 132 | chessboard.updateMove(move); 133 | Piece newBoard[][] = chessboard.getBoard(); 134 | 135 | //Assert board not changes apart from move 136 | for (int x = 0; x < 8; x++) { 137 | for (int y = 0; y < 8; y++) { 138 | if ((x != 1 && y != 0) || (x != 6 && y != 0)) { 139 | assertEquals(oldBoard[x][y], newBoard[x][y]); 140 | } 141 | } 142 | } 143 | 144 | //Check that move has been updated. 145 | assertNull(newBoard[1][0]); 146 | assertTrue(newBoard[6][0] instanceof Pawn); 147 | } 148 | 149 | @Test 150 | public void testContainsEnemyOnEnemyPosition() { 151 | Position white = new Position(0, 0); 152 | Position black = new Position(6, 0); 153 | assertTrue(chessboard.containsEnemy(white, black)); 154 | } 155 | 156 | @Test 157 | public void testContainsEnemyOnFriendlyPosition() { 158 | Position white = new Position(0, 0); 159 | Position friendly = new Position(0, 5); 160 | assertFalse(chessboard.containsEnemy(white, friendly)); 161 | } 162 | 163 | @Test 164 | public void testContainsEnemyOnEmptyPosition() { 165 | Position white = new Position(0, 0); 166 | Position empty = new Position(5, 5); 167 | assertFalse(chessboard.containsEnemy(white, empty)); 168 | 169 | } 170 | 171 | @Test 172 | public void testContainsEnemyOnOwnPosition() { 173 | Position white = new Position(0, 0); 174 | Position self = new Position(0, 0); 175 | assertFalse(chessboard.containsEnemy(white, self)); 176 | } 177 | 178 | @Test 179 | public void testIsFreeOnFreeSquare() { 180 | Position pos = new Position(4,4); 181 | assertTrue(chessboard.isFree(pos)); 182 | } 183 | 184 | @Test 185 | public void testIsFreeOnOccupiedSquare() { 186 | Position pos = new Position(0,0); 187 | assertFalse(chessboard.isFree(pos)); 188 | } 189 | 190 | @Test 191 | public void testIsOutOfBounds() { 192 | Position pos = new Position(5,5); //Mid point 193 | assertFalse(chessboard.isOutOfBounds(pos)); 194 | 195 | pos = new Position(-1,-1); 196 | assertTrue(chessboard.isOutOfBounds(pos)); 197 | 198 | pos = new Position(-1,0); 199 | assertTrue(chessboard.isOutOfBounds(pos)); 200 | 201 | pos = new Position(-1,7); 202 | assertTrue(chessboard.isOutOfBounds(pos)); 203 | 204 | pos = new Position(-1,8); 205 | assertTrue(chessboard.isOutOfBounds(pos)); 206 | 207 | pos = new Position(0,-1); 208 | assertTrue(chessboard.isOutOfBounds(pos)); 209 | 210 | pos = new Position(0,0); 211 | assertFalse(chessboard.isOutOfBounds(pos)); 212 | 213 | pos = new Position(0,7); 214 | assertFalse(chessboard.isOutOfBounds(pos)); 215 | 216 | pos = new Position(0,8); 217 | assertTrue(chessboard.isOutOfBounds(pos)); 218 | 219 | pos = new Position(7,-1); 220 | assertTrue(chessboard.isOutOfBounds(pos)); 221 | 222 | pos = new Position(7,0); 223 | assertFalse(chessboard.isOutOfBounds(pos)); 224 | 225 | pos = new Position(7,7); 226 | assertFalse(chessboard.isOutOfBounds(pos)); 227 | 228 | pos = new Position(7,8); 229 | assertTrue(chessboard.isOutOfBounds(pos)); 230 | 231 | pos = new Position(8,-1); 232 | assertTrue(chessboard.isOutOfBounds(pos)); 233 | 234 | pos = new Position(8,0); 235 | assertTrue(chessboard.isOutOfBounds(pos)); 236 | 237 | pos = new Position(8,7); 238 | assertTrue(chessboard.isOutOfBounds(pos)); 239 | 240 | pos = new Position(8,8); 241 | assertTrue(chessboard.isOutOfBounds(pos)); 242 | } 243 | 244 | @Test 245 | public void testIsInCheck() { 246 | fail("Not implemented"); 247 | } 248 | } 249 | --------------------------------------------------------------------------------