2019-01-26 22:54:08 +00:00
|
|
|
import QtGraphicalEffects 1.0
|
|
|
|
import QtQuick 2.7
|
|
|
|
import QtQuick.Controls 2.4
|
2019-02-04 00:27:30 +00:00
|
|
|
import QtQuick.Controls 1.4
|
2019-01-26 22:54:08 +00:00
|
|
|
import QtQuick.Controls.Material 2.0
|
|
|
|
import QtQuick.Layouts 1.3
|
|
|
|
|
|
|
|
import "../widgets"
|
|
|
|
import "../widgets/controls" as Awesome
|
|
|
|
import "../fonts/Twemoji.js" as T
|
2019-02-04 00:27:30 +00:00
|
|
|
import "../utils.js" as Utils
|
2019-01-26 22:54:08 +00:00
|
|
|
|
|
|
|
ColumnLayout {
|
|
|
|
Layout.fillWidth: true
|
2019-02-04 00:27:30 +00:00
|
|
|
width:parent.width
|
|
|
|
anchors.horizontalCenter: parent.horizontalCenter
|
|
|
|
id: puzzleGame
|
2019-01-26 22:54:08 +00:00
|
|
|
|
|
|
|
|
2019-02-04 00:27:30 +00:00
|
|
|
Connections {
|
|
|
|
target: gcd
|
|
|
|
|
|
|
|
onClearMessages: function() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:54:26 +00:00
|
|
|
onAppendMessage: function(handle, from, displayName, message, image, mid, fromMe, ts, ack, error) {
|
2019-04-10 19:21:30 +00:00
|
|
|
handler(handle, from, displayName, message, image, mid, fromMe, ts, ack, error)
|
2019-04-10 17:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onPrependMessage: function(handle, from, displayName, message, image, mid, fromMe, ts, ack, error) {
|
2019-04-10 19:21:30 +00:00
|
|
|
handler(handle, from, displayName, message, image, mid, fromMe, ts, ack, error)
|
2019-04-10 17:54:26 +00:00
|
|
|
}
|
2019-04-08 20:57:59 +00:00
|
|
|
|
|
|
|
|
2019-04-10 19:21:30 +00:00
|
|
|
function handler(handle, from, displayName, message, image, mid, fromMe, ts) {
|
2019-02-04 00:27:30 +00:00
|
|
|
var msg
|
|
|
|
try {
|
|
|
|
msg = JSON.parse(message)
|
|
|
|
} catch (e) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (msg.o != 3) return
|
|
|
|
|
|
|
|
if (msg.p != undefined && msg.s != undefined) {
|
|
|
|
|
|
|
|
var points = JSON.stringify(msg.s)
|
|
|
|
var path = JSON.stringify(msg.p)
|
|
|
|
|
|
|
|
var remove = -1
|
|
|
|
for(var i=0;i<solutions.count;i++) {
|
|
|
|
var puzzleName = "Puzzle " + points
|
2019-02-13 04:10:46 +00:00
|
|
|
//console.log("Checking Solution " + puzzleName)
|
2019-02-04 00:27:30 +00:00
|
|
|
|
|
|
|
if (puzzleName == solutions.get(i).text) {
|
|
|
|
if (Utils.scorePath(JSON.parse(path)) < Utils.scorePath(JSON.parse(solutions.get(i).path))) {
|
|
|
|
remove = i
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remove > -1) {
|
|
|
|
solutions.remove(remove)
|
2019-02-13 04:10:46 +00:00
|
|
|
//console.log("Better solution found for Puzzle " + "Puzzle " + JSON.stringify(msg.s))
|
2019-02-04 00:27:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
solutions.insert(0,{
|
|
|
|
"text": "Puzzle " + JSON.stringify(msg.s),
|
|
|
|
"points":points,
|
|
|
|
"path": path,
|
|
|
|
"from": from,
|
|
|
|
"displayName": displayName,
|
|
|
|
"timestamp": ts
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ListModel {
|
|
|
|
id: solutions
|
|
|
|
}
|
|
|
|
|
|
|
|
RowLayout {
|
|
|
|
|
|
|
|
anchors.right: parent.right
|
|
|
|
anchors.rightMargin: 2
|
|
|
|
|
|
|
|
ComboBox {
|
|
|
|
width: 200
|
|
|
|
textRole: "text"
|
|
|
|
model: solutions
|
|
|
|
onActivated: {
|
2019-02-13 04:10:46 +00:00
|
|
|
//console.log("Loading puzzle " + index)
|
2019-02-04 00:27:30 +00:00
|
|
|
var solution = solutions.get(index)
|
2019-02-13 04:10:46 +00:00
|
|
|
//console.log("Loading puzzle " + JSON.parse(solution.path))
|
2019-02-04 00:27:30 +00:00
|
|
|
canvas.points = JSON.parse(solution.points)
|
|
|
|
canvas.path = JSON.parse(solution.path)
|
|
|
|
gameScore.text = Utils.scorePath(canvas.path)
|
|
|
|
canvas.requestPaint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Slider {
|
|
|
|
id: gameDifficulty
|
|
|
|
value: 5
|
|
|
|
stepSize: 1.0
|
|
|
|
minimumValue: 4.0
|
|
|
|
maximumValue: 16.0
|
|
|
|
anchors.rightMargin: 2
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleButton { // New Game
|
|
|
|
id: btnNewGame
|
|
|
|
icon: "regular/paper-plane"
|
|
|
|
text: "new game"
|
|
|
|
|
|
|
|
anchors.rightMargin: 2
|
|
|
|
|
|
|
|
property int nextMessageID: 1
|
|
|
|
|
|
|
|
onClicked: {
|
|
|
|
canvas.points = []
|
|
|
|
canvas.path = []
|
|
|
|
gameScore.text = 0
|
|
|
|
for(var i=0;i<gameDifficulty.value;i++) {
|
|
|
|
canvas.points.push(Utils.getRandomInt(0,256))
|
|
|
|
}
|
|
|
|
canvas.requestPaint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Canvas {
|
|
|
|
id: canvas
|
|
|
|
|
|
|
|
width: 640
|
|
|
|
height: 640
|
|
|
|
anchors.horizontalCenter: parent.horizontalCenter
|
|
|
|
|
|
|
|
|
|
|
|
property var points : []
|
|
|
|
property var path : []
|
|
|
|
|
|
|
|
|
|
|
|
Component.onCompleted: {
|
|
|
|
for(var i=0;i<5;i++) {
|
|
|
|
points.push(Utils.getRandomInt(0,256))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onPaint: {
|
|
|
|
var context = getContext("2d")
|
|
|
|
|
|
|
|
var gridSize = 40
|
|
|
|
|
|
|
|
context.beginPath()
|
|
|
|
context.fillStyle = "#FFFFFFFF"
|
|
|
|
context.fillRect(0,0,1000,1000)
|
|
|
|
context.fill()
|
|
|
|
context.beginPath()
|
|
|
|
context.strokeStyle = "#000000"
|
|
|
|
context.fillStyle = "#800880"
|
|
|
|
context.lineWidth = 1
|
|
|
|
for(var x=0; x< 16;x++ ){
|
|
|
|
for(var y=0; y< 16;y++ ){
|
|
|
|
var inPoints = Utils.isGridOccupied(x,y,points)
|
|
|
|
if (inPoints == true) {
|
|
|
|
context.fillRect(x*gridSize,y*gridSize,gridSize, gridSize)
|
|
|
|
} else {
|
|
|
|
context.rect(x*gridSize,y*gridSize, gridSize, gridSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
context.stroke()
|
|
|
|
context.beginPath()
|
|
|
|
context.strokeStyle = "#FFFFFFFF"
|
|
|
|
context.font = "20px sans-serif"
|
|
|
|
for(var i=0;i<=path.length;i++) {
|
|
|
|
context.strokeText(i, (Math.floor(path[i]/16) * gridSize)+15, ((path[i] % 16)*gridSize) + 25)
|
|
|
|
}
|
|
|
|
context.stroke()
|
|
|
|
context.beginPath()
|
|
|
|
//context.strokeStyle = "#000000"
|
|
|
|
//var point = (mymouse.arrpoints["x"]*16)+mymouse.arrpoints["y"]
|
|
|
|
//context.strokeText(mymouse.arrpoints["x"] + " , " + mymouse.arrpoints["y"] + " = " + point, 10,560)
|
|
|
|
context.stroke()
|
|
|
|
}
|
|
|
|
|
|
|
|
MouseArea {
|
|
|
|
id: mymouse
|
|
|
|
anchors.fill: parent
|
|
|
|
property var arrpoints : {"x": 1, "y": 1}
|
|
|
|
onClicked: {
|
|
|
|
arrpoints = Utils.mouseToGrid(mouseX, mouseY)
|
|
|
|
var point = (mymouse.arrpoints["x"]*16)+mymouse.arrpoints["y"]
|
|
|
|
var inPoints = Utils.isGridOccupied(mymouse.arrpoints["x"], mymouse.arrpoints["y"], canvas.points)
|
|
|
|
|
2019-02-13 04:10:46 +00:00
|
|
|
//console.log("Checking Point " + point + " in path " + inPoints + " path length is " + canvas.path.length)
|
2019-02-04 00:27:30 +00:00
|
|
|
if (inPoints == true) {
|
|
|
|
var alreadyInPath = false
|
|
|
|
for(var i=0;i<canvas.path.length;i++) {
|
|
|
|
if (canvas.path[i] == point) {
|
2019-02-13 04:10:46 +00:00
|
|
|
//console.log("Point already in path!!")
|
2019-02-04 00:27:30 +00:00
|
|
|
|
|
|
|
alreadyInPath = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alreadyInPath == false) {
|
|
|
|
canvas.path.push(point)
|
2019-02-13 04:10:46 +00:00
|
|
|
//console.log("Pushing " + point + " to path" + canvas.path)
|
2019-02-04 00:27:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
canvas.path = []
|
|
|
|
}
|
|
|
|
gameScore.text = Utils.scorePath(canvas.path)
|
|
|
|
canvas.requestPaint()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RowLayout {
|
|
|
|
|
|
|
|
anchors.right: parent.right
|
|
|
|
anchors.rightMargin: 2
|
|
|
|
|
|
|
|
Text {
|
|
|
|
id: gameScoreLabel
|
|
|
|
text: "Sequence Score: "
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Text {
|
|
|
|
id: gameScore
|
|
|
|
text: Utils.scorePath(canvas.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleButton { // New Game
|
|
|
|
id: btnSubmitSolution
|
|
|
|
icon: "regular/paper-plane"
|
|
|
|
text: "submit solution"
|
|
|
|
|
|
|
|
anchors.rightMargin: 2
|
|
|
|
|
|
|
|
property int nextMessageID: 1
|
|
|
|
|
|
|
|
onClicked: {
|
|
|
|
if (canvas.path.length == canvas.points.length) {
|
|
|
|
var msg = JSON.stringify({"o":3, "p":canvas.path, "s":canvas.points})
|
|
|
|
gcd.sendMessage(msg, nextMessageID++)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:54:26 +00:00
|
|
|
}
|