我怎么能用x和y球员上课,所以我的球员x的背景颜色为蓝色而我的球员的背景颜色为红色? [英] hi how can i make a class with player x and y so my player x have background color blue and my player y have backgroundcolor red?

查看:90
本文介绍了我怎么能用x和y球员上课,所以我的球员x的背景颜色为蓝色而我的球员的背景颜色为红色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

{
   static public SpelKnapp[] SpelPlan = new SpelKnapp[9];

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "Click a button";


        if (!IsPostBack)
        {
            /*for(int i; i<9; i++)
            {
                SpelPlan[i].Turn(true);
                SpelPlan[].Text("");
            }*/

            for(int i=0; i<9; i++)
            {
                SpelPlan[i] = new SpelKnapp();
            }


        }




    }

    static bool turn = true;
    public bool didwin;


    public void winforX()
    {

        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();

    }
        ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    public void winforY()
    {
        if (
       (Button1.Text == "Y" & Button2.Text == "Y" & Button3.Text == "Y")
    || (Button4.Text == "Y" & Button5.Text == "Y" & Button6.Text == "Y")
    || (Button7.Text == "Y" & Button8.Text == "Y" & Button9.Text == "Y")
    || (Button1.Text == "Y" & Button4.Text == "Y" & Button7.Text == "Y")
    || (Button2.Text == "Y" & Button5.Text == "Y" & Button8.Text == "Y")
    || (Button3.Text == "Y" & Button6.Text == "Y" & Button9.Text == "Y")
    || (Button1.Text == "Y" & Button5.Text == "Y" & Button9.Text == "Y")
    || (Button3.Text == "Y" & Button5.Text == "Y" & Button7.Text == "Y")
          )

        PublishWinY();

        changeturn();

    }

    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    private void PublishWinX()
    {


        txt_box.Text = ("X Wins!");



        didwin = true;
        lockall();

    }

    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    private void PublishWinY()
    {


        txt_box.Text = ("Y Wins!");



        didwin = true;
        lockall();

    }

    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


    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();

    }
    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    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();

    }
    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    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();

    }
    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    protected void Button4_Click(object sender, EventArgs e)
    {
        if (turn == true)
        {
            Button4.Text = "X";
            turn = false;
            //SpelPlan[5].Turn(false);//Knapp5 ska vara av
            //Button4.Enabled = SpelPlan[5].Get_Turn();
        }
        else
        {
            Button4.Text = "Y";
            turn = true;
        }
        Button4.Enabled = false;
        winforX();
        winforY();
        checkforcat();
        // if (turn == true)
        //{
        //    Button4.Text = "X";
        //    SpelPlan[5].Turn(false);//Knapp5 ska vara av
        //    Button4.Enabled = SpelPlan[5].Get_Turn();
        //}
        //else
        //{
        //    Button4.Text = "Y";
        //    turn = true;
        //}

        //SpelPlan[5].Turn(true);//Knapp5 ska vara av
        //Button4.Enabled = SpelPlan[5].Get_Turn();
    }
    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    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();

    }
    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    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();

    }
    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    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();

    }
    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    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();

    }
    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    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?");

                Label1.Text = "Click a Button Again";

            }
        }
    }

    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    private void changeturn()
    {
        if (didwin == true)
        {

            if (turn == true)
            {

                Label1.Text = "x's Turn";

            }
            else
            {
                Label1.Text = "y's Turn";
            }
        }
    }

    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    private void restart()
    {


        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 = (" ");
        txt_box.Enabled = false;


    }

    ///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    protected void btn_restart_Click(object sender, EventArgs e)
    {


        restart();



    }
}

推荐答案

创建一个类代表你的玩家并将背景颜色包含在玩家类的属性中。



你设计的整个游戏都是错误的。您依靠控件的值和状态来确定游戏的状态。那是倒退。您的游戏状态应由专用类维护以维护数据,然后应设置控件以反映该状态。
Create a class that represents your player and include the background color as a property of the player class.

You''re designing your entire game wrong. You''re depending on the values and states of controls to determine the state of the game. That''s backwards. Your game state should be maintained by dedicated classes to maintaining the data, then the controls should be set to reflect that state.


这篇关于我怎么能用x和y球员上课,所以我的球员x的背景颜色为蓝色而我的球员的背景颜色为红色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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