/*
五子棋
*/
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<bios.h>
#include<conio.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define SPACE 0x3920
#define BILI 20
#define JZ 4
#define JS 3
#define N 19
int box[N][N]
int step_x,step_y
int key
int flag=1
void draw_box()
void draw_cicle(int x,int y,int color)
void change()
void judgewho(int x,int y)
void judgekey()
int judgeresult(int x,int y)
void attentoin()
void attention()
{
char ch
window(1,1,80,25)
textbackground(LIGHTBLUE)
textcolor(YELLOW)
clrscr()
gotoxy(15,2)
printf("游戏操作规则:")
gotoxy(15,4)
printf("Play Rules:")
gotoxy(15,6)
printf("1、按左右上下方向键移动棋子")
gotoxy(15,8)
printf("1. Press Left,Right,Up,Down Key to move Piece")
gotoxy(15,10)
printf("2、按空格确定落棋子")
gotoxy(15,12)
printf("2. Press Space to place the Piece")
gotoxy(15,14)
printf("3、禁止在棋盘外按空格")
gotoxy(15,16)
printf("3. DO NOT press Space outside of the chessboard")
gotoxy(15,18)
printf("你是否接受上述的游戏规则(Y/N)")
gotoxy(15,20)
printf("Do you accept the above Playing Rules? [Y/N]:")
while(1)
{
gotoxy(60,20)
ch=getche()
if(ch=='Y'||ch=='y')
break
else if(ch=='N'||ch=='n')
{
window(1,1,80,25)
textbackground(BLACK)
textcolor(LIGHTGRAY)
clrscr()
exit(0)
}
gotoxy(51,12)
printf(" ")
}
}
void draw_box()
{
int x1,x2,y1,y2
setbkcolor(LIGHTBLUE)
setcolor(YELLOW)
gotoxy(7,2)
printf("Left, Right, Up, Down KEY to move, Space to put, ESC-quit.")
for(x1=1,y1=1,y2=18x1<=18x1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x1+JZ)*BILI,(y2+JS)*BILI)
for(x1=1,y1=1,x2=18y1<=18y1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x2+JZ)*BILI,(y1+JS)*BILI)
for(x1=1x1<=18x1++)
for(y1=1y1<=18y1++)
box[x1][y1]=0
}
void draw_circle(int x,int y,int color)
{
setcolor(color)
setlinestyle(SOLID_LINE,0,1)
x=(x+JZ)*BILI
y=(y+JS)*BILI
circle(x,y,8)
}
void judgekey()
{
int i
int j
switch(key)
{
case LEFT :
if(step_x-1<0)
break
else
{
for(i=step_x-1,j=step_yi>=1i--)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE)
break
}
if(i<1)break
step_x=i
judgewho(step_x,step_y)
break
}
case RIGHT :
if(step_x+1>18)
break
else
{
for(i=step_x+1,j=step_yi<=18i++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE)
break
}
if(i>18)break
step_x=i
judgewho(step_x,step_y)
break
}
case DOWN :
if((step_y+1)>18)
break
else
{
for(i=step_x,j=step_y+1j<=18j++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE)
break
}
if(j>18)break
step_y=j
judgewho(step_x,step_y)
break
}
case UP :
if((step_y-1)<0)
break
else
{
for(i=step_x,j=step_y-1j>=1j--)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE)
break
}
if(j<1)break
step_y=j
judgewho(step_x,step_y)
break
}
case ESC :
break
case SPACE :
if(step_x>=1&&step_x<=18&&step_y>=1&&step_y<=18)
{
if(box[step_x][step_y]==0)
{
box[step_x][step_y]=flag
if(judgeresult(step_x,step_y)==1)
{
sound(1000)
delay(1000)
nosound()
gotoxy(30,4)
if(flag==1)
{
setbkcolor(BLUE)
cleardevice()
setviewport(100,100,540,380,1)
/*定义一个图形窗口*/
setfillstyle(1,2)
/*绿色以实填充*/
setcolor(YELLOW)
rectangle(0,0,439,279)
floodfill(50,50,14)
setcolor(12)
settextstyle(1,0,5)
/*三重笔划字体, 水平放?5倍*/
outtextxy(20,20,"The White Win !")
setcolor(15)
settextstyle(3,0,5)
/*无衬笔划字体, 水平放大5倍*/
outtextxy(120,120,"The White Win !")
setcolor(14)
settextstyle(2,0,8)
getch()
closegraph()
exit(0)
}
if(flag==2)
{
setbkcolor(BLUE)
cleardevice()
setviewport(100,100,540,380,1)
/*定义一个图形窗口*/
setfillstyle(1,2)
/*绿色以实填充*/
setcolor(YELLOW)
rectangle(0,0,439,279)
floodfill(50,50,14)
setcolor(12)
settextstyle(1,0,8)
/*三重笔划字体, 水平放大8倍*/
outtextxy(20,20,"The Red Win !")
setcolor(15)
settextstyle(3,0,5)
/*无衬笔划字体, 水平放大5倍*/
outtextxy(120,120,"The Red Win !")
setcolor(14)
settextstyle(2,0,8)
getch()
closegraph()
exit(0)
}
}
change()
break
}
}
else
break
}
}
void change()
{
if(flag==1)
flag=2
else
flag=1
}
void judgewho(int x,int y)
{
if(flag==1)
draw_circle(x,y,15)
if(flag==2)
draw_circle(x,y,4)
}
int judgeresult(int x,int y)
{
int j,k,n1,n2
while(1)
{
n1=0
n2=0
/*水平向左数*/
for(j=x,k=yj>=1j--)
{
if(box[j][k]==flag)
n1++
else
break
}
/*水平向右数*/
for(j=x,k=yj<=18j++)
{
if(box[j][k]==flag)
n2++
else
break
}
if(n1+n2-1>=5)
{
return(1)
break
}
/*垂直向上数*/
n1=0
n2=0
for(j=x,k=yk>=1k--)
{
if(box[j][k]==flag)
n1++
else
break
}
/*垂直向下数*/
for(j=x,k=yk<=18k++)
{
if(box[j][k]==flag)
n2++
else
break
}
if(n1+n2-1>=5)
{
return(1)
break
}
/*向左上方数*/
n1=0
n2=0
for(j=x,k=yj>=1,k>=1j--,k--)
{
if(box[j][k]==flag)
n1++
else
break
}
/*向右下方数*/
for(j=x,k=yj<=18,k<=18j++,k++)
{
if(box[j][k]==flag)
n2++
else
break
}
if(n1+n2-1>=5)
{
return(1)
break
}
/*向右上方数*/
n1=0
n2=0
for(j=x,k=yj<=18,k>=1j++,k--)
{
if(box[j][k]==flag)
n1++
else
break
}
/*向左下方数*/
for(j=x,k=yj>=1,k<=18j--,k++)
{
if(box[j][k]==flag)
n2++
else
break
}
if(n1+n2-1>=5)
{
return(1)
break
}
return(0)
break
}
}
void main()
{
int gdriver=VGA,gmode=VGAHI
clrscr()
attention()
initgraph(&gdriver,&gmode,"c:\\tc")
/* setwritemode(XOR_PUT)*/
flag=1
draw_box()
do
{
step_x=0
step_y=0
/*draw_circle(step_x,step_y,8)*/
judgewho(step_x-1,step_y-1)
do
{
while(bioskey(1)==0)
key=bioskey(0)
judgekey()
}
while(key!=SPACE&&key!=ESC)
}
while(key!=ESC)
closegraph()
}
代码比较长 没有注释脚本说明:
把如下代码加入<body>区域中
<style>
.bigcell {
background-color:#aa9966
border:4px solid #aa9966
text-align:center
}
.cell {
width:40px
height:40px
font-family:Verdana, Arial
font-size:10pt
font-weight:bold
background-color:#996633
color:#ffff33
border-top:2px solid #aa9966
border-left:2px solid #aa9966
border-right:2px solid #663300
border-bottom:2px solid #663300
text-align:center
}
.hole {
width:40px
height:40px
background-color:#aa9966
text-align:center
}
body,h1,h2,h3,.msg,capt1,capt2 {font-family:Verdana,Comic Sans MS,Arial}
body {margin:0px}
h1 {font-size:28ptfont-weight:boldmargin-bottom:0px}
h2 {font-size:16ptmargin:0pxfont-weight:bold}
h3 {font-size:8ptmargin:0pxfont-weight:bold}
.msg {font-size:8ptfont-weight:bold}
.tab {cursor:hand}
.capt1 {font-size:10ptfont-weight:bold}
.capt2 {font-size:9ptfont-weight:bold}
.capt3 {font-size:14ptfont-weight:boldcolor:yellow}
.capt4 {font-size:10ptfont-weight:boldcolor:yellow}
.but {font-size:9ptfont-weight:boldheight:30pxbackground-color:#aaaa99}
</style>
<BODY onLoad="loadBoard(4)">
<script>
var gsize, ghrow, ghcol, gtime, gmoves, gintervalid=-1, gshuffling
function toggleHelp()
{
if (butHelp.value == "Hide Help")
{
help.style.display = "none"
butHelp.value = "Show Help"
}
else
{
help.style.display = ""
butHelp.value = "Hide Help"
}
}
//random number between low and hi
function r(low,hi)
{
return Math.floor((hi-low)*Math.random()+low)
}
//random number between 1 and hi
function r1(hi)
{
return Math.floor((hi-1)*Math.random()+1)
}
//random number between 0 and hi
function r0(hi)
{
return Math.floor((hi)*Math.random())
}
function startGame()
{
shuffle()
gtime = 0
gmoves = 0
tickTime()
gintervalid = setInterval("tickTime()",1000)
}
function stopGame()
{
if (gintervalid==-1) return
clearInterval(gintervalid)
fldStatus.innerHTML = ""
gintervalid=-1
}
function tickTime()
{
showStatus()
gtime++
}
function checkWin()
{
var i, j, s
if (gintervalid==-1) return//game not started!
if (!isHole(gsize-1,gsize-1)) return
for (i=0i<gsizei++)
for (j=0j<gsizej++)
{
if (!(i==gsize-1 &&j==gsize-1)) //ignore last block (ideally a hole)
{
if (getValue(i,j)!=(i*gsize+j+1).toString()) return
}
}
stopGame()
s = "<table cellpadding=4>"
s += "<tr><td align=center class=capt3>!! CONGRATS !!</td></tr>"
s += "<tr class=capt4><td align=center>You have done it in " + gtime + " secs "
s += "with " + gmoves + " moves!</td></tr>"
s += "<tr><td align=center class=capt4>Your speed is " + Math.round(1000*gmoves/gtime)/1000 + " moves/sec</td></tr>"
s += "</table>"
fldStatus.innerHTML = s
// shuffle()
}
function showStatus()
{
fldStatus.innerHTML = "Time: " + gtime + " secs Moves: " + gmoves
}
function showTable()
{
var i, j, s
stopGame()
s = "<table border=3 cellpadding=0 cellspacing=0 bgcolor='#666655'><tr><td class=bigcell>"
s = s + "<table border=0 cellpadding=0 cellspacing=0>"
for (i=0i<gsizei++)
{
s = s + "<tr>"
for (j=0j<gsizej++)
{
s = s + "<td id=a_" + i + "_" + j + " onclick='move(this)' class=cell>" + (i*gsize+j+1) + "</td>"
}
s = s + "</tr>"
}
s = s + "</table>"
s = s + "</td></tr></table>"
return s
}
function getCell(row, col)
{
return eval("a_" + row + "_" + col)
}
function setValue(row,col,val)
{
var v = getCell(row, col)
v.innerHTML = val
v.className = "cell"
}
function getValue(row,col)
{
// alert(row + "," + col)
var v = getCell(row, col)
return v.innerHTML
}
function setHole(row,col)
{
var v = getCell(row, col)
v.innerHTML = ""
v.className = "hole"
ghrow = row
ghcol = col
}
function getRow(obj)
{
var a = obj.id.split("_")
return a[1]
}
function getCol(obj)
{
var a = obj.id.split("_")
return a[2]
}
function isHole(row, col)
{
return (row==ghrow &&col==ghcol) ? true : false
}
function getHoleInRow(row)
{
var i
return (row==ghrow) ? ghcol : -1
}
function getHoleInCol(col)
{
var i
return (col==ghcol) ? ghrow : -1
}
function shiftHoleRow(src,dest,row)
{
var i
//conversion to integer needed in some cases!
src = parseInt(src)
dest = parseInt(dest)
if (src <dest)
{
for (i=srci<desti++)
{
setValue(row,i,getValue(row,i+1))
setHole(row,i+1)
}
}
if (dest <src)
{
for (i=srci>desti--)
{
setValue(row,i,getValue(row,i-1))
setHole(row,i-1)
}
}
}
function shiftHoleCol(src,dest,col)
{
var i
//conversion to integer needed in some cases!
src = parseInt(src)
dest = parseInt(dest)
if (src <dest)
{//alert("src=" + src +" dest=" + dest + " col=" + col)
for (i=srci<desti++)
{//alert(parseInt(i)+1)
setValue(i,col,getValue(i+1,col))
setHole(i+1,col)
}
}
if (dest <src)
{
for (i=srci>desti--)
{
setValue(i,col,getValue(i-1,col))
setHole(i-1,col)
}
}
}
function move(obj)
{
var r, c, hr, hc
if (gintervalid==-1 &&!gshuffling)
{
alert('请点击"开始游戏"按钮')
return
}
r = getRow(obj)
c = getCol(obj)
if (isHole(r,c)) return
hc = getHoleInRow(r)
if (hc != -1)
{
shiftHoleRow(hc,c,r)
gmoves++
checkWin()
return
}
hr = getHoleInCol(c)
if (hr != -1)
{
shiftHoleCol(hr,r,c)
gmoves++
checkWin()
return
}
}
function shuffle()
{
var t,i,j,s,frac
gshuffling = true
frac = 100.0/(gsize*(gsize+10))
s = "% "
for (i=0i<gsizei++)
{
s += "|"
for (j=0j<gsize+10j++)
{
window.status = "Loading " + Math.round((i*(gsize+10) + j)*frac) + s
if (j%2==0)
{
t = r0(gsize)
while (t == ghrow) t = r0(gsize)//skip holes
getCell(t,ghcol).click()
}
else
{
t = r0(gsize)
while (t == ghcol) t = r0(gsize)//skip holes
getCell(ghrow,t).click()
}
}
}
window.status = ""
gshuffling = false
}
function loadBoard(size)
{
gsize = size
board.innerHTML = showTable(gsize)
setHole(gsize-1,gsize-1)
//shuffle()
}
</script>
<div id=test></div>
<table cellpadding=4>
<tr><td align=center>
<b>请选择难度: </B>
<select id=level onchange="loadBoard(parseInt(level.value))">
<option value='3'>3</option>
<option value='4' selected>4</option>
<script>
for (var i=5i<=10i++)
{
document.write("<option value='" + i + "'>" + i + "</option>")
}
</script>
</select>
</td></tr>
<tr><td align=center>
<input type=button class=but value="开始游戏" onclick="startGame()">
<tr><td align=center id=fldStatus class=capt2>
</td></tr>
</table>
<div id=board></div>