C#开发控制台Nim游戏:如何实现跨方法的数组状态更新?
新手提问原文(中文翻译版)
我最近刚开始学习C#,此前没有任何编程经验,目前正在开发一款名为“Nim”的简易数学游戏。
项目目前还远未完成,但我卡在了一个自己解决不了的问题上:游戏里有两个方法,分别用来控制回合归属、玩家从各堆取走的石子数量。我本来以为负责更新石子堆数组的Game方法是可以正常实现数组更新的,但当代码流程回到PlayerTurn方法时,我不得不把数组重置回初始值——因为我完全不知道怎么把Game方法里更新后的新数组值同步回PlayerTurn方法里的数组。
以下是我目前写的代码(写得比较乱,见谅):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Welcome(); PlayerName(); int[] arr = { 5, 5, 5 }; int turn = 3; do { if (turn == 3) { Console.WriteLine("Player one, make a move "); PlayerTurn(turn); turn++; } else { PlayerTurn(turn); turn++; } } while (arr[0] != 0 && arr[1] != 0 && arr[2] != 0); Console.ReadLine(); } static void Welcome() { Console.WriteLine("welcome to nim!"); Console.WriteLine("RULES: "); Console.WriteLine("The game begins with placing five sticks in three different piles."); Console.WriteLine("Then the players take turns picking sticks from them until they are empty."); Console.WriteLine("The player in turn must pick at least one stick, and they must come from the same pile."); Console.WriteLine("The player who has picked the last stick has won the game."); } static int Game(int[] array) { int[] arr = { 5, 5, 5 }; do { Console.WriteLine("Pick pile A, B or C"); string choice = Console.ReadLine(); if (choice == "A") { Console.WriteLine("how many sticks would you like to remove from A?"); int.TryParse(Console.ReadLine(), out int input); arr[0] = arr[0] - input; Console.WriteLine($"Pile A contains {arr[0]}, Pile B contains {arr[1]} & Pile A contains {arr[2]}"); return arr[0]; } if (choice == "B") { Console.WriteLine($"how many sticks would you like to remove from B?"); int.TryParse(Console.ReadLine(), out int input); arr[1] = arr[1] - input; Console.WriteLine($"Pile A contains {arr[0]}, Pile B contains {arr[1]} & Pile A contains {arr[2]}"); return arr[1]; } if (choice == "C") { Console.WriteLine($"how many sticks would you like to remove from C?"); int.TryParse(Console.ReadLine(), out int input); arr[2] = arr[2] - input; Console.WriteLine($"Pile A contains {arr[0]}, Pile B contains {arr[1]} & Pile A contains {arr[2]}"); return arr[2]; } else { return 0; } } while (arr[0] != 0 && arr[1] != 0 && arr[2] != 0); } static int PlayerTurn(int turn) { int[] arr = { 5, 5, 5 }; Game(arr); if (turn % 2 == 0) { Console.WriteLine("Player one, make a move"); turn++; return turn; } if (turn % 2 == 1) { Console.WriteLine("Player two, make a move"); turn++; return turn; } else { Console.WriteLine("error?"); return 0; } } static void PlayerName() { Console.WriteLine(" "); Console.WriteLine("Type the name of player1: "); string name = Console.ReadLine(); Console.WriteLine("Welcome " + name + "!"); Console.WriteLine("Add another player? Type Yes or No"); string YesOrNo = Console.ReadLine(); if (YesOrNo == ("Yes")) { Console.WriteLine("Type the name of player2: "); string name2 = Console.ReadLine(); Console.WriteLine("Welcome " + name2 + "!"); } else if (YesOrNo == ("No")) { Console.WriteLine("You'll be playing against the computer!"); } else { Console.WriteLine("Your answer must contain either Yes or No"); } } } }
问题根因与修正方案
核心问题
- 三个方法里各写了一遍
int[] arr = {5,5,5}的初始化代码,这些数组完全是互不相关的局部变量,你在哪个方法里改了数组,其他方法根本看不见,所以每次操作完数组都自动回到初始值,这就是卡壳的核心原因 - 写的
Game方法虽然留了int[] array的参数位,但根本没用到传进来的东西,转头就在方法里自己重新建了个{5,5,5}的新数组,传进来的引用直接被丢弃 Game方法里刚算完要减多少石子,就直接return了,外面套的那个do-while循环根本没机会运行- 打印堆状态的时候手滑打错了,两次都标注为Pile A,实际C堆的状态被错标成了A堆
- 因为数组从来没被真实改过,Main里的循环条件永远达不到结束值,游戏要么卡循环要么直接退出
修正思路
- C#里数组是引用类型,只要在Main方法里定义一次初始的石子堆数组,之后不管传哪个方法用,只要不重新赋值数组的引用,方法里改数组的元素,所有用到这个数组的地方都能看到最新值,根本不需要每个方法里都重新初始化一遍
{5,5,5} - 把
Game方法里自己新建数组的那行删掉,直接用传进来的数组参数干活 - 去掉选完堆、算完数就直接return的逻辑,等把堆的数值改完、打印完状态再退出方法就行
- 加个简单的输入判断:玩家取的石子数不能小于1,也不能比当前堆的石子数多,不然会算出负数
- 把
PlayerTurn方法里重复初始化数组、重复弹操作提示的代码删掉,把回合切换、提示、操作堆的逻辑串成一条线,不然玩家会被弹好几次输入提示
核心修正代码片段
// Main方法中定义的全局唯一石子堆实例 int[] piles = { 5, 5, 5 }; int currentTurn = 1; // 1代表玩家1,2代表玩家2 do { Console.WriteLine($"\n玩家{(currentTurn == 1 ? "1" : "2")},请进行操作"); // 传入同一个piles数组,方法内修改会直接同步到原数组 Game(piles); // 所有堆石子总数为0时,当前玩家取走最后一颗,获得胜利 if (piles.Sum() == 0) { Console.WriteLine($"玩家{(currentTurn == 1 ? "1" : "2")}取走了最后一颗石子,获得胜利!"); break; } // 切换回合 currentTurn = currentTurn == 1 ? 2 : 1; } while (true); // 重构后的Game方法:直接操作传入的石子堆数组 static void Game(int[] piles) { // 打印当前所有堆的状态 Console.WriteLine($"当前堆状态:A堆={piles[0]},B堆={piles[1]},C堆={piles[2]}"); // 等待玩家选择堆 Console.Write("请选择要取石子的堆(输入A/B/C):"); string choice = Console.ReadLine().Trim().ToUpper(); int targetIndex = choice switch { "A" => 0, "B" => 1, "C" => 2, _ => -1 }; // 输入无效时重新触发输入 if (targetIndex == -1) { Console.WriteLine("输入无效,请重新选择"); Game(piles); return; } // 等待玩家输入取走的石子数量 Console.Write($"请输入从对应堆取走的石子数量(最多{piles[targetIndex]}个):"); int.TryParse(Console.ReadLine(), out int removeCount); // 取数不合法时重新触发输入 if (removeCount < 1 || removeCount > piles[targetIndex]) { Console.WriteLine("取走的数量不合法,请重新输入"); Game(piles); return; } // 修改堆的数值 piles[targetIndex] -= removeCount; }
关键点:C#中数组属于引用类型,传递给方法时传递的是数组的内存地址,不是数组的副本,所以方法内对数组元素的修改,会直接作用到原数组上,这就是解决之前数组不同步问题的核心逻辑。
内容的提问来源于stack exchange,提问作者user14500030
相关产品推荐
相关产品推荐

