如何使用C#将visual复选框链接到visual studio 2010中的图像/随机结果..... [英] How do I link a checkbox to an image/random result in visual studio 2010 using C# .....

查看:77
本文介绍了如何使用C#将visual复选框链接到visual studio 2010中的图像/随机结果.....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有!这是我的第一篇帖子,所以感谢你让我向社区发布关于我的小骰子游戏Greedy的问题,我可以通过观看和阅读其他帖子(包括我在注册之前在这里找到的那​​些帖子)在闲暇时间拼凑起来玩得开心我)。到目前为止我所做的一切都很好,但现在我想添加一些额外的功能,使游戏更具挑战性。



这是我几年前从朋友那里学到的六个骰子游戏,随机骰子结果的各种组合将等于特定的点值。目前我所拥有的内容可以准确地检测每个6个骰子卷中的任何点数结果,并将其显示在文本字段中,允许玩家记下所获得的内容,并记录他们的得分有多高......但是本身有点多余,对游戏的兴趣很快就会消失,因为应用程序没有内置的兴奋感,就像几年前和朋友一起玩的那样。



要改变我想要在每个骰子下面加入复选框(或使我用来显示各种Dice的图形滚动按钮来保存它们)在下一次滚动之前决定是否持有 - 与Yahtzee相似。每次骰子被锁定时,它将保持这种状态,直到所有骰子都被使用或得分,然后才能再次使用以继续获得积分。



问题1:我如何获得复选框以保持所有6个骰子不被重新滚动,同时允许其他骰子继续直到非得分滚动?



当按下按钮时我有这个:



Hey There all! this is my first post so thanks for letting me post my question to the community about my little dice game called Greedy I was able to piece together for fun in my spare time by watching and reading other posts (including those found here before I signed up myself). What I have so far is working well by itself but now I want to add some additional functionality to make the game a little more challenging.

It is a six dice game I learned from a friend years ago and various combos of random dice results would equal specific point values. Currently what I have in place accurately detects any of the point outcomes in each 6 dice roll and displays it into a text field allowing a player to write down what was earned and keep track of how high of a score they go... but that is a bit redundant in itself and interest in playing can be lost quickly because there is no excitement built-in to the application like there was when playing with friends a few years ago as mentioned.

To change that I want to incorporate Checkboxes under (or make the graphic I use to display the various Dice rolled a button to hold them) for each of the dice to decide to hold or not -similarly to Yahtzee- before the next roll. Each time a dice is locked it will remain that way until all dice have been used or score before they can be used again to continue earning points.

Question 1: How do I get the checkboxes to keep up to all 6 dice from being re rolled while allowing the others to continue until a non scoring roll is made?

When the Button is pressed I have this:

private void btn_RollDice_Click(object sender, EventArgs e)
        {
            RollDice();

            GetResults();

            ResetResults();
}





按下每个按钮,我显示的标签显示骰子重置但图像保持不变直到按下滚动按钮再次。



问题2:我是否需要取出重置以使其工作或如何修改以适应不在重置按钮之前重置已检查的骰子再次按下。



这是重置部分:



With each Button press the labels I have showing dice get reset but the image stays until the Roll button is pressed again.

Question 2: Do I need to take out he Reset for this to work or how can it be modified to accommodate not resetting the checked dice before the Roll button is pressed again.

Here is the Reset section:

private void ResetResults()
        {
            for (int i = 0; i < diceResults.Length; i++)
                diceResults[i] = 0;
        }





问题3:我是否需要修改每个复选框(1-6)的以下部分以允许复选框以确定持有与复选框关联的标签的天气是否可以发生?





Question 3: Do I need to modify the following sections for each checkBox (1-6) to allow the checkbox to determine weather holding the label associated with the checkbox can happen or not?

private void checkBox1_CheckChanged(object sender, EventArgs e)





无论如何。我会在那里停下来,非常感谢任何帮助。我期待你的回复。



我看到你的建议,这很有道理,但我不知道如何使用变量将它应用于我的特定游戏项目我已经到位了。你可以告诉我的技能需要一些抛光,所以这里是整个代码:





Anyway. I'll stop there, Any assistance would be greatly appreciated. I look forward to your replies.

I see what you have suggested and it makes sense but I was not sure how to apply it to my specific game project using the variables I already have in place. As you can tell my skills need some polishing so here is the whole code:

//Game structure code and Scoring Logic by RCEditor Sept, 2017. Thanks all for the assist in advance of any contribution to this project.
//Please add name or handle here to be credited with any additions. These comments are to remain with this source code indefinately.

#region Using Statements

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;
#endregion

namespace DiceGame
{         
    public partial class Form1 : Form
    {
        #region Declaration

        Image[] diceImages;
        int[] dice;
        int[] diceResults;
        Random rand;
                
        #endregion

        #region Initialization

        public Form1()
        {
            InitializeComponent();

        }              

        private void Form1_Load(object sender, EventArgs e)
        {            
            diceImages = new Image[7];
            diceImages[0] = Properties.Resources.BlankDice;
            diceImages[1] = Properties.Resources.Dice1;
            diceImages[2] = Properties.Resources.Dice2;
            diceImages[3] = Properties.Resources.Dice3;
            diceImages[4] = Properties.Resources.Dice4;
            diceImages[5] = Properties.Resources.Dice5;
            diceImages[6] = Properties.Resources.Dice6;

            dice = new int[6] { 0, 0, 0, 0, 0, 0 };

            diceResults = new int[6] { 0, 0, 0, 0, 0, 0 };

            rand = new Random();
            
        }

        public class Die
        {
            public bool SetState { get; set; }
            public int Value { get; set; }
        }

        #endregion

        #region Private Methods
                
        private void RollDice()
        {
            for (int i = 0; i < dice.Length; i++)
            {
                dice[i] = rand.Next(1, 6 + 1);

                switch (dice[i])
                {
                    case 1:
                        diceResults[0]++;
                        break;
                    case 2:
                        diceResults[1]++;
                        break;
                    case 3:
                        diceResults[2]++;
                        break;
                    case 4:
                        diceResults[3]++;
                        break;
                    case 5:
                        diceResults[4]++;
                        break;
                    case 6:
                        diceResults[5]++;
                        break;
                }
            }

            label1.Image = diceImages[dice[0]];
            label2.Image = diceImages[dice[1]];
            label3.Image = diceImages[dice[2]];
            label4.Image = diceImages[dice[3]];
            label5.Image = diceImages[dice[4]];
            label6.Image = diceImages[dice[5]];
            
        }
                

        private void btn_RollDice_Click(object sender, EventArgs e)
        {
            RollDice();
            
            GetResults();

            ResetResults();
        }

        private void GetResults()
        {
            bool oneThroughSix = false, allDoubles = false, threeOnes = false, threeOnesPlus = false,
                threeTwos = false, threeTwosPlus = false, threeThrees = false, threeThreesPlus = false, threeFours = false, threeFoursPlus = false,
                threeFives = false, threeFivesPlus = false, threeSixes = false, threeSixesPlus = false, aOne = false, aFive = false,
                haveOneDouble = false, haveTwoDoubles = false, aBust = false, haveSix = false, haveFive = false, haveFour = false, haveThree = false,
                haveTwo = false, haveOne = false;



            #region Scoring Logic
            // Look for 1-6 Bonus
            if (diceResults[0] == 1 &&
                        diceResults[1] == 1 &&
                        diceResults[2] == 1 &&
                        diceResults[3] == 1 &&
                        diceResults[4] == 1 &&
                        diceResults[5] == 1)
                oneThroughSix = true;

            for (int i = 0; i < diceResults.Length; i++)
            {
                // Look for All doubles Bonus
                if (diceResults[0] == 2 &&
                     diceResults[1] == 2 &&
                     diceResults[2] == 2)
                    allDoubles = true;
                else if (diceResults[1] == 2 &&
                     diceResults[2] == 2 &&
                     diceResults[3] == 2)
                    allDoubles = true;
                else if (diceResults[2] == 2 &&
                     diceResults[3] == 2 &&
                     diceResults[4] == 2)
                    allDoubles = true;
                else if (diceResults[3] == 2 &&
                     diceResults[4] == 2 &&
                     diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[4] == 2 &&
                     diceResults[3] == 2 &&
                     diceResults[1] == 2)
                    allDoubles = true;

                else if (diceResults[2] == 2 &&
                    diceResults[3] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[3] == 2 &&
                    diceResults[1] == 2 &&
                    diceResults[0] == 2)
                    allDoubles = true;
                else if (diceResults[3] == 2 &&
                    diceResults[4] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[4] == 2 &&
                    diceResults[5] == 2 &&
                    diceResults[1] == 2)
                    allDoubles = true;
                else if (diceResults[4] == 2 &&
                    diceResults[2] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[2] == 2 &&
                    diceResults[3] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[2] == 2 &&
                    diceResults[4] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[2] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[3] == 2 &&
                    diceResults[4] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[3] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[4] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[2] == 2 &&
                    diceResults[4] == 2 &&
                    diceResults[1] == 2)
                    allDoubles = true;

                else if (diceResults[5] == 2 &&
                    diceResults[0] == 2 &&
                    diceResults[1] == 2)
                    allDoubles = true;
                else if (diceResults[5] == 2 &&
                    diceResults[1] == 2 &&
                    diceResults[2] == 2)
                    allDoubles = true;
                else if (diceResults[3] == 2 &&
                    diceResults[5] == 2 &&
                    diceResults[1] == 2)
                    allDoubles = true;

                else if (diceResults[0] == 3)
                    threeOnes = true;
                else if (diceResults[0] == 4)
                    threeOnesPlus = true;
                else if (diceResults[0] == 5)
                    threeOnesPlus = true;

                else if (diceResults[5] == 3)
                    threeSixes = true;
                else if (diceResults[5] == 4)
                    threeSixesPlus = true;
                else if (diceResults[5] == 5)
                    threeSixesPlus = true;

                else if (diceResults[4] == 3)
                    threeFives = true;
                else if (diceResults[4] == 4)
                    threeFivesPlus = true;
                else if (diceResults[4] == 5)
                    threeFivesPlus = true;

                else if (diceResults[3] == 3)
                    threeFours = true;
                else if (diceResults[3] == 4)
                    threeFoursPlus = true;
                else if (diceResults[3] == 5)
                    threeFoursPlus = true;

                else if (diceResults[2] == 3)
                    threeThrees = true;
                else if (diceResults[2] == 4)
                    threeThreesPlus = true;
                else if (diceResults[2] == 5)
                    threeThreesPlus = true;

                else if (diceResults[1] == 3)
                    threeTwos = true;
                else if (diceResults[1] == 4)
                    threeTwosPlus = true;
                else if (diceResults[1] == 5)
                    threeTwosPlus = true;
                //score a 1
                else if (diceResults[0] == 1)
                    aOne = true;
                else if (diceResults[0] == 2)
                    aOne = true;
                //score a 5
                else if (diceResults[4] == 1)
                    aFive = true;

                //look for two doubles
                for (int j = 0; j < diceResults.Length; j++)
                {
                    if (diceResults[1] == 4)
                        haveTwoDoubles = true;
                    else if (diceResults[0] == 2 &&
                            diceResults[1] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[0] == 2 &&
                            diceResults[2] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[0] == 2 &&
                            diceResults[3] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[0] == 2 &&
                            diceResults[4] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[0] == 2 &&
                            diceResults[5] == 2)
                        haveTwoDoubles = true;                    
                    else if (diceResults[4] == 2 &&
                            diceResults[1] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[4] == 2 &&
                            diceResults[2] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[4] == 2 &&
                            diceResults[3] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[4] == 4)
                        haveTwoDoubles = true;
                    else if (diceResults[4] == 2 &&
                            diceResults[5] == 2)
                        haveTwoDoubles = true;
                }

                // look for doubles
                if (diceResults[0] == 2)
                    haveOneDouble = true;
                else if (diceResults[4] == 2)
                    haveOneDouble = true;

                                        // look for Bust
                else if (diceResults[1] == 1)
                    aBust = true;
                else if (diceResults[2] == 1)
                    aBust = true;
                else if (diceResults[3] == 1)
                    aBust = true;
                else if (diceResults[5] == 1)
                    aBust = true;

            }

            for (int i = 0; i < dice.Length; i++)
            {
                switch (dice[i])
                {
                    case 6:
                        haveSix = true;
                        break;
                    case 5:
                        haveFive = true;
                        break;
                    case 4:
                        haveFour = true;
                        break;
                    case 3:
                        haveThree = true;
                        break;
                    case 2:
                        haveTwo = true;
                        break;
                    case 1:
                        haveOne = true;
                        break;
                }

 #endregion

 #region Message Output
            }

            if (oneThroughSix)
                lbl_displayResults.Text = "One through Six is worth 2000 points!";
            else if (allDoubles)
                lbl_displayResults.Text = "All Doubles are worth 1500 points!";
            else if (threeOnes)
                lbl_displayResults.Text = "Three Ones are worth 1000 points!";
            else if (threeOnesPlus)
                lbl_displayResults.Text = "Three Ones are worth 1000 points and remain consecutive at that value until next roll.";
            else if (threeSixes)
                lbl_displayResults.Text = "Three Sixes are worth 600 points!";
            else if (threeSixesPlus)
                lbl_displayResults.Text = "Three Sixes are worth 600 points and remain consecutive at that value until next roll.";
            else if (threeFives)
                lbl_displayResults.Text = "Three Fives are worth 500 points!";
            else if (threeFivesPlus)
                lbl_displayResults.Text = "Three Fives are worth 500 points and remain consecutive at that value until next roll.";
            else if (threeFours)
                lbl_displayResults.Text = "Three Fours are worth 400 points!";
            else if (threeFoursPlus)
                lbl_displayResults.Text = "Three Fours are worth 400 points and remain consecutive at that value until next roll.";
            else if (threeThrees)
                lbl_displayResults.Text = "Three Threes are worth 300 points!";
            else if (threeThreesPlus)
                lbl_displayResults.Text = "Three Threes are worth 300 points and remain consecutive at that value until next roll.";
            else if (threeTwos)
                lbl_displayResults.Text = "Three Twos are worth 200 points!";
            else if (threeTwosPlus)
                lbl_displayResults.Text = "Three Twos are worth 200 points and remain consecutive at that value until next roll.";
            else if (aOne)
                lbl_displayResults.Text = "Ones are always worth 100 points!";
            else if (aFive)
                lbl_displayResults.Text = "Fives are always worth 50 points!";
            else if (haveTwoDoubles)
                lbl_displayResults.Text = "Two Doubles only score if 1's or 5's\r\n(1's = 100 and 5's = 50)";
            else if (haveOneDouble)
                lbl_displayResults.Text = "Doubles only score if 1's or 5's\r\n(1's = 100 and 5's = 50)";
            else if (aBust)
                lbl_displayResults.Text = "You were too GREEDY!!!\r\nNothing scored!\r\n-GAME OVER-";
        }


        private void ResetResults()
        {            
            for (int i = 0; i < diceResults.Length; i++)
                diceResults[i] = 0;
        }

        #endregion
        

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void lbl_displayResults_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
 }
#endregion





What I have tried:



I have tried assigning each checkbox a boolean value of false and having it become true when checked but could not fill in the gap to associate this action with holding the checked dice before next re-roll. Seems like it should work but I am unsure where to have the result of the checkbox being selected or not in the code to make sure it does not roll those selected when the



What I have tried:

I have tried assigning each checkbox a boolean value of false and having it become true when checked but could not fill in the gap to associate this action with holding the checked dice before next re-roll. Seems like it should work but I am unsure where to have the result of the checkbox being selected or not in the code to make sure it does not roll those selected when the

btn_RollDice_Clicked

AKA The Roll Button variable is next clicked.



I have a couple more snags I have run into so far but I will ask those in a later post if still unclear after the community here puts its heads together to provide a working solution!



Anyway.... Chat with you soon!



And Thanks in advance for taking the time to look over what I have posted here.



-Regards

AKA The Roll Button variable is next clicked.

I have a couple more snags I have run into so far but I will ask those in a later post if still unclear after the community here puts its heads together to provide a working solution!

Anyway.... Chat with you soon!

And Thanks in advance for taking the time to look over what I have posted here.

-Regards

推荐答案

There are many ways of doing this. The simplest is a Die (single Dice) class that holds the value and the set state. Something like:

There are many ways of doing this. The simplest is a Die (single Dice) class that holds the value and the set state. Something like:
public class Die
{
    public bool SetState { get; set; }
    public int Value {get; set; }
}



And hold the Die in a Dice collection:


And hold the Die in a Dice collection:

public List<Die> Dice { get; } = new List<Die>
{
   new Die(),
   new Die(),
   new Die(),
   new Die(),
   new Die(),
   new Die()
}



And when it is time to roll:


And when it is time to roll:

foreach(var die in Dice.Where(x => !x.SetState))
{
   // we can roll the die that is not set...
}



As you the check image, set the PictureBox (check image) Visible property dependant on the SetState property of the Die class.


As you the check image, set the PictureBox (check image) Visible property dependant on the SetState property of the Die class.


这篇关于如何使用C#将visual复选框链接到visual studio 2010中的图像/随机结果.....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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