저는 제 Noughts and Crosses 프로젝트의 마지막 단계에 있습니다. 나는 꽤 꼼꼼히 갇혀 있습니다. 나는 이동 검증 서브 루틴과 서브 루틴을 전적으로 수행했습니다 상자의 빈 공간을 "X"또는 "O"로 변경하는 것에 기인하여 내 코드가 내 코드의 일부가 현재 컨텍스트에 존재하지 않고 완전히 당황한 것으로 알리는 것 같습니다.Naughts and Crosses (Tic Tac Toe), 서브 루틴 쿼리
코드는 다음과 같습니다 내가 갖고있는 것 같다
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[,] grid = new string[3, 3] {{" "," "," "},
{" "," "," "},
{" "," "," "}};
string board = System.IO.File.ReadAllText("E:\\BoardGame.txt");
Console.WriteLine(board);
int player = 0;
var XCoordinate = 0;
var YCoordinate = 0;
int x, y;
GetMoveCoordinates(ref XCoordinate, ref YCoordinate);
if (player == 0)
{
grid[XCoordinate, YCoordinate] = " X ";
player++;
}
else
{
grid[XCoordinate, YCoordinate] = " O ";
player--;
}
UpdateGrid(grid, box);
if (player == 1)
{
}
}
public static void GetMoveCoordinates(ref int XCoordinate, ref int YCoordinate)
{
int CommaLocation;
bool GameHasBeenWon = false;
string CoordinatesInput;
string XChar, YChar;
while (GameHasBeenWon == false)
{
try
{
Console.Write("Enter your coordinates: (x,y) ");
CoordinatesInput = Console.ReadLine();
CommaLocation = CoordinatesInput.IndexOf(",".ToString());
XChar = CoordinatesInput.Substring(CommaLocation - 1, CommaLocation);
YChar = CoordinatesInput.Substring(CommaLocation + 1);
XCoordinate = int.Parse(XChar);
YCoordinate = int.Parse(YChar);
}
catch
{
Console.WriteLine("Invalid Input- Please Try Again");
}
}
}
public static bool CheckValidMove(int XCoordinate, int YCoordinate, string[,] Board)
{
if ((XCoordinate >= 1) || (XCoordinate <= 3))
{
if ((YCoordinate >= 1) || (YCoordinate <= 3))
{
if ((Board[XCoordinate, YCoordinate]) == " ")
{
return true;
}
else return false;
}
else return false;
}
else return false;
}
public static void UpdateGrid(string[,] grid, string box)
{
Console.Clear();
for (int x = 0; x < grid.GetLength(0); x++)
{
for (int y = 0; y < grid.GetLength(1); y++)
{
box = box.Replace((x + 1) + "," + (y + 1), grid[y, x]);
}
}
// In the case not required as clearning the console default the cursor back to 0,0, but left in
// as an example
Console.SetCursorPosition(0, 0);
Console.WriteLine(box);
}
}
}
그러나 문제는 코드가 나에게 UPDAT에서 해당 상자를 알 것 같다 if 문에서, 메인 아래 e (grid, box)는 현재 컨텍스트에 존재하지 않지만 마지막 서브 루틴에서 수행해야합니까? 나는 진술서로 그것을하기로되어 있나, 아니면 뭔가 빠져 있니? 또한 코드를 정리하는 방법에 대한 조언이 있으면 기꺼이 감사하겠습니다 (예 : 매개 변수를 추가하지만 먼저 기호를 그립니다).
이 그리드는이 모습입니다 : 몇 가지 오류가 여기에있다
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+
"코드가 해당 상자에 표시되는 것처럼 보이며 현재 컨텍스트에 존재하지 않는다는 것을 의미합니까?" – EJoshuaS
올바르게 읽으면 (더 명시 적으로 보이는 예외를 호출하는 데 도움이 될 것입니다),'UpdateGrid (grid, box) '에 대한 호출이 실패합니다. 이것은'box'가 존재하지 않기 때문입니다 'Main' 메쏘드에서 (UpdateGrid의 매개 변수 중 하나입니다.) –
예, 저는 @AndrewWhitaker가 여기에 정확하다고 생각합니다. (질문을 올바르게 읽는다면) – EJoshuaS