用C#写一个五子棋程序设计

时间: 2023-08-02 14:17:46 浏览: 46
好的,下面是一个简单的五子棋程序的 C# 设计。这个程序只有基本功能,可以运行并且可以下棋。 首先,需要一个表示棋盘的二维数组,可以用 int 类型表示,0 表示空位,1 表示黑棋,2 表示白棋。定义如下: ``` int[,] board = new int[15, 15]; ``` 然后需要一个方法来显示棋盘,可以使用 Console.WriteLine() 方法,在控制台显示棋盘。定义如下: ``` void DrawBoard() { Console.WriteLine(" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"); for (int i = 0; i < 15; i++) { Console.Write((i + 1).ToString().PadLeft(2) + " "); for (int j = 0; j < 15; j++) { switch (board[i, j]) { case 0: Console.Write("+ "); break; case 1: Console.Write("● "); break; case 2: Console.Write("○ "); break; } } Console.WriteLine(); } } ``` 然后需要一个方法来判断落子是否合法,即判断该位置是否为空。定义如下: ``` bool IsValidMove(int x, int y) { if (x < 0 || x >= 15 || y < 0 || y >= 15) { return false; } if (board[x, y] != 0) { return false; } return true; } ``` 接下来需要一个方法来判断游戏是否结束,即是否有五子连珠。这个方法比较复杂,需要判断横向、纵向、左上到右下、右上到左下四个方向是否有五子连珠。定义如下: ``` bool IsGameOver(int x, int y) { int color = board[x, y]; // 横向判断 int count = 0; for (int i = x - 4; i <= x + 4; i++) { if (i < 0 || i >= 15) { continue; } if (board[i, y] == color) { count++; } else { count = 0; } if (count == 5) { return true; } } // 纵向判断 count = 0; for (int i = y - 4; i <= y + 4; i++) { if (i < 0 || i >= 15) { continue; } if (board[x, i] == color) { count++; } else { count = 0; } if (count == 5) { return true; } } // 左上到右下判断 count = 0; for (int i = -4; i <= 4; i++) { int tx = x + i; int ty = y + i; if (tx < 0 || tx >= 15 || ty < 0 || ty >= 15) { continue; } if (board[tx, ty] == color) { count++; } else { count = 0; } if (count == 5) { return true; } } // 右上到左下判断 count = 0; for (int i = -4; i <= 4; i++) { int tx = x + i; int ty = y - i; if (tx < 0 || tx >= 15 || ty < 0 || ty >= 15) { continue; } if (board[tx, ty] == color) { count++; } else { count = 0; } if (count == 5) { return true; } } return false; } ``` 最后需要一个方法来进行游戏。该方法使用循环,交替下黑棋和白棋,每次落子后判断游戏是否结束。定义如下: ``` void PlayGame() { int player = 1; while (true) { DrawBoard(); Console.WriteLine("轮到玩家 " + player + " 下棋,请输入坐标(如:3,4):"); string input = Console.ReadLine(); string[] inputs = input.Split(','); int x = int.Parse(inputs[0]) - 1; int y = int.Parse(inputs[1]) - 1; if (!IsValidMove(x, y)) { Console.WriteLine("无效落子,请重新输入!"); continue; } board[x, y] = player; if (IsGameOver(x, y)) { DrawBoard(); Console.WriteLine("玩家 " + player + " 获胜!"); break; } player = player == 1 ? 2 : 1; } } ``` 最后在 Main 方法中调用 PlayGame() 方法即可开始游戏。完整代码如下: ``` using System; class Program { static int[,] board = new int[15, 15]; static void Main(string[] args) { PlayGame(); } static void DrawBoard() { Console.WriteLine(" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"); for (int i = 0; i < 15; i++) { Console.Write((i + 1).ToString().PadLeft(2) + " "); for (int j = 0; j < 15; j++) { switch (board[i, j]) { case 0: Console.Write("+ "); break; case 1: Console.Write("● "); break; case 2: Console.Write("○ "); break; } } Console.WriteLine(); } } static bool IsValidMove(int x, int y) { if (x < 0 || x >= 15 || y < 0 || y >= 15) { return false; } if (board[x, y] != 0) { return false; } return true; } static bool IsGameOver(int x, int y) { int color = board[x, y]; // 横向判断 int count = 0; for (int i = x - 4; i <= x + 4; i++) { if (i < 0 || i >= 15) { continue; } if (board[i, y] == color) { count++; } else { count = 0; } if (count == 5) { return true; } } // 纵向判断 count = 0; for (int i = y - 4; i <= y + 4; i++) { if (i < 0 || i >= 15) { continue; } if (board[x, i] == color) { count++; } else { count = 0; } if (count == 5) { return true; } } // 左上到右下判断 count = 0; for (int i = -4; i <= 4; i++) { int tx = x + i; int ty = y + i; if (tx < 0 || tx >= 15 || ty < 0 || ty >= 15) { continue; } if (board[tx, ty] == color) { count++; } else { count = 0; } if (count == 5) { return true; } } // 右上到左下判断 count = 0; for (int i = -4; i <= 4; i++) { int tx = x + i; int ty = y - i; if (tx < 0 || tx >= 15 || ty < 0 || ty >= 15) { continue; } if (board[tx, ty] == color) { count++; } else { count = 0; } if (count == 5) { return true; } } return false; } static void PlayGame() { int player = 1; while (true) { DrawBoard(); Console.WriteLine("轮到玩家 " + player + " 下棋,请输入坐标(如:3,4):"); string input = Console.ReadLine(); string[] inputs = input.Split(','); int x = int.Parse(inputs[0]) - 1; int y = int.Parse(inputs[1]) - 1; if (!IsValidMove(x, y)) { Console.WriteLine("无效落子,请重新输入!"); continue; } board[x, y] = player; if (IsGameOver(x, y)) { DrawBoard(); Console.WriteLine("玩家 " + player + " 获胜!"); break; } player = player == 1 ? 2 : 1; } } } ```

相关推荐

最新推荐

recommend-type

用C#实现启动另一程序的方法实例

一段实例代码,程序的目的是使用C#实现启动另一程序的方法。技术总监給出了我们这样一个有效的启动程序的有效方法,現在和大家分享下
recommend-type

C#获取上个月第一天和最后一天日期的方法

主要介绍了C#获取上个月第一天和最后一天日期的方法,是关于C#日期函数的简单应用,具有一定的实用价值,需要的朋友可以参考下
recommend-type

用C# Winform写个英汉词典

平日里我们学习,生活中可能都要用到各种词典,许多人可能觉得 词典是个多么神奇的东西. 其实 一本简单的词典软件要写出来很简单, 当然 功能强大 智能的词典软件 就另当别论了. 那么,我们今天就来写一款 英汉词典翻译...
recommend-type

C#实现软件监控外部程序运行状态的方法

主要介绍了C#实现软件监控外部程序运行状态的方法,可实现监控另一个程序的运行状态及触发相应事件的功能,是非常实用的技巧,需要的朋友可以参考下
recommend-type

基于C#实现一个最简单的HTTP服务器实例

主要介绍了基于C#实现一个最简单的HTTP服务器的方法,详细分析了http服务器的实现原理与相关技巧,以及对应的注意事项,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】增量式PID的simulink仿真实现

# 2.1 Simulink仿真环境简介 Simulink是MATLAB中用于建模、仿真和分析动态系统的图形化环境。它提供了一个直观的用户界面,允许用户使用块和连接线来创建系统模型。Simulink模型由以下元素组成: - **子系统:**将复杂系统分解成更小的、可管理的模块。 - **块:**代表系统中的组件,如传感器、执行器和控制器。 - **连接线:**表示信号在块之间的流动。 Simulink仿真环境提供了广泛的块库,涵盖了各种工程学科,包括控制系统、电子和机械工程。它还支持用户自定义块的创建,以满足特定仿真需求。 # 2. Simulink仿真环境的搭建和建模 ### 2.
recommend-type

训练集和测试集的准确率都99%,但是预测效果不好

即使训练集和测试集的准确率都很高,但是在实际的预测中表现不佳,可能会有以下几个原因: 1. 数据质量问题:模型训练和测试的数据可能存在问题,比如数据标签错误、数据集采样不均衡、数据集中存在异常值等问题,这些问题可能会导致模型在实际预测中表现不佳。 2. 特征工程问题:模型的特征工程可能存在问题,导致模型无法很好地捕捉数据的特征,从而影响预测效果。 3. 模型选择问题:模型的选择可能不合适,比如选择的模型太简单,无法很好地拟合数据,或者选择的模型太复杂,导致过拟合等问题。 4. 超参数调整问题:模型的超参数可能没有调整到最佳状态,需要进行调整。 针对以上可能的原因,可以采取相应的措施进
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。