五子棋小程序--c语言

#include //Ingredients: ● ○ — ━ │ ┃ ┛ ┓ ┏ ┗ ┼ ┷ ┯ ┨ ┠
#include
#define MAX 15
int Piece[MAX][MAX] = {0};
int X_Cursor = 6, Y_Cursor = 6;
int Current_Player = 1;
char *Draw_Piece(int i, int j)
{
if (Piece[i][j] == 1)
return "●";
else if (Piece[i][j] == 2)
return "○";
else if (i == 0 && j == 0)
return "┏";
else if (i == 0 && j == MAX - 1)
return "┓";
else if (i == MAX - 1 && j == 0)
return "┗";
else if (i == MAX - 1 && j == MAX - 1)
return "┛";
else if (i == 0)
return "┯";
else if (i == MAX - 1)
return "┷";
else if (j == 0)
return "┠";
else if (j == MAX - 1)
return "┨";
else
return "┼";
}
char *Piece_HGap(int i, int j)
{
if (j == MAX - 1)
return "\n";
if (i == 0 || i == MAX - 1)
return "━";
else
return "—";
}
char *Piece_VGap(int i, int k)
{
if (k== 0 || k== MAX - 1)
return "┃";
else
return "│";
}
char *Draw_Cursor(int i, int k)
{
if (k == MAX - 1)
return "\n";
if (i == X_Cursor&&k == Y_Cursor)
return "┏";
else if (i == X_Cursor&&k == Y_Cursor + 1)
return"┓";
else if (i == X_Cursor + 1 && k == Y_Cursor)
return "┗";
else if (i == X_Cursor + 1 && k == Y_Cursor + 1)
return "┛";
else
return " ";
}
void Draw_Board()
{
int i, j, k;
system("cls");
puts("Instructions: w,s,a,d respectively represents up,down,left,right!");
for (i = 0; i < MAX; i++)
if (i == MAX - 1)
{
printf(" ");
for (j = 0; j < MAX; j++)
{
printf(Draw_Piece(i, j));
printf(Piece_HGap(i, j));
}
}
else
{
printf(" ");
for (j = 0; j < MAX; j++)
{
printf(Draw_Piece(i, j));
printf(Piece_HGap(i, j));
}
printf(" ");
for (k = 0; k < MAX; k++)
{
printf(Piece_VGap(i, k));
printf(Draw_Cursor(i, k));
}
}
}
void Move_Cursor()
{
while (1)
{
char Move_Step = getchar();
if (Move_Step == 'w' || Move_Step == 'W')
if (X_Cursor == -1)
break;
else
X_Cursor--;
else if (Move_Step == 's' || Move_Step == 'S')
if (X_Cursor == MAX - 2)
break;
else
X_Cursor++;
else if (Move_Step == 'a' || Move_Step == 'A')
if (Y_Cursor == -1)
break;
else
Y_Cursor--;
else if (Move_Step == 'd' || Move_Step == 'D')
if (Y_Cursor == MAX - 2)
break;
else
Y_Cursor++;
else
break;
}
}
int Move_Piece()
{
if (Piece[X_Cursor + 1][Y_Cursor + 1] == 0)
{
Piece[X_Cursor + 1][Y_Cursor + 1] = Current_Player;
return 1;
}
return 0;
}
void Exchange_Player()
{
Current_Player = 3 - Current_Player;
}
int H_Check()
{
int Count_Left = 0, Count_Right = 0;
int i = X_Cursor, j = Y_Cursor;
while (Piece[i + 1][j] == Current_Player)
{
Count_Left++;
j--;
}
i = X_Cursor, j = Y_Cursor;
while (Piece[i + 1][j + 2] == Current_Player)
{
Count_Right++;
j++;
}
return Count_Left + Count_Rig

ht + 1;
}
int V_Check()
{
int Count_Up = 0, Count_Down = 0;
int i = X_Cursor, j = Y_Cursor;
while (Piece[i][j + 1] == Current_Player)
{
Count_Up++;
i--;
}
i = X_Cursor, j = Y_Cursor;
while (Piece[i + 2][j + 1] == Current_Player)
{
Count_Down++;
i++;
}
return Count_Up + Count_Down + 1;
}
int DU_Check()
{
int Count_DU = 0, Count_DD = 0;
int i = X_Cursor, j = Y_Cursor;
while (Piece[i][j + 2] == Current_Player)
{
Count_DU++;
i--;
j++;
}
i = X_Cursor, j = Y_Cursor;
while (Piece[i + 2][j ] == Current_Player)
{
Count_DD++;
i++;
j--;
}
return Count_DU + Count_DD + 1;
}
int DD_Check()
{
int Count_DU = 0, Count_DD = 0;
int i = X_Cursor, j = Y_Cursor;
while (Piece[i][j] == Current_Player)
{
Count_DU++;
i--;
j--;
}
i = X_Cursor, j = Y_Cursor;
while (Piece[i + 2][j + 2 ] == Current_Player)
{
Count_DD++;
i++;
j++;
}
return Count_DU + Count_DD + 1;
}
int Check_Result()
{
if (H_Check() == 5 || V_Check() == 5 || DU_Check() == 5 || DD_Check() == 5)
return 1;
return 0;
}
void Print_Result()
{
switch (Current_Player)
{
case 1:
printf("● wins\n");
break;
case 2:
printf("○ wins\n");
break;
default:
break;
}
}
int main()
{
system("title SIMPLE GOMOKU");
system("mode con cols=65 lines=35");
system("color 30");
Draw_Board();
while (1)
{
Move_Cursor();
if (Move_Piece())
{
Draw_Board();
if (Check_Result())
{
Print_Result();
system("pause");
break;
}
Exchange_Player();
}
}
return 0;
}

相关文档
最新文档