① 使用c语言及VC绘图库,在VC环境下,设计一个双人五子棋对战单机游戏。
学习不是这样的。以前做题不管怎么样,题会不会,先写个“解”字再说。你连这一步都没做到。最起码,你会建立工程吧,建立一个工程,先做个对话框吧,做个对话框再在上面画棋盘吧……
这就是工程的分解。你要问的应该是如何用VC做图,如何响应鼠标消息,如何记录下棋的步骤等。一拿到东西就先说不会,问有没有人会,这样你永远是个学生;而且是个成绩不好的学生。
② 急需用c语言写中国象棋的代码,只要红色方布局和走棋
#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> #include<windows.h>
int x,y,i,j,k,p,q, num = 1, round; //象棋游戏的全局变量
int place_x1 = 0,place_y1 = 0,place_x2 = 0,place_y2 = 0;
int check_x,check_y,check_turn; //基本参数
char ch, turn = 'O',turn1 = 'N',temp,temp1;
char check_1[9][3] ={"车","马","象","士","将","炮","兵","+-"}; //取棋子时只判断前8合法
char check_2[9][3] ={"车","马","相","仕","帅","炮","卒","+-"}; //下棋时多一空位合法
char check[3];
void check_main1(char* temp,char* temp1,char* turn,char* turn1,int *num,int *if_return,char map[100][100])
{ //(象棋函数 判断 将方 下棋是否合法
check[0] = *temp; check[1] = *temp1; check[2] = '\0'; char a,b;
for ( i = 0; i < 8; i++)
{ if ( strcmp(check_2[i],check) == 0)
{ *temp = *turn; *temp1 = *turn1; *turn = 'O'; *turn1 = 'N';
if( i < 7){ printf(" 帅方的%s被吃\n",check_2[i]); Sleep(500); } *num = *num + 1;
for( k = 4; k <= 8; k = k + 2) //判断 帅 是否死亡
{ for(j = 15; j <= 23; j= j+ 4)
{ if (map[k][j] == check_2[4][0] && map[k][j+1] == check_2[4][1])
{ place_x2 = k; place_y2 = j; break; } }
if( j <= 23) break;
}
if( k == 10)
{printf(" 帅 被将死 将方获得胜利\n"); printf("按任意键返回菜单");
getch( ); *if_return = 1; return;
}
for( k = 18; k <= 22; k = k + 2) //判断 将 是否死亡
{for(j = 15; j <= 23; j= j+ 4)
{if(map[k][j] == check_1[4][0] && map[k][j+1] == check_1[4][1])
{place_x1 = k; place_y1 = j; break; } }
if( j <= 23) break;
}
if ( k == 24)
{printf(" 将 被将死 帅方获得胜利\n"); printf("按任意键返回菜单");
getch( ); *if_return = 1; return;
}
if ( place_y1 == place_y2)
{for( k = place_x2 + 2; k <= place_x1 - 2; k = k +2) {if(map[k][place_y1] != '+') break;}
if( k == place_x1)
{if(round == 1) printf(" 将方对将 帅方胜利");
else if( round == 2) printf(" 帅方对将 将方胜利");
printf("按任意键返回菜单"); getch( ); *if_return = 1; return;
}
}
break;
}
} // for ( i = 0; i < 8; i++)循环结束
if( i == 8) {printf("不合法的走法\n"); Sleep(500); }
}
void check_main2(char* temp,char* temp1,char* turn,char* turn1,int *num,int *if_return,char map[100][100])
{ //象棋函数 判断 帅方 下棋是否合法
check[0] = *temp; check[1] = *temp1; check[2] = '\0'; char a,b;
for ( i = 0; i < 8; i++)
{if ( strcmp(check_1[i],check) == 0)
{ *temp = *turn; *temp1 = *turn1; *turn = 'O'; *turn1 = 'N';
if( i < 7) {printf(" 将方的%s被吃",check_1[i]); Sleep(500); } *num = *num + 1;
for( k = 4; k <= 8; k = k + 2) //判断 帅 是否死亡
{for(j = 15; j <= 23; j= j+ 4)
{if(map[k][j] == check_2[4][0] && map[k][j+1] == check_2[4][1])
{place_x2 = k; place_y2 = j; break; } }
if( j <= 23) break;
}
if( k == 10)
{printf(" 帅 被将死 将方获得胜利\n");printf("按任意键返回菜单"); getch( );
*if_return = 1; return;
}
for( k = 18; k <= 22; k = k + 2) //判断 将 是否死亡
{for(j = 15; j <= 23; j= j+ 4)
{if(map[k][j] == check_1[4][0] && map[k][j+1] == check_1[4][1])
{place_x1 = k; place_y1 = j; break; } }
if( j <= 23) break;
}
if( k == 24)
{printf(" 将 被将死 帅方获得胜利\n");printf("按任意键返回菜单"); getch( );
*if_return = 1; return; }
if( place_y1 == place_y2)
{for( k=place_x2 + 2; k <= place_x1 - 2; k=k +2) {if(map[k][place_y1] != '+') break; }
if( k == place_x1)
{if(round==1)printf(" 将方对将 帅方胜利");else if(round==2)printf(" 帅方对将 将方胜利");
printf("按任意键返回菜单"); getch( ); *if_return = 1; return; }
}
break;
}
} // for ( i = 0; i < 8; i++)循环结束
if( i == 8) {printf("不合法的走法\n"); Sleep(500); }
}
void xiangqi( ) //象棋主程序
{ char map[100][100]= { "[[===================================]]",
"[| ①将 【象棋】 ②帅 |]",
"[[===================================]]",
"[[-----------------------------------]]",
"[[ 车—-马—-相—-仕—-帅—-仕—-相—-马—-车]]",
"[[ | | | | \\ | / | | | | ]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[ | | | | / | \\ | | | | ]]",
"[[ +-—-炮—-+-—-+-—-+-—-+-—-+-—-炮—-+-]]",
"[[ | | | | | | | | | ]]",
"[[ 卒—-+-—-卒—-+-—-卒—-+-—-卒—-+-—-卒]]",
"[[ | | | | | | | | | ]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[===================================]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[ | | | | | | | | | ]]",
"[[ 兵—-+-—-兵—-+-—-兵—-+-—-兵—-+-—-兵]]",
"[[ | | | | | | | | | ]]",
"[[ +-—-炮—-+-—-+-—-+-—-+-—-+-—-炮—-+-]]",
"[[ | | | | \\ | / | | | | ]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[ | | | | / | \\ | | | | ]]",
"[[ 车—-马—-象—-士—-将—-士—-象—-马—-车]]",
"[[-----------------------------------]]",
"[[===================================]]"};
int if_return = 0;
system("mode con cols=45 lines=32"); //迷你界面
system("color 70");
printf("[[==================================]]\n");
printf("[[ -------------------------------- ]]\n");
printf("[[ | | ]]\n");
printf("[[ | 【<<游戏规则>>】 | ]]\n");
printf("[[ | | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 控制wasd双方轮流控制指针下棋| ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 键盘输入大小写 ' M ' | ]]\n");
printf("[[ | 都视为确认下棋 | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 为了方便区分棋子 | ]]\n");
printf("[[ | 后手方全设为繁体复杂字体 | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 我已阅读规则,按任意键继续 | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[==================================]]\n");
getch( ); system("mode con cols=45 lines=32"); //迷你界面
system("color 70");
for ( i = 0; i < 27; i++){ puts(map[i]); Sleep(100); }
x = 6, y = 19; temp = map[x][y]; temp1 = map[x][y+1];
while(num)
{ if (num % 2 == 1 &&num / 2 % 2 == 0){ printf(" 现在是'将'的回合\n");round = 1; }
else if( num %2 == 1){ printf(" 现在轮到'帅'的回合了\n");round = 2; }
ch = getch( );
if ( ch == 's') //下移
{ if ( map[x+1][y]!= '-')
{map[x][y] =temp; map[x][y+1] = temp1; x = x + 2;
temp = map[x][y]; temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if ( ch == 'a') //左移
{ if (map[x][y-1]!=' ')
{map[x][y] =temp; map[x][y+1] = temp1; y = y - 4;
temp = map[x][y]; temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if ( ch == 'w') //上移
{ if ( map[x-1][y]!= '-')
{map[x][y] =temp; map[x][y+1] = temp1; x = x - 2; temp = map[x][y];
temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if ( ch == 'd') //右移
{ if (map[x][y+2]!=']')
{map[x][y] =temp; map[x][y+1] = temp1; y = y + 4; temp = map[x][y];
temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if( ch == 'm' || ch =='M') //M确认要移动的棋子,或确认要移到的目的地
{ if (num % 2 == 1 && temp != '+' && temp1 != '-') //取棋
{check[0] = temp; check[1] = temp1; check[2] = '\0';
if ( round == 1)
{ for ( i = 0; i < 7; i++) //将方
{ if ( strcmp(check_1[i],check) == 0)
{turn = temp; turn1 = temp1; temp = '+'; temp1 = '-';
check_x = x; check_y = y; check_turn = 10 + i; num++; break; }
}
if( i == 7){ printf("这不是你的棋子\n"); Sleep(500); }
}
else if( round == 2)
{for ( i = 0; i < 7; i++) //帅方
{ if( strcmp(check_2[i],check) == 0)
{turn = temp; turn1 = temp1; temp = '+'; temp1 = '-';
check_x = x; check_y = y; check_turn = 20 + i; num++; break; }
}
if( i == 7){ printf("这不是你的棋子\n"); Sleep(500); }
}
}
else if( num % 2 == 0) //放棋
{ char check_1[8][3] ={"车","马","象","士","将","炮","卒","+-"};
char check_2[8][3] ={"俥","马","相","仕","帅","軳","兵","+-"};
//中界 楚河上下坐标 12 15 往下2 往右4
if( check_turn < 20) //将方
{if( check_turn == 10) //车的走法规范
{ if((x == check_x && y == check_y))
{temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;
printf("三思而后行\n"); printf("还是你的回合"); Sleep(500); }
else if( y == check_y )
{ if( x > check_x)
{ for(j = check_x + 2; j < x;j = j + 2)
{ if(map[j][y] == '+'); else{printf("不合法的下法\n"); Sleep(500); break; } }
if( j >= x) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
if( x < check_x){ for(j = check_x - 2; j > x;j = j - 2)
{ if(map[j][y] == '+'); else{printf("不合法的下法\n"); Sleep(500); break; }
}
if( j <= x)check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
}
else if( x == check_x )
{if( y > check_y)
{for(j = check_y + 4; j < y;j = j + 4)
{if(map[x][j] == '+'); else {printf("不合法的下法\n"); Sleep(500); break; }
}
if( j >= y) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
if( y < check_y)
{for(j = check_y - 4; j > y;j = j - 4)
{ if(map[x][j] == '+'); else { printf("不合法的下法\n"); Sleep(500); break; }
}
if( j <= y) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
}
else { printf("不合法的下法\n"); Sleep(500); }
}
if( check_turn == 11) //马的走法规范
{if((x == check_x && y == check_y))
{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;
printf("三思而后行\n"); printf("还是你的回合"); Sleep(500); }
else if( (abs( x - check_x) == 2&& abs( y - check_y) == 8)&& map[check_x][(y+check_y)/2] =='+')
{check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }
else if( (abs( x - check_x) == 4&& abs( y - check_y) == 4)&& map[(x + check_x)/2][check_y] == '+' )
{check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }
else { printf("不合法的下法\n");Sleep(500); }
} //其余代码在后续
③ c语言的五子棋编码
我有用qt写的五子棋,你要是要的话可以发给你
④ C语言五子棋
我浏览了一下你的代码,你对胜利的判断采用的是检查整个棋盘的方式,这样做的最大弊端自然是低效,而且在编写斜方向判断的时候比较复杂。我建议你采用下子后判断的方式,即玩家1下了一个字后,像该子周围8个方向检查是否存在5子的情况,只需要一个判断方法,而且较为简单。
void chooseONE()
{
printf("第一玩家请选择下棋位置\n");
printf("第几个直的\n");
scanf("%d",&co);
printf("第几个横的\n");
scanf("%d",&ro);
while(arr[ro][co]==1||arr[ro][co]==2)
{
printf("重复了!!!\n");
printf("第几个直的\n");
scanf("%d",&co);
printf("第几个横的\n");
scanf("%d",&ro);
}
arr[ro][co]=1;
//这里加判断代码,建议用一个判断函数,我给你写个吧
}
//我这里这个方法写了个大概,你看一下和你的代码结合一下就可以了,我用i和j作为for循环的变量,你用来做最大的长宽值,这些你都要改一下。
flag作为判断是否胜利的标志,playerNum是玩家的标志,分1和2,这个函数只写了左和左上的判断,其他方向楼主仿照着写
int win(int row, int col, int playerNum){
int i = 0,j = 0;
int flag = 1;
if(row - 4 >= 0){
flag = 1;
for(i = row; i >= 0; i--){
if(arr[i][j] != playerNum){
flag = 0;
break;
}
}
if(flag == 1){
return 1;
}
}
if(row - 4 >= 0 && col - 4 >= 0){
flag = 1;
for(i = row ,j = col ; i >= 0&&j>=0; i--,j--){
if(arr[i][j] != playerNum){
flag = 0;
break;
}
}
if(flag == 1){
return 1;
}
}
}
⑤ 用C语言编写一个五子棋的游戏程序
**********************************************************/
/* 程序中用到的库函数所在头文件应用 #include 命令包含进来 */
#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 MAPXOFT 5
#define MAPYOFT 2
/*定义1号玩家的操作键键码*/
#define PLAY1UP 0x1157/*上移--'W'*/
#define PLAY1DOWN 0x1f53/*下移--'S'*/
#define PLAY1LEFT 0x1e41/*左移--'A'*/
#define PLAY1RIGHT 0x2044/*右移--'D'*/
#define PLAY1DO 0x3920/*落子--空格键*/
/*定义2号玩家的操作键键码*/
#define PLAY2UP 0x4800/*上移--方向键up*/
#define PLAY2DOWN 0x5000/*下移--方向键down*/
#define PLAY2LEFT 0x4b00/*左移--方向键left*/
#define PLAY2RIGHT 0x4d00/*右移--方向键right*/
#define PLAY2DO 0x1c0d/*落子--回车键Enter*/
/*若想在游戏中途退出, 可按 Esc 键*/
#define ESCAPE 0x011b
/*定义棋盘上交叉点的状态, 即该点有无棋子 */
/*若有棋子, 还应能指出是哪个玩家的棋子 */
#define CHESSNULL 0 //没有棋子
#define CHESS1 'O'//一号玩家的棋子
#define CHESS2 'X'//二号玩家的棋子
/*定义按键类别*/
#define KEYEXIT 0/*退出键*/
#define KEYFALLCHESS 1/*落子键*/
#define KEYMOVECURSOR 2/*光标移动键*/
#define KEYINVALID 3/*无效键*/
/*定义符号常量: 真, 假 --- 真为1, 假为0 */
#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[19][19];/*用于记录棋盘上各点的状态*/
/**********************************************************/
/**********************************************************/
/*主函数*/
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();
}
break;
/*是光标移动键*/
case KEYMOVECURSOR:
MoveCursor(gPlayOrder,press);
break;
/*是无效键*/
case KEYINVALID:
break;
}
if(bOutWhile==TRUE)
break;
}
/*游戏结束*/
EndGame();
}
/**********************************************************/
/*界面初始化,数据初始化*/
void Init(void)
{
int i,j;
char *Msg[]=
{
"Player1 key:",
" UP----w",
" DOWN--s",
" LEFT--a",
" RIGHT-d",
" DO----space",
"",
"Player2 key:",
" UP----up",
" DOWN--down",
" LEFT--left",
" RIGHT-right",
" DO----ENTER",
"",
"exit game:",
" ESC",
NULL,
};
/*先手方为1号玩家*/
gPlayOrder = CHESS1;
/*棋盘数据清零, 即棋盘上各点开始的时候都没有棋子*/
for(i=0;i<19;i++)
for(j=0;j<19;j++)
gChessBoard[i][j]=CHESSNULL;
/*光标初始位置*/
gCursor.x=gCursor.y=0;
/*画棋盘*/
textmode(C40);
DrawMap();
/*显示操作键说明*/
i=0;
textcolor(BROWN);
while(Msg[i]!=NULL)
{
gotoxy(25,3+i);
cputs(Msg[i]);
i++;
}
/*显示当前行棋方*/
ShowOrderMsg(gPlayOrder);
/*光标移至棋盘的左上角点处*/
gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}
/*画棋盘*/
void DrawMap(void)
{
int i,j;
clrscr();
for(i=0;i<19;i++)
for(j=0;j<19;j++)
DrawCross(i,j);
}
/*画棋盘上的交叉点*/
void DrawCross(int x,int y)
{
gotoxy(x+MAPXOFT,y+MAPYOFT);
/*交叉点上是一号玩家的棋子*/
if(gChessBoard[x][y]==CHESS1)
{
textcolor(LIGHTBLUE);
putch(CHESS1);
return;
}
/*交叉点上是二号玩家的棋子*/
if(gChessBoard[x][y]==CHESS2)
{
textcolor(LIGHTBLUE);
putch(CHESS2);
return;
}
textcolor(GREEN);
/*左上角交叉点*/
if(x==0&&y==0)
{
putch(CROSSLU);
return;
}
/*左下角交叉点*/
if(x==0&&y==18)
{
putch(CROSSLD);
return;
}
/*右上角交叉点*/
if(x==18&&y==0)
{
putch(CROSSRU);
return;
}
/*右下角交叉点*/
if(x==18&&y==18)
{
putch(CROSSRD);
return;
}
/*左边界交叉点*/
if(x==0)
{
putch(CROSSL);
return;
}
/*右边界交叉点*/
if(x==18)
{
putch(CROSSR);
return;
}
/*上边界交叉点*/
if(y==0)
{
putch(CROSSU);
return;
}
/*下边界交叉点*/
if(y==18)
{
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+BLINK);
gotoxy(25,20);
if(Order==CHESS1)
cputs("PLAYER1 WIN!");
else
cputs("PLAYER2 WIN!");
gotoxy(25,21);
cputs(" \\<^+^>/");
getch();
}
/*走棋*/
int ChessGo(int Order,struct point Cursor)
{
/*判断交叉点上有无棋子*/
if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL)
{
/*若没有棋子, 则可以落子*/
gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);
textcolor(LIGHTBLUE);
putch(Order);
gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);
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++)
/*判断在指定方向上是否有连续5个行棋方的棋子*/
if(JudgeWinLine(Order,Cursor,i))
return TRUE;
return FALSE;
}
/*判断在指定方向上是否有连续5个行棋方的棋子*/
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<=18&&pos.y>=0&&pos.y<=18)
{
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 PLAY1UP:
if(Order==CHESS1&&gCursor.y>0)
gCursor.y--;
break;
case PLAY1DOWN:
if(Order==CHESS1&&gCursor.y<18)
gCursor.y++;
break;
case PLAY1LEFT:
if(Order==CHESS1&&gCursor.x>0)
gCursor.x--;
break;
case PLAY1RIGHT:
if(Order==CHESS1&&gCursor.x<18)
gCursor.x++;
break;
case PLAY2UP:
if(Order==CHESS2&&gCursor.y>0)
gCursor.y--;
break;
case PLAY2DOWN:
if(Order==CHESS2&&gCursor.y<18)
gCursor.y++;
break;
case PLAY2LEFT:
if(Order==CHESS2&&gCursor.x>0)
gCursor.x--;
break;
case PLAY2RIGHT:
if(Order==CHESS2&&gCursor.x<18)
gCursor.x++;
break;
}
gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}
/*游戏结束处理*/
void EndGame(void)
{
textmode(C80);
}
/*显示当前行棋方*/
void ShowOrderMsg(int Order)
{
gotoxy(6,MAPYOFT+20);
textcolor(LIGHTRED);
if(Order==CHESS1)
cputs("Player1 go!");
else
cputs("Player2 go!");
gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}
/*落子正确处理*/
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==PLAY1DO && gPlayOrder==CHESS1) ||
( press==PLAY2DO && gPlayOrder==CHESS2)
)
return KEYFALLCHESS;/*是落子键*/
else
if
( press==PLAY1UP || press==PLAY1DOWN ||
press==PLAY1LEFT || press==PLAY1RIGHT ||
press==PLAY2UP || press==PLAY2DOWN ||
press==PLAY2LEFT || press==PLAY2RIGHT
)
return KEYMOVECURSOR;/*是光标移动键*/
else
return KEYINVALID;/*按键无效*/
}
⑥ c语言写一个类似于下棋的程序
帮你写了个当个字符转换的
如果是字符串的话请追问. 再帮你修改.
#include
void main()
{
char a;
a=getchar();
if(a>='A' && a<='Z')
a+=32;
else if(a>='a' && a<='z')
a-=32;
putchar(a);
getch();
}
补充:
这是字符串的.
#include
void main()
{
char a[50];
int i=0;
gets(a);
while(a[i])
{
if(a[i] >= 'A' && a[i] <= 'Z')
a[i] += 32;
else if(a[i] >= 'a' && a[i] <= 'z')
a[i] -= 32;
i++;
}
puts(a);
}
追问:
字符串的就不用了。今天的这个题目就是转换大小写字符的,如果输入字母或者是特殊的符号,怎样加IF语句?
⑦ 使用c语言编写一个人机对战下棋程序
#include "stdafx.h"
#include "resource.h"
#include "define.h"
#include "box.h"
#include "moveGeneration.h"
#pragma comment(lib, "winmm.lib")
#include <Mmsystem.h>
#define MAX_LOADSTRING 100
#define BOXBMP_WIDTH 537 /* 棋盘图片宽度 */
#define BOXBMP_HEIGHT 537 /* 棋盘图片高度 */
#define SPACE 20 /* 棋盘图片和窗口边沿间隔 */
#define UNIT_WIDTH 35 /* 每个棋格宽度 */
#define UNIT_HEIGHT 35 /* 每个棋格高度 */
#define STONE_WIDTH 20 /* 棋子宽度 */
/* 窗口宽度 = 两边空白 + 棋盘图片宽度 + 窗口边框宽度 */
#define WIN_WIDTH 2*SPACE+BOXBMP_WIDTH+2*GetSystemMetrics(SM_CXDLGFRAME)
/* 窗口高度 = 标题栏高度 + 上下两边空白 + 棋盘图片高度 + 窗口边框高度 */
#define WIN_HEIGHT GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CYMENU)+2*SPACE\
+BOXBMP_HEIGHT+2*GetSystemMetrics(SM_CXDLGFRAME)
//////////////////////////////////////////////////////////////////////
// 全局变量:
static HINSTANCE hInst; // 当前实例
static TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
static TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
//////////////////////////////////////////////////////////////////////
// 此代码模块中包含的函数的前向声明:
ATOM InitApplication(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK About(HWND, UINT, WPARAM, LPARAM);
////////////////////////////////////////////////////////////////////// 绘制棋子
void DrawStone(HDC hdc, int column, int row, STONE_TYPE stone)
{
HPEN hpen;
HBRUSH hbrush;
switch (stone)
{
case BLACK_STONE: //黑子
hpen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
hbrush = CreateSolidBrush(RGB(0, 0, 0));
break;
case WHITE_STONE: //白子
hpen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
hbrush = CreateSolidBrush(RGB(255, 255, 255));
break;
default:
return;
}
SelectObject(hdc, hpen);
SelectObject(hdc, hbrush);
Ellipse(hdc, column*34.9+35, row*34.9+35, column*34.9+55, row*34.9+55);
DeleteObject(hpen);
DeleteObject(hbrush);
return;
}
//////////////////////////////////////////////////////////// 绘制棋盘
void DrawChessBox(HDC hdc, STONE_TYPE box[BOX_ROW][BOX_COL])
{
assert(NULL != box);
HBITMAP hBitmap;
HDC hdcSrc;
hBitmap = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_CHESSBOX));
hdcSrc = CreateCompatibleDC(hdc);
SelectObject(hdcSrc, hBitmap);
BitBlt(hdc, SPACE, SPACE, BOXBMP_WIDTH, BOXBMP_HEIGHT, hdcSrc, 0, 0, SRCCOPY);
DeleteObject(hBitmap);
for(int column=0; column<BOX_COL; column++)
{
for(int row=0; row<BOX_ROW; row++)
{
DrawStone(hdc, column, row, box[column][row]);
}
}
}
////////////////////////////////////////////////////////////////////
//重新开始
static STONE_TYPE NewGame(HWND hWnd, STONE_TYPE box[BOX_ROW][BOX_COL])
{
boxInit(box);//清空
InvalidateRect(hWnd, NULL, FALSE);//重绘
return BLACK_STONE;
}
/**
* @brief 放置棋子
* @param hWnd 窗口句柄
* @param box 当前棋盘
* @param point 当前所下棋子在棋盘中的坐标
* @return TRUE 游戏结束, FALSE 游戏未结束
*/
static BOOL PlaceStone(HWND hWnd, STONE_TYPE box[BOX_ROW][BOX_COL], POINTS point, STONE_TYPE stone)
{
STONE_TYPE successStone = NONE_STONE;
boxSetStone(box, point.x, point.y, stone);
InvalidateRect(hWnd, NULL, FALSE);//重绘
PlaySound(MAKEINTRESOURCE(IDW_TADA), hInst, SND_RESOURCE|SND_SYNC);
successStone = boxIsOver(box, point.x, point.y);
if (WHITE_STONE == successStone)
{
MessageBox(hWnd, TEXT("白子获胜!"), TEXT("提示"), MB_OK|MB_ICONINFORMATION);
return TRUE;
}
else if (BLACK_STONE == successStone)
{
MessageBox(hWnd, TEXT("黑子获胜!"), TEXT("提示"), MB_OK|MB_ICONINFORMATION);
return TRUE;
}
return FALSE;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_GOBANG, szWindowClass, MAX_LOADSTRING);
InitApplication(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GOBANG));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: InitApplication()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望
// 此代码与添加到 Windows 95 中的“RegisterClassEx”
// 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
// 这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
ATOM InitApplication(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BIG));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_GOBANG);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
/////////////////////////////////////////////////////////清空棋盘
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
int nWidth, nHeight;
nWidth = WIN_WIDTH/*585*/;
nHeight = WIN_HEIGHT/*630*/;
hWnd = CreateWindow(szWindowClass, szTitle,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
(GetSystemMetrics(SM_CXSCREEN)-nWidth)/2, (GetSystemMetrics(SM_CYSCREEN)-nHeight)/2,
nWidth, nHeight,
NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
///////////////////////////////////////////// “关于”框的消息处理程序。
BOOL CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
///////////////////////////////////////////////////////////////////
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static STONE_TYPE box[BOX_ROW][BOX_COL];
static STONE_TYPE currentStone = BLACK_STONE;
static BOOL startFlag = FALSE;//设置标志符
static int mode = IDM_MODE_MAN2COMPUTER;
HMENU hMenu = NULL;
switch (message)
{
case WM_CREATE : // 创建窗口
hMenu = GetMenu(hWnd);
CheckMenuItem(hMenu, mode, MF_CHECKED);
break;
case WM_COMMAND:
int wmId, wmEvent;
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
hMenu = GetMenu(hWnd);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
PostMessage(hWnd,WM_CLOSE,0,0);
break;
case IDM_START://////////////////////////////////////////////////////////开始下棋
currentStone = NewGame(hWnd, box);//先下的棋子颜色
startFlag = TRUE;
break;
case IDM_MODE_MAN2COMPUTER :
CheckMenuItem(hMenu, mode, MF_UNCHECKED);
mode = IDM_MODE_MAN2COMPUTER;
currentStone = NewGame(hWnd, box);
CheckMenuItem(hMenu, mode, MF_CHECKED);
break;
case IDM_MODE_MAN2MAN :
CheckMenuItem(hMenu, mode, MF_UNCHECKED);
mode = IDM_MODE_MAN2MAN;
currentStone = NewGame(hWnd, box);
CheckMenuItem(hMenu, mode, MF_CHECKED);
break;
}
break;
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);
DrawChessBox(hdc, box);
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONUP: //鼠标提起
if (!startFlag) // 没有点击开始
{
return 0;
}
POINTS point;
point = MAKEPOINTS(lParam);
//判断鼠标是否点击棋盘
if(point.x > 35 && point.x < 542 && point.y > 35 && point.y < 542)
{
//通过鼠标点击位置计算出棋子放置位置
point.x = (point.x - 35) / 34;
point.y = (point.y - 35) / 34;
if(boxGetStone(box, point.x, point.y) == NONE_STONE)
{
if (PlaceStone(hWnd, box, point, currentStone))
{
currentStone = NewGame(hWnd, box);
}
else
{
switch (currentStone)
{
case BLACK_STONE :
currentStone = WHITE_STONE;
break;
case WHITE_STONE :
currentStone = BLACK_STONE;
break;
default :
break;
}
}
// 人机对弈,由计算机搜索最优结果
if (mode == IDM_MODE_MAN2COMPUTER)
{
point = moveGeneration(box);
if (PlaceStone(hWnd, box, point, currentStone))
{
currentStone = NewGame(hWnd, box);
}
else
{
currentStone = BLACK_STONE; /* 人机对弈,人执黑子 */
}
}
}
else
{
MessageBox(hWnd, TEXT("此处已放置了棋子"), TEXT("警告"), MB_OK | MB_ICONWARNING);
}
}
break;
case WM_CLOSE:
if(MessageBox(hWnd, TEXT("确认要退出快乐五子棋游戏吗?"), TEXT("提示"), MB_YESNO | MB_ICONASTERISK)==IDYES)
{
DestroyWindow(hWnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
还是不简单啊,嘿嘿,初学就要这么复杂的代码啊,要是好就给分哈
⑧ 用C语言写一个五子棋游戏
天拉..
我也正在写五子棋,想不出怎么才这么点代码啊???..郁闷.
我也研究下,不知道这五子棋能发挥出什么样的下棋水准?
⑨ 编写一个五子棋的游戏程序,实现人与机对下的功能 求大神发个完整版的能在vc++上运行的C语言版的,谢谢
⑩ 急求论文 c语言开发五子棋的应用
//但是这些代码由于编译器的原因,在TC下仍无法运行。请使用VC。
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>int ChessData[15][15] ={0};
int GuangbiaoData[2]={8,8};
int ChessStepData[255][2]={0};
void gotoxy(int x, int y) //gotoxy在TC中是在一个系统自带的函数,但是在VC中没有这个函数,所以在网上找到了这个函数以实现同样的功能。
//只有这一个函数是网上找的,别的全部我自己写的。
{
COORD c;
c.X=x-1;
c.Y=y-1;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}void GotoChess(int x,int y)
{
x=3*x-2;y=2*y-1;
gotoxy(x,y);
}void Move(int MoveData) //输入参数为用户输入的方向(1表示上,2表示下,3表示左,4表示右)
{
switch (MoveData)
{
case 1:GuangbiaoData[1]-=1;break;
case 2:GuangbiaoData[1]+=1;break;
case 3:GuangbiaoData[0]-=1;break;
case 4:GuangbiaoData[0]+=1;break;
default:printf("Move函数出错");
}
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
}int Get(int *data) //该函数的功能是用户的按键,并转化为01234567(0表示输入错误,1表示上,2表示下,3表示左,4表示右,5表示落子,6表示悔棋,7表示退出。)
{ //并返回输入的用户号码(共同键返回3),错误则返回0
int temp;B: temp=getch();
if (temp==224)
{
temp=getch();
switch (temp)
{
case 72:*data=1;break;
case 80:*data=2;break;
case 75:*data=3;break;
case 77:*data=4;break;
default:goto B;
}
return 2;
}
else
{
switch (temp)
{
case 'w':
case 'W':*data=1;return 1;break;
case 's':
case 'S':*data=2;return 1;break;
case 'a':
case 'A':*data=3;return 1;break;
case 'd':
case 'D':*data=4;return 1;break;
case 13 :*data=5;return 2;break;
case 32 :*data=5;return 1;break;
case 8 :*data=6;break;
case 27 :*data=7;break;
default:*data=0 ;return 0;break;
}
return 3;
}
}
void MoveToEnd()
{
gotoxy(1,30);
}
int LogicBeOut(int a,int b)
{
if (a==-1||a==15||b==-1||b==15) return 1;
else return 0;
}
int win(int v)
{
int i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]+i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]-j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;
return 0;
}void NewShow() //新棋局的开始
{
int i,j;
for (i=0;i<15;i++)
for (j=0;j<15;j++)
ChessData[i][j]=0;
system("cls");
for (i=1;i<=29;i++)
{
for (j=1;j<=43;j++)
if(i%2==1) printf("-");
else if (j%3==1) printf("|");
else printf(" ");
printf("\n");
}
GuangbiaoData[0]=8;GuangbiaoData[1]=8;
MoveToEnd();
printf("现在请用户1下棋 \n");
printf("用户1使用 w,s,a,d移动光标,空格键落子\n");
printf("用户2使用各方向键移动光标,回车键落子\n");
printf("按下Backspace键悔棋,按下esc返回主菜单\n");
GotoChess(8,8);
}int BeOut(int data)
{
int Xiuzheng[2],New[2];
switch (data)
{
case 1:Xiuzheng[0]=0;Xiuzheng[1]=-1 ;break;
case 2:Xiuzheng[0]=0;Xiuzheng[1]=1;break;
case 3:Xiuzheng[0]=-1;Xiuzheng[1]=0;break;
case 4:Xiuzheng[0]=1;Xiuzheng[1]=0 ;break;
case 5:Xiuzheng[0]=0;Xiuzheng[1]=0 ;break;
default:printf("BeOut函数出错");
}
New[0]=GuangbiaoData[0]+Xiuzheng[0];
New[1]=GuangbiaoData[1]+Xiuzheng[1];
if (New[0]>15||New[0]<1||New[1]>15||New[1]<1) return 1;
else return 0;
}void UserChoose(int * choice)
{
system("cls");
printf("________________________________________________________________________________");
printf("________________________________________________________________________________");
printf(" $1.单人游戏 ");
printf(" 2.双人游戏 ");
printf(" 3.退出游戏 ");
printf(" 4.游戏帮助 ");
printf("________________________________________________________________________________");
printf("________________________________________________________________________________");
printf(" ");
printf(" 开心五子棋 ");
printf(" ");
printf(" 出品人:张云聪 ");
printf(" 学号:067108034 ");
printf(" 指导老师:邵艳玲 ");
printf(" ");
printf(" ");
printf(" 温馨提示,游戏时请把窗口最大化,以达到最佳效果 ");
printf("________________________________________________________________________________");
int temp=0,i=0;
do
{
if ((temp=getch())==224)
{
temp=getch();
if (temp==72&&i!=0)
{
gotoxy(34,3+i);
printf(" ");
i--;
gotoxy(34,3+i);
printf("$");
gotoxy(0,0);
}
else if(temp==80&&i!=3)
{
gotoxy(34,3+i);
printf(" ");
i++;
gotoxy(34,3+i);
printf("$");
gotoxy(0,0);
}
} else if (temp==13) {*choice=i+1;return;}
else if (temp==27) {*choice=3;return;}
else if (temp=='1'||temp=='2'||temp=='3'||temp=='4') {*choice=temp-48;return;}}while(1);
}int CannotDo(int v1,int v2,int MoveData,int choice) //第一个输入值为按键的用户号,第二个是本应该按键的用户号,第三个为按下键的对应值,第四个键代表游戏模式。
{
if (v1==3) return 0; //如果用户输入的为共用按键,则CannotDo为假else if (v1==0) return 1;//如果用户输入错误,则CannotDo为真
else if (v1!=v2&&choice==2) return 1; //如果不该此用户输入,而用户进行了输入,则CannotDo为真if (BeOut(MoveData)) return 1; //如果移动出边界则CannotDo为真
return 0;
}int CannotLuozi() //判断是否可以落子。
{
if (ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1])
return 1;
else return 0;
}int luozi(int v) //玩家v落子。
{
ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1]+=v*2;
if (v==1) printf("O");
else if (v==2) printf("X");
else printf("luozi函数出错");
if (win(v)) {MoveToEnd();printf("玩家%d获得了胜利! \n",v);for (int i=1;i<=240;i++) printf(" ");GotoChess(GuangbiaoData[0],GuangbiaoData[1]);getch();return 1;}
MoveToEnd();
printf("现在请用户%d下棋 ",v%2+1);
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
return 0;
}void HuiQi(int step) //输入的是当前的要悔的棋是第几步
{
GuangbiaoData[0]=ChessStepData[step-1][0];
GuangbiaoData[1]=ChessStepData[step-1][1];
ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1]=0;
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
printf("-");
MoveToEnd();
printf("现在请用户%d下棋 ",(step+1)%2+1);
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
}int DataGetAndChoose(int choice)
{
int MoveData=0,i=0,temp; //MoveData 0表示不可移动,1表示上,2表示下,3表示左,4表示右,5表示落子,6表示悔棋,7表示退出。
while(1)
{
loop: while (temp=Get(&MoveData),CannotDo(temp,i%2+1,MoveData,choice));
switch (MoveData)
{
case 1:
case 2:
case 3:
case 4:Move(MoveData);break;
case 7:return 0;
case 6:
if (i==0) {MoveToEnd();printf("现在无法悔棋 ");GotoChess(GuangbiaoData[0],GuangbiaoData[1]);}
else HuiQi(i--);
break;
case 5:
if (CannotLuozi()) goto loop;
if(luozi(i%2+1)) return 0;
ChessStepData[i][0]=GuangbiaoData[0];
ChessStepData[i][1]=GuangbiaoData[1];
i++;
break;
default:printf("DataGetAndChoose函数出错");break;
}
}
return 1;
}void ShowHelp()
{
system("cls");
printf("********************************************************************************");
printf("********************************************************************************");
printf("****** 单人游戏供用户一个人自己与自己下棋研究棋局之用 ******");
printf("****** 双人游戏中,用户1使用wsad控制方向,按空格落子 ******");
printf("****** 用户2按方向键控制方向,回车键落子 ******");
printf("****** 游戏过程中按esc返回主菜单 ******");
printf("****** 游戏过程中退格键悔棋 ******");
printf("****** 双人模式中某人下棋时,另一个用户无法控制光标与落子 ******");
printf("****** ******");
printf("****** 帮助 ******");
printf("****** 按任意键返回 ******");
printf("********************************************************************************");
printf("********************************************************************************");
getch();
}int main()
{
int choice=0;
system("color 2b");
choose: UserChoose(&choice);
if (choice<1||choice>4) goto choose;
if (choice==3) {gotoxy(1,18);<br>printf("\n谢谢您的使用,再见 "); getch();return 0;}
if (choice==4) {ShowHelp(); goto choose;}
NewShow();
DataGetAndChoose(choice);
main();
return 0;
}