📄 井字棋_.htm
字号:
3 程序以数组qp定义棋盘,qp[1]到qp[9]分别表示左图中1到9九个位置(qp[0]不用) */<BR>/* --------- qp[i]的初值为0,表示当前此位置上没子,当人走某个位置时
qp[i]=-1,机器走时为qp[i]=1 */<BR>/* 4 | 5 |
6 用n表示当前走第几步(设第一步为9,以后逐次递减,到0为止) */<BR>/*
--------- 变量z控制搜索策略,人先走z=0,反之z=1 */<BR>/* 7
| 8 |
9 程序开始会要求确认人是否先走,之后只要输入想走位置的标识数字即可, */</P>
<P>#include<stdio.h><BR>#include<ctype.h><BR>#include<conio.h><BR>int
n=9, z=0,
qp[10]={0};<BR>/* 函数chkwin()作用是判断当前棋盘t中选手w的胜负情况:
返回 0,表示当前为平局,返回w,表示选手w胜 */ <BR>int
chkwin(int t[], int w)<BR> {if (t[1]==w
&& t[1]==t[2] && t[2]==t[3])
return(w);<BR> if (t[4]==w &&
t[4]==t[5] && t[5]==t[6])
return(w);<BR> if (t[7]==w &&
t[7]==t[8] && t[8]==t[9])
return(w);<BR> if (t[1]==w &&
t[1]==t[4] && t[4]==t[7])
return(w);<BR> if (t[2]==w &&
t[2]==t[5] && t[5]==t[8])
return(w);<BR> if (t[3]==w &&
t[3]==t[6] && t[6]==t[9])
return(w);<BR> if (t[1]==w &&
t[1]==t[5] && t[5]==t[9])
return(w);<BR> if (t[3]==w &&
t[3]==t[5] && t[5]==t[7])
return(w);<BR> return(0);<BR>
}<BR>/* 搜索函数search()受cgo()函数调用,为电脑提供路径的好坏程度,返回值越大则走该位置的胜率越高 */<BR>/* 搜索方法是用递归方法模拟人机交替走棋,具体方法如下: */<BR>/* 1.当前选手k,依次试走棋盘上剩余的还没走过的位置 */<BR>/*
若k走好当前位置t[i]后棋盘t为平局,则假设这一步已走递归到下一层查看胜负情况,返回值累加于j */ <BR>/*
若k走好当前位置t[i]后棋盘t非平局(即k胜),标志f++ */<BR>/* 2.如果标志f>z,则返回当前步数n的阶乘到j,表示当前所有位置对k都是胜 */ <BR>/* 3.返回j */<BR>long
search(int n, int k, int t[])<BR> {int i, h,
f, g;<BR> long j;<BR> if
(n==0) return(chkwin(t,k));<BR>
for (f=0, j=0, i=1; i<10;
i++)<BR> if
(t[i]==0)<BR>
{t[i]=k; h=chkwin(t,k);<BR> if (h==k)
f++;<BR> else
j+=search(n-1,k*-1,t);<BR> t[i]=0;<BR>
}<BR> if (f>z) for (j=k, g=n;
g>0; j*=g--);<BR>
return(j);<BR>
}<BR>/* 电脑选位置走棋,择位原则1.当前位置必须为空 */<BR>/* 2.若走当前位置电脑能赢,则走这一步,否则转(3) */ <BR>/*
3.若当前位置被人走电脑会输,则走这一步,否则转(4) */<BR>/* 4.试走当前位置并调用
search()函数,求走当前位置电脑的有利程度 */<BR>/* 5.找对电脑有利程度最大的位置走 */<BR>void
cgo()<BR> {int i, ti=0;<BR> long
j=-8000000, t=0;<BR> for (ti=1;
ti>-2;
ti-=2) /*ti=1,先看自己能否赢;ti=-1,看对受能否赢*/<BR>
for (i=1; i<10;
i++)<BR> if
(qp[i]==0)<BR> {qp[i]=ti;<BR> if
(chkwin(qp, ti)!=0) {n--; qp[i]=1;
return;}<BR> qp[i]=0;<BR>
}<BR> for (i=1;
i<10;i++)<BR> if
(qp[i]==0)<BR>
{qp[i]=1; t=search(n-1,-1,qp);<BR> if
(t>j) {j=t;
ti=i;}<BR> qp[i]=0;<BR>
}<BR> n--; qp[ti]=1;<BR>
}<BR>/* 函数mgo(),人输入走棋位置 */<BR>void
mgo()<BR> {int c=0;<BR> printf
("\nPlease enter the Num to go:
");<BR> for (c=getche(); ;
printf("\n"), c=getche()
)<BR> if (isdigit(c)
&& c!='0' &&
qp[c-48]==0)<BR>
{n--; qp[c-48]=-1; return;<BR>
}
}<BR>/* 屏幕输出函数display,在屏幕上输出当前的棋盘 */<BR>int
display(int x)<BR> {int i;<BR>
char t[10]={0};<BR> for (i=1; i<10;
i++)<BR> {if (qp[i]>0)
t[i]=88;<BR> if
(qp[i]<0) t[i]=79;<BR>
}<BR> printf
("\n%c|%c|%c\n-----\n%c|%c", t[1], t[2], t[3],
t[4], t[5]);<BR> printf
("|%c\n-----\n%c|%c|%c\n", t[6], t[7],
t[8], t[9]);<BR> if (x==0)
printf("\ndraw!
\n");<BR> if (x==1) printf("\ncomputer
win!\n");<BR> if (x==2)
printf("\ncontinue
\n");<BR> }<BR>main()<BR> {char
c;<BR> printf ("\nGo first?
[Y/N]:"); /*选择谁先走*/<BR> for
(c=getche();
c!='Y'&&c!='y'&&c!='N'&&c!='n';
c=getche());<BR> if (c=='N'||c=='n')
{cgo(); z=1; display(2);}<BR> while
(1)<BR> {mgo(); if
(!n)
{display(0);
break;} /*人走,若不是最后一步,继续*;否则跳出/<BR>
cgo(); if (chkwin(qp,1)) {display(1);
break;} /*电脑走,若没赢,继续*;否则跳出/<BR>
if
(n)
display(2); /*还没走到最后一步,继续;否则跳出*/<BR>
else
{display(0); break;}<BR>
}<BR> getchar();<BR>
}</P><BR></FONT></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD width="100%" height=25>
<DIV align=center></DIV></TD></TR>
<TR>
<TD width=255 height=20><A class=bottom
href="http://www.cstudyhome.com/wenzhang06/review.asp?NewsID=4817"
target=_blank> <IMG
height=18 src="井字棋__files/icon1.gif" width=18 border=0>
<FONT color=blue>发表评论</FONT></A> </TD></TR>
<TR>
<TD width="100%">
<HR SIZE=1>
</TD></TR>
<TR>
<TD height=8></TD></TR>
<TR>
<TD width="100%" height=18><B>相关专题:</B>
<TR>
<TD width="100%">
<HR SIZE=1>
</TD></TR>
<TR>
<TD height=8></TD></TR>
<TR>
<TD width="100%" height=18><B>相关信息:</B></TD></TR>
<TR>
<TD height=8></TD></TR>
<TR>
<TD width="100%"> 没有相关信息</TD></TR>
<TR>
<TD width="100%">
<HR SIZE=1>
</TD></TR>
<TR>
<TD height=8></TD></TR>
<TR>
<TD width="100%"><B>相关评论:</B></TD></TR>
<TR>
<TD height=8></TD></TR>
<TR>
<TD width="100%"> 没有相关评论 </TD></TR>
<TR>
<TD align=middle width="100%" height=28><A class=bottom
href="http://www.cstudyhome.com/wenzhang06/review.asp?NewsID=4817"
target=_blank><IMG height=11 src="井字棋__files/more.gif"
width=50 border=0> <FONT color=blue>更多评论</FONT></A>
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=750 align=center bgColor=#c1c1c1
border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width=748 align=center bgColor=#a7ccfa
border=0>
<TBODY>
<TR>
<TD width=20 background=井字棋__files/banbg.gif> </TD>
<TD width=255 background=井字棋__files/banbg.gif height=20></TD>
<TD width=214 background=井字棋__files/banbg.gif height=20><A
class=bottom
href="http://www.cstudyhome.com/wenzhang06/send.asp?NewsID=4817"
target=_blank><IMG height=16 src="井字棋__files/mail.gif" width=16
border=0> 将本信息发给好友</A> </TD>
<TD width=168 background=井字棋__files/banbg.gif><IMG height=14
src="井字棋__files/printer.gif" width=16><A class=bottom
href="javascript:window.print()"> 打印本页</A></TD>
<TD width=91 background=井字棋__files/banbg.gif><INPUT onclick="window.close();return false;" type=button value=关闭窗口 name=close>
</TD></TR>
<TR vAlign=top>
<TD width=20 bgColor=#c1c1c1 height=1></TD>
<TD width=469 bgColor=#c1c1c1 colSpan=2 height=1></TD>
<TD width=259 bgColor=#c1c1c1 colSpan=2
height=1></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!--<IFRAME src="http://www.cpok.com/ad/alp.htm?wangfu" width="760" height=110 marginwidth="0" marginheight="0" frameborder="0" scrolling="no"></IFRAME>--><!--<IFRAME src='http://www.cpok.com/ad/alp.htm?wangfu' width='760' height='124' marginwidth='0' marginheight='0' frameborder='0' scrolling='no'></IFRAME>-->
<TABLE cellSpacing=0 cellPadding=0 width=750 align=center border=0>
<TBODY>
<TR>
<TD bgColor=#c1c1c1 height=1></TD></TR></TBODY></TABLE>
<TABLE class=p9 cellSpacing=0 cellPadding=0 width=750 align=center border=0>
<TBODY>
<TR>
<TD align=middle
background="C:\Documents and Settings\online7\桌面\gqh\井字棋__files\BACK1(1).gif"
height=20> </TD></TR>
<TR>
<TD align=middle height=20><A class=bottom
onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.cstudyhome.com/wenzhang06');"
href="http://www.cstudyhome.com/wenzhang06/ReadNews.asp?NewsID=4817#"><BR>设为首页</A>
| <A class=bottom
onclick=window.external.AddFavorite(location.href,document.title);
href="http://www.cstudyhome.com/wenzhang06/ReadNews.asp?NewsID=4817#">加入收藏</A>
|<A class=bottom
href="http://www.cstudyhome.com/wenzhang06/ReadNews.asp?NewsID=1086"
target=_blank>广告服务</A>| <A class=bottom
href="mailto:webmaster@www.cstudyhome.net">联系我们</A><A class=bottom
href="http://www.cstudyhome.com/wenzhang06/admin/login.asp"
target=_blank>.</A><BR><BR><BR>版权所有 Copyright © 2002~2004
C语言之家<BR></TD></TR><BR>
<TR></TR></TBODY></TABLE>
<TABLE class=p9 cellSpacing=0 cellPadding=0 width="100%" align=center
border=0><TBODY>
<TR>
<TD align=middle><!-- <script language=JavaScript>
document.write("<a href='http://www.sunofcn.com/counter13/infolist.asp?admin=dd123'>");
document.write("<img src='http://www.sunofcn.com/counter13/count.asp?admin=dd123&Referer="+escape(top.document.referrer)+"&curURL="+escape(top.document.URL)+"&Width="+escape(screen.width)+"&Height="+escape(screen.height)+"' border=0 width=80 height=30 alt='阳光统计|WWW.SUNOFCN.COM'>");
document.write("</a>");
</script>--></TD></TR></TBODY></TABLE><BR><BR><BR>
<TABLE class=p9 cellSpacing=0 cellPadding=0 width="100%" align=center
border=0><TBODY>
<TR>
<TD align=middle width="7%" background=井字棋__files/bott.gif
height=30> </TD>
<TD align=middle width="80%" background=井字棋__files/footerbg.gif
height=30> <FONT
color=#dde99f>........................................................................................................</FONT>
<SCRIPT src="井字棋__files/Counter.htm"></SCRIPT>
</TD></TR></TBODY></TABLE>7 </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -