main.qml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. import QtQuick 2.11
  26. import QtQuick.Window 2.11
  27. import QtQuick.Controls 2.12
  28. import QtQuick.Controls.Styles 1.0
  29. import snakesimulator 1.0
  30. ApplicationWindow {
  31. id: root
  32. visible: true
  33. property int tileSize: 20
  34. width: field.width*tileSize + sideBar.width
  35. height: field.height*tileSize
  36. Rectangle {
  37. color: "#565656"
  38. anchors.fill: parent
  39. }
  40. Repeater {
  41. model: snake.pointsData.length
  42. Rectangle {
  43. color: "#ffffff"
  44. x: snake.pointsData[model.index].x*tileSize
  45. y: snake.pointsData[model.index].y*tileSize
  46. width: tileSize
  47. height: tileSize
  48. }
  49. }
  50. Rectangle {
  51. color: "#99ee99"
  52. x: field.food.x*tileSize
  53. y: field.food.y*tileSize
  54. width: tileSize
  55. height: tileSize
  56. }
  57. Rectangle {
  58. id: sideBar
  59. width: speedControl.width + contentColumn.anchors.margins*2
  60. color: "#000000"
  61. anchors {
  62. right: parent.right
  63. top: parent.top
  64. bottom: parent.bottom
  65. }
  66. Column {
  67. id: contentColumn
  68. anchors.fill: parent
  69. anchors.margins: 10
  70. spacing: 10
  71. Text {
  72. font.pointSize: 14
  73. font.weight: Font.Bold
  74. color: "#ffffff"
  75. text: "Generation: " + stats.generation
  76. }
  77. Text {
  78. font.pointSize: 14
  79. font.weight: Font.Bold
  80. color: "#ffffff"
  81. text: "Individual: " + stats.individual
  82. }
  83. Text {
  84. font.pointSize: 14
  85. font.weight: Font.Bold
  86. color: "#ffffff"
  87. text: "Move: " + stats.move
  88. }
  89. Text {
  90. font.pointSize: 14
  91. font.weight: Font.Bold
  92. color: "#ffffff"
  93. text: "Speed: " + (speedControl.value > 10 ? "\u221e" : speedControl.value)
  94. }
  95. Slider {
  96. id: speedControl
  97. value: 5
  98. from: 1
  99. stepSize: 1.0
  100. to: 11
  101. snapMode: Slider.SnapAlways
  102. onValueChanged: {
  103. changeTimer.restart()
  104. }
  105. background: Rectangle {
  106. x: speedControl.leftPadding
  107. y: speedControl.topPadding + speedControl.availableHeight / 2 - height / 2
  108. implicitWidth: 200
  109. implicitHeight: 4
  110. width: speedControl.availableWidth
  111. height: implicitHeight
  112. radius: 2
  113. color: "#bdbebf"
  114. Rectangle {
  115. x: 0
  116. width: speedControl.position * parent.width
  117. height: parent.height
  118. color: speedControl.value <= 10 ? "lightgreen" : "#003b6f"
  119. radius: 2
  120. }
  121. }
  122. Timer {
  123. id: changeTimer
  124. repeat: false
  125. interval: 200
  126. onTriggered: {
  127. client.setSpeed(speedControl.value > 10 ? 0 : speedControl.value)
  128. }
  129. }
  130. }
  131. }
  132. // it will play best in the loop afte naturel selection finished
  133. CheckBox {
  134. id: playBestCheckbox
  135. anchors.margins: 10
  136. anchors.bottom: bestBtn.top
  137. anchors.right: parent.right
  138. width: indicator.width + 170
  139. checked: true
  140. onCheckedChanged: {
  141. client.playBestInLoop(playBestCheckbox.checked)
  142. }
  143. contentItem: Text {
  144. id: text
  145. color: "white"
  146. text: "Repeat Best"
  147. wrapMode: Text.NoWrap
  148. anchors.left: playBestCheckbox.indicator.right
  149. anchors.leftMargin: 10
  150. anchors.right: undefined
  151. verticalAlignment: Text.AlignVCenter
  152. }
  153. Component.onCompleted: {
  154. client.playBestInLoop(playBestCheckbox.checked)
  155. }
  156. }
  157. Button {
  158. id: bestBtn
  159. anchors.margins: 10
  160. anchors.bottom: parent.bottom
  161. anchors.right: parent.right
  162. width: 100
  163. height: 50
  164. Rectangle {
  165. anchors.fill: parent
  166. enabled: isPlaying.state
  167. color: isPlaying.state ? "#003b6f" : "lightgreen"
  168. }
  169. Text {
  170. anchors.centerIn: parent
  171. horizontalAlignment: Text.AlignVCenter
  172. text: "Play best"
  173. }
  174. onClicked: {
  175. client.playBest()
  176. }
  177. }
  178. }
  179. Connections {
  180. target: field
  181. onWidthChanged: {
  182. console.log("New width: " + field.width)
  183. }
  184. }
  185. }