骰子传奇

Posted BenjaminBardon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了骰子传奇相关的知识,希望对你有一定的参考价值。

我觉得题目难度还行,计划用40分钟做出来

想通过可视化界面去展现骰子随机的结果和更良好的交互体验

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DiceGanme
{
    public partial class Form1 : Form
    {

        int dice = 0, dice2 = 0;

        PictureBox[] picturebox = new PictureBox[4];
       
        Dice ran = new Dice();


        public Form1()
        {
            InitializeComponent();

            picturebox[0] = pictureBox2;
            picturebox[1] = pictureBox3;
            picturebox[2] = pictureBox4;
            picturebox[3] = pictureBox5;

        }

        private void 一个ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox2.Visible = true;
            pictureBox3.Visible = false;
            pictureBox4.Visible = false;
            pictureBox5.Visible = false;

            dice = 1;
        }

        private void 两个ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox2.Visible = true;
            pictureBox3.Visible = true;
            pictureBox4.Visible = false;
            pictureBox5.Visible = false;

            dice = 2;
        }

        private void 三个ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox2.Visible = true;
            pictureBox3.Visible = true;
            pictureBox4.Visible = true;
            pictureBox5.Visible = false;

            dice = 3;
        }

        private void 四个ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox2.Visible = true;
            pictureBox3.Visible = true;
            pictureBox4.Visible = true;
            pictureBox5.Visible = true;

            dice = 4;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (dice2 = dice; dice2 > 0; dice2--)
            {
                picturebox[dice2 - 1].Image = Image.FromFile("C:\\\\Users\\\\Benjamin\\\\documents\\\\visual studio 2017\\\\Projects\\\\DiceGanme\\\\DiceGanme\\\\Resources\\\\timg1.gif");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (dice2 = dice; dice2 > 0; dice2--)
            {
                picturebox[dice2-1].Image = Image.FromFile(Arithmetic.DiceJudge(ran.DiceCreat()));
            }
        }
    }

}
Form1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiceGanme
{
    class Arithmetic
    {
        public static string DiceJudge(int dice)
        {
            string filename=null;
            switch (dice)
            {
                case 1: filename = "C:\\\\Users\\\\Benjamin\\\\documents\\\\visual studio 2017\\\\Projects\\\\DiceGanme\\\\DiceGanme\\\\Resources\\\\1.png";break;
                case 2: filename = "C:\\\\Users\\\\Benjamin\\\\documents\\\\visual studio 2017\\\\Projects\\\\DiceGanme\\\\DiceGanme\\\\Resources\\\\2.png"; break;
                case 3: filename = "C:\\\\Users\\\\Benjamin\\\\documents\\\\visual studio 2017\\\\Projects\\\\DiceGanme\\\\DiceGanme\\\\Resources\\\\3.png"; break;
                case 4: filename = "C:\\\\Users\\\\Benjamin\\\\documents\\\\visual studio 2017\\\\Projects\\\\DiceGanme\\\\DiceGanme\\\\Resources\\\\4.png"; break;
                case 5: filename = "C:\\\\Users\\\\Benjamin\\\\documents\\\\visual studio 2017\\\\Projects\\\\DiceGanme\\\\DiceGanme\\\\Resources\\\\5.png"; break;
                case 6: filename = "C:\\\\Users\\\\Benjamin\\\\documents\\\\visual studio 2017\\\\Projects\\\\DiceGanme\\\\DiceGanme\\\\Resources\\\\6.png"; break;
            }
            return filename;
        }
    }
}
Arithmetic
using System;
using System.Collections.Generic;
using Sysusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiceGanme
{
    class Dice
    {
        private Random dice = new Random();

        public int DiceCreat()
        {
            int x = dice.Next(1, 6);
            return x;
        }
    }
}tem.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiceGanme
{
    class Dice
    {
        private Random dice = new Random();

        public int DiceCreat()
        {
            int x = dice.Next(1, 6);
            return x;
        }
    }
}
Dice

From1主要是界面的代码段,包括在按钮点击事件中调用Arithmetic中判断返回贴图的方法,以及Dice返回随机数的方法。

Arithmetic中有一个静态方法来判断当前是什么数值,并返回相应的贴图

Dice则返回一个随机数

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

和自己预估的时间差不多,主要耗费在贴图的返回算法,和界面各个按钮之间的关系协调上。

点击一个按钮

点击两个按钮

点击三个按钮

点击四个按钮

点击停止

点击重新开始

 

以上是关于骰子传奇的主要内容,如果未能解决你的问题,请参考以下文章

编程算法 - n个骰子的点数(递归) 代码(C)

数学与物理故事相关链接

《剑指Offer——丑数,n个骰子的点数》代码

和12岁小同志搞创客开发:手撕代码,做一款数字骰子

python掷骰子游戏

如何在 y>4 时停止此代码骰子游戏?