好吧我暂时已经避免了这个问题,但是在我可以继续之前我需要理清我的代码范围,我对C#编程很新,所以任何有关不良约定的帮助我一直在使用会很好
这是我当前的Player类(我知道你可以使用“Name {get; set}”例如,但是我有一些问题定义,下面是我的2个类,我的顶级王牌级别(10个顶级王牌是后来创建的)和一个玩家类。
//Top trumps class, layout of top trump card defined in here
public class TopTrumps
{
public int height;
public int length;
public int speed;
public int CardID;
public TopTrumps(int a, int b, int c, int d)
{
this.height = a;
this.length = b;
this.speed = c;
}
}
// The player class, Containing player name, Score etc.
public class Player
{
public string Name;
public int Score;
public bool Turn;
// public List<TopTrumps> PlayerDeck = new List<TopTrumps>();
public Player(string a, int b, bool c)
{
this.Name = a;
this.Score = b;
this.Turn = c;
// this.PlayerDeck = d;
}
}
我的玩家类的目标是为玩家存储名称,分数,转弯以及一副5张牌,然后是计算机,以列表的形式存储。我没有问题在课外做这个
/////////////////////////////
// Create computer card deck
/////////////////////////////
Player Computer = new Player("Computer", 0, true); //Create new player
List<TopTrumps> ComputerDeck = new List<TopTrumps>();
ComputerDeck = Trumps.GetRange(5, 5);
for (int i = 0; i <ComputerDeck.Count; i++)
{
listBox3.Items.Add("COMP Card [" + i + "] : " + ComputerDeck[i].height);
}
listBox3.Items.Add(Computer.Name);
然而,当我尝试访问与该功能之外的播放器有关的任何内容时 “public void DealCards_Click(object sender,EventArgs e)” 以上代码所在的是什么,然后我将始终收到无法访问的错误。例如,我在下面发布的按钮上给出错误“错误1当前上下文中不存在名称'计算机'”
private void button1_Click(object sender, EventArgs e)
{
Player1.Name = PlayerName.Text;
}
任何关于如何正确组织这些代码的帮助都会有很大的帮助,直到我能弄清楚如何分解我的程序,并获得一个在类中工作的列表,我才发布了一个完整的源代码因为如果你需要更好地理解我的程序,非常感谢
- 汤姆
public partial class Game : Form
{
//Top trumps class, layout of top trump card defined in here
public class TopTrumps
{
public int height;
public int length;
public int speed;
public int CardID;
public TopTrumps(int a, int b, int c, int d) //Pass height length and speed as arguements to class, Example 'TopTrumps(10,10,20);'
{
this.height = a; // Set objects height to the parsed value of a
this.length = b;
this.speed = c;
this.CardID = d;
}
}
// The player class, Containing player name, Score etc.
public class Player
{
public string Name;
public int Score;
public bool Turn;
// public List<TopTrumps> PlayerDeck = new List<TopTrumps>();
public Player(string a, int b, bool c)
{
this.Name = a;
this.Score = b;
this.Turn = c;
// this.PlayerDeck = d;
}
}
public Player player1 = new Player("New Player", 0, true); //Create new player
public Game()
{
InitializeComponent();
}
private void Hide_Click(object sender, EventArgs e)
{
Form1 MainScreen = new Form1();
this.Hide();
MainScreen.ShowDialog();
}
public void DealCards_Click(object sender, EventArgs e)
{
List<TopTrumps> Trumps = new List<TopTrumps>(); //Create a list of deck of top trumps
// We can now easily access each top trump card variables, For example 'Trumps[1].height = 5', will modify the 2nd cards height
Trumps.Add(new TopTrumps(10, 20, 50,1)); //Add each top trump card (10 of them) to the newly created list
Trumps.Add(new TopTrumps(15, 50, 40,2)); //Format = (height, Length, speed, CardID)
Trumps.Add(new TopTrumps(6, 4, 20,3));
Trumps.Add(new TopTrumps(11, 20, 30,4));
Trumps.Add(new TopTrumps(10, 70, 25,5));
Trumps.Add(new TopTrumps(10, 14, 35,6));
Trumps.Add(new TopTrumps(20, 80, 40,7));
Trumps.Add(new TopTrumps(10, 44, 45,8));
Trumps.Add(new TopTrumps(13, 67, 30,9));
Trumps.Add(new TopTrumps(14, 12, 20,10));
/////////////////////
//Shuffle routine
/////////////////////
Random random = new Random(); //Create new random number
int n = Trumps.Count; //Create variable of Trump decks length
// listBox1.Items.Add(n);
while (n > 1)
{
n--; //n immedietely decreased
int k = random.Next(n + 1); //Create a random number between 0 and 9 (The adressable range of the list)
TopTrumps nth_value = Trumps[k]; //Store random number index contents, in temp storage
Trumps[k] = Trumps[n]; //Swap the random number index with the nth index (On 1st loop, Random number index will swap values with 10th card)
Trumps[n] = nth_value; //Set the nth card to the random numbers index contents
}
for (int i = 0; i < Trumps.Count; i++)
{
listBox2.Items.Add("Card [" + i + "] : " + Trumps[i].CardID);
}
/////////////////////////////
// Create player 1 card deck
/////////////////////////////
List<TopTrumps> PlayerDeck = new List<TopTrumps>();
listBox1.Items.Add(PlayerDeck.Count);
PlayerDeck = Trumps.GetRange(0, 5);
listBox1.Items.Add(PlayerDeck.Count);
for (int i = 0; i < PlayerDeck.Count; i++)
{
listBox1.Items.Add("PLAYER Card [" + i + "] : " + PlayerDeck[i].height);
}
/////////////////////////////
// Create computer card deck
/////////////////////////////
Player Computer = new Player("Computer", 0, true); //Create new player
List<TopTrumps> ComputerDeck = new List<TopTrumps>();
ComputerDeck = Trumps.GetRange(5, 5);
for (int i = 0; i <ComputerDeck.Count; i++)
{
listBox3.Items.Add("COMP Card [" + i + "] : " + ComputerDeck[i].height);
}
listBox3.Items.Add(Computer.Name);
/////////////////////////
//Initial deck set up
/////////////////////////
CardID.Text = "Card ID: " + PlayerDeck[0].CardID;
Height.Text = "Height: " + PlayerDeck[0].height;
Length.Text = "Length: " + PlayerDeck[0].length;
Speed.Text = "Speed: " + PlayerDeck[0].speed;
listBox1.Items.Clear();
listBox1.Items.Add("Card Number: "+PlayerDeck[0].CardID);
listBox1.Items.Add("");
listBox1.Items.Add("Height: " + PlayerDeck[0].height);
listBox1.Items.Add("length: " + PlayerDeck[0].length);
listBox1.Items.Add("speed: " + PlayerDeck[0].speed);
}
private void button1_Click(object sender, EventArgs e)
{
Computer.Name = PlayerName.Text;
}
private void PlayCard_Click(object sender, EventArgs e)
{
bool PlayerWon = false;
if (Height.Checked)
{
// PlayerDe
}
}
}
}
答案 0 :(得分:1)
我希望你不要指望工作代码作为答案,所以我建议你这样做:
将您的代码划分为“模型”类以及属于您的UI的所有内容,这将使很多事情变得更加容易。 重构您的模型,以便它保存所有不是UI特定的模型。在你的情况下,我猜这样的事情是有道理的:
MyCardGame
类Player
模型(计算机或人类现在对模型无关紧要)TopTrump
班级你的MyCardGame
课程应该引用其玩家,显然,玩家应该能够知道他的牌等。
可以这样想:您希望整个游戏模型能够在控制台窗口,WinForms项目或Web应用程序中运行。这听起来有点矫枉过正,但它会有所回报。
然后重写您的UI代码,理想情况下,您的UI不应该为自己处理卡片,它应该告诉您MyCardGame
它现在应该处理新一轮。然后,您的UI可以从模型中显示它想要的任何内容。
对于您的实际问题,我认为它会自行解决,或者如果您尝试执行上述操作,您将看到解决方案。
答案 1 :(得分:0)
计算机是在DealCards_Click
方法中定义的,因此尝试在方法外部访问它将失败。如果您将计算机视为类成员,则将其定义为已定义Player1
此外,一些建议使您的代码更易于阅读(和维护)