- Home /
Question by
craftercis · Nov 25, 2017 at 03:44 PM ·
c#dice
Highest number wins | Dice Game
Hello Unity users,
I make a simple dice game for school. You have 2 dice and the computer also has 2. the idea is that only their highest are compared with each other.
How can I make this?
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Tweedices : MonoBehaviour {
public Text SpelerDice1;
public Text PCDice1;
public Text SpelerDice2;
public Text PCDice2;
public Text score;
public Text score2;
public Text resultyou;
public Text resultpc;
public int dice1 = 0;
public int dice1extra = 0;
public int dice2extra = 0;
public int dice2 = 0;
public int point1 = 0;
public int point2 = 0;
public Text win;
public int resulty;
public int resultp;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void onButtonClick1()
{
Debug.Log("Dobbelen");
dice1 = Random.Range(1, 7);
SpelerDice1.text = dice1.ToString();
dice2 = Random.Range(1, 7);
PCDice1.text = dice2.ToString();
dice1extra = Random.Range(1, 7);
SpelerDice2.text = dice1extra.ToString();
dice2extra = Random.Range(1, 7);
PCDice2.text = dice2extra.ToString();
// Is dice1 + dice1extra groter dan dice2 + dice2extra dan wint de speler
if (dice1 + dice1extra > dice2 + dice2extra)
{
win.text = "You won!";
point1++; //waarde
score.text = point1.ToString();
}
// Is dice2 + dice2extra groter dan dice1 + dice1extra dan wint de computetr
if (dice2 + dice2extra > dice1 + dice1extra)
{
win.text = "You lose!";
point2++; //waarde
score2.text = point2.ToString();
}
// Is dice2 + dice2extra gelijk aan dice1 + dice1extra dan staat het gelijk
if (dice2 + dice2extra == dice1 + dice1extra)
{
win.text = "Tie!";
}
}
public void resultYou()
{
resulty = dice1 + dice1extra;
resultyou.text = resulty.ToString();
}
public void resultPc()
{
resultp = dice2 + dice2extra;
resultpc.text = resultp.ToString();
}
public void onButtonClick2()
{
SceneManager.LoadScene("Roll the die2");
}
}
Comment
Best Answer
Answer by Dray · Nov 25, 2017 at 04:51 PM
I guess you just need to assign the higher value for each player to a variable. Something like
var dice1highest = dice1 > dice1extra ? dice1 : dice1extra;
var dice2highest = dice2 > dice2extra ? dice2 : dice2extra;
if (dice1highest > dice2highest) {
// player wins
} else {
// computer wins
}