1

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


    }

}

}

4

2 回答 2

1

我希望你不要期望工作代码作为答案,所以这是我向你推荐的:

将您的代码划分为您的“模型”类和属于您的 UI 的所有内容,这将使很多事情变得更容易。重构您的模型,使其包含非特定于 UI 的所有内容。在你的情况下,我想这样的事情是有道理的:

  • 一个MyCardGame
  • 模型Player(计算机或人现在对模型无关紧要)
  • 你的TopTrump班级

你的MyCardGame类应该引用它的玩家,显然,玩家应该能够知道他的牌等等。这样想:你希望你的整个游戏模型能够在控制台窗口、WinForms 项目或 Web 应用程序中运行. 这听起来有点矫枉过正,但它会得到回报。

然后重写你的 UI 代码,理想情况下你的 UI 不应该为自己发牌,它应该告诉你MyCardGame它现在应该发新一轮。然后,您的 UI 可以从模型中显示它想要的任何内容。

对于您的实际问题,我认为它会自行解决,或者如果您尝试执行上述操作,您将看到解决方案。

于 2013-03-04T15:18:50.523 回答
0

计算机是在DealCards_Click方法中定义的,因此尝试在方法之外访问它会失败。如果您打算让计算机成为班级成员,则按照您定义的方式定义它Player1

此外,还有一些建议可以使您的代码更易于阅读(和维护)

  • 以小写字母开头命名变量
  • 在同一个地方定义所有班级成员/属性(更容易找到)
  • 使用一些符号将类成员与局部范围变量分开(我通常以 m_ 开头
于 2013-03-04T15:18:44.287 回答