main.qml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 1.4
  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: speedTextField.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: "
  94. }
  95. TextField {
  96. id: speedTextField
  97. font.pointSize: 14
  98. validator: RegExpValidator {
  99. regExp: /1?[0-9]{1}/
  100. }
  101. onAccepted: {
  102. client.setSpeed(parseInt(text, 10))
  103. }
  104. }
  105. }
  106. // it will play best in the loop afte naturel selection finished
  107. CheckBox {
  108. id: playBestCheckbox
  109. anchors.margins: 10
  110. anchors.bottom: bestBtn.top
  111. anchors.right: parent.right
  112. checked: true
  113. onCheckedChanged: {
  114. client.playBestInLoop(playBestCheckbox.checked)
  115. }
  116. style: CheckBoxStyle {
  117. label: Text {
  118. color: "white"
  119. text: "Repeat Best"
  120. }
  121. }
  122. Component.onCompleted: {
  123. client.playBestInLoop(playBestCheckbox.checked)
  124. }
  125. }
  126. Button {
  127. id: bestBtn
  128. anchors.margins: 10
  129. anchors.bottom: parent.bottom
  130. anchors.right: parent.right
  131. width: 100
  132. height: 50
  133. Rectangle {
  134. anchors.fill: parent
  135. enabled: isPlaying.state
  136. color: isPlaying.state ? "#003b6f" : "lightgreen"
  137. }
  138. Text {
  139. anchors.centerIn: parent
  140. horizontalAlignment: Text.AlignVCenter
  141. text: "Play best"
  142. }
  143. onClicked: {
  144. client.playBest()
  145. }
  146. }
  147. }
  148. Connections {
  149. target: field
  150. onWidthChanged: {
  151. console.log("New width: " + field.width)
  152. }
  153. }
  154. }