class Program
{
static void Main(string[] args)
{
//五子棋 棋盘
int[,] arr = new int[,] {
{0,0,0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0,0 }
};
// 1.显示棋盘 2.落子 3.检查胜利 4.切换玩家输入 5.循环落子
int _i = 0;//用来接收玩家的输入行
int _j = 0;//用来接收玩家的输入列
bool isPlay1 = true;//true 1号玩家 false 2号玩家
bool isWin = false;//输赢状态
while (true)//死循环重复接收控制台输入
{
ShowCheckerBoard(arr);//显示棋盘到控制台
if (isPlay1)//判断玩家
{
Console.WriteLine("玩家1输入目标行(0-8):");
_i = int.Parse(Console.ReadLine());
Console.WriteLine("玩家1输入目标列(0-8):");
_j = int.Parse(Console.ReadLine());
if (CheckInsert(arr, _i, _j, isPlay1))//成功下子
{ //检查输赢
if (Check_Is_Win(arr, _i, _j, isPlay1)) {
ShowCheckerBoard(arr);
Console.WriteLine("玩家1胜利");
break;
}
isPlay1 = false;
}
}
else {
Console.WriteLine("玩家2输入目标行(0-8):");
_i = int.Parse(Console.ReadLine());
Console.WriteLine("玩家2输入目标列(0-8):");
_j = int.Parse(Console.ReadLine());
if (CheckInsert(arr, _i, _j, isPlay1))//成功下子
{//检查输赢
if (Check_Is_Win(arr, _i, _j, isPlay1))
{
ShowCheckerBoard(arr);
Console.WriteLine("玩家2胜利");
break;
}
isPlay1 = true;
}
}
}
}//end Main
/// <summary>
/// 判定是否连成5个
/// </summary>
/// <param name="i">行</param>
/// <param name="j">列</param>
/// <returns></returns>
static bool Check_Is_Win(int[,] arr,int i,int j,bool isPlayer1)
{
int target = isPlayer1 ? 1 : 2;
//检查行
bool is_i_win= Chenk_I(arr,i,j,target);
//检查列
bool is_j_win = Chenk_J(arr,i,j,target);
//检查对角线
bool is_k_win = Chenk_Catercorner(arr, i, j, target);
if (is_i_win||is_j_win||is_k_win)
{
return true;
}
return false;
}
//检查行
static bool Chenk_I(int[,] arr,int i,int j, int target,int sum=1)
{//判断行上有没有5个 是遍历j列的值
int temp = j;
while (true)
{
j++;
if (j >= arr.GetLength(1)) break;//防止出界
if (arr[i, j] != target) break;
sum++;
}
j = temp;
while (true)
{
j--;
if (j < 0) break;
if (arr[i, j] != target) break;
sum++;
}
if (sum >= 5) return true;
return false;
}
//检查列
static bool Chenk_J(int[,] arr, int i, int j, int target,int sum= 1)
{
int temp = i;
while (true)
{
i++;
if (i >= arr.GetLength(0)) break;//防止出界
if (arr[i, j] != target) break;
sum++;
}
i = temp;
while (true)
{
i--;
if (i < 0) break;
if (arr[i, j] != target) break;
sum++;
}
if (sum >= 5) return true;
return false;
}
//检查对角线
static bool Chenk_Catercorner(int[,] arr, int i, int j,int target, int sum = 1)
{
int tempI = i;
int tempJ = j;
while (true)
{
i++;
j++;
if (i >= arr.GetLength(0) || j >= arr.GetLength(0)) break;
if (arr[i, j] != target) break;
if (Math.Abs(tempI - i) == Math.Abs(tempJ - j))
{
sum++;
}
}
i = tempI;
j = tempJ;
while (true)
{
i--;
j--;
if (i < 0 || j <0) break;
if (arr[i, j] != target) break;
if (Math.Abs(tempI - i) == Math.Abs(tempJ - j))
{
sum++;
}
}
if (sum >= 5) return true;
return false;
}
/// <summary>
/// 检查玩家输入位置可以落子? 可以改变值来标记不同玩家
/// </summary>
static bool CheckInsert(int[,] arr,int i,int j,bool isPlayer1)
{
int temp = arr.GetLength(0);
if (i>= temp || j>= temp)
{
Console.WriteLine("输入错误越界了");
return false;
}
if (arr[i,j]!=0)
{
Console.WriteLine("目标已经落子了");
return false;
}
else//目标值==0可以放子
{
if (isPlayer1)//是玩家1
{
arr[i, j] = 1;
return true;
}
else//是玩家2
{
arr[i, j] = 2;
return true;
}
}
}
/// <summary>
/// 显示棋盘
/// </summary>
static void ShowCheckerBoard(int[,] arr) {
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i,j]+",");
}
Console.WriteLine();
}
}
}