memorygame.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. var board = new Array(0)
  5. var cardSource = "card.qml";
  6. var component; // will hold the card.qml which will be used to instantiate cards
  7. var card1 // will hold the first selected card
  8. var card2 // will hold the second selected card
  9. // Function used to sort the array to random order
  10. function randOrd() {
  11. return (Math.round(Math.random())-0.5);
  12. }
  13. function createCards() {
  14. if(component.status == Component.Error) {
  15. console.log("Error creating card component");
  16. console.log(component.errorString())
  17. return false;
  18. }
  19. for(var i=0; i<cards; i++) {
  20. var dynamicObject = component.createObject(game);
  21. if(dynamicObject == null) {
  22. print("Error creating card");
  23. return false;
  24. }
  25. dynamicObject.number = Math.floor(i / 2 + 1)
  26. board[i] = dynamicObject
  27. }
  28. board.sort(randOrd);
  29. for(var i=0; i<cards; i++) {
  30. board[i].index = i
  31. board[i].parent = game
  32. board[i].z = 1
  33. board[i].born()
  34. }
  35. return true;
  36. }
  37. // Creates new card objects and reparents them to the grid
  38. // The cards are shuffle
  39. function initBoard(cards) {
  40. for(var i=0; i<board.length; i++) {
  41. if(board[i] != null) {
  42. board[i].destroy();
  43. }
  44. }
  45. board = new Array(cards)
  46. if(component == null) {
  47. component = Qt.createComponent(cardSource);
  48. //component = createComponent(cardSource);
  49. if(component.status == Component.Loading) {
  50. component.statusChanged.connect(createCards)
  51. }
  52. else {
  53. createCards()
  54. }
  55. }
  56. else {
  57. createCards()
  58. }
  59. }
  60. function gameEndCheck() {
  61. var allVanished = true;
  62. for(var i=0; i<board.length;i++) {
  63. if(board[i].vanished == false) {
  64. allVanished = false;
  65. break;
  66. }
  67. }
  68. if(allVanished == true) {
  69. gameStarted = false
  70. for(var i=0; i<board.length; i++) {
  71. board[i].die();
  72. }
  73. }
  74. }
  75. function selectCard(index) {
  76. var card = board[index]
  77. if(card1 == null) {
  78. card1 = card;
  79. card.turned = true
  80. }
  81. else if(card2 == null) {
  82. if(card == card1) {
  83. // don't allow user select same card twice
  84. return
  85. }
  86. card2 = card;
  87. card.turned = true
  88. }
  89. else {
  90. card1.turned = false
  91. card2.turned = false
  92. card1 = null;
  93. card2 = null;
  94. return
  95. }
  96. if((card1 != null) && (card2 != null)) {
  97. if(card1.number == card2.number) {
  98. card1.vanish()
  99. card2.vanish()
  100. card1 = null;
  101. card2 = null;
  102. gameEndCheck()
  103. }
  104. }
  105. }