Here is my current Player class (I know you can use "Name {get; set}" for example, However I was having some problems defining, Below are my 2 classes, My top trumps class (10 top trump cards are later created), and a player class.
//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;
}
}
My aim of the player class is to store the Name, Score, Turn as well as a deck of 5 cards for both the player, Then the computer, In the form of a list. I have no problem doing this outside of the class such as here
/////////////////////////////
// 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);
However when I try to access anything to do with the player outside of the function
"public void DealCards_Click(object sender, EventArgs e)"
Which is what the above code resides in, Then I will always get an error that it cannot be accessed. For example on the button I have posted below I am given the error "Error 1 The name 'Computer' does not exist in the current context"
private void button1_Click(object sender, EventArgs e)
{
Player1.Name = PlayerName.Text;
}
Any help on how to organise this code properly would help a huge amount can't really continue until I can work out how to break down my program, and get a list working within a class, I have posted below a full source for if you need to get better understanding of my program, Thanks a lot
-Tom
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
}
}
}
}

New Topic/Question
Reply



MultiQuote






|