C#五子棋游戏人机对战
时间: 2024-02-05 17:08:50 浏览: 31
以下是一个C#编写的五子棋游戏人机对战的示例代码:
```csharp
using System;
namespace Gomoku
{
class Program
{
static void Main(string[] args)
{
// 初始化棋盘
char[,] board = new char[15, 15];
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
board[i, j] = ' ';
}
}
// 游戏循环
while (true)
{
// 人类玩家下棋
Console.WriteLine("请下棋,输入行号和列号(以空格分隔):");
string[] input = Console.ReadLine().Split(' ');
int row = int.Parse(input[0]);
int col = int.Parse(input[1]);
board[row, col] = 'X';
// 判断是否胜利
if (CheckWin(board, row, col, 'X'))
{
Console.WriteLine("你赢了!");
break;
}
// 电脑下棋
Random random = new Random();
while (true)
{
int randomRow = random.Next(0, 15);
int randomCol = random.Next(0, 15);
if (board[randomRow, randomCol] == ' ')
{
board[randomRow, randomCol] = 'O';
break;
}
}
// 判断是否胜利
if (CheckWin(board, row, col, 'O'))
{
Console.WriteLine("电脑赢了!");
break;
}
// 打印棋盘
PrintBoard(board);
}
}
// 打印棋盘
static void PrintBoard(char[,] board)
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
Console.Write(board[i, j]);
}
Console.WriteLine();
}
}
// 判断是否胜利
static bool CheckWin(char[,] board, int row, int col, char player)
{
// 判断行
int count = 0;
for (int i = Math.Max(0, col - 4); i <= Math.Min(14, col + 4); i++)
{
if (board[row, i] == player)
{
count++;
if (count == 5)
{
return true;
}
}
else
{
count = 0;
}
}
// 判断列
count = 0;
for (int i = Math.Max(0, row - 4); i <= Math.Min(14, row + 4); i++)
{
if (board[i, col] == player)
{
count++;
if (count == 5)
{
return true;
}
}
else
{
count = 0;
}
}
// 判断主对角线
count = 0;
int startRow = row - Math.Min(row, col);
int startCol = col - Math.Min(row, col);
int endRow = row + Math.Min(14 - row, 14 - col);
int endCol = col + Math.Min(14 - row, 14 - col);
for (int i = startRow, j = startCol; i <= endRow && j <= endCol; i++, j++)
{
if (board[i, j] == player)
{
count++;
if (count == 5)
{
return true;
}
}
else
{
count = 0;
}
}
// 判断副对角线
count = 0;
startRow = row - Math.Min(row, 14 - col);
startCol = col + Math.Min(row, 14 - col);
endRow = row + Math.Min(14 - row, col);
endCol = col - Math.Min(14 - row, col);
for (int i = startRow, j = startCol; i <= endRow && j >= endCol; i++, j--)
{
if (board[i, j] == player)
{
count++;
if (count == 5)
{
return true;
}
}
else
{
count = 0;
}
}
return false;
}
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)