- 注册时间
- 2004-8-27
- 最后登录
- 1970-1-1
|
已经用LAVA重写了,在下是对着资料写的,第一次用LAVA,LAVA认证机器认证是FOR ALL的~~呵呵[upload=rar]viewFile.asp?ID=44[/upload]
以下是代码:
/************************************************
程 序 名:极限井字棋
采用语言:LAVA
创 作 于:2004-8
修 改:PM 10:20 2004/8/30
原 作:空小子
第二作者:
第三作者:
....
规 则:Copyleft(自由版权)
说 明:无
************************************************/
#define TRUE 1
#define FALSE 0
//棋子代码,如:玩家1对应代码为1
#define ID_BLANK 0
#define ID_PLAYER1 1
#define ID_PLAYER2 3
#define ID_COMPUTER 2
#define MAN_VS_COMPUTER 1
#define MAN_VS_MAN 2
char box[10];
int x,y; //棋子坐标
int vs_mode; //对战,如:人机对战(player1 VS computer)
int piece_num; //棋子数目,传统模式有9个,极限模式有6个
int player; //当前下子的一方,如:ID_PLAYER1 表示玩家1
int AIOpPos; //智能操作点
int Checkkey(int key)
{
if (key==27) return 27;
else if (key==98) return 1;
else if (key==110) return 2;
else if (key==109) return 3;
else if (key==103) return 4;
else if (key==104) return 5;
else if (key==106) return 6;
else if (key==116) return 7;
else if (key==121) return 8;
else if (key==117) return 9;
else return 0;
}
//设置棋子坐标
void set_xy(int key)
{
if (key==1||key==4||key==7) x=60;
if (key==2||key==5||key==8) x=80;
if (key==3||key==6||key==9) x=100;
if (key==1||key==2||key==3) y=60;
if (key==4||key==5||key==6) y=40;
if (key==7||key==8||key==9) y=20;
}
//检测目标
int SeekAim(int AimID1_num, int AimID1,int AimID2,int SeekTimes)
{
int i,Num;
Num=AimID1_num;
AIOpPos=0;
for(i=1;i<10;i=i+4)
{
if (box==AimID1) Num--;
if (box==AimID2) AIOpPos=i;
}
if (0==Num && AIOpPos!=0) SeekTimes--;
if (0==SeekTimes) return 1;
Num=AimID1_num;
AIOpPos=0;
for(i=3;i<8;i=i+2)
{
if (box==AimID1) Num--;
if (box==AimID2) AIOpPos=i;
}
if (0==Num && AIOpPos!=0) SeekTimes--;
if (0==SeekTimes) return 2;
Num=AimID1_num;
AIOpPos=0;
for(i=1;i<4;i++)
{
if (box==AimID1) Num--;
if (box==AimID2) AIOpPos=i;
}
if (0==Num && AIOpPos!=0) SeekTimes--;
if (0==SeekTimes) return 3;
Num=AimID1_num;
AIOpPos=0;
for(i=4;i<7;i++)
{
if (box==AimID1) Num--;
if (box==AimID2) AIOpPos=i;
}
if (0==Num && AIOpPos!=0) SeekTimes--;
if (0==SeekTimes) return 4;
Num=AimID1_num;
AIOpPos=0;
for(i=7;i<10;i++)
{
if (box==AimID1) Num--;
if (box==AimID2) AIOpPos=i;
}
if (0==Num && AIOpPos!=0) SeekTimes--;
if (0==SeekTimes) return 5;
Num=AimID1_num;
AIOpPos=0;
for(i=1;i<8;i=i+3)
{
if (box==AimID1) Num--;
if (box==AimID2) AIOpPos=i;
}
if (0==Num && AIOpPos!=0) SeekTimes--;
if (0==SeekTimes) return 6;
Num=AimID1_num;
AIOpPos=0;
for(i=2;i<9;i=i+3)
{
if (box==AimID1) Num--;
if (box==AimID2) AIOpPos=i;
}
if (0==Num && AIOpPos!=0) SeekTimes--;
if (0==SeekTimes) return 7;
Num=AimID1_num;
AIOpPos=0;
for(i=3;i<10;i=i+3)
{
if (box==AimID1) Num--;
if (box==AimID2) AIOpPos=i;
}
if (0==Num && AIOpPos!=0) SeekTimes--;
if (0==SeekTimes) return 8;
return 0;
}
void Player1()
{
char key,OldKey;
OldKey=0;
while(player==ID_PLAYER1)
{
key=Checkkey(getchar());
if (key==27) exit(0);
set_xy(key);
if (key!=OldKey && piece_num>0 && box[key]==ID_BLANK)
{
box[key]=ID_PLAYER1;
piece_num--;
Circle(x,y,7,0,1);
if (vs_mode==MAN_VS_COMPUTER) player=ID_COMPUTER; else player=ID_PLAYER2;
}
else if (piece_num==0 && box[key]==ID_PLAYER1)
{
OldKey=key;
box[key]=ID_BLANK;
piece_num++;
Circle(x,y,7,1,0);
player=ID_PLAYER1;
}
}
//胜利后的效果待如,可采用积分制
if (SeekAim(3,ID_PLAYER1,ID_PLAYER1,1))
{
TextOut(20,8,"玩家1获胜!",192);
getchar();
exit(0);
}
}
void Player2()
{
char key,OldKey;
OldKey=0;
while(player==ID_PLAYER2)
{
key=CheckKey(getchar());
if (key==27) exit(0);
set_xy(key);
if (key!=OldKey && piece_num>0 && box[key]==ID_BLANK)
{
box[key]=ID_PLAYER2;
piece_num--;
Circle(x,y,7,0,1);
player=ID_PLAYER1;
}
else if (piece_num==0 && box[key]==ID_PLAYER2)
{
OldKey=key;
box[key]=ID_BLANK;
piece_num++;
Circle(x,y,7,1,1);
player=ID_PLAYER2;
}
}
//胜利后的效果待如,可采用积分制
if (SeekAim(3,ID_PLAYER2,ID_PLAYER2,1))
{
TextOut(20,8,"玩家2获胜!",192);
getchar();
exit(0);
}
}
void computer()
{
int i,j,BakAIOpPos,UnHoldPos1,UnHoldPos2,Win;
//赢
Win=SeekAim(2,ID_COMPUTER,ID_BLANK,1);
if (Win)
{
if (0==piece_num)
for(i=1;i<10;i++)
//直线中的子不许提
if ((Win==1 && i!=1 && i!=5 && i!=9 && box==ID_COMPUTER) ||
(Win==2 && i!=3 && i!=5 && i!=7 && box==ID_COMPUTER) ||
(Win==3 && i!=1 && i!=2 && i!=3 && box==ID_COMPUTER) ||
(Win==4 && i!=4 && i!=5 && i!=6 && box==ID_COMPUTER) ||
(Win==5 && i!=7 && i!=8 && i!=9 && box==ID_COMPUTER) ||
(Win==6 && i!=1 && i!=4 && i!=7 && box==ID_COMPUTER) ||
(Win==7 && i!=2 && i!=5 && i!=8 && box==ID_COMPUTER) ||
(Win==8 && i!=3 && i!=6 && i!=9 && box==ID_COMPUTER))
{
set_xy(i);
Circle(x,y,7,1,0);
break;
}
set_xy(AIOpPos);
Circle(x,y,7,1,1);
//胜利后的效果待如,可采用积分制
TextOut(20,8,"电脑获胜!",192);
getchar();
exit(0);
}
//禁止提子位置
if (0==piece_num)
{
UnHoldPos1=0;
UnHoldPos2=0;
if (SeekAim(2,ID_PLAYER1,ID_COMPUTER,1)) UnHoldPos1=AIOpPos;
if (SeekAim(2,ID_PLAYER1,ID_COMPUTER,2)) UnHoldPos2=AIOpPos;
}
//防
if (SeekAim(2,ID_PLAYER1,ID_BLANK,1))
{
if (0==piece_num)
for(i=1;i<10;i++)
if (i!=UnHoldPos1 && i!=UnHoldPos2 && box==ID_COMPUTER)
{
box=ID_BLANK;
set_xy(i);
Circle(x,y,7,1,0);
piece_num++;
break;
}
box[AIOpPos]=ID_COMPUTER;
set_xy(AIOpPos);
Circle(x,y,7,1,1);
player=ID_PLAYER1;
piece_num--;
return;
}
//此随机操作将中间格的优先权去掉,降低了难度了
while(1)
{
j=rand()*10/32767;
if (box[j]==ID_BLANK)
{
if (0==piece_num)
for(i=1;i<10;i++)
if (i!=UnHoldPos1 && i!=UnHoldPos2 && box==ID_COMPUTER)
{
box=ID_BLANK;
set_xy(i);
Circle(x,y,7,1,0);
piece_num++;
break;
}
box[j]=ID_COMPUTER;
set_xy(j);
Circle(x,y,7,1,1);
player=ID_PLAYER1;
piece_num--;
return;
}
}
}
//关于
void about()
{
ClearScreen();
SetScreen(1);
Locate(2,1);
printf("编 写:空小子");
Locate(3,1);
printf("规 则:自由版权");
Locate(4,1);
printf("E-MAIL:kxz-kong@163.com");
UpdateLCD(0);
TextOut(20,8,"【极限井字棋】",192);
//画3D框
Box(1,1,155,75,0,1);
Box(3,3,153,73,0,1);
Box(5, 75, 160, 80, 1,1);
Box(155, 5, 160, 80, 1,1);
getchar();
}
//菜单
int set_menu()
{
char ch;
CATCH_KEY:
ch=getchar();
if (ch==27) exit(0);
else if(ch=='b') return 1;
else if(ch=='n') return 2;
goto CATCH_KEY;
}
//设置
void set()
{
ClearScreen();
SetScreen(0);
Locate(0,0);
printf("%s","对战:");
Locate(1,4);
printf("%s","1.人机对战");
Locate(3,4);
printf("%s","2.人脑对战");
UpdateLCD(0);
vs_mode=set_menu();
ClearScreen();
SetScreen(0);
Locate(0,0);
printf("%s","模式:");
Locate(1,4);
printf("%s","1.极限模式");
Locate(3,4);
printf("%s","2.传统模式");
UpdateLCD(0);
//设置棋子数目
piece_num=(set_menu())*3 + 3;
ClearScreen();
SetScreen(0);
Locate(0,0);
printf("%s","优先:");
Locate(1,4);
printf("%s"," 1.人先行 ");
Locate(3,4);
printf("%s"," 2.电脑先 ");
UpdateLCD(0);
//设置优先级
if (vs_mode==MAN_VS_COMPUTER) player=set_menu();
else player=ID_PLAYER1;
}
//初始化
void init()
{
int i;
//数组初始化
for(i=1;i<10;i++) box=0;
ClearScreen();
SetScreen(0);
UpdateLCD(0);
//棋盘
Box(48,8,112,72,0,1);
Box(50,10,110,70,0,1);
Box(70,10,90,70,0,1);
Box(50,30,110,50,0,1);
//阴影
Box(112,15,115,72,1,1);
Box(55,72,115,75,1,1);
}
void start()
{
while(1)
{
if (vs_mode==MAN_VS_COMPUTER)
{
if (player==ID_PLAYER1) Player1(); else computer();
}
else
{
Player1();
Player2();
}
}
}
void main()
{
about();
set();
init();
start();
}
|
|