c语言五子棋

时间:2022-09-27 19:37:26

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>

//界宽
#define WIDTH 60
//界高
#define HIGHT 28

//界面落子坐标
struct Position
{
int X;
int Y;
}POS = { 34, 11 };

//界面坐标
int data[60][28] = { 0 };

//1为黑 2为白
int Q = 1;

void gotoxy(int x, int y);

//输出界面
void PaintInterface();

void HideCursor();

//判断胜利
void WIN();

//作用于main
void PlayGame();

int main()
{
PlayGame();
return 0;
}
void gotoxy(int x1, int y1)
{
COORD pos = { x1, y1 };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int bbb = 0;
void PaintInterface()
{
int i, j;

for (i = 2; i <= WIDTH; i += 2)
{
for (j = 1; j <= HIGHT; j++)
{
//界面核心算法
i == 2 && j == 1 ? gotoxy(i, j), printf("┏"), gotoxy(i + 2, j), printf("━"), gotoxy(i, j + 1), printf("┃") :
i == 2 && j == HIGHT ? gotoxy(i, 27), printf("┗") :
i == WIDTH && j == 1 ? gotoxy(i - 2, 1), printf("┓"), gotoxy(i - 2, 1), printf("━"), gotoxy(i - 2, j + 1), printf("┃") :
i == WIDTH && j == HIGHT ? gotoxy(i - 2, 27), printf("┛"), gotoxy(i - 1, j), printf("━") :
i == 2 && j != 1 && j != HIGHT ? gotoxy(2, j + 1), printf("┠"), gotoxy(i, j + 2), printf("┃"), j++ :
i == WIDTH && j != 1 && j != HIGHT ? gotoxy(i - 2, j + 1), printf("┨"), j < 27 ? gotoxy(i - 2, j + 2), printf("┃"), j++ : j++ :
j == 1 && i != 2 && i != WIDTH ? gotoxy(i + 2, j), printf("┯"), gotoxy(i, j - 2), printf("━"), i++ :
j == HIGHT && i != 2 && i != WIDTH ? gotoxy(i + 1, j - 1), printf("┷"), gotoxy(i - 1, j - 1), printf("━"), i++ : bbb++;
}
}

for (i = 6; i <= 56; i += 4)
{
for (j = 3; j <= 26; j += 2)
{
gotoxy(i - 2, j);
printf("─");
gotoxy(i + 2, j);
printf("─");
gotoxy(i, j - 1);
printf("│");
gotoxy(i, j + 1);
printf("│");
gotoxy(i, j);
printf("┼");
}
}
for (i = 60; i <= 76; i += 2)
{
gotoxy(i, 1);
printf("━");
gotoxy(i, 9);
printf("━");
gotoxy(i, 15);
printf("━");
gotoxy(i, 27);
printf("━");
}
for (j = 1; j <= 27; j++)
{
gotoxy(78, j);
printf("┃");

}
gotoxy(66, 3); printf("作者:");
gotoxy(63, 5); printf("仙人immortal");
gotoxy(60, 11); printf("提示:");
gotoxy(66, 12), printf("黑棋走");
gotoxy(64, 17), printf("五子棋游戏");
gotoxy(65, 19), printf("游戏玩法");
gotoxy(62, 22); printf("移动:↑↓←→");
gotoxy(64, 25); printf("落子: 空格");
gotoxy(58, 1); printf("┓");
gotoxy(2, 28); printf(" ");
gotoxy(60, 28); printf(" ");
}

void WIN()
{

/*横*/if (data[POS.X - 16][POS.Y] == 1 && data[POS.X - 12][POS.Y] == 1 && data[POS.X - 8][POS.Y] == 1 && data[POS.X - 4][POS.Y] == 1 && data[POS.X][POS.Y] == 1 ||
data[POS.X - 12][POS.Y] == 1 && data[POS.X - 8][POS.Y] == 1 && data[POS.X - 4][POS.Y] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X + 4][POS.Y] == 1 ||
data[POS.X - 8][POS.Y] == 1 && data[POS.X - 4][POS.Y] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X + 4][POS.Y] == 1 && data[POS.X + 8][POS.Y] == 1 ||
data[POS.X - 4][POS.Y] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X + 4][POS.Y] == 1 && data[POS.X + 8][POS.Y] == 1 && data[POS.X + 12][POS.Y] == 1 ||
data[POS.X][POS.Y] == 1 && data[POS.X + 4][POS.Y] == 1 && data[POS.X + 8][POS.Y] == 1 && data[POS.X + 12][POS.Y] == 1 && data[POS.X + 16][POS.Y] == 1 ||

/*竖*/ data[POS.X][POS.Y - 8] == 1 && data[POS.X][POS.Y - 6] == 1 && data[POS.X][POS.Y - 4] == 1 && data[POS.X][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 ||
data[POS.X][POS.Y - 6] == 1 && data[POS.X][POS.Y - 4] == 1 && data[POS.X][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X][POS.Y + 2] == 1 ||
data[POS.X][POS.Y - 4] == 1 && data[POS.X][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X][POS.Y + 2] == 1 && data[POS.X][POS.Y + 4] == 1 ||
data[POS.X][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X][POS.Y + 2] == 1 && data[POS.X][POS.Y + 4] == 1 && data[POS.X][POS.Y + 6] == 1 ||
data[POS.X][POS.Y] == 1 && data[POS.X][POS.Y + 2] == 1 && data[POS.X][POS.Y + 4] == 1 && data[POS.X][POS.Y + 6] == 1 && data[POS.X][POS.Y + 8] == 1 ||

/*捺*/ data[POS.X + 16][POS.Y - 8] == 1 && data[POS.X + 12][POS.Y - 6] == 1 && data[POS.X + 8][POS.Y - 4] == 1 && data[POS.X + 4][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 ||
data[POS.X + 12][POS.Y - 6] == 1 && data[POS.X + 8][POS.Y - 4] == 1 && data[POS.X + 4][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X - 4][POS.Y + 2] == 1 ||
data[POS.X + 8][POS.Y - 4] == 1 && data[POS.X + 4][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X - 4][POS.Y + 2] == 1 && data[POS.X - 8][POS.Y + 4] == 1 ||
data[POS.X + 4][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X - 4][POS.Y + 2] == 1 && data[POS.X - 8][POS.Y + 4] == 1 && data[POS.X - 12][POS.Y + 6] == 1 ||
data[POS.X][POS.Y] == 1 && data[POS.X - 4][POS.Y + 2] == 1 && data[POS.X - 8][POS.Y + 4] == 1 && data[POS.X - 12][POS.Y + 6] == 1 && data[POS.X - 16][POS.Y + 8] == 1 ||

/*撇*/ data[POS.X - 16][POS.Y - 8] == 1 && data[POS.X - 12][POS.Y - 6] == 1 && data[POS.X - 8][POS.Y - 4] == 1 && data[POS.X - 4][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 ||
data[POS.X - 12][POS.Y - 6] == 1 && data[POS.X - 8][POS.Y - 4] == 1 && data[POS.X - 4][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X + 4][POS.Y + 2] == 1 ||
data[POS.X - 8][POS.Y - 4] == 1 && data[POS.X - 4][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X + 4][POS.Y + 2] == 1 && data[POS.X + 8][POS.Y + 4] == 1 ||
data[POS.X - 4][POS.Y - 2] == 1 && data[POS.X][POS.Y] == 1 && data[POS.X + 4][POS.Y + 2] == 1 && data[POS.X + 8][POS.Y + 4] == 1 && data[POS.X + 12][POS.Y + 6] == 1 ||
data[POS.X][POS.Y] == 1 && data[POS.X + 4][POS.Y + 2] == 1 && data[POS.X + 8][POS.Y + 4] == 1 && data[POS.X + 12][POS.Y + 6] == 1 && data[POS.X + 16][POS.Y + 8] == 1)
{
gotoxy(30, 14); printf("黑棋胜!");
gotoxy(30, 16); printf("10秒后退出游戏!");
Sleep(10000);
exit(-1);
}
/*横*/if (data[POS.X - 16][POS.Y] == 2 && data[POS.X - 12][POS.Y] == 2 && data[POS.X - 8][POS.Y] == 2 && data[POS.X - 4][POS.Y] == 2 && data[POS.X][POS.Y] == 2 ||
data[POS.X - 12][POS.Y] == 2 && data[POS.X - 8][POS.Y] == 2 && data[POS.X - 4][POS.Y] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X + 4][POS.Y] == 2 ||
data[POS.X - 8][POS.Y] == 2 && data[POS.X - 4][POS.Y] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X + 4][POS.Y] == 2 && data[POS.X + 8][POS.Y] == 2 ||
data[POS.X - 4][POS.Y] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X + 4][POS.Y] == 2 && data[POS.X + 8][POS.Y] == 2 && data[POS.X + 12][POS.Y] == 2 ||
data[POS.X][POS.Y] == 2 && data[POS.X + 4][POS.Y] == 2 && data[POS.X + 8][POS.Y] == 2 && data[POS.X + 12][POS.Y] == 2 && data[POS.X + 16][POS.Y] == 2 ||

/*竖*/ data[POS.X][POS.Y - 8] == 2 && data[POS.X][POS.Y - 6] == 2 && data[POS.X][POS.Y - 4] == 2 && data[POS.X][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 ||
data[POS.X][POS.Y - 6] == 2 && data[POS.X][POS.Y - 4] == 2 && data[POS.X][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X][POS.Y + 2] == 2 ||
data[POS.X][POS.Y - 4] == 2 && data[POS.X][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X][POS.Y + 2] == 2 && data[POS.X][POS.Y + 4] == 2 ||
data[POS.X][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X][POS.Y + 2] == 2 && data[POS.X][POS.Y + 4] == 2 && data[POS.X][POS.Y + 6] == 2 ||
data[POS.X][POS.Y] == 2 && data[POS.X][POS.Y + 2] == 2 && data[POS.X][POS.Y + 4] == 2 && data[POS.X][POS.Y + 6] == 2 && data[POS.X][POS.Y + 8] == 2 ||

/*捺*/ data[POS.X + 16][POS.Y - 8] == 2 && data[POS.X + 12][POS.Y - 6] == 2 && data[POS.X + 8][POS.Y - 4] == 2 && data[POS.X + 4][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 ||
data[POS.X + 12][POS.Y - 6] == 2 && data[POS.X + 8][POS.Y - 4] == 2 && data[POS.X + 4][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X - 4][POS.Y + 2] == 2 ||
data[POS.X + 8][POS.Y - 4] == 2 && data[POS.X + 4][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X - 4][POS.Y + 2] == 2 && data[POS.X - 8][POS.Y + 4] == 2 ||
data[POS.X + 4][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X - 4][POS.Y + 2] == 2 && data[POS.X - 8][POS.Y + 4] == 2 && data[POS.X - 12][POS.Y + 6] == 2 ||
data[POS.X][POS.Y] == 1 && data[POS.X - 4][POS.Y + 2] == 2 && data[POS.X - 8][POS.Y + 4] == 2 && data[POS.X - 12][POS.Y + 6] == 2 && data[POS.X - 16][POS.Y + 8] == 2 ||

/*撇*/ data[POS.X - 16][POS.Y - 8] == 2 && data[POS.X - 12][POS.Y - 6] == 2 && data[POS.X - 8][POS.Y - 4] == 2 && data[POS.X - 4][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 ||
data[POS.X - 12][POS.Y - 6] == 2 && data[POS.X - 8][POS.Y - 4] == 2 && data[POS.X - 4][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X + 4][POS.Y + 2] == 2 ||
data[POS.X - 8][POS.Y - 4] == 2 && data[POS.X - 4][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X + 4][POS.Y + 2] == 2 && data[POS.X + 8][POS.Y + 4] == 2 ||
data[POS.X - 4][POS.Y - 2] == 2 && data[POS.X][POS.Y] == 2 && data[POS.X + 4][POS.Y + 2] == 2 && data[POS.X + 8][POS.Y + 4] == 2 && data[POS.X + 12][POS.Y + 6] == 2 ||
data[POS.X][POS.Y] == 2 && data[POS.X + 4][POS.Y + 2] == 2 && data[POS.X + 8][POS.Y + 4] == 2 && data[POS.X + 12][POS.Y + 6] == 2 && data[POS.X + 16][POS.Y + 8] == 2)
{
gotoxy(30, 14); printf("白棋胜!");
gotoxy(30, 16); printf("10秒后退出游戏!");
Sleep(10000);
exit(-1);
}

}

void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void PlayGame()
{
system("title 五子棋");
system("mode con cols=80 lines=29");
system("color a2");
HideCursor();
PaintInterface();
gotoxy(POS.X - 2, POS.Y - 1); printf("*");
gotoxy(POS.X - 2, POS.Y + 1); printf("*");
gotoxy(POS.X + 2, POS.Y + 1); printf("*");
gotoxy(POS.X + 2, POS.Y - 1); printf("*");
char ch;
while (1)
{
ch = _getch();
switch (ch)
{
case 0x48:
gotoxy(POS.X + 2, POS.Y + 1); printf(" ");
gotoxy(POS.X - 2, POS.Y + 1); printf(" ");
POS.Y -= 2;
break;
case 0x50:
gotoxy(POS.X + 2, POS.Y - 1); printf(" ");
gotoxy(POS.X - 2, POS.Y - 1); printf(" ");
POS.Y += 2;
break;
case 0x4B:
gotoxy(POS.X + 2, POS.Y - 1); printf(" ");
gotoxy(POS.X + 2, POS.Y + 1); printf(" ");
POS.X -= 4;
break;
case 0x4D:
gotoxy(POS.X - 2, POS.Y - 1); printf(" ");
gotoxy(POS.X - 2, POS.Y + 1); printf(" ");
POS.X += 4;
break;
case 0x20:
if (Q == 1 && data[POS.X][POS.Y] == 1 || data[POS.X][POS.Y] == 2)
break;
else if (Q == 2 && data[POS.X][POS.Y] == 2 || data[POS.X][POS.Y] == 1)
break;
gotoxy(POS.X, POS.Y);
Q == 1 ? data[POS.X][POS.Y] = 1, printf("●"), Q = 2 : Q == 2 ? data[POS.X][POS.Y] = 2, printf("○"), Q = 1 : bbb++;
if (Q == 1)
{
gotoxy(66, 12);
printf("黑棋走");
}
else
{
gotoxy(66, 12);
printf("白棋走");
}
break;
}
if (POS.X > 58)
{
gotoxy(60, POS.Y - 1); printf(" ");
gotoxy(60, POS.Y + 1); printf(" ");
POS.X = 2;
}
if (POS.X < 0)
{
gotoxy(0, POS.Y - 1); printf(" ");
gotoxy(0, POS.Y + 1); printf(" ");
POS.X = 58;
}
if (POS.Y < 0)
{
gotoxy(POS.X - 2, 0); printf(" ");
gotoxy(POS.X + 2, 0); printf(" ");
POS.Y = 27;
}
if (POS.Y > 27)
{
gotoxy(POS.X - 2, 28); printf(" ");
gotoxy(POS.X + 2, 28); printf(" ");
POS.Y = 1;
}
gotoxy(POS.X - 2, POS.Y - 1); printf("*");
gotoxy(POS.X - 2, POS.Y + 1); printf("*");
gotoxy(POS.X + 2, POS.Y + 1); printf("*");
gotoxy(POS.X + 2, POS.Y - 1); printf("*");
WIN();
}
}