Can smbd help me to convert some values from real to virtual?

C Sharp Game Help - Passing Values?

  • Hi, I have to create a dice game in C#, I have the basic code down, but I want to create a class then create a new instance of that class for each player. However when I do this the random object for the dice doesnt work nor does it calculate its position. I have included the working code, but im asking for your help in trying to create this other class. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace AdvancedSolution { public partial class Form2 : Form { //Variables Random randomGenerator; dice gameDice; public int P1RollValue; public int P2RollValue; public int playerOnePosition; public int playerTwoPosition; int turn; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { lblPlayerOneName.Text = Form1.playerOneName; lblPlayerTwoName.Text = Form1.playerTwoName; randomGenerator = new Random(); gameDice = new dice(randomGenerator); } public void playerOneRoll() { P1RollValue = gameDice.Roll(); lblRollValue.Text = Convert.ToString(P1RollValue); } public void playerTwoRoll() { P2RollValue = gameDice.Roll(); lblRollValue.Text = Convert.ToString(P2RollValue); } public int getP1Position() { return playerOnePosition += P1RollValue; } public void playerOnePositionOutput() { int p1Pos = getP1Position(); lblPlayerOnePositionValue.Text = Convert.ToString(p1Pos); } public int getP2Position() { return playerTwoPosition += P2RollValue; } public void playerTwoPositionOutput() { int p2Pos = getP2Position(); lblPlayerTwoPositionValue.Text = Convert.ToString(p2Pos); } private void btnRoll_Click(object sender, EventArgs e) { if (turn % 2 == 0) //if turn is even { playerOneRoll(); playerOnePositionOutput(); lblCurrentPlayer.Text = "Player Two, Click Roll to roll the Dice!"; //player1 turn turn++; } else //otherwise its odd { playerTwoRoll(); playerTwoPositionOutput(); lblCurrentPlayer.Text = "Player One, Click Roll to roll the Dice!"; //player2 turn turn++; } } } }

  • Answer:

    1. using System; 2. 3. namespace dicegame1 4. { 5. public class RandDice 6. { 7. public static void Main() 8. { 9. Random ran = new Random(); 10. 11. int player1; 12. int player2; 13. int player1RoundScore; 14. int player2RoundScore; 15. 16. bool player1RoundScore = false; 17. bool player2RoundScore = false; 18. 19. int roundNumber; 20. } 21. // Random dice rolling // 22. { 23. int diceOne = ran.Next(1,7); 24. int diceTwo = ran.Next(1,7); 25. int diceThree = ran.Next(1,7); 26. 27. Console.write(ran.Next(1,7) + " "); 28. Console.write(ran.Next(1,7) + " "); 29. Console.write(ran.Next(1,7)); 30. } 31. { 32. if (diceOne == diceTwo || diceTwo == diceThree || diceOne == diceThree) 33. 34. } 35. 36. 37. } 38. } 39. }

Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.