~hansueli-burri/+junk/Mastermind

« back to all changes in this revision

Viewing changes to Database/JsGame.qml

  • Committer: Hansueli Burri
  • Date: 2013-03-28 00:40:11 UTC
  • Revision ID: hansueli.burri@gmail.com-20130328004011-swb7knrvoosg8ij3
Forgot to Addd Database-folder

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import QtQuick 2.0
 
2
import QtQuick.LocalStorage 2.0
 
3
 
 
4
Rectangle{
 
5
    DbAccess{id:dbAccess}
 
6
    JsConfiguration{id:dbConf}
 
7
 
 
8
    function reset(){
 
9
        var numberOfColors = dbConf.getNumberOfColors();
 
10
        var numberOfFields = dbConf.getNumberOfFields();
 
11
 
 
12
        var db = dbAccess.getDatabase();
 
13
 
 
14
        for(var i=0;i<numberOfFields;i++){
 
15
            db.transaction(function(tx) {
 
16
                tx.executeSql('INSERT OR REPLACE INTO solution VALUES (?,?);', [i,Math.round(Math.random()*(numberOfColors-1))+1]);
 
17
            }
 
18
            );
 
19
        }
 
20
        resetTry();
 
21
    }
 
22
    function rate(){
 
23
        return hidden.compare(hidden.getSolution(),getTry());
 
24
    }
 
25
    function submit(){
 
26
        var aTry = getTry();
 
27
        for(var i=0;i<aTry.length;i++){
 
28
            if(aTry[i]===0) return false;
 
29
        }
 
30
        return true;
 
31
    }
 
32
    function hasWon(){
 
33
        var res = rate();
 
34
        resetTry();
 
35
        for(var i=0;i<dbConf.getNumberOfFields();i++){
 
36
            console.log(i+":"+res[i]);
 
37
            if(res[i]!==2){return false; }
 
38
        }
 
39
        return true;
 
40
    }
 
41
 
 
42
    Item{
 
43
        id:hidden;
 
44
 
 
45
        function compare(Solution,tryCombination) {
 
46
            var numberOfFields=dbConf.getNumberOfFields();
 
47
            var numberOfColors=dbConf.getNumberOfColors();
 
48
            var result = new Array(numberOfFields);
 
49
            var colors = new Array(numberOfColors+1);
 
50
            for(var k=0;k<numberOfFields;k++){
 
51
                result[k]=0;
 
52
            }
 
53
            var colorsGuessed = new Array(numberOfColors+1);
 
54
 
 
55
            //console.log("Number of Colors: "+numberOfColors+" Number of Fields:"+numberOfFields);
 
56
            for(k=0;k<numberOfColors+1;k++){
 
57
                colorsGuessed[k]=0;
 
58
                colors[k]=0;
 
59
            }
 
60
            for (var i = 0; i < numberOfFields; i++) {
 
61
                colors[Solution[i]]++;
 
62
                //console.log("Colors of Solution: "+Solution[i]);
 
63
            }
 
64
 
 
65
            for (i = 0; i < numberOfFields; i++) {
 
66
                //console.log("Colors Guessed: "+tryCombination[i]+","+colorsGuessed[tryCombination[i]]+" colors Solution: "+colors[tryCombination[i]]);
 
67
                if (colorsGuessed[tryCombination[i]] < colors[tryCombination[i]]) {
 
68
                    colorsGuessed[tryCombination[i]]++;
 
69
                    //console.log("Colors Guessed: "+tryCombination[i]);
 
70
                }
 
71
            }
 
72
 
 
73
            var j = 0;
 
74
            for ( i = 0; i < tryCombination.length; i++) {
 
75
                if (tryCombination[i] === Solution[i]) {
 
76
                    result[j] = 2;
 
77
                    colorsGuessed[tryCombination[i]]--;
 
78
                    j++;
 
79
                    //console.log("Colors and Position Guessed: "+tryCombination[i]);
 
80
                }
 
81
            }
 
82
            for ( i = 0; i < tryCombination.length && j < numberOfFields; i++) {
 
83
                if (colorsGuessed[tryCombination[i]] > 0) {
 
84
                    colorsGuessed[tryCombination[i]]--;
 
85
                    result[j] = 1;
 
86
                    j++;
 
87
                    i--;
 
88
                }
 
89
            }
 
90
            return result;
 
91
        }
 
92
 
 
93
        function setTryAt(tIndex,value){
 
94
            var db = dbAccess.getDatabase();
 
95
            var res = "";
 
96
            db.transaction(function(tx) {
 
97
                var rs = tx.executeSql('INSERT OR REPLACE INTO currentTry VALUES (?,?);', [tIndex,value]);
 
98
                if (rs.rowsAffected > 0) {
 
99
                    res = "OK";
 
100
                } else {
 
101
                    res = "Error";
 
102
                }
 
103
            }
 
104
            );
 
105
            return res;
 
106
        }
 
107
        function getSolution(){
 
108
            var res = new Array(dbConf.getNumberOfFields());
 
109
            for(var i=0;i<dbConf.getNumberOfFields();i++){
 
110
                res[i]=getSolutionAt(i);
 
111
            }
 
112
            return res;
 
113
        }
 
114
        function getSolutionAt(tIndex) {
 
115
            var db = dbAccess.getDatabase();
 
116
            var res=0;
 
117
            db.transaction(function(tx) {
 
118
                var rs = tx.executeSql('SELECT value FROM solution WHERE tIndex=?;', [tIndex]);
 
119
                if (rs.rows.length > 0) {
 
120
                    res = rs.rows.item(0).value;
 
121
                } else {
 
122
                    res = 0;
 
123
                }
 
124
            })
 
125
            return res
 
126
        }
 
127
 
 
128
    }
 
129
 
 
130
 
 
131
 
 
132
 
 
133
    function nextTryAt(tIndex){
 
134
        var res = getTryAt(tIndex);
 
135
        res++;
 
136
        if(res>dbConf.getNumberOfColors())
 
137
        {
 
138
            res=0;
 
139
        }
 
140
        hidden.setTryAt(tIndex,res);
 
141
        return res;
 
142
    }
 
143
    function getTryAt(tIndex) {
 
144
        var db = dbAccess.getDatabase();
 
145
        var res=0;
 
146
        db.transaction(function(tx) {
 
147
            var rs = tx.executeSql('SELECT value FROM currentTry WHERE tIndex=?;', [tIndex]);
 
148
            if (rs.rows.length > 0) {
 
149
                res = rs.rows.item(0).value;
 
150
            } else {
 
151
                res = 0;
 
152
            }
 
153
        })
 
154
        return res
 
155
    }
 
156
 
 
157
    function getTry(){
 
158
        var res = new Array(dbConf.getNumberOfFields());
 
159
        for(var i=0;i<dbConf.getNumberOfFields();i++){
 
160
            res[i]=getTryAt(i);
 
161
            //console.log("Try at:"+i+" : "+res[i]);
 
162
        }
 
163
        return res;
 
164
    }
 
165
 
 
166
    function resetTry(){
 
167
        var res = new Array(dbConf.getNumberOfFields());
 
168
        for(var i=0;i<dbConf.getNumberOfFields();i++){
 
169
            hidden.setTryAt(i,0);
 
170
        }
 
171
    }
 
172
}