using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 五子棋
{
class Map
{
public void printMap(int[,] map)
{
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
switch (map[i, j])
{
case 0:
Console.Write("十");
break;
case 1:
Console.Write("●");
break;
case 2:
Console.Write("○");
break;
case 3:
Console.Write("■");
break;
}
}
Console.WriteLine();
}
}
public bool IsWin(int[,] map, int x, int y, int who)
{
int x_temp = x;
int y_temp = y;
bool _is = false;
int num = -1; //横向判断
while (map[x_temp, y_temp] == who)
{
num++;
x_temp++;
}
x_temp = x;
while (map[x_temp, y_temp] == who)
{
num++;
x_temp--;
}
if (num >= 5)
{
_is = true;
}
num = -1; //纵向判断
x_temp = x;
y_temp = y;
while (map[x_temp, y_temp] == who)
{
num++;
y_temp--;
}
y_temp = y;
while (map[x_temp, y_temp] == who)
{
num++;
y_temp++;
}
if (num >= 5)
{
_is = true;
}
num = -1; //主对角线判断
x_temp = x;
y_temp = y;
while (map[x_temp, y_temp] == who)
{
num++;
x_temp++;
y_temp++;
}
x_temp = x;
y_temp = y;
while (map[x_temp, y_temp] == who)
{
num++;
x_temp--;
y_temp--;
}
if (num >= 5)
{
_is = true;
}
num = -1; //副对角线判断
x_temp = x;
y_temp = y;
while (map[x_temp, y_temp] == who)
{
num++;
x_temp++;
y_temp--;
}
x_temp = x;
y_temp = y;
while (map[x_temp, y_temp] == who)
{
num++;
x_temp--;
y_temp++;
}
if (num >= 5)
{
_is = true;
}
return _is;
}
}
class Program
{
static void Main(string[] args)
{
int hang = 10;
int lie = 10;
int temp = 0;
int turn = 1;
string Input;
Map m = new Map();
int[,] map = new int[20, 20];
map[10, 10] = 3;
m.printMap(map);
while (true)
{
Console.Clear();
m.printMap(map);
Console.WriteLine("按W,A,S,D移动光标,按N键下棋,按P键退出:");
if (turn == 1)
{
Console.WriteLine("轮到白方下棋:");
}
else
{
Console.WriteLine("轮到黑方下棋:");
}
Input = Console.ReadKey().Key.ToString();
switch (Input)
{
case "W":
if (hang > 0)
{
map[hang, lie] = temp;
hang--;
temp = map[hang, lie];
map[hang, lie] = 3;
}
break;
case "A":
if (lie > 0)
{
map[hang, lie] = temp;
lie--;
temp = map[hang, lie];
map[hang, lie] = 3;
}
break;
case "S":
if (hang < 19)
{
map[hang, lie] = temp;
hang++;
temp = map[hang, lie];
map[hang, lie] = 3;
}
break;
case "D":
if (lie < 19)
{
map[hang, lie] = temp;
lie++;
temp = map[hang, lie];
map[hang, lie] = 3;
}
break;
case "N":
if (temp == 0)
{
map[hang, lie] = turn;
if (turn == 1)
{
if (m.IsWin(map, hang, lie, turn))
{
Console.WriteLine();
Console.Clear();
m.printMap(map);
Console.WriteLine("白方胜利");
return;
}
temp = 1;
turn = 2;
}
else
{
if (m.IsWin(map, hang, lie, turn))
{
Console.WriteLine();
Console.Clear();
m.printMap(map);
Console.WriteLine("黑方胜利");
return;
}
temp = 2;
turn = 1;
}
}
break;
case "P":
return;
break;
default:
break;
}
}
}
}
}