script.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var arr = new Array; // Array of free slots
  2. var items = new Array;
  3. arr.length = 9;
  4. var canAct = false;
  5. var isII = false;
  6. function step(x,y) {
  7. if (!canAct) return
  8. var component = Qt.createComponent("Xor.qml");
  9. if (component.status == Component.Ready) {
  10. // Find clicked square indexes
  11. var i = x/100
  12. var j = y/90
  13. var newX = 0
  14. var newY = 0
  15. if (i>0 && i<1) { newX = 0; i = 0 }
  16. else if (i>1 && i<2) { newX = 100; i = 1; }
  17. else if (i>2 && i<3) { newX = 200; i = 2; }
  18. if (j>2 && j<3) { newY = 180; j = 2 }
  19. else if (j>0 && j<1) { newY = 0; j = 0; }
  20. else if (j>1 && j<2) { newY = 90; j = 1; }
  21. var ind = i*3 + j
  22. if (arr[ind] != 0)
  23. return;
  24. var newItem = component.createObject(container);
  25. if (tick)
  26. newItem.source = "o.png"
  27. newItem.index = ind
  28. arr[ind] = tick? -1 : 1;
  29. tick = !tick
  30. items.push(newItem)
  31. newItem.x = newX + 90
  32. newItem.y = newY + 5
  33. newItem.opacity = 1
  34. checkState()
  35. if (isII)
  36. computerStep()
  37. }
  38. }
  39. function computerStep()
  40. {
  41. if (!canAct) return
  42. var step = -1;
  43. while(true)
  44. {
  45. step = Math.round(Math.random()*10)
  46. if (step>8) step = 8
  47. if (arr[step] == 0)
  48. break;
  49. }
  50. var compJ = step%3
  51. var compI = (step - compJ) / 3
  52. var comp = Qt.createComponent("Xor.qml")
  53. if (comp.status == Component.Ready)
  54. {
  55. var compItem = comp.createObject(container);
  56. var newX = 0
  57. var newY = 0
  58. if (compI == 0) { newX = 0 }
  59. else if (compI == 1) { newX = 100 }
  60. else if (compI == 2) { newX = 200 }
  61. if (compJ == 2) { newY = 180 }
  62. else if (compJ == 0) { newY = 0 }
  63. else if (compJ == 1) { newY = 90 }
  64. if (tick)
  65. compItem.source = "o.png"
  66. arr[step] = tick? -1 : 1;
  67. tick = !tick
  68. compItem.index = step
  69. items.push(compItem);
  70. compItem.y = newY + 5
  71. compItem.x = newX + 90
  72. compItem.opacity = 1
  73. checkState()
  74. }
  75. }
  76. function newGame(onII)
  77. {
  78. isII = onII;
  79. console.log("In new game!")
  80. tick = false
  81. canAct = true
  82. for (var i=0; i<9; i++)
  83. {
  84. arr[i] = 0
  85. }
  86. for (var i=0; i<items.length; i++)
  87. items[i].destroy()
  88. items.length = 0
  89. console.log(colorButton)
  90. colorButton = false
  91. }
  92. function checkState()
  93. {
  94. if (compare(arr[0], arr[1], arr[2])) { hightlightWinner(0,1,2,arr[1]); canAct = false; return }
  95. else if (compare(arr[3], arr[4], arr[5])) { hightlightWinner(3,4,5,arr[4]); canAct = false; return }
  96. else if (compare(arr[6], arr[7], arr[8])) { hightlightWinner(6,7,8,arr[7]); canAct = false; return }
  97. else if (compare(arr[0], arr[3], arr[6])) { hightlightWinner(0,3,6,arr[3]); canAct = false; return }
  98. else if (compare(arr[1], arr[4], arr[7])) { hightlightWinner(1,4,7,arr[4]); canAct = false; return }
  99. else if (compare(arr[2], arr[5], arr[8])) { hightlightWinner(2,5,8,arr[5]); canAct = false; return }
  100. else if (compare(arr[0], arr[4], arr[8])) { hightlightWinner(0,4,8,arr[4]); canAct = false; return }
  101. else if (compare(arr[6], arr[4], arr[2])) { hightlightWinner(6,4,2,arr[4]); canAct = false; return }
  102. var anyStepsAvailable = false;
  103. for (var i=0; i<arr.length; i++)
  104. if (arr[i] == 0)
  105. anyStepsAvailable = true;
  106. if (!anyStepsAvailable)
  107. {
  108. canAct = false
  109. colorButton = true
  110. }
  111. }
  112. function hightlightWinner(a, b, c, tm)
  113. {
  114. for(var i=0; i<items.length;i++)
  115. {
  116. var v = items[i].index
  117. if (v == a || v == b || v == c)
  118. {
  119. if (tm == 1)
  120. items[i].source = "xr.png"
  121. else items[i].source = "or.png"
  122. }
  123. }
  124. colorButton = true
  125. }
  126. function compare(a,b,c)
  127. {
  128. if (a == 0 || b == 0 || c == 0) return false
  129. if ((a==b)&&(b==c)&&(c==a)) return true
  130. }