#include
#include
#include
#include
#include
#define N 10
#define M 10
#define TRUE 1
#define FALSE 0
// 控制柄的宏定义
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'
#define SPACE ' '
struct coordinate // 记录当前位置
{
int x;
int y;
}pos;
void gotoxy(int x, int y); // 光标定位
void hidecursor(); // 隐藏光标
void InitChessboard(); // 初始化棋盘
void Move(int i, int j); // 棋子移动
int main(void)
{
InitChessboard();
hidecursor();
while(1)
{
Move(pos.x, pos.y);
}
system("pause");
return 0;
}
// =======================================光标定位
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
// =======================================隐藏光标
void hidecursor()
{
CONSOLE_CURSOR_INFO cursor;
cursor.dwSize = sizeof(CONSOLE_CURSOR_INFO);
cursor.bVisible = false;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
}
// ==================================== 初始化棋盘
void InitChessboard()
{
int i, j;
// 初始化棋盘
for(i = 0; i < N; i++)
{
for(j = 0; j < M; j++)
{
gotoxy(2 * j, i);
printf("十");
}
printf("\n");
}
// 初始化当前位置
pos.x = M / 2 - 1;
pos.y = N / 2 - 1;
}
// =======================================
void Move(int x, int y)
{
char key;
gotoxy(20, 0);
printf("(%d, %d)", x, y);
key = getch();
switch(key)
{
// 向上移动
case UP:
{
if(y <= 0)
break;
gotoxy(x, y);
printf("十");
gotoxy(x, --y);
printf("□");
break;
}
// 向下移动
case DOWN:
{
if(y >= N-1)
break;
gotoxy(x, y);
printf("十");
gotoxy(x, ++y);
printf("□");
break;
}
// 向左移动
case LEFT:
{
if(x <= 0)
break;
gotoxy(2 * x, y);
printf("十");
gotoxy(2 * (--x) , y);
printf("□");
break;
}
// 向右移动
case RIGHT:
{
if(x >= M-1)
break;
gotoxy(2 * x, y);
printf("十");
gotoxy(2 * (++x), y);
printf("□");
break;
}
case SPACE:
{
printf("○");
break;
}
default:
gotoxy(0, 13);
printf("不是有效按键");
}
pos.x = x;
pos.y = y;
}
为什么上下左右单独移动没问题
混合运动就开始出问题了
#include
#include
#include
#define N 10
#define M 10
#define TRUE 1
#define FALSE 0
// 控制柄的宏定义
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'
#define SPACE ' '
struct coordinate // 记录当前位置
{
int x;
int y;
}pos;
void gotoxy(int x, int y); // 光标定位
void hidecursor(); // 隐藏光标
void InitChessboard(); // 初始化棋盘
void Move(int i, int j); // 棋子移动
int main(void)
{
InitChessboard();
hidecursor();
while(1)
{
Move(pos.x, pos.y);
}
system("pause");
return 0;
}
// =======================================光标定位
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
// =======================================隐藏光标
void hidecursor()
{
CONSOLE_CURSOR_INFO cursor;
cursor.dwSize = sizeof(CONSOLE_CURSOR_INFO);
cursor.bVisible = false;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
}
// ==================================== 初始化棋盘
void InitChessboard()
{
int i, j;
// 初始化棋盘
for(i = 0; i < N; i++)
{
for(j = 0; j < M; j++)
{
gotoxy(2 *i, j);
printf("十");
}
printf("\n");
}
// 初始化当前位置
pos.x = M / 2 - 1;
pos.y = N / 2 - 1;
gotoxy(2 * pos.x, --pos.y);
printf("□");
}
// =======================================
void Move(int x, int y)
{
char key;
gotoxy(20, 0);
printf("(%d, %d)", x, y);
key = getch();
switch(key)
{
// 向上移动
case UP:
{
if(y <= 0)
break;
gotoxy(2 * x, y);
printf("十");
gotoxy(2 * x, --y);
printf("□");
break;
}
// 向下移动
case DOWN:
{
if(y >= N-1)
break;
gotoxy(2 * x, y);
printf("十");
gotoxy(2 * x, ++y);
printf("□");
break;
}
// 向左移动
case LEFT:
{
if(x <= 0)
break;
gotoxy(2 * x, y);
printf("十");
gotoxy(2 * (--x) , y);
printf("□");
break;
}
// 向右移动
case RIGHT:
{
if(x >= M-1)
break;
gotoxy(2 * x, y);
printf("十");
gotoxy(2 * (++x), y);
printf("□");
break;
}
case SPACE:
{
gotoxy(2 * x, y);
printf("○");
break;
}
default:
gotoxy(0, 13);
printf("不是有效按键");
}
pos.x = x;
pos.y = y;
}
帮你把问题解决了,逻辑问题 ,你对照着看一下,你的程序如果想实现真正的走棋功能,还有很长的路要走。