玩家x和y的一个类 [英] a class for player x and y

查看:72
本文介绍了玩家x和y的一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "Click a button";
        Label2.Text = "X's Score: 0";
        Label3.Text = "Y's Score: 0";
   }
    //viktig med static eller public
    static bool turn = true;
    public bool didwin;
    //static bool Xdidwin;
    //static bool Ydidwin;
    public void winforX()
    {
        int winX = 0;
        if (Button1.Text == "X" & Button2.Text == "X" & Button3.Text == "X")
        {
            txt_box.Text = ("X Wins!");
            winX += 1;
            Label2.Text = "X's Score: " + winX.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button4.Text == "X" & Button5.Text == "X" & Button6.Text == "X")
        {
            txt_box.Text = ("X Wins!");
            winX += 1;
            Label2.Text = "X's Score: " + winX.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button7.Text == "X" & Button8.Text == "X" & Button9.Text == "X")
        {
            txt_box.Text = ("X Wins!");
            winX += 1;
            didwin = true;
            lockall();
            Label2.Text = "X's Score: " + winX.ToString();
            enablebuttons();
        }
        if (Button1.Text == "X" & Button4.Text == "X" & Button7.Text == "X")
        {
            txt_box.Text = ("X Wins!");
            winX += 1;
            Label2.Text = "X's Score: " + winX.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button2.Text == "X" & Button5.Text == "X" & Button8.Text == "X")
        {
            txt_box.Text = ("X Wins!");
            winX += 1;
            Label2.Text = "X's Score: " + winX.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button3.Text == "X" & Button6.Text == "X" & Button9.Text == "X")
        {
            txt_box.Text = ("X Wins!");
            winX += 1;
            Label2.Text = "X's Score: " + winX.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button1.Text == "X" & Button5.Text == "X" & Button9.Text == "X")
        {
            txt_box.Text = ("X Wins!");
            winX += 1;
            Label2.Text = "X's Score: " + winX.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button3.Text == "X" & Button5.Text == "X" & Button7.Text == "X")
        {
            txt_box.Text = ("X Wins!");
            winX += 1;
            Label2.Text = "X's Score: " + winX.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }       //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        changeturn();
    }
    public void winforY()
    {
        int winY = 0;
        if (Button1.Text == "Y" & Button2.Text == "Y" & Button3.Text == "Y")
        {
            txt_box.Text = ("Y Wins!");
            winY += 1;
            Label3.Text = "Y's Score: " + winY.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button4.Text == "Y" & Button5.Text == "Y" & Button6.Text == "Y")
        {
            txt_box.Text = ("Y Wins!");
            winY += 1;
            Label3.Text = "Y's Score: " + winY.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button7.Text == "Y" & Button8.Text == "Y" & Button9.Text == "Y")
        {
            txt_box.Text = ("Y Wins!");
            winY += 1;
            Label3.Text = "Y's Score: " + winY.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button1.Text == "Y" & Button4.Text == "Y" & Button7.Text == "Y")
        {
            txt_box.Text = ("Y Wins!");
            winY += 1;
            Label3.Text = "Y's Score: " + winY.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button2.Text == "Y" & Button5.Text == "Y" & Button8.Text == "Y")
        {
            txt_box.Text = ("Y Wins!");
            winY += 1;
            Label3.Text = "Y's Score: " + winY.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button3.Text == "Y" & Button6.Text == "Y" & Button9.Text == "Y")
        {
            txt_box.Text = ("Y Wins!");
            winY += 1;
            didwin = true;
            lockall();
            Label3.Text = "Y's Score: " + winY.ToString();
            enablebuttons();
        }
        if (Button1.Text == "Y" & Button5.Text == "Y" & Button9.Text == "Y")
        {
            txt_box.Text = ("Y Wins!");
            winY += 1;
            Label3.Text = "Y's Score: " + winY.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        if (Button3.Text == "Y" & Button5.Text == "Y" & Button7.Text == "Y")
        {
            txt_box.Text = ("Y Wins!");
            winY += 1;
            Label3.Text = "Y's Score: " + winY.ToString();
            didwin = true;
            lockall();
            enablebuttons();
        }
        changeturn();
    }
    public void enablebuttons()
    {
    }
    public void lockall()
    {
        Button1.Enabled = false;
        Button2.Enabled = false;
        Button3.Enabled = false;
        Button4.Enabled = false;
        Button5.Enabled = false;
        Button6.Enabled = false;
        Button7.Enabled = false;
        Button8.Enabled = false;
        Button9.Enabled = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (turn == true)
        {
            Button1.Text = "X";
            turn = false;
        }
        else
        {
            Button1.Text = "Y";
            turn = true;
        }
        Button1.Enabled = false;
        winforX();
        winforY();
        checkforcat();
        //  AI(sender, e)
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (turn == true)
        {
            Button2.Text = "X";
            turn = false;
        }
        else
        {
            Button2.Text = "Y";
            turn = true;
        }
        Button2.Enabled = false;
        winforX();
        winforY();
        checkforcat();
        //  AI(sender, e)
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (turn == true)
        {
            Button3.Text = "X";
            turn = false;
        }
        else
        {
            Button3.Text = "Y";
            turn = true;
        }
        Button3.Enabled = false;
        winforX();
        winforY();
        checkforcat();
        //  AI(sender, e)
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        if (turn == true)
        {
            Button4.Text = "X";
            turn = false;
        }
        else
        {
            Button4.Text = "Y";
            turn = true;
        }
        Button4.Enabled = false;
        winforX();
        winforY();
        checkforcat();
        //  AI(sender, e)
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        if (turn == true)
        {
            Button5.Text = "X";
            turn = false;
        }
        else
        {
            Button5.Text = "Y";
            turn = true;
        }
        Button5.Enabled = false;
        winforX();
        winforY();
        checkforcat();
        // AI(sender, e)
    }
    protected void Button10_Click(object sender, EventArgs e)
    {
        if (turn == true)
        {
            Button6.Text = "X";
            turn = false;
        }
        else
        {
            Button6.Text = "Y";
            turn = true;
        }
        Button6.Enabled = false;
        winforX();
        winforY();
        checkforcat();
        //  AI(sender, e)
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        if (turn == true)
        {
            Button7.Text = "X";
            turn = false;
        }
        else
        {
            Button7.Text = "Y";
            turn = true;
        }
        Button7.Enabled = false;
        winforX();
        winforY();
        checkforcat();
        // AI(sender, e)
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        if (turn == true)
        {
            Button8.Text = "X";
            turn = false;
        }
        else
        {
            Button8.Text = "Y";
            turn = true;
        }
        Button8.Enabled = false;
        winforX();
        winforY();
        checkforcat();
        //  AI(sender, e)
    }
    protected void Button9_Click(object sender, EventArgs e)
    {
        if (turn == true)
        {
            Button9.Text = "X";
            turn = false;
        }
        else
        {
            Button9.Text = "Y";
            turn = true;
        }
        Button9.Enabled = false;
        winforX();
        winforY();
        checkforcat();
    }
    public void checkforcat()
    {
        if (Button1.Enabled == false & Button2.Enabled == false & Button3.Enabled == false & Button4.Enabled == false & Button5.Enabled == false & Button6.Enabled == false & Button7.Enabled == false & Button8.Enabled == false & Button9.Enabled == false)
        {
            if (didwin == false)
            {
                txt_box.Text = ("no one wins :( play again?");
                enablebuttons();
            }
            else
            {
                //do nothing
            }
        }
        else
        {
        }
    }
    private void changeturn()
    {
        if (turn == true)
        {
            Label1.Text = "X's Turn";
        }
        else
        {
            Label1.Text = "Y's Turn";
        }
    }
    private void restart()
    {
        winforX();
        winforY();
        //int winX = 0;
        //int winY = 0;

        //if (Ydidwin == true)
        //{
        //    winY += 1;
        //    Label3.Text = "Y's Score: " + winY.ToString();
        //    win();
        //}
        //else if (Xdidwin == true)
        //{
        //    winX += 1;
        //    Label3.Text = "Y's Score: " + winX.ToString();
        //    win();
        //}

        Button1.Enabled = true;
        Button1.Text = " ";
        Button2.Enabled = true;
        Button2.Text = " ";
        Button3.Enabled = true;
        Button3.Text = " ";
        Button4.Enabled = true;
        Button4.Text = " ";
        Button5.Enabled = true;
        Button5.Text = " ";
        Button6.Enabled = true;
        Button6.Text = " ";
        Button7.Enabled = true;
        Button7.Text = " ";
        Button8.Enabled = true;
        Button8.Text = " ";
        Button9.Enabled = true;
        Button9.Text = " ";
        txt_box.Text = ("");
    }
    protected void btn_restart_Click(object sender, EventArgs e)
    {
        restart();
        winforX();
        winforY();
    }
}

推荐答案

所有看起来都可能在某种程度上起作用。所以我把你的问题作为尝试以正确的方式学习如何完成。



当然你可以创建一个类播放器并实例化两次。让班级有一个玩家名字的字段和另一个他赢得的轮次数。



但你游戏的主要逻辑是以这种尴尬的方式编码,我建议你扔掉它,因为你想要以OO的方式,在新创建的类板的实例中实现它(类似于棋盘游戏)。



最重要的是,改变胜利的确定方式。当前代码基本上列出了玩家获胜的所有可能性。将其更改为规则描述是什么让玩家获胜。一个例子:

当前实现:X有按钮1,2,3。

目标:X有一个竞争行。

这是一个概括。你得到了一堆循环和可能性

a)水平

b)垂直

c)上升的对角线

d )掉落对角线



哦,阅读DRY原则。确定X的胜利可以这样做:
That all looks like it could work in a way. So I take your question as an attempt to learn how it is done the right way.

Of course you could create a class Player and instantiate it twice. Let the class have a field for the player''s name and another one for the number of rounds he won so far.

But the main logic of your game is coded in such an awkward style that I suggest you throw it away and, since you want to go the OO way, implement it in an instance of the newly-to-create class Board (an analogy to a board game).

Most important, change the way a win is determined. The current code basically lists all possibilities for a player to win. Change that into a description of the rules what makes a player win. An example:
Current implementation: X has buttons 1, 2, 3.
Goal: X has a compete row.
This is a generalization. You get that with a bunch of loops and the possibilities
a) horizontal
b) vertical
c) rising diagonal
d) falling diagonal

Oh and read up on the "DRY" principle. Determining a win for X could be done that way:
if (
    (Button1.Text == "X" & Button2.Text == "X" & Button3.Text == "X")
    || (Button4.Text == "X" & Button5.Text == "X" & Button6.Text == "X")
    || (Button7.Text == "X" & Button8.Text == "X" & Button9.Text == "X")
    || (Button1.Text == "X" & Button4.Text == "X" & Button7.Text == "X")
    || (Button2.Text == "X" & Button5.Text == "X" & Button8.Text == "X")
    || (Button3.Text == "X" & Button6.Text == "X" & Button9.Text == "X")
    || (Button1.Text == "X" & Button5.Text == "X" & Button9.Text == "X")
    || (Button3.Text == "X" & Button5.Text == "X" & Button7.Text == "X")
)
{
    PublishWinX();
}

private void PublishWinX()
{
    txt_box.Text = ("X Wins!");
    winX += 1;
    Label2.Text = "X's Score: " + winX.ToString();
    didwin = true;
    lockall();
    enablebuttons();
}

由于我上面提到的原因,它仍然很可怕。仅仅是为了说明执行分支不需要重复八次。一遍又一遍地重复它很容易出现难以检测的输入错误(什么组合给出了错误信息?)。

重复的东西属于一种方法,每次需要时都会被调用。



我就是这样做的:

It''s still horrible because of the reasons I mentioned above. Just for the purpose of stating that the execution branch doesn''t need to be repeated eight times. Repeating it over and over is prone to typing errors that are difficult to detect ("What combination gave the erronuous message?").
Repeated stuff belongs in a method and is called every time you need it.

That''s how I would do it:

private void PublishWin(Player winner)
{
    txt_box.Text = winner.Name + " wins!";
    winner.NumberOfRoundsWon++;
    // What ever
}





我希望能让你前进。当你卡在某个地方时随意问。并得到一本关于面向对象的书。





简单的播放器课程:



I hope that gets you going. Feel free to ask when you get stuck somewhere. And get a book on object orientation.


Simple Player class:

public class Player
{
    // Declarations
    private string _name = "PlayerName"; // Initialized to a standard value
    private int _roundsWon;              // Automatically initialized to zero

    // Properties
    public string Name
    {
        get{ return(_name); }
        set{ _name = value; }
    }


    public int RoundsWon
    {
        get{ return(_roundsWon); }
        set{ _roundsWon = value; }
    }
}



[/编辑]


[/Edit]


这篇关于玩家x和y的一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆