C#井字棋游戏:如何为玩家的X/O标记添加颜色?
解决方案
你的代码里设置Console.BackgroundColor后没有立刻生效,因为Board方法是一次性输出整个棋盘,颜色设置会应用到后续所有输出,而非单独的目标位置。要实现单个位置的颜色标记,需要在输出每个字符时单独设置颜色,输出后立即恢复默认。
以下是修改后的完整代码:
namespace TIC_TAC_TOE { class Program { static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; static ConsoleColor?[] cellColors = new ConsoleColor?[10]; // 存储每个位置的背景色,null代表默认颜色 static int player = 1; // 默认玩家1先手 static int choice; static int flag = 0; static void Main(string[] args) { do { Console.Clear(); Console.WriteLine("玩家1:X(绿色背景) 玩家2:O(蓝色背景)"); Console.WriteLine("\n"); if (player % 2 == 0) { Console.WriteLine("玩家2回合"); } else { Console.WriteLine("玩家1回合"); } Console.WriteLine("\n"); Board(); try { choice = int.Parse(Console.ReadLine()); } catch (System.FormatException) { Console.WriteLine("格式错误!请输入1-9的数字,否则程序将无法继续\n"); } try { if (arr[choice] != 'X' && arr[choice] != 'O') { if (player % 2 == 0) { arr[choice] = 'O'; cellColors[choice] = ConsoleColor.Blue; // 玩家2的O用蓝色背景 player++; } else { arr[choice] = 'X'; cellColors[choice] = ConsoleColor.Green; // 玩家1的X用绿色背景 player++; } } else { Console.WriteLine("抱歉,位置{0}已经被标记为{1}", choice, arr[choice]); Console.WriteLine("\n"); Console.WriteLine("请等待4秒,棋盘将重新加载....."); System.Threading.Thread.Sleep(4000); } flag = CheckWin(); } catch (IndexOutOfRangeException) { Console.WriteLine("输入超出范围!请输入1-9的数字"); } } while (flag != 1 && flag != -1); Console.Clear(); Board(); if (flag == 1) { Console.WriteLine("玩家{0}获胜", (player % 2) + 1); } else { Console.WriteLine("平局"); } Console.ReadLine(); } private static void Board() { // 逐行输出棋盘,逐个处理单元格颜色 Console.WriteLine(" | | "); PrintCell(1); Console.Write(" | "); PrintCell(2); Console.Write(" | "); PrintCell(3); Console.WriteLine(); Console.WriteLine("_____|_____|_____ "); Console.WriteLine(" | | "); PrintCell(4); Console.Write(" | "); PrintCell(5); Console.Write(" | "); PrintCell(6); Console.WriteLine(); Console.WriteLine("_____|_____|_____ "); Console.WriteLine(" | | "); PrintCell(7); Console.Write(" | "); PrintCell(8); Console.Write(" | "); PrintCell(9); Console.WriteLine(); Console.WriteLine(" | | "); } // 单独输出一个单元格,处理颜色逻辑 private static void PrintCell(int index) { if (cellColors[index] != null) { Console.BackgroundColor = cellColors[index].Value; Console.Write($" {arr[index]} "); Console.ResetColor(); // 恢复默认颜色,避免影响后续输出 } else { Console.Write($" {arr[index]} "); } } private static int CheckWin() { #region 横向获胜条件 if (arr[1] == arr[2] && arr[2] == arr[3]) { return 1; } else if (arr[4] == arr[5] && arr[5] == arr[6]) { return 1; } else if (arr[7] == arr[8] && arr[8] == arr[9]) // 修正原代码的错误判断 { return 1; } #endregion #region 纵向获胜条件 else if (arr[1] == arr[4] && arr[4] == arr[7]) { return 1; } else if (arr[2] == arr[5] && arr[5] == arr[8]) { return 1; } else if (arr[3] == arr[6] && arr[6] == arr[9]) { return 1; } #endregion #region 斜向获胜条件 else if (arr[1] == arr[5] && arr[5] == arr[9]) { return 1; } else if (arr[3] == arr[5] && arr[5] == arr[7]) { return 1; } #endregion #region 平局检查 else if (arr[1] != '1' && arr[2] != '2' && arr[3] != '3' && arr[4] != '4' && arr[5] != '5' && arr[6] != '6' && arr[7] != '7' && arr[8] != '8' && arr[9] != '9') { return -1; } #endregion else { return 0; } } } }
核心修改说明:
- 新增颜色存储数组:添加
cellColors数组记录每个棋盘位置的背景色,默认null表示使用控制台默认样式。 - 关联颜色与玩家标记:玩家选择位置时,同步设置对应位置的颜色值。
- 拆分单元格输出:新增
PrintCell方法,单独处理每个单元格的颜色设置与输出,完成后立即恢复默认颜色,避免影响其他内容。 - 修正获胜条件错误:修复原代码中第三行横向获胜的判断逻辑错误。
内容的提问来源于stack exchange,提问作者Dabiun
相关产品推荐
相关产品推荐

