vendredi 14 novembre 2014

c# - affiche une valeur numérique relative à l'image du dé montré - Stack Overflow


I'm fairly new to programming and for a uni project I have created a simple game where there are two payers who each have a die. The dice are then rolled and whoever has the highest number wins, simple.


The program works as expected but I want to add an extra textbox below the die which will display the numerical value of the image.


I believe the DisplayDice method assigns the value to the correct image, so do I need to somehow assign this to the textbox? and am I right in thinking that I can't assign an int to a textbox so do I need to use int.Parse? here is my code;


public partial class Form1 : Form
{
// Attributes
// Values live for as long as the form is open
Random _rnd = new Random(); // Random number generator
string[] _diceImages; // store names of all image files
int[] _playerDice; // Dice values for each player
int _spinCount; // Count of number of animation ticks

const int NUMBER_OF_FACES = 6;
const int NUMBER_OF_PLAYERS = 2;
const int PLAYER_ONE = 0;
const int PLAYER_TWO = 1;
const int NUMBER_OF_SPINS = 5;

public Form1()
{
InitializeComponent();

_diceImages = new string[NUMBER_OF_FACES]; // _diceImages is set to the value given to NUMBER_OF_FACES
SetupDiceImages();

// One die per player, create dice
_playerDice = new int[NUMBER_OF_PLAYERS]; // _playerDice is set to the value given to NUMBER_OF_PLAYERS
ChangeDiceValue();

DisplayDice();
}

// sets the correct dice image to the relevant value in the array
// there are six faces on a die so the array needs six values, 0-5
private void SetupDiceImages()
{
_diceImages[0] = "one.gif";
_diceImages[1] = "two.gif";
_diceImages[2] = "three.gif";
_diceImages[3] = "four.gif";
_diceImages[4] = "five.gif";
_diceImages[5] = "six.gif";
}

// random number is generated to change the value displayed on the dice
private void ChangeDiceValue()
{
// Generate random values between 0 and 5
_playerDice[PLAYER_ONE] = RandomValue(NUMBER_OF_FACES - 1); //random number generated for player 1
_playerDice[PLAYER_TWO] = RandomValue(NUMBER_OF_FACES - 1); //random number generated for player 2
}

// tells the program which number to display
private void DisplayDice()
{
int diceValue;

diceValue = _playerDice[PLAYER_ONE]; // Value thrown by player one
playerDicePictureBox1.Image = Image.FromFile("../../Images/" + _diceImages[diceValue]); // tells the program where to get the image for player 1's picture box

diceValue = _playerDice[PLAYER_TWO]; // Value thrown by player two
playerDicePictureBox2.Image = Image.FromFile("../../Images/" + _diceImages[diceValue]); // tells the program where to get the image for player 1's picture box

}

// Generate random value between 0 and maxValue
// including 0 and maxValue
private int RandomValue(int maxValue)
{
return _rnd.Next(0, maxValue + 1);
}


// animation starts when the button is clicked
private void throwDiceButton_Click(object sender, EventArgs e)
{
_spinCount = 0; // Start counting again
animationTimer.Enabled = true; // Start the timer

}

private void FindWinner()
{
// Check values of dice here
int player1Dice;
int player2Dice;

player1Dice = _playerDice[PLAYER_ONE] + 1; // 0 To 5 hence + 1
player2Dice = _playerDice[PLAYER_TWO] + 1;
}

private void animationTimer_Tick(object sender, EventArgs e)
{
ChangeDiceValue();
DisplayDice();

if (_spinCount + 1 < NUMBER_OF_SPINS)
{
// Safe to implement the count
_spinCount++;
}
else
{
// Stop animation now
_spinCount = 0;
animationTimer.Enabled = false; // Stop the timer
FindWinner();
// if statement is used to find a winner
if (_playerDice[PLAYER_ONE] > _playerDice[PLAYER_TWO]) // if player one's die is greater than player two's
{
resultTxtBox.Text = ("Player 1 WINS!!!"); // this message is displayed if the above is true
}
else
{
resultTxtBox.Text = ("Player 2 WINS!!!"); // if the above is false then this message is displayed
}
if (_playerDice[PLAYER_ONE] == _playerDice[PLAYER_TWO]) // player one and two have the same number
{
resultTxtBox.Text = ("It's a DRAW!!!"); // if the above is true then this message is displayed
}
}

}

}

any help would be greatly appreciated as it will also help me with a blackjack game that I am also trying to make.




You are correct in saying that you can't assign an int to a textbox, however, all objects in .NET have the "ToString()" function, so you can get the string value with:


Player1ScoreTextBox.Text = player1Dice.ToString();

More info on MSDN


Note that if you had a large dice count, or floats, or something else that you need formatted a specific way, you can pass ToString a formatting string. For example ToString("F2") formats as a fixed point number with 2 decimal places of precision, so 2.1 becomes 2.10.


The other strings can also be found on MSDN




you can assign the text representation of a integer value to the text of a textbox like:


tbPlayer1Value.Text = diceValue.ToString();


I'm fairly new to programming and for a uni project I have created a simple game where there are two payers who each have a die. The dice are then rolled and whoever has the highest number wins, simple.


The program works as expected but I want to add an extra textbox below the die which will display the numerical value of the image.


I believe the DisplayDice method assigns the value to the correct image, so do I need to somehow assign this to the textbox? and am I right in thinking that I can't assign an int to a textbox so do I need to use int.Parse? here is my code;


public partial class Form1 : Form
{
// Attributes
// Values live for as long as the form is open
Random _rnd = new Random(); // Random number generator
string[] _diceImages; // store names of all image files
int[] _playerDice; // Dice values for each player
int _spinCount; // Count of number of animation ticks

const int NUMBER_OF_FACES = 6;
const int NUMBER_OF_PLAYERS = 2;
const int PLAYER_ONE = 0;
const int PLAYER_TWO = 1;
const int NUMBER_OF_SPINS = 5;

public Form1()
{
InitializeComponent();

_diceImages = new string[NUMBER_OF_FACES]; // _diceImages is set to the value given to NUMBER_OF_FACES
SetupDiceImages();

// One die per player, create dice
_playerDice = new int[NUMBER_OF_PLAYERS]; // _playerDice is set to the value given to NUMBER_OF_PLAYERS
ChangeDiceValue();

DisplayDice();
}

// sets the correct dice image to the relevant value in the array
// there are six faces on a die so the array needs six values, 0-5
private void SetupDiceImages()
{
_diceImages[0] = "one.gif";
_diceImages[1] = "two.gif";
_diceImages[2] = "three.gif";
_diceImages[3] = "four.gif";
_diceImages[4] = "five.gif";
_diceImages[5] = "six.gif";
}

// random number is generated to change the value displayed on the dice
private void ChangeDiceValue()
{
// Generate random values between 0 and 5
_playerDice[PLAYER_ONE] = RandomValue(NUMBER_OF_FACES - 1); //random number generated for player 1
_playerDice[PLAYER_TWO] = RandomValue(NUMBER_OF_FACES - 1); //random number generated for player 2
}

// tells the program which number to display
private void DisplayDice()
{
int diceValue;

diceValue = _playerDice[PLAYER_ONE]; // Value thrown by player one
playerDicePictureBox1.Image = Image.FromFile("../../Images/" + _diceImages[diceValue]); // tells the program where to get the image for player 1's picture box

diceValue = _playerDice[PLAYER_TWO]; // Value thrown by player two
playerDicePictureBox2.Image = Image.FromFile("../../Images/" + _diceImages[diceValue]); // tells the program where to get the image for player 1's picture box

}

// Generate random value between 0 and maxValue
// including 0 and maxValue
private int RandomValue(int maxValue)
{
return _rnd.Next(0, maxValue + 1);
}


// animation starts when the button is clicked
private void throwDiceButton_Click(object sender, EventArgs e)
{
_spinCount = 0; // Start counting again
animationTimer.Enabled = true; // Start the timer

}

private void FindWinner()
{
// Check values of dice here
int player1Dice;
int player2Dice;

player1Dice = _playerDice[PLAYER_ONE] + 1; // 0 To 5 hence + 1
player2Dice = _playerDice[PLAYER_TWO] + 1;
}

private void animationTimer_Tick(object sender, EventArgs e)
{
ChangeDiceValue();
DisplayDice();

if (_spinCount + 1 < NUMBER_OF_SPINS)
{
// Safe to implement the count
_spinCount++;
}
else
{
// Stop animation now
_spinCount = 0;
animationTimer.Enabled = false; // Stop the timer
FindWinner();
// if statement is used to find a winner
if (_playerDice[PLAYER_ONE] > _playerDice[PLAYER_TWO]) // if player one's die is greater than player two's
{
resultTxtBox.Text = ("Player 1 WINS!!!"); // this message is displayed if the above is true
}
else
{
resultTxtBox.Text = ("Player 2 WINS!!!"); // if the above is false then this message is displayed
}
if (_playerDice[PLAYER_ONE] == _playerDice[PLAYER_TWO]) // player one and two have the same number
{
resultTxtBox.Text = ("It's a DRAW!!!"); // if the above is true then this message is displayed
}
}

}

}

any help would be greatly appreciated as it will also help me with a blackjack game that I am also trying to make.



You are correct in saying that you can't assign an int to a textbox, however, all objects in .NET have the "ToString()" function, so you can get the string value with:


Player1ScoreTextBox.Text = player1Dice.ToString();

More info on MSDN


Note that if you had a large dice count, or floats, or something else that you need formatted a specific way, you can pass ToString a formatting string. For example ToString("F2") formats as a fixed point number with 2 decimal places of precision, so 2.1 becomes 2.10.


The other strings can also be found on MSDN



you can assign the text representation of a integer value to the text of a textbox like:


tbPlayer1Value.Text = diceValue.ToString();

0 commentaires:

Enregistrer un commentaire