You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# 如何基于两个ListBox实现配对游戏并解决重复项问题

完整实现代码

public partial class game : Form
{
    // 原始数字和动物的正确对应关系(按索引一一对应)
    public static List<string> animals = new List<string> { "cat", "dog", "bird", "frog", "snake", "duck", "tiger" };
    public static List<string> values = new List<string> { "400", "200", "300", "800", "100", "500", "600" };

    // 运行时用到的全局变量
    private Dictionary<string, string> _correctPairs = new Dictionary<string, string>();
    private string _selectedNumber = null;
    private int _correctCount = 0;
    private const int TotalPairs = 4; // 总配对题数

    public game()
    {
        InitializeComponent();
    }

    // 开始游戏按钮点击事件
    private void button1_Click(object sender, EventArgs e)
    {
        // 重置所有状态
        listBox1.Items.Clear();
        listBox2.Items.Clear();
        _correctPairs.Clear();
        _selectedNumber = null;
        _correctCount = 0;

        Random random = new Random();

        // 第一步:随机选4组不重复的正确配对
        List<int> availableIndexs = Enumerable.Range(0, values.Count).ToList();
        for (int i = 0; i < TotalPairs; i++)
        {
            int randomIndex = random.Next(availableIndexs.Count);
            int targetIndex = availableIndexs[randomIndex];
            // 存入正确配对字典
            _correctPairs.Add(values[targetIndex], animals[targetIndex]);
            // 把选过的索引移除,避免重复
            availableIndexs.RemoveAt(randomIndex);
            // 数字加入左侧ListBox
            listBox1.Items.Add(values[targetIndex]);
        }

        // 第二步:生成右侧ListBox的选项(4个正确动物+3个随机干扰动物,无重复)
        List<string> rightItems = _correctPairs.Values.ToList();
        // 选剩下的3个动物作为干扰项
        List<string> remainAnimals = animals.Except(_correctPairs.Values).ToList();
        rightItems.AddRange(remainAnimals);
        // 打乱右侧选项顺序
        rightItems = rightItems.OrderBy(x => random.Next()).ToList();
        // 加入右侧ListBox
        listBox2.Items.AddRange(rightItems.ToArray());
    }

    // 左侧数字ListBox选中事件
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem != null)
        {
            _selectedNumber = listBox1.SelectedItem.ToString();
        }
    }

    // 右侧动物ListBox选中事件
    private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        // 还没选左侧数字的话不处理
        if (_selectedNumber == null || listBox2.SelectedItem == null)
            return;

        string selectedAnimal = listBox2.SelectedItem.ToString();
        // 比对是否匹配
        if (_correctPairs[_selectedNumber] == selectedAnimal)
        {
            _correctCount++;
            // 匹配正确,移除已配对的两个项
            listBox1.Items.Remove(_selectedNumber);
            listBox2.Items.Remove(selectedAnimal);
        }
        else
        {
            MessageBox.Show("匹配错误,请重新选择");
            // 取消选中状态
            listBox1.ClearSelected();
            listBox2.ClearSelected();
        }

        // 重置选中的数字
        _selectedNumber = null;

        // 所有配对完成,展示结果
        if (listBox1.Items.Count == 0)
        {
            MessageBox.Show($"游戏结束!你一共答对了 {_correctCount}/{TotalPairs} 题");
        }
    }
}

核心逻辑说明

  • 重复项问题解决:抽取选项时维护了临时可用索引/可用项列表,每次抽取后移除已选中的项,从根源避免重复值出现
  • 配对绑定实现:用Dictionary<string, string>存储数字与动物的对应关系,天然保证一一映射,匹配时直接取值比对即可
  • 点击匹配逻辑:先记录用户选中的左侧数字,待选中右侧动物后直接和字典中对应值比对,符合即为答对
  • 得分统计逻辑:每匹配正确一次计数+1,左侧ListBox项全部清空即代表所有配对完成,弹出最终得分

注意事项

需要在窗体设计界面,分别双击两个ListBox控件,将生成的默认事件方法与上面的listBox1_SelectedIndexChanged、listBox2_SelectedIndexChanged逻辑对应即可正常运行。

内容的提问来源于stack exchange,提问作者Bavish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 10:45:08