#include <algorithm>
#include <iostream>
#include <math.h>
#include <windows.h>
#include <stdio.h>
using namespace std;
char mp[15][15];
const int Len = 15;
const int win_count = 5;
bool Judge(const char map[15][15], char a)//判断游戏是否结束
{
// printf("## %c\n", a);
for (int i = 0; i < Len; i++)
{
for (int j = 0; j < Len; j++)
{
if (map[i][j] == a)
{
int count_a = 1;
//1. ----------------------------------------------------------------
//竖直方向
for (int x = i - 1, times = 0; x >= 0; x--, times++)
{
if (map[x][j] == a)
{
count_a++;
}
else
break;
if (times > win_count - 1)
break;
}
for (int y = i + 1, times = 0; y < Len; y++, times++)
{
if (map[y][j] == a)
{
count_a++;
}
else
break;
if (times > win_count - 1)
break;
}
if (count_a >= win_count)
{
return true;
}
//2. --------------------------------------------------------------------
//横方向
count_a = 1;
for (int x = j - 1, times = 0; x >= 0; x--, times++)
{
if (map[i][x] == a)
{
count_a++;
}
else
break;
if (times > win_count - 1)
break;
}
for (int y = j + 1, times = 0; y < Len; y++, times++)
{
if (map[i][y] == a)
{
count_a++;
}
else
break;
if (times > win_count - 1)
break;
}
if (count_a >= win_count)
{
return true;
}
//3. -------------------------------------------------------------------
//上斜下方向
count_a = 1;
for (int x = i - 1, y = j - 1, times = 0; i >= 0 && j >= 0; x--, y--, times++)
{
if (map[x][y] == a)
{
count_a++;
}
else
break;
if (times > win_count - 1)
break;
}
for (int x = i + 1, y = j + 1, times = 0; i < Len && j < Len; x++, y++, times++)
{
if (map[x][y] == a)
{
count_a++;
}
else
break;
if (times > win_count - 1)
break;
}
if (count_a >= win_count)
{
return true;
}
//4. ------------------------------------------------------------------
//下斜上方向
count_a = 1;
for (int x = i + 1, y = j - 1, times = 0; x < Len && y >= 0; x++, y--, times++)
{
if (map[x][y] == a)
{
count_a++;
}
else
break;
if (times > win_count - 1)
break;
}
for (int x = i - 1, y = j + 1, times = 0; x >= 0 && y < Len; x--, y++, times++)
{
if (map[x][y] == a)
{
count_a++;
}
else
break;
if (times > win_count - 1)
break;
}
if (count_a >= win_count)
{
return true;
}
//-------------------------------------------------------------------
}
}
}
return false;
}
void f()//打印游戏规则
{
printf(" --------------------------------------------------------\n");
printf(" | _________________________________ |\n");
printf(" | | The Game will begin!!! | |\n");
printf(" | |_______________________________| |\n");
printf(" | *游戏规则* |\n");
printf(" | 请两边选手轮流输入放棋子的坐标(以空格隔开) |\n");
printf(" | 黑(@)棋:@ |\n");
printf(" | 白(O)棋:O |\n");
printf(" | 黑棋先放 |\n");
printf(" ---------------------------------------------------------\n");
}
int main()
{
f();
for (int i = 10; i >= 1;i--)
Sleep(1000), printf(" 还有%2d秒开始游戏\n", i);
system("cls");
for (int i = 0; i <= 15; i++)
for (int j = 0; j <= 15; j++)
mp[i][j] = ' ';
//打印结果(调试)
printf(" ");
for (int i = 1; i <= 15; i++)
printf("%-4d", i);
printf("\n");
printf(" ____________________________________________________________\n");
for (int i = 0; i < 15; i++)
{
if (i >= 0)
printf("%-3d", i + 1);
else
printf(" ");
printf("|");
for (int j = 0; j < 15; j++)
printf(" %-2c|", mp[i][j]);
printf("\n ____________________________________________________________\n");
}
printf("\n\n");
int heng, zong, flag = 0;
printf(" 请输入黑(@)棋坐标:\n");
mp[15][15] = ' ';
while (scanf("%d%d", &zong, &heng))
{
//输入
// printf("%"
// system("cls");)
while (heng < 1 || zong < 1 || heng > Len || zong > Len || mp[zong-1][heng-1] != ' ')
{
// printf("h=%d z=%d l=%d %c\n", heng, zong, Len, mp[zong][heng]);
printf("Input error,input again!!!\n");
if (flag % 2 == 0)
printf("请输入黑(@)棋坐标:\n");
else
printf("请输入白(O)棋坐标:\n");
scanf("%d%d", &zong, &heng);
}
cout << "Input success!!!\n";
system("cls");
if (flag % 2 == 0)
mp[zong - 1][heng - 1] = '@';
else
mp[zong - 1][heng - 1] = 'O';
//打印新的地图
printf(" ");
for (int i = 1; i <= 15; i++)
printf("%-4d", i);
printf("\n");
printf(" ____________________________________________________________\n");
for (int i = 0; i < 15; i++)
{
if (i >= 0)
printf("%-3d", i + 1);
else
printf(" ");
printf("|");
for (int j = 0; j < 15; j++)
printf(" %-2c|", mp[i][j]);
printf("\n ____________________________________________________________\n");
}
printf("\n\n");
//进行判断看 那一方获胜
if (flag % 2 == 0)
{
if (Judge(mp, '@'))
{
printf(" 黑棋获胜 !!! \n");
break;
}
}
else
{
if (Judge(mp, 'O'))
{
printf(" 白棋获胜 !!! \n");
break;
}
}
flag++;
if (flag % 2 == 0)
printf("请输入黑(@)棋坐标:\n");
else
printf("请输入白(O)棋坐标:\n");
}
printf(" 游戏结束!!!\n");
char p[100];
gets(p);
return 0;
}
C语言课程设计作业源代码(自存)
最新推荐文章于 2024-07-27 15:32:33 发布