好吧,我已经避免这个问题有一段时间了,但到了我需要整理代码范围才能继续的地步,我对 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
}
}
}
}