/********************************************************************
C-4.29-1:
实现五子棋游戏
操作说明:用方向键或者"w","s","a","d"控制棋子放置位置,
使用空格键放置棋子,使用“ESC”键退出游戏
测试环境:Redhat5.5
********************************************************************/
#include
#include
#include
#define ROW 30
#define COL 36
#define MOVECOLOR 36
#define MOVECHAR ‘*‘
//初始化五子棋表格
void initbox(char p[][COL])
{
int i = 0, j = 0;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
p[i][j] = ‘+‘;
}
}
}
//打印显示五子棋表格
void show(char p[][COL])
{
int i = 0, j = 0;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
printf("%c ", p[i][j]);
}
putchar(10);
}
}
//将当前光标定位于指定的(x, y)处
void gotoxy(int x, int y)
{
printf("\033[%d;%dH", x, y);
}
//显示光标所在位置的坐标值(x, y)
void showxy(int x, int y)
{
int i = 0;
printf("\033[s");
gotoxy(ROW+1, COL-6);
for (i = 0; i < 14; i++)
{
printf(" ");
}
//printf("\033[K\033[10;10m");
printf("\033[14D");
printf("x = %d y = %d\n", x, y);
printf("\033[u");
}
//移动光标到要下棋点
void move(char rect[][COL], char *buf, int *x, int *y, int player)
{
char ch = 0;
if (0 == player % 2)
{
ch = ‘@‘;
}
else
{
ch = ‘#‘;
}
//方向键控制区按键识别,并处理
if (buf[0] == 27 && buf[1] == 91)
{
switch (buf[2])
{
case 65:
if (*x > 1 && *x <= ROW)//上移处理
{
if (rect[*x-1][*y-1] == ‘+‘)//下棋点位置在移动时要判断该位置是否为空,为空则将空的颜色恢复黑色
printf("\033[10;10m+\033[D\033[0m");
printf("\033[A");//上移
if (rect[*x-2][*y-1] == ‘+‘)
printf("\033[%d;10m%c\033[D\033[0m", MOVECOLOR, MOVECHAR);//将下棋点位置标明为特殊字符,以便于识别
*x -= 1;
}
break;
case 66:
if (*x >= 1 && *x < ROW)//下移处理
{
if (rect[*x-1][*y-1] == ‘+‘)
printf("\033[10;10m+\033[D\033[0m");
printf("\033[B");
if (rect[*x][*y-1] == ‘+‘)
printf("\033[%d;10m%c\033[D\033[0m", MOVECOLOR, MOVECHAR);
*x += 1;
}
break;
case 67:
if (*y >= 1 && *y < COL)//右移处理
{
if (rect[*x-1][*y-1] == ‘+‘)
printf("\033[10;10m+\033[D\033[0m");
printf("\033[2C");
if (rect[*x-1][*y] == ‘+‘)
printf("\033[%d;10m%c\033[D\033[0m", MOVECOLOR, MOVECHAR);
*y += 1;
}
break;
case 68:
if (*y > 1 && *y <= COL)//左移处理
{
if (rect[*x-1][*y-1] == ‘+‘)
printf("\033[10;10m+\033[D\033[0m");
printf("\033[2D");
if (rect[*x-1][*y-2] == ‘+‘)
printf("\033[%d;10m%c\033[D\033[0m", MOVECOLOR, MOVECHAR);
*y -= 1;
}
break;
default:
break;
}
}
//左侧控制键"w","s","a","d"识别,并处理
if (0 == buf[1])
{
switch (buf[0])
{
case 119:
if (*x > 1 && *x <= ROW)//上移处理
{
if (rect[*x-1][*y-1] == ‘+‘)//下棋点位置在移动时要判断该位置是否为空,为空则将空的颜色恢复
printf("\033[10;10m+\033[D\033[0m");
printf("\033[A");//上移
if (rect[*x-2][*y-1] == ‘+‘)
printf("\033[%d;10m%c\033[D\033[0m", MOVECOLOR, MOVECHAR);//将下棋点位置标明为特殊字符,以便于识别
*x -= 1;
}
break;
case 115:
if (*x >= 1 && *x < ROW)//下移处理
{
if (rect[*x-1][*y-1] == ‘+‘)
printf("\033[10;10m+\033[D\033[0m");
printf("\033[B");
if (rect[*x][*y-1] == ‘+‘)
printf("\033[%d;10m%c\033[D\033[0m", MOVECOLOR, MOVECHAR);
*x += 1;
}
break;
case 100:
if (*y >= 1 && *y < COL)//右移处理
{
if (rect[*x-1][*y-1] == ‘+‘)
printf("\033[10;10m+\033[D\033[0m");
printf("\033[2C");
if (rect[*x-1][*y] == ‘+‘)
printf("\033[%d;10m%c\033[D\033[0m", MOVECOLOR, MOVECHAR);
*y += 1;
}
break;
case 97:
if (*y > 1 && *y <= COL)//左移处理
{
if (rect[*x-1][*y-1] == ‘+‘)
printf("\033[10;10m+\033[D\033[0m");
printf("\033[2D");
if (rect[*x-1][*y-2] == ‘+‘)
printf("\033[%d;10m%c\033[D\033[0m", MOVECOLOR, MOVECHAR);
*y -= 1;
}
break;
default:
break;
}
}
showxy(*x, *y);
}
void playerchk(int *player)
{
if (0 == *player % 2)
{
printf("\033[s");
gotoxy(ROW+1, 0);
printf("\033[K");
gotoxy(ROW+1, 2);
printf("\033[10;10mPlayerA\033[K\033[0m");
printf("\033[u");
}
else if (1 == *player % 2)
{
printf("\033[s");
gotoxy(ROW+1, 0);
printf("\033[K");
gotoxy(ROW+1, 32);
printf("\033[10;10mPlayerB\033[K\033[0m");
printf("\033[u");
}
}
//检查是否有五个连续并且一样的字符,有则游戏结束
int resultchk(char rect[][COL])
{
int i = 0, j = 0, k = 0;
int count = 0;
char cha = ‘@‘;
char chb = ‘#‘;
char ch = 0;
//检查各行是否有五个连续字符
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL-4; j++)
{
if ((rect[i][j] != ‘+‘) && (rect[i][j] == rect[i][j+1]) && (rect[i][j] == rect[i][j+2]) && (rect[i][j] == rect[i][j+3]) && (rect[i][j] == rect[i][j+4]))
{
if (cha == rect[i][j])
{
ch = cha;
gotoxy (ROW+2, COL-11);
printf("Game over! PlayerA win!\n");
}
else if (chb == rect[i][j])
{
ch = chb;
gotoxy (ROW+2, COL-11);
printf("Game over! PlayerB win!\n");
}
for (k = 0; k < 5; k++)
{
gotoxy(i+1, (j+k+1) * 2-1);
printf("\033[10;43m%c\033[0m", ch);
}
return 1;
}
}
}
//检查各列是否有五个连续字符
for (j = 0; j < ROW; j++)
{
for (i = 0; i < COL-4; i++)
{
if ((rect[i][j] != ‘+‘) && (rect[i][j] == rect[i+1][j]) && (rect[i][j] == rect[i+2][j]) && (rect[i][j] == rect[i+3][j]) && (rect[i][j] == rect[i+4][j]))
{
if (cha == rect[i][j])
{
ch = cha;
gotoxy (ROW+2, COL-11);
printf("Game over! PlayerA win!\n");
}
else if (chb == rect[i][j])
{
ch = chb;
gotoxy (ROW+2, COL-11);
printf("Game over! PlayerB win!\n");
}
for (k = 0; k < 5; k++)
{
gotoxy(i+k+1, (j+1) * 2-1);
printf("\033[10;43m%c\033[0m", ch);
}
return 1;
}
}
}
//检查左上-右下斜线上是否有五个连续字符相同
for (i = 0; i < ROW-4; i++)
{
for (j = 0; j < COL-4; j++)
{
if ((rect[i][j] != ‘+‘) && (rect[i][j] == rect[i+1][j+1]) && (rect[i][j] == rect[i+2][j+2]) && (rect[i][j] == rect[i+3][j+3]) && (rect[i][j] == rect[i+4][j+4]))
{
if (cha == rect[i][j])
{
ch = cha;
gotoxy (ROW+2, COL-11);
printf("Game over! PlayerA win!\n");
}
else if (‘#‘ == rect[i][j])
{
ch = chb;
gotoxy (ROW+2, COL-11);
printf("Game over! PlayerB win!\n");
}
for (k = 0; k < 5; k++)
{
gotoxy(i+k+1, (j+k+1) * 2-1);
printf("\033[10;43m%c\033[0m", ch);
}
return 1;
}
}
}
//检查左下-右上斜线上是否有五个连续字符相同
for (i = 0; i < ROW-4; i++)
{
for (j = 4; j < COL; j++)
{
if ((rect[i][j] != ‘+‘) && (rect[i][j] == rect[i+1][j-1]) && (rect[i][j] == rect[i+2][j-2]) && (rect[i][j] == rect[i+3][j-3]) && (rect[i][j] == rect[i+4][j-4]))
{
if (cha == rect[i][j])
{
ch = cha;
gotoxy (ROW+2, COL-11);
printf("Game over! PlayerA win!\n");
}
else if (ch == rect[i][j])
{
ch = chb;
gotoxy (ROW+2, COL-11);
printf("Game over! PlayerB win!\n");
}
for (k = 0; k < 5; k++)
{
gotoxy(i+k+1, (j-k+1) * 2-1);
printf("\033[10;43m%c\033[0m", ch);
}
return 1;
}
}
}
}
int main(void)
{
int i = 0, j = 0;
char rect[ROW][COL] = { 0 };
int ret = 0;
int x = ROW / 2, y = COL / 2;
int player = 0;
system("clear");
system("stty -echo");
system("stty -icanon");
initbox(rect);
show(rect);
printf("\033[s");
gotoxy(ROW+1, 2);
printf("\033[10;10mPlayerA(@)\033[K\033[0m");
printf("\033[u");
gotoxy(x, y * 2 - 1);
showxy(x, y);
printf("\033[?25l");
if (rect[x-1][y-1] == ‘+‘)
{
printf("\033[%d;10m%c\033[D\033[0m", MOVECOLOR, MOVECHAR);
}
fflush(NULL);
while (1)
{
int i = 0;
char buf[10] = { 0 };
ret = read(0, buf, 10);
move(rect, buf, &x, &y, player);
//if (buf[0] == 119)
//for (i = 0; i < ret; i++)
//{
//printf("buf[%d] = %d\n", i, buf[i]);
//}
if (buf[0] == 32 && buf[1] == 0)//按下空格键,在当前位置放下棋子
{
if ((0 == player % 2) && (rect[x-1][y-1] == ‘+‘))//玩家一回合内,按下空格,放置‘@‘
{
rect[x-1][y-1] = ‘@‘;
printf("\033[31;[email protected]\033[D\033[0m");
printf("\033[s");
gotoxy(ROW+1, 2);
printf("\033[K");
printf("\033[u");
showxy(x, y);
printf("\033[s");
gotoxy(ROW+1, COL * 2 - 10);
printf("\033[K");
printf("\033[10;10mPlayerB(#)\033[K\033[0m");
printf("\033[u");
player++;
}
else if ((1 == player % 2) && (rect[x-1][y-1] == ‘+‘))//玩家二回合内,按下空格,放置‘#‘
{
rect[x-1][y-1] = ‘#‘;
printf("\033[32;10m#\033[D\033[0m");
printf("\033[s");
gotoxy(ROW+1, 2);
printf("\033[K");
printf("\033[10;10mPlayerA(@)\033[K\033[0m");
printf("\033[u");
showxy(x, y);
player++;
}
}
if (buf[0] == 27 && buf[1] == 0)//按下ESC键退出游戏
{
if (‘+‘ == rect[x-1][y-1])
printf("\033[10;10m+");
gotoxy(ROW+2, 0);
//show(rect);
break;
}
if (resultchk(rect) == 1)
{
break;
}
fflush(NULL);
}
gotoxy(ROW+3, 0);
printf("\033[?25h");
system("stty echo");
system("stty icanon");
return 0;
}