我是C#语言的新手,很抱歉,如果这是一个容易解决的问题,我已经检查了网站,目前遇到的问题是变量已更改,但方法未引起任何变化,这听起来很模糊,所以下面是一些代码。
public string PlayerToIcon(int LocA, int LocB)
{
PlayGame play = new PlayGame();
int[,] Board = play.Board;
int LocData = (Board[LocA, LocB]);
if (LocData == 0) { return " "; }
else if (LocData == 1) { return "X"; }
else { return "O"; }
}
木板如下所示,基本上是一个简单的阵列。 -这在另一个文件中
public class PlayGame
{
public int[,] Board = new int[3, 3]
{ //A B C
{0,0,0 }, // 1
{0,0,0 }, // 2
{0,0,0 } // 3
};
}
我制作了一个函数来更改数组中的数字,并且在使用直接命令时可以显示该数字,但是在PlayerToIcon中看不到数字发生了变化。 例如,如果它是0,而我将其更改为1,PlayerToIcon仍将其读取为0
谢谢 抱歉,这是一个简单的问题
这就是更改数组中变量的原因,“返回状态”确保无论是1还是2都不会更改
public class PlayGame
{
public bool SelectSquare(string Location, int Player)
{
List<int> ConvertedLocation = ConvertLoc(Location);
Console.WriteLine(ReturnState(Location));
Console.WriteLine(ConvertedLocation[0].ToString() + ConvertedLocation[1].ToString());
if (ReturnState(Location) == 0) { Board[ConvertedLocation[0], ConvertedLocation[1]] = Player; return true;}
else { return false; }
}
}
在显示器上无法识别代码
public class Interface
{
PlayGame play = new PlayGame();
public void GenMap()
{
string NewLine;
Console.Clear();
Console.WriteLine(" | A | B | C |");
Console.WriteLine("-----------------");
new PlayGame();
NewLine = System.String.Format("| 1 | {0} | {1} | {2} |", (play.PlayerToIcon(play,0, 0)), (play.PlayerToIcon(play, 0, 1)), (play.PlayerToIcon(play, 0, 2)));
Console.WriteLine((string)NewLine);
Console.WriteLine("----------------");
NewLine = System.String.Format("| 2 | {0} | {1} | {2} |", (play.PlayerToIcon(play, 1, 0)), (play.PlayerToIcon(play, 1, 1)), (play.PlayerToIcon(play, 1, 2)));
Console.WriteLine((string)NewLine);
Console.WriteLine("-----------------");
NewLine = System.String.Format("| 3 | {0} | {1} | {2} |", (play.PlayerToIcon(play, 2, 0)), (play.PlayerToIcon(play, 2, 1)), (play.PlayerToIcon(play, 2, 2)));
Console.WriteLine((string)NewLine);
Console.WriteLine("-----------------");
}
public string PlayerToIcon(int LocA, int LocB)
{
int[,] Board = play.Board;
int LocData = (Board[LocA, LocB]);
if (LocData == 0) { return " "; }
else if (LocData == 1) { return "X"; }
else { return "O"; }
}
}
答案 0 :(得分:0)
public string PlayerToIcon(int LocA, int LocB)
{
PlayGame play = new PlayGame(); // creates a new PlayGame
int[,] Board = play.Board;
int LocData = (Board[LocA, LocB]);
if (LocData == 0) { return " "; }
else if (LocData == 1) { return "X"; }
else { return "O"; }
}
应更改为以下内容:
// added as param ---------vvvvvvvvvvvvv
public string PlayerToIcon(PlayGame play, int LocA, int LocB)
{
int[,] Board = play.Board;
int LocData = (Board[LocA, LocB]);
if (LocData == 0) { return " "; }
else if (LocData == 1) { return "X"; }
else { return "O"; }
}
然后这样称呼:
var icon = PlayerToIcon(myPlayer, 1, 1);
您甚至可以将其作为函数添加到您的PlayGame
类中:
public class PlayGame
{
public int[,] Board = new int[3, 3]
{ //A B C
{0,0,0 }, // 1
{0,0,0 }, // 2
{0,0,0 } // 3
};
public string PlayerToIcon(int LocA, int LocB)
{
//PlayGame play = new PlayGame();
//int[,] Board = play.Board;
int LocData = (Board[LocA, LocB]);
if (LocData == 0) { return " "; }
else if (LocData == 1) { return "X"; }
else { return "O"; }
}
}
然后以这种方式调用它:
var icon = myPlayer.PlayerToIcon(1, 1);
public class Interface
{
PlayGame play = new PlayGame();
public void GenMap()
{
string NewLine;
Console.Clear();
Console.WriteLine(" | A | B | C |");
Console.WriteLine("-----------------");
//new PlayGame(); (WHY??)
for(int i=0;i<3;i++)
{
NewLine = System.String.Format("| {0} | {1} | {2} | {3} |",
i+1,
/*play.*/PlayerToIcon(/*play, */i, 0),
/*play.*/PlayerToIcon(/*play, */i, 1),
/*play.*/PlayerToIcon(/*play, */i, 2));
Console.WriteLine(NewLine);
Console.WriteLine("-----------------");
}
}
public string PlayerToIcon(int LocA, int LocB)
{
int[,] Board = play.Board;
int LocData = (Board[LocA, LocB]);
if (LocData == 0) { return " "; }
else if (LocData == 1) { return "X"; }
else { return "O"; }
}
}
这是我开始时得到的输出:
| A | B | C |
-----------------
| 1 | | | |
-----------------
| 2 | | | |
-----------------
| 3 | | | |
-----------------
答案 1 :(得分:0)
在调用PlayerToIcon之前,将您的PlayGame变量设置为变量,然后将其传递给他,如下所示:
public string PlayerToIcon(PlayGame myGame, int LocA, int LocB)
{
int[,] Board = myGame.Board;
int LocData = (Board[LocA, LocB]);
if (LocData == 0) { return " "; }
else if (LocData == 1) { return "X"; }
else { return "O"; }
}
您必须这样做:
new PlayGame();
仅当您要将板重置为“启动”时,才需要花费很多时间。
澄清这样的事情:
PlayGame myGame = new PlayGame();
PlayerToIcon(myGame, 1, 1);
PlayerToIcon(myGame, 1, 2);
PlayerToIcon(myGame, 0, 1);
[...]
不创建游戏时间:)
答案 2 :(得分:-1)
class Program
{
static void Main()
{
string playerToIcon = new PlayGame().PlayerToIcon(new PlayGame(), 0, 1);
}
}
public class PlayGame
{
public int[,] Board = new int[3, 3]
{ //A B C
{0,0,0 }, // 1
{0,0,0 }, // 2
{0,0,0 } // 3
};
public string PlayerToIcon(PlayGame play, int LocA, int LocB)
{
int[,] Board = play.Board;
int LocData = (Board[LocA, LocB]);
if (LocData == 0)
{
return "";
}
else if (LocData == 1)
{
return "X";
}
else
{
return "0";
}
}
}