匹配游戏,使照片在C#中匹配

时间:2015-04-22 17:12:08

标签: c# arrays matching

我一直在用C#制作卡片匹配游戏。它使用图片阵列设置了12张卡片(每张照片中有2张,所以总共6张照片),当你按下卡片时,它会通过使用另一个数组(数字为1的int数组)找出照片是否匹配-6重复两次,与照片相同)我使用此代码随机化数字

            Image away;
            int tagger;
            for (int i = 1; i < 13; i++)
            {
                cards[i].Visible = true;
                away = pics[i];
                tagger = tags[i];

                int h = random.Next(1, 6);
                pics[i] = pics[h];
                tags[i] = tags[h];
                pics[h] = away;
                tags[h] = tagger;
            }
            for (int i = 1; i < 13; i++)
            {
                cards[i].Image = pics[i];
            }

它适用于大约4场比赛,然后它说其余的比赛是错误的......有人可以帮忙吗?

这是我的其余代码:

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;
using System.Threading;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
         int a = 0;
         PictureBox card = null;
         Image[] pics = new Image[13];
         int[] tags = new int[13];
    PictureBox[] cards = new PictureBox[13];
    Random random = new Random();
    public Form1()
    {
        InitializeComponent();
        pics[1] = Image.FromFile(@"h:\profile\desktop\game\photos\g1.jpg");
        pics[2] = Image.FromFile(@"h:\profile\desktop\game\photos\g1.jpg");
        pics[3] = Image.FromFile(@"h:\profile\desktop\game\photos\g2.jpg");
        pics[4] = Image.FromFile(@"h:\profile\desktop\game\photos\g2.jpg");
        pics[5] = Image.FromFile(@"h:\profile\desktop\game\photos\g3.jpg");
        pics[6] = Image.FromFile(@"h:\profile\desktop\game\photos\g3.jpg");
        pics[7] = Image.FromFile(@"h:\profile\desktop\game\photos\g4.jpg");
        pics[8] = Image.FromFile(@"h:\profile\desktop\game\photos\g4.jpg");
        pics[9] = Image.FromFile(@"h:\profile\desktop\game\photos\g5.jpg");
        pics[10] = Image.FromFile(@"h:\profile\desktop\game\photos\g5.jpg");
        pics[11] = Image.FromFile(@"h:\profile\desktop\game\photos\g6.jpg");
        pics[12] = Image.FromFile(@"h:\profile\desktop\game\photos\g6.jpg");
        tags[1] = 1;
        tags[2] = 1;
        tags[3] = 2;
        tags[4] = 2;
        tags[5] = 3;
        tags[6] = 3;
        tags[7] = 4;
        tags[8] = 4;
        tags[9] = 5;
        tags[10] = 5;
        tags[11] = 6;
        tags[12] = 6;

        cards[1] = pictureBox1;
        cards[2] = pictureBox2;
        cards[3] = pictureBox3;
        cards[4] = pictureBox4;
        cards[5] = pictureBox8;
        cards[6] = pictureBox7;
        cards[7] = pictureBox6;
        cards[8] = pictureBox5;
        cards[9] = pictureBox9;
        cards[10] = pictureBox10;
        cards[11] = pictureBox11;
        cards[12] = pictureBox12;
    }

    private void click_card(object sender, EventArgs e)
    {
        PictureBox pic = sender as PictureBox;
        string s = pic.Name;
        string s1 = s.Substring(10);
        int num = int.Parse(s1);
        if (card == null)
        {
            card = pic;
            a = num;
        }
        else if (a > 0)
        {
            int one = tags[a];
            int two = tags[num];
            if (one == two)
            {
                System.Windows.Forms.MessageBox.Show("Correct!");
                a = 0;
                pic.Visible = false;
                card.Visible = false;
                card = null;
            }
            else if (one != two)
            {
                System.Windows.Forms.MessageBox.Show("Wrong!");
                card = null;
                a = 0;
            }
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
            Image away;
            int tagger;
            for (int i = 1; i < 13; i++)
            {
                cards[i].Visible = true;
                away = pics[i];
                tagger = tags[i];

                int h = random.Next(1, 6);
                pics[i] = pics[h];
                tags[i] = tags[h];
                pics[h] = away;
                tags[h] = tagger;
            }
            for (int i = 1; i < 13; i++)
            {
                cards[i].Image = pics[i];
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

评论你的&#34; shuffler&#34;。

如果牌是你的数组,那么你的for循环应该从0开始。数组基于零。你有12张牌,所以你有0到11个索引。然后你应该使用数组的Length属性来控制你的for循环。

当你使用random.next(1,6)时,你只能从1到5生成一个随机数。索引0&amp; 6 - 11,永远不会被生成,所以永远不会被洗牌。

最后,我不明白为什么你需要额外的for循环,只需在交换后添加Image分配到for循环的底部。

Image away;
int tagger;
for (int i = 1; i < cards.Length; i++)
{
    cards[i].Visible = true;
    away = pics[i];
    tagger = tags[i];

    int h = random.Next(1, cards.Length + 1);
    pics[i] = pics[h];
    tags[i] = tags[h];
    pics[h] = away;
    tags[h] = tagger;

    cards[i].Image = pics[i];
}