首页 > 用户发贴区 > 编程问题提问区 > 高手请教 急急急
2008
01-16

本人属于新手菜鸟级。。。 用C做一个五子棋的小游戏 再网上看了一些一些程序 可目前网上提供的程序大多棋子都是用宏定义的 棋子样子用ASC代码显示 在走棋时是   光标显示 确定后在显示棋子 我想把棋子各改成 黑白实心的圆 走棋时用一个正方形框显示走棋的地方 请个高手赐教。。原代码如下。。。如果能帮助的话 希望高手帮我加一个悔棋功能 谢谢啦。。不胜感激!!!!!!!!!!!!!!


 #include <stdio.h>
#include <bios.h>
#include <ctype.h>
#include <conio.h>
#include <dos.h>
#define CROSSRU 0xbf
#define CROSSLU 0xda
#define CROSSLD 0xc0
#define CROSSRD 0xd9
#define CROSSL 0xc3
#define CROSSR 0xb4
#define CROSSU 0xc2
#define CROSSD 0xc1
#define CROSS 0xc5



#define BLACKUP 0×1157
#define BLACKDOWN 0x1f53
#define BLACKLEFT 0x1e41
#define BLACKRIGHT 0×2044
#define BLACKDO 0×3920


#define WHITEUP 0×4800
#define WHITEDOWN 0×5000
#define WHITELEFT 0x4b00
#define WHITERIGHT 0x4d00
#define WHITEDO 0x1c0d
#define ESCAPE 0x011b



#define CHESSNULL 0
#define CHESS1 001
#define CHESS2 002
#define KEYEXIT 0
#define KEYFALLCHESS 1
#define KEYMOVECURSOR 2
#define KEYINVALID 3
#define TRUE 1
#define FALSE 0


struct point
{
 int x,y;
 };
void Init(void);
int GetKey(void);
int CheckKey(int press);
int ChangeOrder(void);
int ChessGo(int Order,struct point Cursor);
void DoError(void);
void DoOK(void);
void DoWin(int Order);
void MoveCursor(int Order,int press);
void DrawCross(int x,int y);
void DrawMap(void);
int JudgeWin(int Order,struct point Cursor);
int JudgeWinLine(int Order,struct point Cursor,int direction);
void ShowOrderMsg(int Order);
void EndGame(void);


int gPlayOrder;
struct point gCursor;
char gChessBoard[15][15];


void main()
{
int press;
int bOutWhile=FALSE;


Init();


while(1)
{
press=GetKey();
switch(CheckKey(press))
{
case KEYEXIT:
clrscr();
bOutWhile = TRUE;
break;
case KEYFALLCHESS:
if(ChessGo(gPlayOrder,gCursor)==FALSE)
DoError();
else
{
DoOK();


if(JudgeWin(gPlayOrder,gCursor)==TRUE)
{
DoWin(gPlayOrder);
bOutWhile = TRUE;
}
else
ChangeOrder();
ShowOrderMsg(gPlayOrder);
}
break;
case KEYMOVECURSOR:
MoveCursor(gPlayOrder,press);
break;
case KEYINVALID:
break;
}


if(bOutWhile==TRUE)
break;
}


EndGame();
}
void Init(void)
{
int i,j;
char *Msg[]=
{
“BLACK key:”,
” UP—-w”,
” DOWN–s”,
” LEFT–a”,
” RIGHT-d”,
” DO—-space”,
“”,
“WHITE key:”,
” UP—-up”,
” DOWN–down”,
” LEFT–left”,
” RIGHT-right”,
” DO—-ENTER”,
“”,
“exit game:”,
” ESC”,
NULL,
};


gPlayOrder = CHESS1;
for(i=0;i<15;i++)
for(j=0;j<15;j++)
gChessBoard[i][j]=CHESSNULL;
gCursor.x=gCursor.y=7;
textmode(C40);
DrawMap();
i=0;
textcolor(WHITE);
while(Msg[i]!=NULL)
{
gotoxy(20,3+i);
cputs(Msg[i]);
i++;
}


ShowOrderMsg(gPlayOrder);
gotoxy(gCursor.x,gCursor.y);
}
void DrawMap(void)
{
int i,j;


clrscr();


for(i=0;i<15;i++)
for(j=0;j<15;j++)
DrawCross(i,j);


}
void DrawCross(int x,int y)
{
gotoxy(x,y);
if(gChessBoard[x][y]==CHESS1)
{
textcolor(RED);
putch(CHESS1);
return;
}
if(gChessBoard[x][y]==CHESS2)
{
textcolor(RED);
putch(CHESS2);
return;
}


textcolor(RED);


if(x==0&&y==0)
{
putch(CROSSLU);
return;
}
if(x==0&&y==14)
{
putch(CROSSLD);
return;
}
if(x==14&&y==0)
{
putch(CROSSRU);
return;
}
if(x==14&&y==14)
{
putch(CROSSRD);
return;
}
if(x==0)
{
putch(CROSSL);
return;
}
if(x==14)
{
putch(CROSSR);
return;
}
if(y==0)
{
putch(CROSSU);
return;
}
if(y==14)
{
putch(CROSSD);
return;
}
putch(CROSS);
}
int ChangeOrder(void)
{
if(gPlayOrder==CHESS1)
gPlayOrder=CHESS2;
else
gPlayOrder=CHESS1;


return(gPlayOrder);
}
int GetKey(void)
{
char lowbyte;
int press;


while (bioskey(1) == 0);


press=bioskey(0);
lowbyte=press&0xff;
press=press&0xff00 + toupper(lowbyte);
return(press);
}
void DoError(void)
{
sound(1200);
delay(50);
nosound();
}
void DoWin(int Order)
{
sound(1500);delay(100);
sound(0); delay(50);
sound(800); delay(100);
sound(0); delay(50);
sound(1500);delay(100);
sound(0); delay(50);
sound(800); delay(100);
sound(0); delay(50);
nosound();


textcolor(RED);
gotoxy(25,20);
if(Order==CHESS1)
cputs(“BLACK WIN!”);
else
cputs(“WHITE WIN!”);
gotoxy(25,21);
cputs(“\n”);
getch();
}
int ChessGo(int Order,struct point Cursor)
{
if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL)
{
gotoxy(Cursor.x,Cursor.y);
textcolor(WHITE);
putch(Order);
gotoxy(Cursor.x,Cursor.y);
gChessBoard[Cursor.x][Cursor.y]=Order;
return TRUE;
}
else
return FALSE;
}
int JudgeWin(int Order,struct point Cursor)
{
int i;
for(i=0;i<4;i++)
if(JudgeWinLine(Order,Cursor,i))
return TRUE;
return FALSE;
}
int JudgeWinLine(int Order,struct point Cursor,int direction)
{
int i;
struct point pos,dpos;
const int testnum = 5;
int count;


switch(direction)
{
case 0:
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y;
dpos.x=1;
dpos.y=0;
break;
case 1:
pos.x=Cursor.x;
pos.y=Cursor.y-(testnum-1);
dpos.x=0;
dpos.y=1;
break;
case 2:
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y+(testnum-1);
dpos.x=1;
dpos.y=-1;
break;
case 3:
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y-(testnum-1);
dpos.x=1;
dpos.y=1;
break;
}


count=0;
for(i=0;i<testnum*2+1;i++)
{
if(pos.x>=0&&pos.x<=14&&pos.y>=0&&pos.y<=14)
{
if(gChessBoard[pos.x][pos.y]==Order)
{
count++;
if(count>=testnum)
return TRUE;
}
else
count=0;
}
pos.x+=dpos.x;
pos.y+=dpos.y;
}


return FALSE;
}
void MoveCursor(int Order,int press)
{
switch(press)
{
case BLACKUP:
if(Order==CHESS1&&gCursor.y>0)
gCursor.y–;
break;
case BLACKDOWN:
if(Order==CHESS1&&gCursor.y<14)
gCursor.y++;
break;
case BLACKLEFT:
if(Order==CHESS1&&gCursor.x>0)
gCursor.x–;
break;
case BLACKRIGHT:
if(Order==CHESS1&&gCursor.x<14)
gCursor.x++;
break;


case WHITEUP:
if(Order==CHESS2&&gCursor.y>0)
gCursor.y–;
break;
case WHITEDOWN:
if(Order==CHESS2&&gCursor.y<14)
gCursor.y++;
break;
case WHITELEFT:
if(Order==CHESS2&&gCursor.x>0)
gCursor.x–;
break;
case WHITERIGHT:
if(Order==CHESS2&&gCursor.x<14)
gCursor.x++;
break;
}


gotoxy(gCursor.x,gCursor.y);
}
void EndGame(void)
{
textmode(C80);
}
void ShowOrderMsg(int Order)
{
gotoxy(6,20);
textcolor(LIGHTCYAN);
if(Order==CHESS1)
cputs(“BLACK go!”);
else
cputs(“WHITE go!”);


gotoxy(gCursor.x,gCursor.y);
}
void DoOK(void)
{
 sound(500);
delay(70);
sound(600);
delay(50);
sound(1000);
delay(100);
nosound();
}
int CheckKey(int press)
{
if(press==ESCAPE)
return KEYEXIT;


else
if
( ( press==BLACKDO && gPlayOrder==CHESS1) ||
( press==WHITEDO && gPlayOrder==CHESS2)
)
return KEYFALLCHESS;


else
if
( press==BLACKUP || press==BLACKDOWN ||
press==BLACKLEFT || press==BLACKRIGHT ||
press==WHITEUP || press==WHITEDOWN ||
press==WHITELEFT || press==WHITERIGHT
)
return KEYMOVECURSOR;


else
return KEYINVALID;
}


留下一个回复