forked from bdsword/MatlabGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadSgf.m
49 lines (48 loc) · 1.31 KB
/
readSgf.m
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
function history = readSgf( filePath )
fileID = fopen(filePath,'r');
history = {};
if fileID<0
msgbox('Open sgf file error.');
return;
end
while ~feof(fileID)
inC = fscanf(fileID,'%c',1);
if strcmp(inC,';')==true
nodeName = readToLeftQuart(fileID);
nodeContent=readToRightQuart(fileID);
chessColor=0;
if strcmp(nodeName,'B')
chessColor=1;
elseif strcmp(nodeName,'W')
chessColor=2;
end
if chessColor~=0
if isempty(nodeContent)
continue;
end
history{end+1}=[nodeContent(1)-'a'+1,nodeContent(2)-'a'+1,chessColor];
end
end
end
fclose(fileID);
function nodeTag = readToLeftQuart(fileID)
nodeTag = '';
while true
inCC = fscanf(fileID,'%c',1);
if strcmp(inCC,'[')==true
break;
end
nodeTag = [nodeTag,inCC];
end
end
function content = readToRightQuart(fileID)
content = '';
while true
inCC = fscanf(fileID,'%c',1);
if strcmp(inCC,']')==true
break;
end
content = [content,inCC];
end
end
end