Skip to content

Commit a3176a7

Browse files
Add files via upload
1 parent cffde9f commit a3176a7

27 files changed

+1773
-0
lines changed

CHESS.m

+259
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
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()

Untitled2.m

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function [] = exam_2018 (task)
2+
task=4;
3+
if(task==4)
4+
syms x y z w
5+
eqn1=x+2*y+3*z+4*w==5;
6+
eqn2=2*x+3*y+4*z+5*w==6;
7+
eqn3=3*x-4*y-5*z-6*w==7;
8+
eqn4=5*x-4*y+3*z-2*w==1;
9+
10+
sol = solve([eqn1, eqn2, eqn3, eqn4], [x, y, z, w]);
11+
xSol = sol.x;
12+
ySol = sol.y;
13+
zSol = sol.z;
14+
wSol = sol.w;
15+
x=xSol;
16+
y=ySol;
17+
z=zSol;
18+
w=wSol;
19+
20+
disp(['x= ', x]);
21+
disp(['y= ', y]);
22+
disp(['z= ', z]);
23+
disp(['w= ', w]);
24+
end
25+
end

class20180109_IT2N2_elfun.m

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
%commenting can be done by typing sign %
2+
a=2+7
3+
b=3-9;%semicolon prevents printing on screen
4+
b%printing afterwards
5+
A=[3 7];%a table of numbers
6+
A
7+
B=[3;7]%semicolon is a row separator
8+
C=[3,2.5;4 6;6,0]%column separator is a comma or blank space
9+
operations=[2+4 4-9;...%addition
10+
2*4 4/4;...%multiplication
11+
2.*4 [2]*[4];...%tables can consist of tables if sizes agree
12+
4^2 4^0.5;...%power and root
13+
8^(1/3) 2^(-2);...
14+
sqrt(2) sqrt(-1)]%running into complex numbers
15+
%NOTE complex valued sqrt(-1) changes
16+
%the type of ALL the elements
17+
%of the table to be complex
18+
19+
help sqrt%the manual
20+
21+
moreoperations=...
22+
[log(2) log(exp(1));...%logarithm
23+
exp(1) exp(2);...%natural exponential
24+
log(8)/log(2) log10(8)/log10(2);...%arbitrary base logarithm
25+
sin(90) sind(90);...%argument in rad by default
26+
sin(pi/2) pi;...%pi=3.141592654...
27+
asin(1) pi/2;...%inverse trigonometric
28+
atan(-1) atan2(1,-1);...
29+
atand(-1) atan(-1)+pi]
30+
31+
z1=3+4i;
32+
z2=3+4*i;
33+
complex1=[z1;z2;i*i;j*j;i*j]
34+
35+
complex2=[z1,real(z1),imag(z1),conj(z1),conj(z1)*z1,abs(z1)].'
36+
%' stands for matrix (conjugate) TRANSPOSE (in case of complex valued elements)
37+
38+
%correct the error here according the message
39+
E=[1,2;3 0]

class20180110_IT2N2_visual.m

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function [ ] = class20180110_IT2N2_visual (fig)
2+
%Whatever you write here as comments
3+
%will show in the manual ("help class20180110_IT2N2_visual").
4+
5+
fig=1;
6+
x=[-1 3 2];
7+
y=[3 4 0];
8+
x(end+1)=x(1);%copying the first element to last (added) element
9+
y(end+1)=y(1);
10+
11+
%drawing a circle
12+
N=700;%no of points
13+
R=2;%the radius
14+
dx=2*R/(N-1);%following step size assuming constant wrt x
15+
X=[-R:dx:R];%choosing x-coordinates in Cartesian system
16+
Y=sqrt(R^2-X.^2);%y-coordinates from Pythagoras
17+
18+
figure(fig);%open frame
19+
clf;%clear figure
20+
hold on;%allow consecutive plots without erasing the previous
21+
plot(x,y,'bo-');%'xxx' is a string, see "help plot"
22+
plot(y,x,'rs--');
23+
plot(X,Y,'go-');%circle, upper half
24+
plot(X,-Y,'go-');%circle, lower half
25+
26+
figure(fig+1);
27+
clf;
28+
hold on;
29+
xSL=[-10^25:10^24:10^26];
30+
plot(xSL,straightline(xSL,2,10^26),'bo-');%calling self made function within a function...
31+
plot(xSL,parabola(xSL),'rs-');
32+
33+
end%class20180110_IT2N2_visual
34+
35+
%...and defining them
36+
function [y] = straightline (x,a,b)
37+
y=a+b*x;
38+
end%straightline
39+
40+
function [y] = parabola (x)
41+
y=1*x.^2+5+x+2;
42+
end%parabola

0 commit comments

Comments
 (0)