#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)// Determine if the game is over
{
// 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. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
// Vertical direction
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. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
/ / transverse direction
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. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
// go up and down
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. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
// go up
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(a)// Print the game rules
{
printf(" --------------------------------------------------------\n");
printf(" | _________________________________ |\n");
printf(" | | The Game will begin!!! | |\n");
printf(" | |_______________________________| |\n");
printf("The rules of the game * | | * \ n");
printf("| please both sides players take turns to put pieces of input coordinates (separated by Spaces) | \ n");
printf("| black (@) is: @ | \ n");
printf("| | white (O) is: O \ n");
printf("Put | | black first \ n");
printf(" ---------------------------------------------------------\n");
}
int main(a)
{
f(a);for (int i = 10; i >= 1; i--)Sleep(1000), printf("%2d seconds to start game \n", i);
system("cls");
for (int i = 0; i <= 15; i++)
for (int j = 0; j <= 15; j++)
mp[i][j] = ' ';
// Print the result (debug)
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("Please enter black (@) coordinates :\n");
mp[15] [15] = ' ';
while (scanf("%d%d", &zong, &heng))
{
/ / input
// 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("Please enter black (@) coordinates :\n");
else
printf("Please enter white (O) coordinates :\n");
scanf("%d%d", &zong, &heng);
}
cout << "Input success!!! \n";
system("cls");
if (flag % 2= =0)
mp[zong - 1][heng - 1] = The '@';
else
mp[zong - 1][heng - 1] = 'O';
// Print a new map
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");
// Decide which side wins
if (flag % 2= =0)
{
if (Judge(mp, The '@'))
{
printf("Black wins!! \n");
break; }}else
{
if (Judge(mp, 'O'))
{
printf("White wins!! \n");
break;
}
}
flag++;
if (flag % 2= =0)
printf("Please enter black (@) coordinates :\n");
else
printf("Please enter white (O) coordinates :\n");
}
printf("Game over!! \n");
char p[100];
gets(p);
return 0;
}
Copy the code