摇滚 - 纸 - 剪刀:如何添加得分?

时间:2015-01-04 17:56:26

标签: c#

(我尝试了所有类型的组合并进行了查找,但大多数解决方案都使用其他语言,如python。)

我正在尝试使用ASP.NET MVC在C#中制作一个简单的石头剪刀游戏,我可以在其中添加分数。获得10分的第一个(计算机与玩家)赢得比赛。我还想添加选项,你可以投注超过1分。

我使用简单的嵌套if语句制作游戏,并在两者之间添加了得分。问题是,当我按下播放(发送到服务器..)再次进行时,他重置分数并重新开始。我该如何解决这个问题?

第二个问题是:如何添加我在分数中实施的投注系统?只需输入

即可
  

vScoreUser + =(投注* 1);

似乎不起作用。

以下是我班级的代码:

    public class BladSteenSchaar
    {
    //Fields       
    private string vChoice;
    private int vChoiceComputer;
    private string vResult;
    Random vRandom = new Random();

    //Properties 
    public string Choice
    {
        get { return vChoice; }
        set 
        { 
            vChoice = value; 

            //Spel opstarten
            this.GeefResult();
        }
    }        
    public int ChoiceComputer
    {
        get { return vChoiceComputer; }
        //set { vChoiceComputer = value; }
    }
    public int Bet { get; set; }
    public string Result
    {
        get { return vResult; }
        //set { vResult = value; }
    }

    private int vScoreUser;

    public int ScoreUser
    {
        get { return vScoreUser; }
        set { vScoreUser = value; }
    }
    private int vScoreComputer;

    public int ScoreComputer
    {
        get { return vScoreComputer; }
        set { vScoreComputer = value; }
    }


    //Methods
    public void GeefResult() 
    { 
        //Vars

        //Generate random choice where: 0 = Rock, 1 = Paper en 2 = Siccors)            
        vChoiceComputer = vRandom.Next(0, 3);

        //Nested if statements met vResult als output
        if (Choice == "Rock")
        {
            if (vChoiceComputer == 0) //Rock
            {
                vResult = "Draw!";                   
            }
            else if (vChoiceComputer == 1) //Paper
            {
                vResult = "U lost!";
                vScoreComputer = vScoreComputer + 1;
            }
            else if (vChoiceComputer == 2) //Siccors
            {
                vResult = "U won!";
                vScoreUser = vScoreUser + 1;
            }   
        }
        else if (Choice == "Paper")
        {
            if (vChoiceComputer == 0) //Rock
            {
                vResult = "U won!";
                vScoreUser = vScoreUser + 1;
            }
            else if (vChoiceComputer == 1) //Paper
            {
                vResult = "Draw!";
            }
            else if (vChoiceComputer == 2) //Siccors
            {
                vResult = "U lost!";
                vScoreComputer = vScoreComputer + 1;
            }    
        }
        else if (Choice == "Siccors")
        {
            if (vChoiceComputer == 0) //Rock
            {
                vResult = "U lost!";
                vScoreComputer = vScoreComputer + 1;
            }
            else if (vChoiceComputer == 1) //Paper
            {                   
                vResult = "U won!";
                vScoreUser = vScoreUser + 1;
            }
            else if (vChoiceComputer == 2) //Siccors
            {
                vResult = "Draw!";
            }
        }

    }

控制器:

    public ActionResult Oef127()
    {
        //Default-object
        BladSteenSchaar vBladSteenSchaar = new BladSteenSchaar();
        vBladSteenSchaar.Bet = 1;

        //Show
        return View(vBladSteenSchaar);
    }
    [HttpPost]
    public ActionResult Oef127(BladSteenSchaar vBladSteenSchaar)
    {


        //Show
        return View(vBladSteenSchaar);
    }

HTML / CSS:

@model Project_CS.Classes.BladSteenSchaar

@{
ViewBag.Title = "Oef127";
}

<style>

form {
    background-color: lightblue;
    padding: 10px 20px 20px 20px;
    width: 500px;
}

td {
    text-align: center;
    padding: 5px;
}

</style>

<form method="post">

<h2>Rock - Paper - Siccors!</h2>
<hr />

Make your Choice: <br/>
<input type="radio" name="Choice" value="Rock">Rock
<br />
<input type="radio" name="Choice" value="Paper">Paper
<br />
<input type="radio" name="Choice" value="Siccors">Siccors
<br />

<br/>
Do you want to play for 1, 2 or 3 points?
<br/>
<input type="text" name="Bet" value="@Model.Bet" />

<hr/>
<input type="submit" value="Send to Server!" />

<br/>

</form>

<h3>Result:</h3>

@Model.Result

<br /><br />

<b>Current score:</b> <br />

<table>
<tr>
    <td>Player</td>
    <td>Computer</td>
</tr>
<tr>
    <td>@Model.ScoreUser</td>
    <td>@Model.ScoreComputer</td>
</tr>
</table>

如果您发现任何其他错误,请告诉我。

1 个答案:

答案 0 :(得分:0)

我认为问题出在这个属性的集合中

 public string Choice
    {
        get { return vChoice; }
        set 
        { 
            vChoice = value; 

            //Spel opstarten
            this.GeefResult();
        }
    } 

每次设置选项时,都会执行GeefResult功能。也许在这个函数中,你将一些东西初始化为0,并重置每个选择。尝试在BladSteenSchaar课程中将分数信息添加为属性。通过这种方式,您将能够存储BladSteenSchaar类的每个实例的分数信息。