-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgui.js
93 lines (85 loc) · 2.16 KB
/
gui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var suggestions = [];
var wordBot = new WordBot();
function drawBoard(spaces)
{
html = '<table>\n';
for (var y=0;y<spaces.length;y++)
{
html += '<tr>'
for (x=0;x<spaces[y].length;x++)
{
space = spaces[y][x];
spaceHtml = '<td><input value="' + space[1] + '" id="s' + y.toString() + '_' + x.toString() + '"';
if (space[0]!='') spaceHtml += ' class="T' + space[0] + '"';
spaceHtml += ' /></td>'
html += spaceHtml;
}
html += '</tr>\n'
}
html +='</table>'
$('#boardHolder').html(html);
}
function readBoard()
{
result = [];
var rows = $('#boardHolder table tr');
for (var y=0;y<rows.length;y++)
{
result[y] = [];
for (x=0;x<rows[y].children.length;x++)
{
var space = $('#s' + y.toString() + '_' + x.toString());
var cssClass = space.attr('class');
if (typeof cssClass != 'undefined') cssClass = cssClass.replace(/^T/,'')
result[y][x] = [cssClass,space.val().toUpperCase()];
}
}
return result;
}
function readHand()
{
var handTiles = [];
for (var i=0;i<7;i++)
{
var letter = $('#h' + (i+1)).val().toUpperCase();
if (letter!='') handTiles[handTiles.length] = wordBot.getTileByLetter(letter);
}
return handTiles;
}
function showSuggestion(idx)
{
var board = readBoard();
for (var y=0; y<board.length; y++)
{
for (var x=0; x<board[y].length; x++)
{
var id = '#s' + y + '_' + x;
$(id).attr('placeholder','');
}
}
if (suggestions.length>idx)
{
var suggestion = suggestions[idx];
for (var i=0;i<suggestion.letters.length;i++)
{
var id = '#s' + suggestion.letters[i].y + '_' + suggestion.letters[i].x;
$(id).attr('placeholder',suggestion.letters[i].letter);
}
}
}
function updateSuggestions()
{
suggestions = wordBot.getSuggestions(readHand(), readBoard());
result = "<h3>Suggestions</h3>"
for (var i=0;i<10 && i<suggestions.length; i++) result += '<a href="javascript:showSuggestion(' + i.toString() + ');">' + suggestions[i].words[0].word + ' - ' + suggestions[i].score + '</a><br/>';
$('#suggestionsHolder').html(result);
showSuggestion(0);
}
$(function(){
resetGame();
});
function resetGame()
{
if ($('#gameType').val() == 'WWF') wordBot.initWWF(); else wordBot.initScrabble();
drawBoard(wordBot.board);
}