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程序设计》上的一道作业,不知道我是不是想复杂了?管他呢,现在谁还需要这种游戏?

Pascal小游戏 井字棋

时间: 2024-08-07 16:02:20

Pascal小游戏 井字棋的相关文章

井字棋的最优策略竟是先占角!

http://www.guokr.com/article/4754/ 井字棋可能是最简单的棋类游戏了,它简单到了成年人之间玩几乎总是平局的地步.因此,这个游戏貌似最多只能哄哄小孩子.不过,对井字棋游戏中所有可能的情况进行一番细致的分析,你会发现一个你或许不会料到的惊人结论——先手的最优策略不是稳坐正中央,而是先占一个角! 几年前,果壳网小编曾经自己动手写过一个和人下井字棋的电脑程序,运行之后却发现电脑先走时总爱把第一步棋下在角上:检查程序代码许久后才意识到,电脑程序可能并没有问题.人们往往有一个

『HTML5实现人工智能』小游戏《井字棋》发布,据说IQ上200才能赢【算法&amp;代码讲解+资源打包下载】

一,什么是TicTacToe(井字棋) 本游戏为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿童欢迎.可能我说了半天,对它名字不熟悉的朋友也不懂我在说神马.那没关系,我就引用Wiki(维基百科)的介绍作为大家对它名字的认识,顺便也勾起我们儿时的回忆: 井字棋,大陆.台湾又称为井字游戏.圈圈叉叉:另外也有打井游戏.OX棋的称呼,香港多称井字过三关.过三关,是种纸笔游戏.两个玩家,一个打圈(O),一个打

井字棋游戏升级版 - TopTicTacToe项目 简介

一.游戏简介 井字棋是一款世界闻名的游戏,不用我说,你一定知道它的游戏规则. 这款游戏简单易学,玩起来很有意思,不过已经证明出这款游戏如果两个玩家都足够聪明的话, 是很容易无法分出胜负的,即我们得到的结果是平局. 我们的项目,就是井字棋游戏的升级版!游戏有九个小棋盘,每个棋盘构成了整体布局的一部分,要想获得游戏的胜利,你要把握整个局势才行! 二.亮点 创新 传统的井字棋只有九个格,玩法简单,但是变化也相当有限.初玩者很容易被这新颖的游戏吸引住,但是玩了一段时间后,很容易摸出规律,很轻松达到不败的

人工智能博弈树算法做的井字棋游戏

不会输,超碉!井字棋这个游戏真是太无聊啦! 算法大概就是,有一个给状况进行估价的函数,深搜每种状况,假设每个人都按对自己最有利的方式走(假设玩家也是不傻),最后让电脑走出最有利的一步. 代码: 1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<

[游戏学习22] MFC 井字棋 双人对战

>_<:太多啦,感觉用英语说的太慢啦,没想到一年做的东西竟然这么多.....接下来要加速啦! >_<:注意这里必须用MFC和前面的Win32不一样啦! >_<:这也是第一次出现MFC游戏,其框架和逻辑的写法和Win32有很大的区别,建议先看一下MFC的基础再理解代码: >_<:TicTac.h 1 #define EX 1 //该点左鼠标 2 #define OH 2 //该点右鼠标 3 4 class CMyApp : public CWinApp 5 {

C++井字棋游戏,DOS界面版

据说有一个能保证不败的算法.明天看看先再写个PVC版的. 正题.今天无聊写了个井字棋游戏,顺便逐渐让自己习惯良好的代码风格,放上来给新手学习学习. jzq2.cpp /* N字棋游戏PVP版,DOS版 本棋盘可扩充,仅仅需调整检測条件就可以,其它接口不需改变. 非人机对战型.PVP类型; @author:天下无双 @date:2014-5-25 @version:1.0 */ #include <iostream> #include <string> #define INVALID

[CareerCup] 17.2 Tic Tac Toe 井字棋游戏

17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏,有下面几点需要考虑: 1. 判断是否能赢hasWon函数是调用一次还是多次,如果是多次,我们可能为了优化而需要加入一些预处理. 2. 井字棋游戏通常是3x3的大小,我们是否想要实现NxN的大小? 3. 我们需要在代码紧凑,执行速度和代码清晰之间做出选择. #include <iostream> #

JavaScript写的一个带AI的井字棋

最近有一门课结束了,需要做一个井字棋的游戏,我用JavaScript写了一个.首先界面应该问题不大,用html稍微写一下就可以.主要是人机对弈时的ai算法,如何使电脑方聪明起来,是值得思考一下的.开始游戏后,由玩家先行.那么站在计算机的角度,可以对多种情况进行分析,并按照重要程度赋予权值.情况如下: 1.同一排(行.列.对角线)有且只有两个棋子,并且都是自己的,只要再进一步就能获胜,那么剩下的那个位置权值最高,优先级最大.赋予一级权值. 2.同一排(行.列.对角线)有且只有两个棋子,并且都是对方

python井字棋算法及代码

井字棋盘看起来像一个大的井字符号(#),有9 个空格,可以包含X.O 或关于落子问题由于只能采用键盘输入,所以需要对棋盘进行坐标表示:即直接用1-9个9个数字来表示位置,7|8|9-+-+-4|5|6-+-+-1|2|3其索引顺序与数字键盘上的数字键排列一致,下棋时看着数字键下,较为简便.计算机的算法--寻找最佳落子位置首先简单的将棋盘划分为三个部分--中心(1),角(4),边(4).中心虽然只有一个但却不是最重要的,三个部分落子的优先顺序依次为:角.中心.边.因此,井字棋的计算机算法计算最佳落