A Chess Engine
Total Page:16
File Type:pdf, Size:1020Kb
A Chess Engine Paul Dailly Dominik Gotojuch Neil Henning Keir Lawson Alec Macdonald Tamerlan Tajaddinov University of Glasgow Department of Computing Science Sir Alwyn Williams Building Lilybank Gardens Glasgow G12 8QQ March 18, 2008 Abstract Though many computer chess engines are available, the number of engines using object orientated approaches to the problem is minimal. This report documents an implementation of an object oriented chess engine. Traditionally, in order to gain the advantage of speed, the C language is used for implementation, however, being an older language, it lacks many modern language features. The chess engine documented within this report uses the modern Java language, providing features such as reflection and generics that are used extensively, allowing for complex but understandable code. Also of interest are the various depth first search algorithms used to produce a fast game, and the numerous functions for evaluating different characteristics of the board. These two fundamental components, the evaluator and the analyser, combine to produce a fast and relatively skillful chess engine. We discuss both the design and implementation of the engine, along with details of other approaches that could be taken, and in what manner the engine could be expanded. We conclude by examining the engine empirically, and from this evaluation, reflecting on the advantages and disadvantages of our chosen approach. Education Use Consent We hereby give our permission for this project to be shown to other University of Glasgow students and to be distributed in an electronic format. Please note that you are under no obligation to sign this declaration, but doing so would help future students. Name: Signature: Name: Signature: Name: Signature: Name: Signature: Name: Signature: Name: Signature: 1 Contents 1 Introduction 7 1.1 Motivation . 7 1.2 Aims . 7 1.3 Background . 8 1.4 Preliminaries . 8 1.5 Outline . 9 2 Background 10 2.1 History of Computer Chess . 10 2.2 Traditional Engine Design . 12 2.3 Present Day . 16 2.3.1 Engine Ratings . 16 2.3.2 Current Chess Engines . 16 2.3.3 Man Versus Machine . 17 3 Design 19 3.1 Overview . 19 3.1.1 Chess Engine Communication Protocol . 19 3.1.2 Major Classes . 19 3.1.3 Threading . 20 3.1.4 Rejected Designs . 20 3.2 Board . 21 2 3.2.1 Board representation . 21 3.2.2 Move representation . 24 3.2.3 Piece representation . 24 3.3 Analyser . 25 3.3.1 Time Control . 25 3.3.2 Alpha Beta Pruning . 25 3.4 Evaluator . 26 3.4.1 Material Evaluation . 27 3.4.2 Passed Pawns . 27 3.4.3 Crowded Files . 28 3.4.4 Pawn Quantity . 28 3.4.5 Crippled Pawns . 29 3.4.6 Isolated Pawns . 30 3.4.7 King Pawns . 30 3.4.8 Piece Freedom . 31 3.4.9 Center Control . 33 3.4.10 King Attack . 33 3.4.11 Center King . 34 3.4.12 Has Castled . 35 3.4.13 End Game Multiplier . 36 4 Implementation 37 4.1 Overview . 37 4.1.1 Language Choice . 37 4.1.2 Language Style . 37 4.1.3 Game Class . 37 4.1.4 Communication between objects . 37 4.1.5 Threads . 38 3 4.1.6 Chess Engine Communication Protocol versus Bespoke UI . 38 4.2 Board . 38 4.2.1 Board representation . 39 4.2.2 Move representation . 40 4.2.3 Piece representation . 42 4.2.4 Move legality . 48 4.2.5 Checks, checkmates and stalemates . 51 4.2.6 Castling . 52 4.2.7 Promotions . 54 4.2.8 En Passant . 56 4.2.9 Move committing and reversing . 58 4.2.10 Legal move generation . 59 4.2.11 Board hashing . 60 4.3 Analyser . 63 4.4 Evaluator . 65 4.4.1 Material Evaluation . 67 4.4.2 Passed Pawns . 68 4.4.3 Crowded Files . 69 4.4.4 Pawn Quantity . 70 4.4.5 Crippled Pawns . 71 4.4.6 Isolated Pawns . 71 4.4.7 King Pawns . 72 4.4.8 Piece Freedom . 73 4.4.9 Center Control . 74 4.4.10 King Attack . 75 4.4.11 Center King . 76 4.4.12 Has Castled . 76 4 4.4.13 End Game Multiplier . 77 5 Evaluation 78 5.1 Testing Rules . 78 5.1.1 Outline . 78 5.1.2 Piece Movement . 78 5.1.3 Captures . 84 5.1.4 End of Game Recognition . 85 5.2 Player vs Computer . 87 5.3 Computer vs Computer . 88 5.3.1 GNUChess vs Chess Mantis . 89 5.3.2 Crafty vs Chess Mantis . 90 5.3.3 Beowulf vs Chess Mantis . 92 6 Conclusion 93 Appendices 95 A Contributions 95 A.1 Paul Dailly . 95 A.2 Dominik Gotojuch . 95 A.3 Neil Henning . 96 A.4 Keir Lawson . 97 A.5 Alec Macdonald . 97 A.6 Tamerlan Tajaddinov . 98 B Status Report 99 B.1 Product . 99 B.2 Deficiencies . 99 B.2.1 XBoard Commands . ..