arrays - Matching Game, Making the photos match in C# -
i have been making card matching game in c#. has 12 card set (2 of each photo 6 photos in total) using picture array, , when press cards finds out if photos match using array (a int array numbers 1-6 repeated twice, same photos) use code randomize numbers
image away; int tagger; (int = 1; < 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; } (int = 1; < 13; i++) { cards[i].image = pics[i]; }
it works 4 of matches, says matches wrong rest... can help?
here rest of code:
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 = 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 picturebox; string s = pic.name; string s1 = s.substring(10); int num = int.parse(s1); if (card == null) { card = pic; = num; } else if (a > 0) { int 1 = tags[a]; int 2 = tags[num]; if (one == two) { system.windows.forms.messagebox.show("correct!"); = 0; pic.visible = false; card.visible = false; card = null; } else if (one != two) { system.windows.forms.messagebox.show("wrong!"); card = null; = 0; } } } private void button1_click(object sender, eventargs e) { image away; int tagger; (int = 1; < 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; } (int = 1; < 13; i++) { cards[i].image = pics[i]; } } } }
to comment on "shuffler".
if cards array, loop should start @ 0. arrays 0 based. have 12 cards, you'll 0 - 11 indexes. should use length property of array control loop.
when use random.next(1, 6), you're generating random number 1 - 5. indexes 0 & 6 - 11, never being generated, never being shuffled.
lastly, don't see why need loop, add assigning of image bottom of loop after swapping.
image away; int tagger; (int = 1; < 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]; }
Comments
Post a Comment