1
+ % -------------------------------------------------------------------------%
2
+ % ------------------------------Notations----------------------------------%
3
+ % -------------------------------------------------------------------------%
4
+ %
5
+ % This is a function to play the game Chess.
6
+ % Fuctions' jobs:
7
+ % 1. CHESS() is the main function to run the game
8
+ % 2. boardSetup() is the function to set up the board
9
+ % to the original placement. Used when "play" or "restart"
10
+ % is called.
11
+ % 3. checkMove() is the function to check if a move is valid.
12
+ % 4. moveOrTake() is the function to either move the assigned piece to
13
+ % an empty space ("e") or take an opposing piece.
14
+ % 5. checkPoints() is the function to check th current points (Positive
15
+ % means White has the advantage, Negative means Black has the advantage).
16
+ % 6. checkTurn() is the function to check which player's turn it is.
17
+ % 7. invalCHESS() is the function to handle invalid inputs.
18
+ % 8. endCHESS() is the function that handles what happens after the game
19
+ % has ended (either by one player winning or the other forfeiting).
20
+ % Usage:
21
+ %
22
+
23
+ % -------------------------------------------------------------------------%
24
+ % --------------------------------CHESS------------------------------------%
25
+ % -------------------------------------------------------------------------%
26
+ %
27
+ function [board ] = CHESS()
28
+ % This is where the main funtion is
29
+
30
+ checkPoints();
31
+ [turn ,winner ] = checkTurn();
32
+ disp(turn ,' to move.' );
33
+ prompt= (' Next move: ' ); % msg before taking the input
34
+ move= input(prompt ,' s' ); % input from keyboard
35
+ if move(ceil(length(move )/2 )) == ' x'
36
+ r= [' a' ' b' ' c' ' d' ' e' ' f' ' g' ' h' ];
37
+ fromLet = move(start );
38
+ toLet = move(end - 1 );
39
+
40
+ fromNumRow = find(r == fromLet );
41
+ fromNumCol = str2double(move(start + 1 ));
42
+ toNumRow = find(r == toLet );
43
+ toNumCol = str2double(move(end ));
44
+
45
+ output = checkMove(fromNumRow ,fromNumCol ,toNumRow ,toNumCol );
46
+ if output == 1
47
+ moveOrTake()
48
+ else
49
+ % sth here
50
+ end
51
+
52
+ end
53
+ end % CHESS()
54
+
55
+ % -------------------------------------------------------------------------%
56
+ % ----------------------------Local Functions------------------------------%
57
+ % -------------------------------------------------------------------------%
58
+ %
59
+ function [board ] = boardSetup()
60
+ % Function to set up the board
61
+
62
+ board = [" wR" " wP" " e" " e" " e" " e" " bP" " bR"
63
+ " wKg" " wP" " e" " e" " e" " e" " bP" " bKg"
64
+ " wB" " wP" " e" " e" " e" " e" " bP" " bB"
65
+ " wQ" " wP" " e" " e" " e" " e" " bP" " bQ"
66
+ " wK" " wP" " e" " e" " e" " e" " bP" " bK"
67
+ " wB" " wP" " e" " e" " e" " e" " bP" " bB"
68
+ " wKg" " wP" " e" " e" " e" " e" " bP" " bKg"
69
+ " wR" " wP" " e" " e" " e" " e" " bP" " bR" ];
70
+ end % boardSetup()
71
+
72
+ function [output ] = checkMove(fromNumRow ,fromNumCol ,toNumRow ,toNumCol )
73
+ % Function to check move
74
+
75
+ slope= abs((toNumRow - fromNumRow )/(toNumCol - fromNumCol )); % abs(rise/run)
76
+
77
+ if isnan(slop )==1 % if the piece move to it's own current spot
78
+ output= 0 ;
79
+ return ;
80
+ end
81
+
82
+ % Indicating Piece Id
83
+ if strlength(board(fromNumRow ,fromNumCol ))==3
84
+ piece= {" Knight" , 3 };
85
+ elseif strlength(board(fromNumRow ,fromNumCol ))==2
86
+ if board(fromNumRow ,fromNumCol )==" wR" || board(fromNumRow ,fromNumCol )==" bR"
87
+ piece= {" Rook" , 5 };
88
+ elseif board(fromNumRow ,fromNumCol )==" wB" || board(fromNumRow ,fromNumCol )==" bB"
89
+ piece= {" Bishop" , 3 };
90
+ elseif board(fromNumRow ,fromNumCol )==" wQ" || board(fromNumRow ,fromNumCol )==" bQ"
91
+ piece= {" Queen" , 9 };
92
+ elseif board(fromNumRow ,fromNumCol )==" wK" || board(fromNumRow ,fromNumCol )==" bK"
93
+ piece= {" King" , 100 };
94
+ elseif board(fromNumRow ,fromNumCol )==" wP"
95
+ piece= {" WhitePawn" , 1 };
96
+ elseif board(fromNumRow ,fromNumCol )==" bP"
97
+ piece= {" BlackPawn" , 1 };
98
+ end
99
+ elseif strlength(board(fromNumRow ,fromNumCol ))==1
100
+ piece= {" empty" , 0 };
101
+ end
102
+
103
+ % Defining Piece movement
104
+ if piece{1 } == " Knight"
105
+ if (slope == 2 || slope == 0.5 ) && abs(toNumRow - fromNumRow )<3 && abs(toNumCol - fromNumCol )<3
106
+ output= 1 ;
107
+ else
108
+ output= 0 ;
109
+ end
110
+
111
+ elseif piece{1 } == " Rook"
112
+ if slope == 0 % toNumRow=fromNumRow
113
+ min2max(fromNumCol ,toNumCol );
114
+ if (toNumCol - fromNumCol )>1
115
+ for j= (fromNumCol + 1 ): (toNumCol - 1 )
116
+ if (board(fromNumRow ,j )==" e" )
117
+ output= 1 ;
118
+ else
119
+ output= 0 ;
120
+ break ;
121
+ end
122
+ end
123
+ elseif (toNumCol - fromNumCol )==1
124
+ output= 1 ;
125
+ end
126
+ elseif isinf(slope )==1 % toNumCol=fromNumCol
127
+ min2max(fromNumRow ,toNumRow );
128
+ if (toNumRow - fromNumRow )>1
129
+ for i= (fromNumRow + 1 ): (toNumRow - 1 )
130
+ if (board(i ,fromNumCol )==" e" )
131
+ output= 1 ;
132
+ else
133
+ output= 0 ;
134
+ break ;
135
+ end
136
+ end
137
+ elseif (toNumRow - fromNumRow )==1
138
+ output= 1 ;
139
+ end
140
+ else
141
+ output= 0 ;
142
+ end
143
+
144
+ elseif piece{1 } == " Bishop"
145
+ if slope == 1 % abs(toNumRow-fromNumRow)=abs(toNumCol-fromNumCol)
146
+ originFromNumRow= fromNumRow ;
147
+ originFromNumCol= fromNumCol ;
148
+ min2max(fromNumRow ,toNumRow );
149
+ min2max(fromNumCol ,toNumCol );
150
+ for i= (fromNumRow + 1 ): (toNumRow - 1 )
151
+ for j= (fromNumCol + 1 ): (toNumCol - 1 )
152
+ if abs((i - originFromNumRow )/(j - originFromNumCol ))==1 && (board(i ,j )==" e" )
153
+ output= 1 ;
154
+ else
155
+ output= 0 ;
156
+ break ;
157
+ end
158
+ end
159
+ end
160
+ else
161
+ output= 0 ;
162
+ end
163
+
164
+ elseif piece{1 } == " Queen"
165
+ % Similar to Rook's movement
166
+ if slope == 0 % toNumRow=fromNumRow
167
+ min2max(fromNumCol ,toNumCol );
168
+ if (toNumCol - fromNumCol )>1
169
+ for j= (fromNumCol + 1 ): (toNumCol - 1 )
170
+ if (board(fromNumRow ,j )==" e" )
171
+ output= 1 ;
172
+ else
173
+ output= 0 ;
174
+ break ;
175
+ end
176
+ end
177
+ elseif (toNumCol - fromNumCol )==1
178
+ output= 1 ;
179
+ end
180
+ elseif isinf(slope )==1 % toNumCol=fromNumCol
181
+ min2max(fromNumRow ,toNumRow );
182
+ if (toNumRow - fromNumRow )>1
183
+ for i= (fromNumRow + 1 ): (toNumRow - 1 )
184
+ if (board(i ,fromNumCol )==" e" )
185
+ output= 1 ;
186
+ else
187
+ output= 0 ;
188
+ break ;
189
+ end
190
+ end
191
+ elseif (toNumRow - fromNumRow )==1
192
+ output= 1 ;
193
+ end
194
+ else
195
+ output= 0 ;
196
+ end
197
+ % Similar to Bishop's movement
198
+ if slope == 1 % abs(toNumRow-fromNumRow)=abs(toNumCol-fromNumCol)
199
+ originFromNumRow= fromNumRow ;
200
+ originFromNumCol= fromNumCol ;
201
+ min2max(fromNumRow ,toNumRow );
202
+ min2max(fromNumCol ,toNumCol );
203
+ for i= (fromNumRow + 1 ): (toNumRow - 1 )
204
+ for j= (fromNumCol + 1 ): (toNumCol - 1 )
205
+ if abs((i - originFromNumRow )/(j - originFromNumCol ))==1 && (board(i ,j )==" e" )
206
+ output= 1 ;
207
+ else
208
+ output= 0 ;
209
+ break ;
210
+ end
211
+ end
212
+ end
213
+ else
214
+ output= 0 ;
215
+ end
216
+
217
+ elseif piece{1 } == " King"
218
+ if slope == 0 && abs(toNumRow - fromNumRow )<2 && abs(toNumCol - fromNumCol )<2 % toNumRow=fromNumRow
219
+ output= 1 ;
220
+ else
221
+ output= 0 ;
222
+ end
223
+
224
+ end % Defining Piece movement
225
+ end % checkMove()
226
+
227
+ function [board ] = moveOrTake()
228
+ % Function to move the assinged piece
229
+
230
+ end % moveOrTake()
231
+
232
+ function [] = checkPoints()
233
+ % Function to check points
234
+
235
+ % Piece Value
236
+ wP= 1 ;
237
+ bP= 1 ;
238
+ wKg= 3 ;
239
+ bKg= 3 ;
240
+ wB= 3 ;
241
+ bB= 3 ;
242
+ wR= 5 ;
243
+ bR= 5 ;
244
+ wQ= 9 ;
245
+ bQ= 9 ;
246
+ e= 0 ;
247
+ wK= 100 ;
248
+ bK= 100 ;
249
+ end % checkPoints
250
+
251
+ function [turn ,winner ] = checkTurn()
252
+ % Function to check player's turn
253
+
254
+ if mod(count / 2 ) ~= 0
255
+ turn = " White" ;
256
+ else
257
+ turn = " Black" ;
258
+ end
259
+ end % checkTurn()
0 commit comments