私はC#の新機能ですが、何か挑戦的なプログラムを試してみることにしました。私はOne Row Nimのゲームをプログラムすることに決めました。このゲームでは、コンピュータが常に勝つコンピュータと対戦します。私は自分のコードを完成させ、 "すべてのコードパスが値を返すわけではない"というエラーを受けています。私はこれが何を意味するのか知っていると思うが、どうして私がそれを得ているのか分からない。(C#)メッセージパスが「すべてのコードパスが値を返すのではない」というメッセージが表示されるのはなぜですか?
using System;
namespace OneRowNimThing {
class NimSetup {
static string OneRowNim(int count, int turn, int prevPlayerInput) {
int playerInput, CPUInput, newCount;
if (turn == 0) {
if (count <= 0) {
return "You Lose!";
}
else {
Console.WriteLine("It is now your turn.");
Console.WriteLine("Would you like to take 1, 2, or 3 pieces?");
playerInput = Convert.ToInt32(Console.ReadLine());
if (playerInput >= 4) {
Console.WriteLine("{0} is not a valid input.", playerInput);
OneRowNim(count, turn, prevPlayerInput);
}
else if (playerInput < 1) {
Console.WriteLine("{0} is not a valid input.", playerInput);
OneRowNim(count, turn, prevPlayerInput);
}
else {
newCount = count - playerInput;
Console.WriteLine("You have taken {0} pieces, there are {1} pieces remaining.", playerInput, newCount);
OneRowNim(newCount, 1, playerInput);
}
}
}
if (turn == 1) {
if (count <= 0) {
return "You Win!";
}
else {
Console.WriteLine("It is now the computer's turn.");
CPUInput = 4 - prevPlayerInput;
newCount = count - CPUInput;
Console.WriteLine("The computer took {0} pieces. There are {1} pieces remaining.", CPUInput, newCount);
OneRowNim(newCount, 0, prevPlayerInput);
}
}
}
static void Main(string[] args) {
OneRowNim(12, 0, 0);
}
}
}
あなたはニム作品が、それは基本的にあなたが山の外に駒を取るするゲームだか、そして最後の勝利を取る誰でもわからない場合。
else節の「返品」はどこですか? –
多分、あなたは 'OneRowNim(newCount、1、playerInput)return 'を使う必要があります。' – Hamed
'OneRowNim'の再帰呼び出しの前に' return'を追加するのが簡単ですが、 、 おもう。 –