-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.txt
179 lines (149 loc) · 6.45 KB
/
README.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
I wrote this chess program as part of an interview for a scala programming
position at Novus (www.novus.com). They asked me to share my answer with them
via github.
I only had about a day to do it, so it is far from perfect. It doesn't
implement castling, en-passant, pawn promotion, there is no real UI, and there
is no AI.
UPDATE: I later implemented a breadth-first mini-max search for a simple AI
player.
UPDATE: later implemented castling and pawn promotion. Still no en-passant
UPDATE: I made this a bit more generic, and added a connect4 game that can be
played by the same minimax AI player. Sample usage below is a bit out of date
because the package name for the import has changed. I might rename this
project, so current github URL subject to change.
My Requirements:
====================================================================
1) You choose the programming language (Scala is a huge plus thou)
2) We care about:
-class design (under Scala, it also means correct use of immutable
vs mutable class implementations)
-idiomatic language use
-code clarity
3) We don't care if the UI is "beautiful" or not. But we do care if the
code is "beautiful"
4) It should run (as a text-based command line application, as a web
app, or use a GUI)
5) There is no need to implement advanced moves such as castling, en
passant, or promotion, but it should be easy to add these
6) I recommend implementing "check" and "check mate"
====================================================================
To play, just open an `sbt console` and make API calls directly. The
coordinates are indexed such that "a1" is (0,0), and "h8" is (7,7).
Example output from sbt is below. The piece names begin with "w" or "b" to
signify white or black pieces. Sample usage can also be found in the unit
tests. Exceptions are thrown on invalid moves.
scala> brian@brian-laptop:~/unison/Projects/chess$ sbt clean compile test
[info] Set current project to default-64fda4 (in build file:/home/brian/unison/Projects/chess/)
[success] Total time: 0 s, completed Sep 4, 2012 10:42:48 AM
[info] Updating {file:/home/brian/unison/Projects/chess/}default-64fda4...
[info] Resolving org.scala-lang#scala-library;2.9.1 ...
[info] Resolving org.scalatest#scalatest_2.9.1;1.8 ...
[info] Done updating.
[info] Compiling 1 Scala source to /home/brian/unison/Projects/chess/target/scala-2.9.1/classes...
[success] Total time: 11 s, completed Sep 4, 2012 10:42:59 AM
[info] Compiling 1 Scala source to /home/brian/unison/Projects/chess/target/scala-2.9.1/test-classes...
[info] ChessSpec:
[info] A newly created default chess board
[info] - should have appropriate pieces
[info] - should throw InvalidMoveException if an illegal move is attempted
[info] - should throw invalid move exception if an opponents piece is moved
[info] - should have 20 possible moves
[info] - should find a king in the right place
[info] a particular game of chess
[info] - should detect check, but not check mate
[info] a foolsmate game
[info] - should be detected as checkmate
[info] an empty board
[info] - should be detected as a stalemate
[info] Passed: : Total 8, Failed 0, Errors 0, Passed 8, Skipped 0
[success] Total time: 4 s, completed Sep 4, 2012 10:43:03 AM
brian@brian-laptop:~/unison/Projects/chess$
brian@brian-laptop:~/unison/Projects/chess$ sbt console
[info] Set current project to default-64fda4 (in build file:/home/brian/unison/Projects/chess/)
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import net.brianvaughan.scala.chess._
import net.brianvaughan.scala.chess._
scala> var board = BoardState.startingBoard
board: net.brianvaughan.scala.chess.BoardState =
bRo|bKn|bBi|bQu|bKi|bBi|bKn|bRo|
bPa|bPa|bPa|bPa|bPa|bPa|bPa|bPa|
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
wPa|wPa|wPa|wPa|wPa|wPa|wPa|wPa|
wRo|wKn|wBi|wQu|wKi|wBi|wKn|wRo|
scala> board = board.movePiece(4,1,4,3)
board: net.brianvaughan.scala.chess.BoardState =
bRo|bKn|bBi|bQu|bKi|bBi|bKn|bRo|
bPa|bPa|bPa|bPa|bPa|bPa|bPa|bPa|
| | | | | | | |
| | | | | | | |
| | | |wPa| | | |
| | | | | | | |
wPa|wPa|wPa|wPa| |wPa|wPa|wPa|
wRo|wKn|wBi|wQu|wKi|wBi|wKn|wRo|
scala> board = board.movePiece(1,6,1,5)
board: net.brianvaughan.scala.chess.BoardState =
bRo|bKn|bBi|bQu|bKi|bBi|bKn|bRo|
bPa| |bPa|bPa|bPa|bPa|bPa|bPa|
|bPa| | | | | | |
| | | | | | | |
| | | |wPa| | | |
| | | | | | | |
wPa|wPa|wPa|wPa| |wPa|wPa|wPa|
wRo|wKn|wBi|wQu|wKi|wBi|wKn|wRo|
scala> board = board.movePiece(5,0,2,3)
board: net.brianvaughan.scala.chess.BoardState =
bRo|bKn|bBi|bQu|bKi|bBi|bKn|bRo|
bPa| |bPa|bPa|bPa|bPa|bPa|bPa|
|bPa| | | | | | |
| | | | | | | |
| |wBi| |wPa| | | |
| | | | | | | |
wPa|wPa|wPa|wPa| |wPa|wPa|wPa|
wRo|wKn|wBi|wQu|wKi| |wKn|wRo|
scala> board = board.movePiece(3,6,3,4)
board: net.brianvaughan.scala.chess.BoardState =
bRo|bKn|bBi|bQu|bKi|bBi|bKn|bRo|
bPa| |bPa| |bPa|bPa|bPa|bPa|
|bPa| | | | | | |
| | |bPa| | | | |
| |wBi| |wPa| | | |
| | | | | | | |
wPa|wPa|wPa|wPa| |wPa|wPa|wPa|
wRo|wKn|wBi|wQu|wKi| |wKn|wRo|
scala> board = board.movePiece(2,3,3,4)
board: net.brianvaughan.scala.chess.BoardState =
bRo|bKn|bBi|bQu|bKi|bBi|bKn|bRo|
bPa| |bPa| |bPa|bPa|bPa|bPa|
|bPa| | | | | | |
| | |wBi| | | | |
| | | |wPa| | | |
| | | | | | | |
wPa|wPa|wPa|wPa| |wPa|wPa|wPa|
wRo|wKn|wBi|wQu|wKi| |wKn|wRo|
scala> board = board.movePiece(5,6,5,4)
board: net.brianvaughan.scala.chess.BoardState =
bRo|bKn|bBi|bQu|bKi|bBi|bKn|bRo|
bPa| |bPa| |bPa| |bPa|bPa|
|bPa| | | | | | |
| | |wBi| |bPa| | |
| | | |wPa| | | |
| | | | | | | |
wPa|wPa|wPa|wPa| |wPa|wPa|wPa|
wRo|wKn|wBi|wQu|wKi| |wKn|wRo|
scala> board = board.movePiece(4,3,5,4)
board: net.brianvaughan.scala.chess.BoardState =
bRo|bKn|bBi|bQu|bKi|bBi|bKn|bRo|
bPa| |bPa| |bPa| |bPa|bPa|
|bPa| | | | | | |
| | |wBi| |wPa| | |
| | | | | | | |
| | | | | | | |
wPa|wPa|wPa|wPa| |wPa|wPa|wPa|
wRo|wKn|wBi|wQu|wKi| |wKn|wRo|