📜  多米诺斯坦 c# (1)

📅  最后修改于: 2023-12-03 15:37:51.908000             🧑  作者: Mango

多米诺斯坦 C#

简介

多米诺斯坦是一款使用 C# 编写的益智游戏。游戏规则是在一个矩形方格中放置若干个骨牌,每个骨牌都有两个数字(0 - 6),一个数字在骨牌的上面,一个数字在骨牌的下面,数字相同的骨牌被视为相同的骨牌。

游戏开始前会生成一组数字序列,玩家需要根据序列摆放骨牌,使得相邻骨牌的相同数字相连。如果成功,游戏胜利。

开发环境
  • C# 编程语言
  • .NET Framework 4.0 或更高版本
项目结构
  • Domino.cs: 骨牌类
  • Dominoes.cs: 骨牌序列类
  • Form1.cs: 游戏界面类
  • Program.cs: 游戏入口
示例代码
骨牌类
public class Domino
{
    private int _upper;
    private int _lower;

    public Domino(int upper, int lower)
    {
        _upper = upper;
        _lower = lower;
    }

    public int Upper
    {
        get { return _upper; }
    }

    public int Lower
    {
        get { return _lower; }
    }
}
骨牌序列类
public class Dominoes
{
    private List<Domino> _dominoes;

    public Dominoes(IEnumerable<Domino> dominoes)
    {
        _dominoes = new List<Domino>(dominoes);
    }

    public bool IsValidSequence(int[] seq)
    {
        if (seq.Length != _dominoes.Count) return false;

        int last = -1;
        foreach (int i in seq)
        {
            if (i < 0 || i >= _dominoes.Count) return false;

            if (last != -1 && _dominoes[last].Lower != _dominoes[i].Upper) return false;

            last = i;
        }

        return true;
    }
}
游戏界面类
public partial class Form1 : Form
{
    private Dominoes _dominoes;
    private int[] _sequence;
    private int _selectedIndex;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        _dominoes = new Dominoes(new Domino[] {
            new Domino(0, 0),
            new Domino(0, 1),
            new Domino(0, 2),
            new Domino(0, 3),
            new Domino(0, 4),
            new Domino(0, 5),
            new Domino(0, 6),
            // ...
        });

        _sequence = new int[_dominoes.Count];
        _selectedIndex = -1;

        UpdateDominoes();
        UpdateSequence();
    }

    private void UpdateDominoes()
    {
        listBox1.DataSource = null;
        listBox1.DataSource = _dominoes;
        listBox1.DisplayMember = "DisplayString";
    }

    private void UpdateSequence()
    {
        textBox1.Text = String.Join(", ", _sequence);
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        _selectedIndex = listBox1.SelectedIndex;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (_selectedIndex != -1)
        {
            _sequence[_selectedIndex] = (_sequence[_selectedIndex] + 1) % _dominoes.Count;
            UpdateSequence();
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (_selectedIndex != -1)
        {
            _sequence[_selectedIndex] = (_sequence[_selectedIndex] + _dominoes.Count - 1) % _dominoes.Count;
            UpdateSequence();
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (_dominoes.IsValidSequence(_sequence))
        {
            MessageBox.Show("Congratulations, you win!");
        }
        else
        {
            MessageBox.Show("Sorry, the sequence is invalid.");
        }
    }
}
游戏入口
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
参考资料