{load the question datafile}
Procedure loadfile;
Begin;
randomize;
z:=Random(5)+1;
Case ch Of
'1':assign(datafile,'qeasy.txt');
'2':assign(datafile,'qmedium.txt');
'3':assign(datafile,'qhard.txt');
'4':assign(datafile,'qchall.txt');
'5':assign(insfile,'instr.txt')
End;
Reset(datafile);
For i:=1 To (z-1)*9 Do
readln(datafile);
For i:=1 To 9 Do
Begin;
For j:=1 To 9 Do
Begin
read(datafile,qeasy[j,i]);
save[j,i]:=qeasy[j,i];
writexy(j*4-2+left,i*2-1+up,qeasy[j,i])
End;
readln(datafile);
End;
Close(datafile)
End;
本段為載入題庫之子程式。
RANDOMIZE意即隨機抽取(題目),這樣每一次抽取的題目不會重複。次行的語句意義為在五條題目中(每一個等級都有五條題目)隨即抽取一題。由於電腦會由第0行開始讀題,因此需要增補『+1』 ,以使電腦可以從第一行開始抽題。CASE OF用于多條件語句,其作用等同於IFTHEN; IFTHEN等等。ASSIGN是讀取的命令字符,屬於保留字,括號內的即文本文檔名稱。為了不使多層數據一起出現,需要加一項RESET()來重設螢幕內容。
下一行和再下三行都是限定抽取題目範圍的命令語句,隨後顯示隨即抽取的題目。『J,I』變量是佈局圖中每一個非空格的坐標,只有寫了這一行,程式才可以
將一條題目中的所有數據一一讀取。
{load the answer datafile}
Procedure loadanswer;
Begin;
Assign(datafile,'aeasy.txt');
reset(datafile);
For i:=1 To (z-1)*9 Do
readln(datafile);
For i:=1 To 9 Do
Begin;
For j:=1 To 9 Do
read(datafile,aeasy[j,i]);
readln(datafile);
End;
Close(datafile)
End;
本段為讀取答案的部分。
{marks and conclusion}
Procedure calculation;
Begin
If memory[x,y]=False
Then
Begin
If ch=aeasy[x,y]
Then Begin
mark:=mark+10;
memory[x,y]:=True;
gotoxy(1,23);
TextBackground(BLACK);
textColor(Yellow);
writeln('Exactly!Right answer ');
writeln('You got ',mark,' marks. ');
{ Program to demonstrate the TextColor function. }
qeasy[x,y]:=ch
End
Else Begin
memory[x,y]:=True;
gotoxy(1,23);
TextBackground(BLACK);
TextColor(WHITE);
writeln('This is a wrong answer.');
End;
End
Else Begin
If ch<>aeasy[x,y]
Then Begin
textcolor(RED);
gotoxy(1,23);
writeln('Wrong! ')
End;
If ch=aeasy[x,y]
Then Begin
gotoxy(1,23);
TextColor(YELLOW);
writeln('Right answer. ');
writeln;
qeasy[x,y]:=ch
End;
End;
End;
本段為檢驗答案對於否的子程式。
{second input}
Procedure scd;
Begin
For i:=1 To 9 Do
For j:=1 To 9 Do
memory[j,i]:=False
End;
本段為為布爾值賦值的子程式,與上一段相聯繫。
{quit}
Procedure quit;
Begin
gotoxy(15,5);
TextColor(GREEN);
writeln('*==================================================*');
gotoxy(15,6);
writeln('| |');
gotoxy(15,7);
writeln('| |');
gotoxy(15,8);
TextColor(WHITE);
writeln('| Do you want to end the game? |');
gotoxy(15,9);
TextColor(GREEN);
writeln('| |');
gotoxy(15,10);
TextColor(WHITE);
writeln('| Push the button to select |');
gotoxy(15,10);
TextColor(YELLOW);
gotoxy(29,10);
writeln('Y/N');
gotoxy(15,11);
TextColor(GREEN);
writeln('| |');
gotoxy(15,12);
writeln('| |');
gotoxy(15,13);
writeln('| |');
gotoxy(15,14);
writeln('| |');
gotoxy(15,15);
writeln('| |');
gotoxy(15,16);
TextColor(WHITE);
writeln('| One sentense for everyday |');
gotoxy(15,17);
writeln('| "Think carefully before quiting the game." |');
gotoxy(15,18);
TextColor(GREEN);
writeln('| |');
gotoxy(15,19);
writeln('*==================================================*');
press:=readkey;
readln(press);
writeln(press);
If (press='Y') Or (press='y')
Then Begin
ClrScr;
TextColor(YELLOW);
gotoxy(29,13);
writeln('See you next time!');
readln;
Repeat
Until KeyPressed;
ClrScr
End;
If (press='N') Or (press='n')
Then Begin
ClrScr;
mapview;
For i:=1 To 9 Do
For j:=1 To 9 Do
writexy(j*4-2+left,i*2-1+up,save[j,i])
End;
End;
本段為顯示退岀版面的子程式。
{main part}
Begin
start;
title;
enter;
gotoxy(25,10);
writeln('Choose the difficulty');
gotoxy(25,13);
writeln('"1"-Easy "2"-Medium');
gotoxy(25,15);
writeln('"3"-Hard "4"-Challenger');
gotoxy(25,17);
writeln('"5"-Instruction');
readln;
Repeat
Until KeyPressed;
ch:=readkey;
Case ch Of
'1':assign(datafile,'qeasy.txt');
'2':assign(datafile,'qmedium.txt');
'3':assign(datafile,'qhard.txt');
'4':assign(datafile,'qchall.txt');
'5':Begin
clrscr;
GotoXY(1,1);
assign(insfile,'instr.txt');
reset(insfile);
While Not eof(insfile) Do
Begin
readln(insfile,content);
writeln(content);
End;
Repeat;
Until KeyPressed;
ClrScr
End;
End;
ch:=ReadKey;
Begin
Window(20,1,50,30);
mapview;
loadfile;
loadanswer;
sec:=0;
min:=0;
hr:=0;
x:=1;
y:=1;
mark:=0;
Repeat;
Repeat;
gotoxy(x*4-2+left,y*2-1+up);
ch:=readkey;
{only if..then sentense can be used}
If (ch=
#0) Then ch:=readkey;
If (ch=
#75) Then x:=x-1;
If (ch=
#77) Then x:=x+1;
If (ch=
#72) Then y:=y-1;
If (ch=
#80) Then y:=y+1;
If (x<1) Then x:=9;
If (x>9) Then x:=1;
If (y<1) Then y:=9;
If (y>9) Then y:=1;
If (ch>='1') And (ch<='9') And (qeasy[x,y]=' ')
Then Begin
write(ch);
save[x,y]:=ch;
calculation;
End;
window(1,15,15,2);
{Time counting}
Begin
Repeat;
Begin
If sec>=60
Then Begin
min:=min+1;
sec:=0
End;
If min>=60
Then Begin
hr:=hr+1;
min:=0
End;
Delay(10); {Wait one second}
gotoxy(60,5);
sec:=sec+0.008;
TextBackground(BLACK);
TextColor(WHITE);
writeln(hr:2:0,' hr ',min:2:0,' min ',sec:2:0,' sec');
gotoxy(x*4-2+left,y*2-1+up)
End;
Until KeyPressed;
End;
{Ready to quit}
Until ch=
#27;
quit;
Until (press='Y') Or (press='y');
readln
End;
End.
本段為主程式。