开发者

Generating Random Paired Images in C#

开发者 https://www.devze.com 2022-12-23 08:43 出处:网络
im trying create cards matching game. normally these type of games they match paired cards together (with the same file name \"A.jpg with A.jpg\")

im trying create cards matching game. normally these type of games they match paired cards together (with the same file name "A.jpg with A.jpg")

but in my case, im matching cards with different names "B.jpg with A.jpg" (correct), "C.jpg with D.jpg" (correct) but with "B.jpg with C.jpg" (incorrect answer).

A.jpg-B.jpg <--correct

C.jpg-D.jpg <--correct

E.jpg-F.jpg <--correct

i face a problem when i generate the cards开发者_运维技巧 in random. I manage to generate random cards but i dont manage to generate it with their paired onces. Below is an illustration of the problem

A.jpg-B.jpg <--correct

C.jpg-F.jpg <--incorrect

so how should i code it so that it always generate with their paired onces, so that my game can proceed?


This reminds me of Dijkstra Parable: looks like the best and simplest option here is to do it in two steps:

  1. Generate all matching pairs.
  2. Pick random pairs, from a a list of already valid pairs.


Well, you could see if two cards match like this: A = 0, B=1, C=2, D=3,...

card1 = 0;
card2 = 1;

//Match?
if((card1%2 == 0 && card2 == (card1 + 1)) ||
   (card1%2 == 1 && card1 == (card2 + 1)))
  return true;//Match!
0

精彩评论

暂无评论...
验证码 换一张
取 消