1

さて、私はしばらくこの問題を回避してきましたが、続行する前にコードのスコープを整理する必要があるところまで来ました.使用するといいでしょう

これが私の現在の 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 The name 'Computer' does not exist in the current context」というエラーが表示されます。

        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