home *** CD-ROM | disk | FTP | other *** search
- { Name: OTHELLO.PAS }
- { Programmer: Calvin A. Jones }
- { Date written: 11/24/84 }
- { Date modified: / / }
- { Description: Original PET version modified for Turbo Pascal }
- { under MS-DOS. }
- program Othello;
-
- const
- fff = green;
- bbb = black;
-
- c: array[1..2] of integer = (blue,red);
- i4: array[0..7] of integer = (-1, 0, 1,1,1,0,-1,-1);
- j4: array[0..7] of integer = (-1,-1,-1,0,1,1, 1, 0);
-
- type
- charset = set of char;
-
- var
- ch: char;
- sc: array[1..2] of integer;
- a: array[0..9,0..9] of integer;
- player: array[1..2] of string[15];
- n1,np,op,pt,s1,s2,s3,s4,s5: integer;
- xl,xh,yl,yh: integer;
- done,over: boolean;
-
- procedure getchar(var ch: char; range: charset);
- begin
- repeat
- read(kbd,ch);
- if ch=#27 then halt;
- ch:=upcase(ch);
- until ch in range;
- end;
-
- procedure score;
- var
- i,j: integer;
- begin
- window(1,1,40,20);
- textbackground(cyan);
- for i:=1 to 8 do
- for j:=1 to 8 do
- if a[i,j]<>0 then
- begin
- textcolor(c[a[i,j]]);
- gotoxy(4*i+1,2*j+3); write(chr(a[i,j]));
- end;
- textcolor(c[1]);
- gotoxy(38,5); write(sc[1]:2);
- textcolor(c[2]);
- gotoxy(38,19); write(sc[2]:2);
- textcolor(fff); textbackground(bbb);
- if (sc[op]=0) or (n1=64) then
- begin
- window(1,21,40,24);
- clrscr;
- writeln(player[1],' has ',sc[1],' pieces');
- writeln(player[2],' has ',sc[2],' pieces');
- if sc[1]=sc[2] then writeln('It is a tie !!')
- else
- begin
- if sc[1]>sc[2] then write(player[1]) else write(player[2]);
- writeln(' won !!!');
- end;
- over:=true;
- write('Do you want to play again? ');
- getchar(ch,['Y','N']);
- if ch='N' then done:=true;
- end;
- end;
-
- procedure intro;
- var
- i: integer;
- begin
- textmode(c40);
- textcolor(white); textbackground(cyan);
- gotoxy(19,5); write('IBM');
- gotoxy(12,7); write('Personal Computer');
- gotoxy(8,10); write('╒═══════════════════════╕');
- gotoxy(8,11); write('│ -*- OTHELLO -*- │');
- gotoxy(8,12); write('│ │');
- gotoxy(8,13); write('│ Author: Unkown │');
- gotoxy(8,14); write('│ Adapted by: P. Leabo │');
- gotoxy(8,15); write('│Enhanced by: R. Vollmer│');
- gotoxy(8,16); write('│Pacsal Ver.: C. Jones │');
- gotoxy(8,17); write('╘═══════════════════════╛');
- gotoxy(5,20); write('Orig. written for: PET computer');
- gotoxy(10,21); write('Last update: 11/21/84');
- i:=0;
- repeat i:=i+1 until (i=maxint) or keypressed;
- if keypressed then read(kbd,ch);
- end;
-
- procedure instructions;
- begin
- textmode(c80);
- textcolor(7); textbackground(1);
- clrscr;
- window(10,1,70,24);
- gotoxy(20,4); writeln('GREETINGS FROM OTHELLO');
- writeln;
- writeln('Othello is played on an 8 x 8 board, rows numbered 1 to 8');
- writeln('and columns numbered A to H. The initial configuration is');
- writeln('all blank except for the four center squares. Try to place');
- writeln('your pieces so that it outflanks your opponent, creating');
- writeln('horizontal, vertical, or diagonal runs of opposing pieces,');
- writeln('turning them into yours.');
- writeln;
- writeln('Make your move by entering a number for a row and a letter');
- writeln('for a column.');
- writeln;
- writeln('Note: You must capture at least one of your opponent''s');
- writeln('pieces. If it is not possible, you forfeit your move by');
- writeln('typing a <CR> for your move.');
- writeln; writeln;
- write('Press any key to continue...'); read(kbd,ch);
- end;
-
- procedure initialize;
- var
- i,j: integer;
- begin
- window(1,1,80,24);
- textmode(c40);
- done:=false; over:=false;
- xl:=3; xh:=6;
- yl:=3; yh:=6;
- write('How many players? (1 or 2) ');
- getchar(ch,['1','2']); writeln(ch);
- np:=ord(ch)-ord('0');
- writeln;
- write('Player 1''s name: '); readln(player[1]);
- if np=2 then
- begin
- write('Player 2''s name: '); readln(player[2]);
- end;
- if np<>2 then
- begin
- player[2]:='Computer';
- writeln; write('Should I play my best? ');
- getchar(ch,['Y','N']);
- if ch='Y' then
- begin
- writeln('YES');
- s2:=2; s4:=1; s5:=-2;
- end
- else
- begin
- writeln('NO');
- s2:=0; s4:=0; s5:=0;
- end;
- end;
- for i:=0 to 9 do
- for j:=0 to 9 do a[i,j]:=0;
- a[4,4]:=1; a[4,5]:=2;
- a[5,4]:=2; a[5,5]:=1;
- n1:=4;
- for i:=1 to 2 do sc[i]:=2;
- end;
-
- procedure draw_board;
- begin
- clrscr;
- textcolor(magenta); textbackground(blue);
- gotoxy(13,1); writeln('O T H E L L O');
- gotoxy(1,3);
- textcolor(brown); textbackground(lightgray);
- writeln(' 1 2 3 4 5 6 7 8 ');
- writeln(' ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗');
- writeln('A ║ ║ ║ ║ ║ ║ ║ ║ ║');
- writeln(' ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣');
- writeln('B ║ ║ ║ ║ ║ ║ ║ ║ ║');
- writeln(' ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣');
- writeln('C ║ ║ ║ ║ ║ ║ ║ ║ ║');
- writeln(' ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣');
- writeln('D ║ ║ ║ ║ ║ ║ ║ ║ ║');
- writeln(' ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣');
- writeln('E ║ ║ ║ ║ ║ ║ ║ ║ ║');
- writeln(' ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣');
- writeln('F ║ ║ ║ ║ ║ ║ ║ ║ ║');
- writeln(' ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣');
- writeln('G ║ ║ ║ ║ ║ ║ ║ ║ ║');
- writeln(' ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣');
- writeln('H ║ ║ ║ ║ ║ ║ ║ ║ ║');
- writeln(' ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝');
- textcolor(blue);
- gotoxy(36,5); write(chr(1));
- textcolor(red);
- gotoxy(36,19); write(chr(2));
- score;
- end;
-
- function test_move(x,y: integer): boolean;
- var i,j: integer;
- begin
- test_move:=false;
- for i:=-1 to 1 do
- for j:=-1 to 1 do
- if a[x+i,y+j]=op then test_move:=true;
- end;
-
- procedure count_flank(x,y,z: integer);
- var i5,j5,i6,j6,k,k1: integer;
- begin
- s1:=0; k:=0;
- while k<8 do
- begin
- s3:=0;
- i5:=i4[k]; j5:=j4[k]; i6:=x+i5; j6:=y+j5;
- if a[i6,j6]=op then
- begin
- repeat
- s3:=s3+1;
- i6:=i6+i5; j6:=j6+j5;
- until (a[i6,j6]=0) or (a[i6,j6]=pt);
- if a[i6,j6]=pt then
- begin
- s1:=s1+s3;
- if z=1 then
- begin
- i6:=x; j6:=y;
- for k1:=0 to s3 do
- begin
- a[i6,j6]:=pt;
- i6:=i6+i5; j6:=j6+j5;
- end;
- end;
- end;
- end;
- k:=k+1;
- end;
- end;
-
- procedure show_move(x,y: integer);
- begin
- window(1,1,40,20);
- gotoxy(4*x+1,2*y+3);
- textcolor(c[pt]+blink); textbackground(lightgray);
- write(chr(pt));
- textcolor(fff); textbackground(bbb);
- delay(2500);
- window(1,21,40,24);
- gotoxy(1,1);
- count_flank(x,y,1);
- sc[pt]:=sc[pt]+s1+1;
- sc[op]:=sc[op]-s1;
- n1:=n1+1;
- end;
-
- procedure computer_move;
- var i,j,b1,i3,j3: integer;
- begin
- window(1,21,40,25);
- clrscr;
- writeln;
- textcolor(fff+blink);
- writeln('I am thinking!');
- textcolor(fff);
- b1:=-1; i3:=0; j3:=0;
- for i:=xl to xh do
- for j:=yl to yh do
- if a[i,j]=0 then
- if test_move(i,j) then
- begin
- count_flank(i,j,0);
- if s1>0 then
- begin
- if (i=1) or (i=8) then s1:=s1+s2;
- if (j=1) or (j=8) then s1:=s1+s2;
- if (i=2) or (i=7) then s1:=s1+s5;
- if (j=2) or (j=7) then s1:=s1+s5;
- if (i=3) or (i=6) then s1:=s1+s4;
- if (j=3) or (j=6) then s1:=s1+s4;
- if s1>=b1 then
- if (s1>b1) or (random>0.5) then
- begin
- b1:=s1; i3:=i; j3:=j;
- end;
- end;
- end;
- if (i3 in [1..8]) and (j3 in [1..8]) then
- begin
- i:=i3; j:=j3;
- show_move(i,j);
- if (i<=xl) and (i<>1) then xl:=xl-1;
- if (i>=xh) and (i<>8) then xh:=xh+1;
- if (j<=yl) and (j<>1) then yl:=yl-1;
- if (j>=yh) and (j<>8) then yh:=yh+1;
- end
- else writeln('Computer passes.');
- delay(2500);
- end;
-
- procedure player_move;
- const
- term: charset = ['1'..'8','A'..'H',^M];
- var
- i,j: integer;
- goodmove: boolean;
- begin
- window(1,21,40,25);
- clrscr;
- writeln;
- goodmove:=false;
- repeat
- write(player[pt],' ');
- textcolor(c[pt]); write(chr(pt));
- textcolor(fff); write(', enter your move: ');
- i:=-1; j:=-1;
- repeat
- getchar(ch,term);
- case ch of
- '1'..'8': begin
- write(ch,' ');
- if i=-1 then i:=ord(ch)-ord('0');
- end;
- 'A'..'H': begin
- write(ch,' ');
- if j=-1 then j:=ord(ch)-ord('@');
- end;
- ^M: begin
- i:=0; j:=0;
- end;
- end;
- until (i>-1) and (j>-1);
- writeln;
- if i=0 then
- begin
- write('Are you passing? ');
- getchar(ch,['Y','N']);
- if ch='Y' then
- begin
- writeln('YES');
- goodmove:=true;
- end
- else writeln('NO');
- end
- else
- begin
- if a[i,j]=0 then
- begin
- if test_move(i,j) then
- begin
- count_flank(i,j,0);
- if s1>0 then
- begin
- goodmove:=true;
- show_move(i,j);
- end
- else writeln('Sorry, does not flank a row.')
- end
- else writeln('Sorry, not next to opponents pieces.')
- end
- else writeln('Sorry, square occupied; try again.');
- end;
- until goodmove;
- end;
-
- begin
- intro;
- instructions;
- repeat
- initialize;
- draw_board;
- repeat
- pt:=1; op:=2;
- player_move;
- score;
- if not over then
- begin
- pt:=2; op:=1;
- if np=2 then player_move else computer_move;
- score;
- end;
- until over;
- until done;
- window(1,1,80,24);
- textmode(c80);
- end.
-