Pascal小游戏 井字棋
一个很经典的井字棋游戏 Pascal源码Chaobs奉上
注意:1.有的FP版本不支持汉语,将会出现乱码。2.别想赢电脑了,平手不错了。
井字过三关:
program TicTacToe;
uses crt;
var
a:Array [1..3] of Array [1..3] of char;
b:Array [1..3] of Array [1..3] of integer;
i,n,g,e,p:integer;
t:text;
c:char;
o:integer;
r:integer;
s,h:integer;
ch,cal,ek,ck,hc:integer;
time:integer;
label xh,exit1;
begin
randomize;
clrscr;
writeln('1',chr(179),'2',chr(179),'3');
writeln(chr(196),chr(197),chr(196),chr(197),chr(196));
writeln('4',chr(179),'5',chr(179),'6');
writeln(chr(196),chr(197),chr(196),chr(197),chr(196));
writeln('7',chr(179),'8',chr(179),'9');
writeln('press enter');
readln;
xh:
ek:=0;
repeat
repeat
clrscr;
if ek=1 then writeln('wrong');
case b[1,1] of
0:a[1,1]:=' ';
1:a[1,1]:='o';
2:a[1,1]:='x';
end;
case b[1,2] of
0:a[1,2]:=' ';
1:a[1,2]:='o';
2:a[1,2]:='x';
end;
case b[1,3] of
0:a[1,3]:=' ';
1:a[1,3]:='o';
2:a[1,3]:='x';
end;
case b[2,1] of
0:a[2,1]:=' ';
1:a[2,1]:='o';
2:a[2,1]:='x';
end;
case b[2,2] of
0:a[2,2]:=' ';
1:a[2,2]:='o';
2:a[2,2]:='x';
end;
case b[2,3] of
0:a[2,3]:=' ';
1:a[2,3]:='o';
2:a[2,3]:='x';
end;
case b[3,1] of
0:a[3,1]:=' ';
1:a[3,1]:='o';
2:a[3,1]:='x';
end;
case b[3,2] of
0:a[3,2]:=' ';
1:a[3,2]:='o';
2:a[3,2]:='x';
end;
case b[3,3] of
0:a[3,3]:=' ';
1:a[3,3]:='o';
2:a[3,3]:='x';
end;
writeln(a[1,1],chr(179),a[1,2],chr(179),a[1,3]);
writeln(chr(196),chr(197),chr(196),chr(197),chr(196));
writeln(a[2,1],chr(179),a[2,2],chr(179),a[2,3]);
writeln(chr(196),chr(197),chr(196),chr(197),chr(196));
writeln(a[3,1],chr(179),a[3,2],chr(179),a[3,3]);
write('press location');
c:=readkey;
if c='e' then
begin
ch:=3;
goto exit1;
end;
o:=ord(c);
p:=o-48;
case p of
1,2,3:g:=1;
4,5,6:g:=2;
7,8,9:g:=3;
end;
e:=o mod 3;
if e=0 then e:=3;
writeln;
ek:=1;
until ((c='0') or (c='1') or (c='2') or (c='3') or (c='4')
or (c='5') or (c='6') or (c='7') or (c='8') or (c='9'));
until (a[g,e]<>'x') and (a[g,e]<>'o');
time:=time+1;
if c='1' then b[1,1]:=1;
if c='2' then b[1,2]:=1;
if c='3' then b[1,3]:=1;
if c='4' then b[2,1]:=1;
if c='5' then b[2,2]:=1;
if c='6' then b[2,3]:=1;
if c='7' then b[3,1]:=1;
if c='8' then b[3,2]:=1;
if c='9' then b[3,3]:=1;
clrscr;
for s:=1 to 3 do begin
for h:=1 to 3 do begin
if b[s,h]=1 then write('o');
if b[s,h]=2 then write('x');
if (b[s,h]<>1) and (b[s,h]<>2) then write(' ');
if h<>3 then write(chr(179));
end;
writeln;
if s<>3 then writeln(chr(196),chr(197),chr(196),chr(197),chr(196));
end;
if ((b[1,2]=1) and (b[2,2]=1) and (b[3,2]=1))
or ((b[1,1]=1) and (b[1,2]=1) and (b[1,3]=1))
or ((b[2,1]=1) and (b[2,2]=1) and (b[2,3]=1))
or ((b[3,1]=1) and (b[3,2]=1) and (b[3,3]=1))
or ((b[1,1]=1) and (b[2,1]=1) and (b[3,1]=1))
or ((b[1,3]=1) and (b[2,3]=1) and (b[3,3]=1))
or ((b[1,1]=1) and (b[2,2]=1) and (b[3,3]=1))
or ((b[1,3]=1) and (b[2,2]=1) and (b[3,1]=1))
then begin ch:=1; goto exit1; end;
cal:=0;
for s:=1 to 3 do begin
for h:=1 to 3 do begin
if (b[s,h]=1) or (b[s,h]=2) then cal:=cal+1;
end;
end;
if cal=9 then goto exit1;
writeln('press enter');
readln;
repeat
hc:=0;
ck:=0;
if (((b[1,2]=2) and (b[1,3]=2))
or ((b[2,1]=2) and (b[3,1]=2))
or ((b[2,2]=2) and (b[3,3]=2)))
and (b[1,1]<>1) then
begin
hc:=1;
ck:=ck+1;
r:=1;
end;
if (((b[1,1]=2) and (b[1,3]=2))
or ((b[2,2]=2) and (b[3,2]=2)))
and (b[1,2]<>1) then
begin
hc:=1;
ck:=ck+1;
r:=2;
end;
if (((b[1,1]=2) and (b[1,2]=2))
or ((b[2,3]=2) and (b[3,3]=2))
or ((b[2,2]=2) and (b[3,1]=2)))
and (b[1,3]<>1) then
begin
hc:=1;
ck:=ck+1;
r:=3;
end;
if (((b[1,1]=2) and (b[3,1]=2))
or ((b[2,2]=2) and (b[2,3]=2)))
and (b[2,1]<>1) then
begin
hc:=1;
ck:=ck+1;
r:=4;
end;
if (((b[1,1]=2) and (b[3,3]=2))
or ((b[1,3]=2) and (b[3,1]=2))
or ((b[1,2]=2) and (b[3,2]=2))
or ((b[2,1]=2) and (b[2,3]=2)))
and (b[2,2]<>1) then
begin
hc:=1;
ck:=ck+1;
r:=5;
end;
if (((b[2,1]=2) and (b[2,2]=2))
or ((b[1,3]=2) and (b[3,3]=2)))
and (b[2,3]<>1) then
begin
hc:=1;
ck:=ck+1;
r:=6;
end;
if (((b[1,1]=2) and (b[2,1]=2))
or ((b[3,2]=2) and (b[3,3]=2))
or ((b[1,3]=2) and (b[2,2]=2)))
and (b[3,1]<>1) then
begin
hc:=1;
ck:=ck+1;
r:=7;
end;
if (((b[1,2]=2) and (b[2,2]=2))
or ((b[3,1]=2) and (b[3,3]=2)))
and (b[3,2]<>1) then
begin
hc:=1;
ck:=ck+1;
r:=8;
end;
if (((b[1,1]=2) and (b[2,2]=2))
or ((b[1,3]=2) and (b[2,3]=2))
or ((b[1,1]=2) and (b[2,2]=2)))
and (b[3,3]<>1) then
begin
hc:=1;
ck:=ck+1;
r:=9;
end;
if hc=0 then begin
if time=1 then begin
if c='5' then r:=1;
if c<>'5' then r:=5;
end
else
begin
if (((b[1,2]=1) and (b[1,3]=1))
or ((b[2,1]=1) and (b[3,1]=1))
or ((b[2,2]=1) and (b[3,3]=1)))
and (b[1,1]<>2) then
begin
ck:=ck+1;
r:=1;
hc:=3;
end;
if (((b[1,1]=1) and (b[1,3]=1))
or ((b[2,2]=1) and (b[3,2]=1)))
and (b[1,2]<>2) then
begin
ck:=ck+1;
r:=2;
hc:=3;
end;
if (((b[1,1]=1) and (b[1,2]=1))
or ((b[2,3]=1) and (b[3,3]=1))
or ((b[2,2]=1) and (b[3,1]=1)))
and (b[1,3]<>2) then
begin
ck:=ck+1;
ck:=ck+1;
r:=3;
hc:=3;
end;
if (((b[1,1]=1) and (b[3,1]=1))
or ((b[2,2]=1) and (b[2,3]=1)))
and (b[2,1]<>2) then
begin
ck:=ck+1;
r:=4;
hc:=3;
end;
if (((b[1,1]=1) and (b[3,3]=1))
or ((b[1,3]=1) and (b[3,1]=1))
or ((b[1,2]=1) and (b[3,2]=1))
or ((b[2,1]=1) and (b[2,3]=1)))
and (b[2,2]<>2) then
begin
ck:=ck+1;
r:=5;
hc:=3;
end;
if (((b[2,1]=1) and (b[2,2]=1))
or ((b[1,3]=1) and (b[3,3]=1)))
and (b[2,3]<>2) then
begin
ck:=ck+1;
r:=6;
hc:=3;
end;
if (((b[1,1]=1) and (b[2,1]=1))
or ((b[3,2]=1) and (b[3,3]=1))
or ((b[1,3]=1) and (b[2,2]=1)))
and (b[3,1]<>2) then
begin
ck:=ck+1;
r:=7;
hc:=3;
end;
if (((b[1,2]=1) and (b[2,2]=1))
or ((b[3,1]=1) and (b[3,3]=1)))
and (b[3,2]<>2) then
begin
ck:=ck+1;
r:=8;
hc:=3;
end;
if (((b[1,1]=1) and (b[2,2]=1))
or ((b[1,3]=1) and (b[2,3]=1))
or ((b[1,1]=1) and (b[2,2]=1)))
and (b[3,3]<>2) then
begin
ck:=ck+1;
r:=9;
hc:=3;
end;
end;{else}
end;{if}
if ek>1 then writeln('you can be the winner');
if hc=0 then r:=random(9);
if time=1 then begin
if c='5' then r:=1;
if c<>'5' then r:=5;
end;
i:=r mod 3;
case r of
1,2,3:n:=1;
4,5,6:n:=2;
7,8,9:n:=3;
end;
if i=0 then i:=3;
writeln;
until (b[n,i]<>1) and (b[n,i]<>2) and (r<>0);
b[n,i]:=2;
clrscr;
case b[1,1] of
0:a[1,1]:=' ';
1:a[1,1]:='o';
2:a[1,1]:='x';
end;
case b[1,2] of
0:a[1,2]:=' ';
1:a[1,2]:='o';
2:a[1,2]:='x';
end;
case b[1,3] of
0:a[1,3]:=' ';
1:a[1,3]:='o';
2:a[1,3]:='x';
end;
case b[2,1] of
0:a[2,1]:=' ';
1:a[2,1]:='o';
2:a[2,1]:='x';
end;
case b[2,2] of
0:a[2,2]:=' ';
1:a[2,2]:='o';
2:a[2,2]:='x';
end;
case b[2,3] of
0:a[2,3]:=' ';
1:a[2,3]:='o';
2:a[2,3]:='x';
end;
case b[3,1] of
0:a[3,1]:=' ';
1:a[3,1]:='o';
2:a[3,1]:='x';
end;
case b[3,2] of
0:a[3,2]:=' ';
1:a[3,2]:='o';
2:a[3,2]:='x';
end;
case b[3,3] of
0:a[3,3]:=' ';
1:a[3,3]:='o';
2:a[3,3]:='x';
end;
writeln(a[1,1],chr(179),a[1,2],chr(179),a[1,3]);
writeln(chr(196),chr(197),chr(196),chr(197),chr(196));
writeln(a[2,1],chr(179),a[2,2],chr(179),a[2,3]);
writeln(chr(196),chr(197),chr(196),chr(197),chr(196));
writeln(a[3,1],chr(179),a[3,2],chr(179),a[3,3]);
if ((b[1,2]=2) and (b[2,2]=2) and (b[3,2]=2))
or ((b[1,1]=2) and (b[1,2]=2) and (b[1,3]=2))
or ((b[2,1]=2) and (b[2,2]=2) and (b[2,3]=2))
or ((b[3,1]=2) and (b[3,2]=2) and (b[3,3]=2))
or ((b[1,1]=2) and (b[2,1]=2) and (b[3,1]=2))
or ((b[1,3]=2) and (b[2,3]=2) and (b[3,3]=2))
or ((b[1,1]=2) and (b[2,2]=2) and (b[3,3]=2))
or ((b[1,3]=2) and (b[2,2]=2) and (b[3,1]=2))
then begin ch:=2; goto exit1; end;
goto xh;
exit1:
if cal=9 then writeln('deuce');
if ch=2 then writeln('lost');
if ch=1 then writeln('win');
if ch=3 then
begin
clrscr;
writeln('exited');
end;
writeln('press enter');
readln;
end.
Pascal小游戏 井字棋的更多相关文章
- Java 小游戏 - 井字棋 v1.0 (初步完成) (2018.4.16更新)
井字棋游戏初步完成 实现功能:输入位置数据->打印棋盘->判断是否胜利->继续游戏/退出游戏 缺点:没有清屏函数 判断胜利方法太过无脑 package MYGAME; ...
- C++小游戏——井字棋
#include<cstdio> #include<windows.h> #include<ctime> int main() { srand(time(NULL) ...
- python 游戏(井字棋)
1. 游戏思路和流程图 实现功能,现实生活中的井字棋玩法 游戏流程图 2. 使用模块和游戏提示 import random def game_info(): print('欢迎来到井字棋游戏') pr ...
- [HTML5实现人工智能]小游戏《井字棋》发布,据说IQ上200才能赢
一,什么是TicTacToe(井字棋) 本 游戏 为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿 ...
- 『HTML5实现人工智能』小游戏《井字棋》发布,据说IQ上200才能赢【算法&代码讲解+资源打包下载】
一,什么是TicTacToe(井字棋) 本游戏为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿童欢迎. ...
- 井字棋小游戏(C语言)
最近沉迷于<NetHack>.<DCSS>等字符游戏,对其很感兴趣,于是用C语言写了个字符界面的井字棋小游戏.欢迎大家指教. 编写时遇到了一些问题,我原先准备用循环,直到读取到 ...
- 井字棋游戏升级版 - TopTicTacToe项目 简介
一.游戏简介 井字棋是一款世界闻名的游戏,不用我说,你一定知道它的游戏规则. 这款游戏简单易学,玩起来很有意思,不过已经证明出这款游戏如果两个玩家都足够聪明的话, 是很容易无法分出胜负的,即我们得到的 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- [C++] 井字棋游戏源码
TicTac.h #define EX 1 //该点左鼠标 #define OH 2 //该点右鼠标 class CMyApp : public CWinApp { public: virtual B ...
随机推荐
- vs2008使用mysql链接错误
原因是因为安装了64位的mysql,而开发工具室32位的,需要安装32位的开发库就可以了
- Android(java)学习笔记61:Android中的 Application类用法
1. 简介 如果想在整个应用中使用全局变量,在java中一般是使用静态变量,public类型:而在android中如果使用这样的全局变量就不符合Android的框架架构,但是可以使用一种更优雅的方式就 ...
- Android(java)学习笔记59:类继承的 注意事项
1. 类继承的注意事项: /* 继承的注意事项: A:子类只能继承父类所有非私有的成员(成员方法和成员变量) B:子类不能继承父类的构造方法,但是可以通过super(马上讲)关键字去访问父类构造方法. ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) C】最小边覆盖(简单题)
点此看题面 大致题意: 给你一个边集的子集,问你这可不可能是这张图的最小边覆盖. 大致思路 考虑到,如果一条边连接的两个点度数都大于等于\(2\),则这条边完全可以删去. 因此,我们只要判断是否存在这 ...
- P1980 计数问题
题目描述 试计算在区间 11 到 nn的所有整数中,数字x(0 ≤ x ≤ 9)x(0≤x≤9)共出现了多少次?例如,在 11到1111中,即在 1,2,3,4,5,6,7,8,9,10,111,2, ...
- 20145238-荆玉茗 《Java程序设计》第二周学习总结
20145238 <Java程序设计>第2周学习总结 教材学习内容总结 关于一些格式方面的问题: 1.关键字:在定义java文件名的时候要避免这些关键字的出现,因为他们在java程序语言中 ...
- 使用免费公开的api接口示例(iOS)
做项目难免需要测试,要测试就需要一些接口,现在网上的很多接口都是需要收费的. 以下是目前找到的免费 JSON API免费接口 云聚数据 网吧数据 其中选取了一个百度百科的接口 百度接口 百度百科接口: ...
- margin与padding大比拼
用margin还是用padding这个问题相信是每个学css的人都想要去深入了解的. CSS边距属性定义元素周围的空间.通过使用单独的属性,可以对上.右.下.左的外边距进行设置.也可以使用简写的外边距 ...
- POJ2409 Let it Bead(Polya定理)
Let it Bead Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6443 Accepted: 4315 Descr ...
- java通过FreeMarker模板生成Excel文件之.ftl模板制作
关于怎么通过freemarker模板生成excel的文章很多,关键点在于怎么制作模板文件.ftl 网上的办法是: (1)把Excel模板的格式调好,另存为xml文件 (2)新建一个.ftl文件,把xm ...