前言:
眼前你们对“五子棋 js”都比较注意,兄弟们都需要分析一些“五子棋 js”的相关资讯。那么小编同时在网上网罗了一些关于“五子棋 js””的相关知识,希望我们能喜欢,看官们一起来了解一下吧!下面是一个简单的示例,演示如何使用C#实现一个基本的五子棋游戏:
csharpCopy Codeusing System;namespace FiveInARow{ class Program { static char[,] board; static bool isPlayer1Turn; static void Main(string[] args) { InitializeBoard(); isPlayer1Turn = true; while (true) { DrawBoard(); Console.WriteLine("请输入您要下棋的位置(例如:A1):"); string input = Console.ReadLine().ToUpper(); if (input == "QUIT") break; if (input.Length != 2 || !Char.IsLetter(input[0]) || !Char.IsDigit(input[1])) { Console.WriteLine("无效的输入,请重新输入!"); continue; } int row = input[1] - '1'; int col = input[0] - 'A'; if (row < 0 || row >= 15 || col < 0 || col >= 15 || board[row, col] != '-') { Console.WriteLine("无效的位置,请重新输入!"); continue; } board[row, col] = isPlayer1Turn ? 'X' : 'O'; if (CheckWin(row, col)) { DrawBoard(); Console.WriteLine($"玩家{(isPlayer1Turn ? "1" : "2")}获胜!"); break; } if (IsBoardFull()) { DrawBoard(); Console.WriteLine("平局!"); break; } isPlayer1Turn = !isPlayer1Turn; } } static void InitializeBoard() { board = new char[15, 15]; for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { board[i, j] = '-'; } } } static void DrawBoard() { Console.Clear(); Console.WriteLine(" A B C D E F G H I J K L M N O"); for (int i = 0; i < 15; i++) { Console.Write($"{i + 1} "); for (int j = 0; j < 15; j++) { Console.Write(board[i, j] + " "); } Console.WriteLine(); } } static bool CheckWin(int row, int col) { char symbol = board[row, col]; // 检查水平方向 int count = 1; int left = col - 1; while (left >= 0 && board[row, left] == symbol) { count++; left--; } int right = col + 1; while (right < 15 && board[row, right] == symbol) { count++; right++; } if (count >= 5) return true; // 检查垂直方向 count = 1; int up = row - 1; while (up >= 0 && board[up, col] == symbol) { count++; up--; } int down = row + 1; while (down < 15 && board[down, col] == symbol) { count++; down++; } if (count >= 5) return true; // 检查正斜方向 count = 1; int topLeftRow = row - 1; int topLeftCol = col - 1; while (topLeftRow >= 0 && topLeftCol >= 0 && board[topLeftRow, topLeftCol] == symbol) { count++; topLeftRow--; topLeftCol--; } int bottomRightRow = row + 1; int bottomRightCol = col + 1; while (bottomRightRow < 15 && bottomRightCol < 15 && board[bottomRightRow, bottomRightCol] == symbol) { count++; bottomRightRow++; bottomRightCol++; } if (count >= 5) return true; // 检查反斜方向 count = 1; int topRightRow = row - 1; int topRightCol = col + 1; while (topRightRow >= 0 && topRightCol < 15 && board[topRightRow, topRightCol] == symbol) { count++; topRightRow--; topRightCol++; } int bottomLeftRow = row + 1; int bottomLeftCol = col - 1; while (bottomLeftRow < 15 && bottomLeftCol >= 0 && board[bottomLeftRow, bottomLeftCol] == symbol) { count++; bottomLeftRow++; bottomLeftCol--; } if (count >= 5) return true; return false; } static bool IsBoardFull() { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (board[i, j] == '-') return false; } } return true; } }}
这个示例通过控制台界面实现了一个简单的五子棋游戏。玩家可以输入行列坐标进行下棋,输入"QUIT"结束游戏。程序会在每次下棋后检查是否有玩家获胜或者平局,并相应地输出结果。
请注意,这只是一个基础的实现示例,可能还需要添加更多的功能和优化来完善游戏体验。
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #五子棋 js