33 | Waiting for other player to register.
34 | You may start a single player game while waiting.
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/multiplayer/language/English.php:
--------------------------------------------------------------------------------
1 | 1,
35 | "B" => 3,
36 | "C" => 3,
37 | "D" => 2,
38 | "E" => 1,
39 | "F" => 4,
40 | "G" => 2,
41 | "H" => 4,
42 | "I" => 1,
43 | "J" => 8,
44 | "K" => 5,
45 | "L" => 1,
46 | "M" => 3,
47 | "N" => 1,
48 | "O" => 1,
49 | "P" => 3,
50 | "Q" => 10,
51 | "R" => 1,
52 | "S" => 1,
53 | "T" => 1,
54 | "U" => 1,
55 | "V" => 4,
56 | "W" => 4,
57 | "X" => 8,
58 | "Y" => 4,
59 | "Z" => 10,
60 | ];
61 | }
--------------------------------------------------------------------------------
/multiplayer/connector/LoungeConnector.php:
--------------------------------------------------------------------------------
1 | redis = new Redis();
10 | $this->redis->connect('127.0.0.1', 6379);
11 | }
12 |
13 | public function add(Player $player) {
14 | $this->redis->sAdd(self::REDIS_LOUNGE_KEY . $player->language, $player->id);
15 | }
16 |
17 | public function getPair(string $playerId, string $language) {
18 | $key = self::REDIS_LOUNGE_KEY . $language;
19 |
20 | $reservePlayerId = $this->redis->sRem($key, $playerId);
21 | if (!$reservePlayerId) {
22 | return false;
23 | }
24 |
25 | $tryAgain = true;
26 | while ($tryAgain) {
27 | $secondPlayerId = $this->redis->sPop($key);
28 |
29 | if (!$secondPlayerId) {
30 | $this->redis->sAdd($key, $playerId);
31 | return false;
32 | }
33 |
34 | $tryAgain = false;
35 | $playerConnector = new PlayerConnector();
36 | $lastSeen = $playerConnector->getLastSeenSecondsAgo($secondPlayerId);
37 | if ($lastSeen > 10) {
38 | $playerConnector->delete($secondPlayerId);
39 | $secondPlayerId = false;
40 | $tryAgain = true;
41 | }
42 | }
43 |
44 | return [
45 | $playerId,
46 | $secondPlayerId,
47 | ];
48 | }
49 | }
--------------------------------------------------------------------------------
/multiplayer/language/German.php:
--------------------------------------------------------------------------------
1 | 1,
38 | "B" => 3,
39 | "C" => 4,
40 | "D" => 1,
41 | "E" => 1,
42 | "F" => 4,
43 | "G" => 2,
44 | "H" => 2,
45 | "I" => 1,
46 | "J" => 6,
47 | "K" => 4,
48 | "L" => 2,
49 | "M" => 3,
50 | "N" => 1,
51 | "O" => 2,
52 | "P" => 4,
53 | "Q" => 10,
54 | "R" => 1,
55 | "S" => 1,
56 | "T" => 1,
57 | "U" => 1,
58 | "V" => 6,
59 | "W" => 3,
60 | "X" => 8,
61 | "Y" => 10,
62 | "Z" => 3,
63 | "Ä" => 6,
64 | "Ö" => 8,
65 | "Ü" => 6
66 | ];
67 | }
--------------------------------------------------------------------------------
/multiplayer/1_matchmaker.php:
--------------------------------------------------------------------------------
1 | get($playerId);
8 | if (!$player) {
9 | die('invalid id');
10 | }
11 | $playerConnector->setLastSeen($playerId);
12 |
13 | if ($player->gameId) {
14 | header("Location: game/?playerId=$player->id&lang=$player->language");
15 | exit();
16 | }
17 |
18 | $loungeConnector = new LoungeConnector();
19 | $playerIds = $loungeConnector->getPair($player->id, $player->language);
20 |
21 | if (!$playerIds) {
22 | header("Location: lounge.html?playerId=$player->id");
23 | exit();
24 | }
25 |
26 | $game = new Game();
27 | $game->boardLetters = array_fill(0, 225, '');
28 | $game->bothPlayerPassCount = 0;
29 | $game->isFinished = false;
30 | $game->language = $player->language;
31 | $game->playerIds = $playerIds;
32 | $game->playerToMoveId = $player->id;
33 | $game->playerLetters = [
34 | $playerIds[0] => [],
35 | $playerIds[1] => [],
36 | ];
37 | $game->playerScores = [
38 | $playerIds[0] => 0,
39 | $playerIds[1] => 0
40 | ];
41 | $game->stashLetters = array_values($player->language === "english" ? English::LETTER_STASH : German::LETTER_STASH);
42 | $game->drawTiles();
43 |
44 | $gameConnector = new GameConnector();
45 | $gameConnector->save($game);
46 |
47 | $player->gameId = $game->id;
48 | $playerConnector->save($player);
49 |
50 | $playerTwo = $playerConnector->get($playerIds[1]);
51 | $playerTwo->gameId = $game->id;
52 | $playerConnector->save($playerTwo);
53 |
54 | header("Location: game/?playerId=$player->id&lang=$player->language");
55 |
--------------------------------------------------------------------------------
/multiplayer/connector/PlayerConnector.php:
--------------------------------------------------------------------------------
1 | redis = new Redis();
11 | $this->redis->connect('127.0.0.1', 6379);
12 | }
13 |
14 | private function getKeyByPlayerId(string $playerId): string {
15 | return self::REDIS_PLAYER_PREFIX . $playerId;
16 | }
17 |
18 | private function generatePlayerId(): string {
19 | return bin2hex(openssl_random_pseudo_bytes(16));
20 | }
21 |
22 | public function save(Player $player): Player {
23 | if (!$player->id) {
24 | $player->id = $this->generatePlayerId();
25 | }
26 |
27 | $key = $this->getKeyByPlayerId($player->id);
28 | $this->redis->set($key, json_encode($player));
29 |
30 | return $player;
31 | }
32 |
33 | public function get(string $playerId): ?Player {
34 | $key = $this->getKeyByPlayerId($playerId);
35 | $json = $this->redis->get($key);
36 |
37 | $player = new Player();
38 |
39 | if (!$json) {
40 | return null;
41 | }
42 |
43 | $data = json_decode($json, true);
44 | foreach ($data as $key => $value) $player->{$key} = $value;
45 |
46 | return $player;
47 | }
48 |
49 | private function getKeyLastSeen(string $playerId): string {
50 | return self::REDIS_PLAYER_LAST_SEEN_PREFIX . $playerId;
51 | }
52 |
53 | public function setLastSeen(string $playerId) {
54 | $key = $this->getKeyLastSeen($playerId);
55 | $this->redis->set($key, time());
56 | }
57 |
58 | public function getLastSeenSecondsAgo(string $playerId) {
59 | $key = $this->getKeyLastSeen($playerId);
60 | $lastSeenTimestamp = (int) $this->redis->get($key);
61 |
62 | return time() - $lastSeenTimestamp;
63 | }
64 |
65 | public function delete(string $playerId) {
66 | $key = $this->getKeyByPlayerId($playerId);
67 | $this->redis->delete($key);
68 |
69 | $timeKey = $this->getKeyLastSeen($playerId);
70 | $this->redis->delete($timeKey);
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/styles.css:
--------------------------------------------------------------------------------
1 | body {
2 | background: #3b874b;
3 | font-family: Verdana,Arial,Helvetica,sans-serif;
4 | padding: 20px;
5 | }
6 |
7 | table {
8 | border-collapse: collapse;
9 | }
10 |
11 | td {
12 | background: #47b75e;
13 | border: 1px solid #d4fcdb;
14 | min-width: 24px;
15 | height: 27px;
16 | text-align: center;
17 | }
18 |
19 | .input_letter {
20 | cursor: pointer;
21 | font-weight: bold;
22 | display: inline-block;
23 | margin: 5px;
24 | padding: 5px;
25 | border: 1px solid orange;
26 | background-color: white;
27 | color: initial;
28 | width: 18px;
29 | height: 18px;
30 | text-align: center;
31 | }
32 |
33 | #input_container {
34 | display: none;
35 | border: 1px solid;
36 | height: 100%;
37 | width: 100%;
38 | position: absolute;
39 | top: 0;
40 | left: 0;
41 | background-color: black;
42 | color: white;
43 | opacity: 0.7;
44 | }
45 |
46 | .hand_letter {
47 | background: #f3e797;
48 | font-weight: bold;
49 | display: inline-block;
50 | text-align: left;
51 | margin: 10px;
52 | width: 24px;
53 | height: 24px;
54 | padding-top: 7px;
55 | padding-left: 9px;
56 | }
57 | .hand_letter_points {
58 | display: inline;
59 | font-size: 8pt;
60 | }
61 |
62 | .letters_left {
63 | display: inline-block;
64 | margin-left: 102px;
65 | width: 301px;
66 | }
67 |
68 | .player_name {
69 | display: inline-block;
70 | margin-left: 5px;
71 | }
72 |
73 | .tw {
74 | background:#E60000;
75 | }
76 | .dw {
77 | background:#F86969;
78 | }
79 | .tl {
80 | background:#3675FA;
81 | }
82 | .dl {
83 | background:#22ACD8;
84 | }
85 | .start {
86 | background:#ff5500;
87 | }
88 |
89 | .color_instruction {
90 | display: inline-block;
91 | height: 20px;
92 | width: 20px;
93 | margin: 2px;
94 | }
95 |
96 | .score_container {
97 | padding: 10px;
98 | }
99 |
100 | #board {
101 | font-weight: bold;
102 | }
103 |
104 | #your_letters {
105 | margin-top: 10px;
106 | }
107 |
108 | .input_here {
109 | background: #ff0000;
110 | }
111 |
112 | .player_set_tile {
113 | background: #ff9900;
114 | }
115 |
116 | .ki_set_tile {
117 | background: #d3f7db;
118 | }
119 |
120 | .language_selector a {
121 | color: #2f2f2d;
122 | margin-top: 20px;
123 | margin-bottom: 10px;
124 | display: block;
125 | }
126 |
127 | img {
128 | margin-right: 5px;
129 | }
130 |
131 | .button {
132 | box-shadow: rgb(202, 239, 171) 0px 1px 0px 0px inset;
133 | border-radius: 3px;
134 | border: 1px solid rgb(38, 138, 22);
135 | cursor: pointer;
136 | font-weight: bold;
137 | padding: 6px;
138 | background: linear-gradient(rgb(92, 184, 17) 5%, rgb(119, 212, 42) 100%) rgb(92, 184, 17);
139 | }
140 |
141 | .button:disabled, .button[disabled] {
142 | cursor: auto;
143 | }
144 |
145 | .game {
146 | margin-right: 50px;
147 | }
148 |
149 | .manual {
150 | max-width: 550px;
151 | }
152 |
153 | h1, h2 {
154 | font-size: 100%;
155 | }
156 |
157 | .wrapper {
158 | max-width: 1150px;
159 | margin-left: auto;
160 | margin-right: auto;
161 | display: flex;
162 | flex-wrap: wrap;
163 | }
164 |
--------------------------------------------------------------------------------
/multiplayer/game/styles.css:
--------------------------------------------------------------------------------
1 | body {
2 | background: #3b874b;
3 | font-family: Verdana,Arial,Helvetica,sans-serif;
4 | padding: 20px;
5 | }
6 |
7 | table {
8 | border-collapse: collapse;
9 | }
10 |
11 | td {
12 | background: #47b75e;
13 | border: 1px solid #d4fcdb;
14 | min-width: 24px;
15 | height: 27px;
16 | text-align: center;
17 | }
18 |
19 | .input_letter {
20 | cursor: pointer;
21 | font-weight: bold;
22 | display: inline-block;
23 | margin: 5px;
24 | padding: 5px;
25 | border: 1px solid orange;
26 | background-color: white;
27 | color: initial;
28 | width: 18px;
29 | height: 18px;
30 | text-align: center;
31 | }
32 |
33 | #input_container {
34 | display: none;
35 | border: 1px solid;
36 | height: 100%;
37 | width: 100%;
38 | position: absolute;
39 | top: 0;
40 | left: 0;
41 | background-color: black;
42 | color: white;
43 | opacity: 0.7;
44 | }
45 |
46 | .hand_letter {
47 | background: #f3e797;
48 | font-weight: bold;
49 | display: inline-block;
50 | text-align: left;
51 | margin: 10px;
52 | width: 24px;
53 | height: 24px;
54 | padding-top: 7px;
55 | padding-left: 9px;
56 | }
57 | .hand_letter_points {
58 | display: inline;
59 | font-size: 8pt;
60 | }
61 |
62 | .letters_left {
63 | display: inline-block;
64 | margin-left: 102px;
65 | width: 301px;
66 | }
67 |
68 | .player_name {
69 | display: inline-block;
70 | margin-left: 5px;
71 | }
72 |
73 | .tw {
74 | background:#E60000;
75 | }
76 | .dw {
77 | background:#F86969;
78 | }
79 | .tl {
80 | background:#3675FA;
81 | }
82 | .dl {
83 | background:#22ACD8;
84 | }
85 | .start {
86 | background:#ff5500;
87 | }
88 |
89 | .color_instruction {
90 | display: inline-block;
91 | height: 20px;
92 | width: 20px;
93 | margin: 2px;
94 | }
95 |
96 | .score_container {
97 | padding: 10px;
98 | }
99 |
100 | #board {
101 | font-weight: bold;
102 | }
103 |
104 | #your_letters {
105 | margin-top: 10px;
106 | }
107 |
108 | .input_here {
109 | background: #ff0000;
110 | }
111 |
112 | .player_set_tile {
113 | background: #ff9900;
114 | }
115 |
116 | .ki_set_tile {
117 | background: #d3f7db;
118 | }
119 |
120 | .language_selector a {
121 | color: #2f2f2d;
122 | margin-top: 20px;
123 | margin-bottom: 10px;
124 | display: block;
125 | }
126 |
127 | img {
128 | margin-right: 5px;
129 | }
130 |
131 | .button {
132 | box-shadow: rgb(202, 239, 171) 0px 1px 0px 0px inset;
133 | border-radius: 3px;
134 | border: 1px solid rgb(38, 138, 22);
135 | cursor: pointer;
136 | font-weight: bold;
137 | padding: 6px;
138 | background: linear-gradient(rgb(92, 184, 17) 5%, rgb(119, 212, 42) 100%) rgb(92, 184, 17);
139 | }
140 |
141 | .button:disabled, .button[disabled] {
142 | cursor: auto;
143 | }
144 |
145 | .game {
146 | margin-right: 50px;
147 | }
148 |
149 | .manual {
150 | max-width: 550px;
151 | }
152 |
153 | h1, h2 {
154 | font-size: 100%;
155 | }
156 |
157 | .wrapper {
158 | max-width: 1150px;
159 | margin-left: auto;
160 | margin-right: auto;
161 | display: flex;
162 | flex-wrap: wrap;
163 | }
164 |
--------------------------------------------------------------------------------
/multiplayer/model/Game.php:
--------------------------------------------------------------------------------
1 | playerLetters as $playerId => $playerLetters) {
18 | while (count($playerLetters) < 7) {
19 | $stashSize = count($this->stashLetters);
20 | $id = random_int(0, $stashSize - 1);
21 | $playerLetters[] = $this->stashLetters[$id];
22 | array_splice($this->stashLetters, $id, 1);
23 | }
24 | $this->playerLetters[$playerId] = array_values($playerLetters);
25 | }
26 | }
27 |
28 | public function getOtherPlayerId(string $playerId): string {
29 | foreach ($this->playerIds as $candidateId) {
30 | if ($candidateId !== $playerId) {
31 | return $candidateId;
32 | }
33 | }
34 | throw new Exception('no other player found');
35 | }
36 |
37 | public function getResponse(string $playerId): array {
38 | return [
39 | 'boardLetters' => $this->boardLetters,
40 | 'isFinished' => $this->isFinished,
41 | 'playerScores' => [
42 | 'you' => $this->playerScores[$playerId],
43 | 'other' => $this->playerScores[$this->getOtherPlayerId($playerId)],
44 | ],
45 | 'stashSize' => count($this->stashLetters),
46 | 'yourLetters' => $this->playerLetters[$playerId],
47 | 'yourTurn' => $this->playerToMoveId === $playerId,
48 | ];
49 | }
50 |
51 | public function calculateEnding() {
52 | if ($this->isFinished || !$this->checkEnded()) {
53 | return false;
54 | }
55 |
56 | $this->isFinished = true;
57 |
58 | $scoreArray = $this->getScoreArray();
59 | foreach ($this->playerLetters as $playerId => $letters) {
60 | foreach ($letters as $char) {
61 | $this->playerScores[$playerId] -= $scoreArray[$char];
62 | }
63 | }
64 |
65 | return true;
66 | }
67 |
68 | private function getScoreArray(): array {
69 | switch ($this->language) {
70 | case 'german':
71 | return German::LETTER_POINTS;
72 | case 'english':
73 | default:
74 | return English::LETTER_POINTS;
75 | }
76 | }
77 |
78 | private function checkEnded(): bool {
79 | if ($this->bothPlayerPassCount === count($this->playerIds) * 2) {
80 | return true;
81 | }
82 |
83 | foreach ($this->playerLetters as $playerId => $playerLetters) {
84 | if (count($playerLetters) === 0 && count($this->stashLetters) === 0) {
85 | return true;
86 | }
87 | }
88 |
89 | return false;
90 | }
91 | }
--------------------------------------------------------------------------------
/translation.js:
--------------------------------------------------------------------------------
1 | var TRANSLATION_MAP = {
2 | "alles zurück auf die Hand":{
3 | "english":"Unset selected letters"
4 | },
5 | "Buchstaben tauschen (passen)":{
6 | "german":"passen",
7 | "english":"Skip turn"
8 | },
9 | "Übrige Buchstaben":{
10 | "english":"Letters left in stash"
11 | },
12 | "Deine Buchstaben":{
13 | "english":"Your letters"
14 | },
15 | "spielen":{
16 | "english":"Set letters"
17 | },
18 | "Dreifacher Buchstabenwert":{
19 | "english":"Triple letter value"
20 | },
21 | "Doppelter Buchtstabenwert":{
22 | "english":"Double letter value"
23 | },
24 | "Dreifacher Wortwert":{
25 | "english":"Triple word value"
26 | },
27 | "Doppelter Wortwert":{
28 | "english":"Double word value"
29 | },
30 | "Welchen Buchstaben möchtest du hier setzen?":{
31 | "english":"Which letter do you want to set?"
32 | },
33 | "DU":{
34 | "english":"YOU"
35 | },
36 | "KI":{
37 | "english":"AI"
38 | },
39 | "Wähle die Buchstaben aus, welche Du tauschen möchtest, dann klicke hier":{
40 | "english":"Choose the letters you want to swap, then click here"
41 | },
42 | "Das Spiel ist aus.":{
43 | "english":"Game is over."
44 | },
45 | "Wähle die Stärke des Computers":{
46 | "english":"Choose the strength of the ai"
47 | },
48 | "schwach":{
49 | "english":"weak"
50 | },
51 | "normal":{
52 | "english":"normal"
53 | },
54 | "stark":{
55 | "english":"strong"
56 | },
57 | "sehr stark":{
58 | "english":"very strong"
59 | },
60 | "sehr sehr stark":{
61 | "english":"very very strong"
62 | },
63 | "unbesiegbar":{
64 | "english":"unbeatable"
65 | },
66 | "für":{
67 | "english":"for"
68 | },
69 | "punkte":{
70 | "english":"points"
71 | },
72 | "Legen des ersten Wortes":{
73 | "english":"Inserting the first word"
74 | },
75 | "Der erste Spieler bildet ein Wort aus mindestens zwei Buchstaben. Er platziert es so auf den Spielplan, dass ein Spielstein das Feld in der Mitte des Spielplans bedeckt. Wörter können waagerecht oder senkrecht gelegt werden. Wörter dürfen nicht diagonal gelegt werden. Alle nachfolgenden Buchstaben müssen in einer Linie durchgehend entweder waagerecht oder senkrecht gelegt werden.":{
76 | "german":"Bilde ein Wort aus mindestens zwei Buchstaben und platziere es so auf den Spielplan, dass ein Spielstein das Feld in der Mitte des Spielplans bedeckt. Wörter können waagerecht oder senkrecht gelegt werden. Wörter dürfen nicht diagonal gelegt werden. Alle nachfolgenden Buchstaben müssen in einer Linie durchgehend entweder waagerecht oder senkrecht gelegt werden.",
77 | "english":"Form a word of at least two letters and place it on the board in such a way that a tile covers the field in the middle of the board. Words can be placed horizontally or vertically. Words must not be placed diagonally. All subsequent letters must be placed in a continuous line, either horizontally or vertically."
78 | },
79 | "Aufgabe der Spieler ist es, aus Buchstabensteinen mit unterschiedlichem Wert Wörter zusammenzusetzen und so auszulegen, dass sie nach Art eines Kreuzworträtsels miteinander in Verbindung stehen. Jeder Spieler muss darum bemüht sein, eine möglichst hohe Punktzahl zu erzielen, indem er aus den zur Verfügung stehenden Buchstaben Wörter bildet, deren Buchstabenwert in Kombination mit den Prämienfeldern des Spielplans eine möglichst hohe Punktzahl ergeben. Die in einer Partie erreichbare Gesamtpunktzahl liegt bei etwa 400 bis 800 und mehr, je nach dem spielerischen Können der Beteiligten.":{
80 | "german":"Aufgabe der Spieler ist es, aus Buchstabensteinen mit unterschiedlichem Wert Wörter zusammenzusetzen und so auszulegen, dass sie nach Art eines Kreuzworträtsels miteinander in Verbindung stehen. Jeder Spieler muss darum bemüht sein, eine möglichst hohe Punktzahl zu erzielen, indem er aus den zur Verfügung stehenden Buchstaben Wörter bildet, deren Buchstabenwert in Kombination mit den Prämienfeldern des Spielplans eine möglichst hohe Punktzahl ergeben. Dabei gibt es 50 extra Punkte, wenn alle 7 Buchstabensteine von der Hand auf einmal benutzt werden. Die in einer Partie erreichbare Gesamtpunktzahl liegt bei etwa 400 bis 800 und mehr, je nach dem spielerischen Können der Beteiligten.",
81 | "english":"The task of the players is to construct words from letter stones with different values and to interpret them in such a way that they are connected with each other according to the type of a crossword puzzle. Each player must strive to achieve the highest possible score by making words out of the available letters, the letter value of which, in combination with the bonus fields of the game plan, results in the highest possible score. There are 50 extra points if all 7 letters from your hand are used at once. The total score that can be achieved in a game is about 400 to 800 and more, depending on the players' ability to play."
82 | },
83 | "Felder für Buchstabenprämien":{
84 | "english":"Letter Award Fields"
85 | },
86 | "Wird ein Buchstabe auf ein hellblaues Feld gesetzt, so verdoppelt sich sein Wert. Wird ein Buchstabe auf ein dunkelblaues Feld gesetzt, so verdreifacht sich sein Wert.":{
87 | "english":"If a letter is placed on a light blue field, its value doubles. If a letter is placed on a dark blue field, its value triples."
88 | },
89 | "Felder für Wortprämien":{
90 | "english":"Word Premium Fields"
91 | },
92 | "Wird ein Buchstabe auf ein hellrotes Feld gesetzt, so verdoppelt sich der Wert des gesamten Wortes. Wird ein Buchstabe auf ein dunkelrotes Feld gesetzt, so verdreifacht sich der Wert des gesamten Wortes.":{
93 | "english":"If a letter is placed on a light red field, the value of the entire word doubles. If a letter is placed on a dark red field, the value of the entire word is tripled."
94 | },
95 | "Austausch von Buchstabensteinen":{
96 | "english":"Replacement of letter stones"
97 | },
98 | "Ein Spieler hat die Möglichkeit einen oder all seine Buchstabensteine auszutauschen, wenn er an der Reihe ist. Er legt die Buchstabensteine mit dem Buchstaben nach unten zur Seite und zieht die gleiche Anzahl neuer Buchstabensteine aus dem Beutel. Die zur Seite gelegten Buchstabensteine werden dann in den Beutel gelegt und mit den anderen Steinen gemischt. Er muss diese Runde dann aussetzen.":{
99 | "german":"Du hast die Möglichkeit einen oder alle Buchstabensteine auszutauschen. Wähle die zu tauschenden Buchstaben aus, die gleiche Anzahl neuer Buchstabensteine bekommst du aus den übrigen Buchstaben. Die abgelegten Buchstabensteine werden dann zurück zum Vorrat gegeben. Du musst diese Runde dann aussetzen.",
100 | "english":"You have the possibility to replace one or all of your letter stones when it is your turn. Select the letter stones to be abandoned. The same number of new letter stones will be drawn form the stash. The abandoned letter stones are then placed in the stash. You must then suspend this round."
101 | },
102 | "Du gewinnst.":{
103 | "english":"You win."
104 | },
105 | "Du verlierst.":{
106 | "english":"You loose."
107 | }
108 | };
109 |
--------------------------------------------------------------------------------
/multiplayer/game/translation.js:
--------------------------------------------------------------------------------
1 | var TRANSLATION_MAP = {
2 | "alles zurück auf die Hand":{
3 | "english":"Unset selected letters"
4 | },
5 | "Buchstaben tauschen (passen)":{
6 | "german":"passen",
7 | "english":"skip turn"
8 | },
9 | "Übrige Buchstaben":{
10 | "english":"Letters left in stash"
11 | },
12 | "Deine Buchstaben":{
13 | "english":"Your letters"
14 | },
15 | "spielen":{
16 | "english":"set letters"
17 | },
18 | "Dreifacher Buchstabenwert":{
19 | "english":"triple letter value"
20 | },
21 | "Doppelter Buchtstabenwert":{
22 | "english":"double letter value"
23 | },
24 | "Dreifacher Wortwert":{
25 | "english":"triple word value"
26 | },
27 | "Doppelter Wortwert":{
28 | "english":"double word value"
29 | },
30 | "Welchen Buchstaben möchtest du hier setzen?":{
31 | "english":"Which letter do you want to set?"
32 | },
33 | "DU":{
34 | "english":"YOU"
35 | },
36 | "KI":{
37 | "english":"AI"
38 | },
39 | "Wähle die Buchstaben aus, welche Du tauschen möchtest, dann klicke hier":{
40 | "english":"Choose the letters you want to swap, then click here"
41 | },
42 | "Das Spiel ist aus.":{
43 | "english":"Game is over."
44 | },
45 | "Wähle die Stärke des Computers":{
46 | "english":"Choose the strength of the ai"
47 | },
48 | "schwach":{
49 | "english":"weak"
50 | },
51 | "normal":{
52 | "english":"normal"
53 | },
54 | "stark":{
55 | "english":"strong"
56 | },
57 | "sehr stark":{
58 | "english":"very strong"
59 | },
60 | "sehr sehr stark":{
61 | "english":"very very strong"
62 | },
63 | "unbesiegbar":{
64 | "english":"unbeatable"
65 | },
66 | "für":{
67 | "english":"for"
68 | },
69 | "punkte":{
70 | "english":"points"
71 | },
72 | "Legen des ersten Wortes":{
73 | "english":"Inserting the first word"
74 | },
75 | "Der erste Spieler bildet ein Wort aus mindestens zwei Buchstaben. Er platziert es so auf den Spielplan, dass ein Spielstein das Feld in der Mitte des Spielplans bedeckt. Wörter können waagerecht oder senkrecht gelegt werden. Wörter dürfen nicht diagonal gelegt werden. Alle nachfolgenden Buchstaben müssen in einer Linie durchgehend entweder waagerecht oder senkrecht gelegt werden.":{
76 | "german":"Bilde ein Wort aus mindestens zwei Buchstaben und platziere es so auf den Spielplan, dass ein Spielstein das Feld in der Mitte des Spielplans bedeckt. Wörter können waagerecht oder senkrecht gelegt werden. Wörter dürfen nicht diagonal gelegt werden. Alle nachfolgenden Buchstaben müssen in einer Linie durchgehend entweder waagerecht oder senkrecht gelegt werden.",
77 | "english":"Form a word of at least two letters and place it on the board in such a way that a tile covers the field in the middle of the board. Words can be placed horizontally or vertically. Words must not be placed diagonally. All subsequent letters must be placed in a continuous line, either horizontally or vertically."
78 | },
79 | "Aufgabe der Spieler ist es, aus Buchstabensteinen mit unterschiedlichem Wert Wörter zusammenzusetzen und so auszulegen, dass sie nach Art eines Kreuzworträtsels miteinander in Verbindung stehen. Jeder Spieler muss darum bemüht sein, eine möglichst hohe Punktzahl zu erzielen, indem er aus den zur Verfügung stehenden Buchstaben Wörter bildet, deren Buchstabenwert in Kombination mit den Prämienfeldern des Spielplans eine möglichst hohe Punktzahl ergeben. Die in einer Partie erreichbare Gesamtpunktzahl liegt bei etwa 400 bis 800 und mehr, je nach dem spielerischen Können der Beteiligten.":{
80 | "german":"Aufgabe der Spieler ist es, aus Buchstabensteinen mit unterschiedlichem Wert Wörter zusammenzusetzen und so auszulegen, dass sie nach Art eines Kreuzworträtsels miteinander in Verbindung stehen. Jeder Spieler muss darum bemüht sein, eine möglichst hohe Punktzahl zu erzielen, indem er aus den zur Verfügung stehenden Buchstaben Wörter bildet, deren Buchstabenwert in Kombination mit den Prämienfeldern des Spielplans eine möglichst hohe Punktzahl ergeben. Dabei gibt es 50 extra Punkte, wenn alle 7 Buchstabensteine von der Hand auf einmal benutzt werden. Die in einer Partie erreichbare Gesamtpunktzahl liegt bei etwa 400 bis 800 und mehr, je nach dem spielerischen Können der Beteiligten.",
81 | "english":"The task of the players is to construct words from letter stones with different values and to interpret them in such a way that they are connected with each other according to the type of a crossword puzzle. Each player must strive to achieve the highest possible score by making words out of the available letters, the letter value of which, in combination with the bonus fields of the game plan, results in the highest possible score. There are 50 extra points if all 7 letters from your hand are used at once. The total score that can be achieved in a game is about 400 to 800 and more, depending on the players' ability to play."
82 | },
83 | "Felder für Buchstabenprämien":{
84 | "english":"Letter Award Fields"
85 | },
86 | "Wird ein Buchstabe auf ein hellblaues Feld gesetzt, so verdoppelt sich sein Wert. Wird ein Buchstabe auf ein dunkelblaues Feld gesetzt, so verdreifacht sich sein Wert.":{
87 | "english":"If a letter is placed on a light blue field, its value doubles. If a letter is placed on a dark blue field, its value triples."
88 | },
89 | "Felder für Wortprämien":{
90 | "english":"Word Premium Fields"
91 | },
92 | "Wird ein Buchstabe auf ein hellrotes Feld gesetzt, so verdoppelt sich der Wert des gesamten Wortes. Wird ein Buchstabe auf ein dunkelrotes Feld gesetzt, so verdreifacht sich der Wert des gesamten Wortes.":{
93 | "english":"If a letter is placed on a light red field, the value of the entire word doubles. If a letter is placed on a dark red field, the value of the entire word is tripled."
94 | },
95 | "Austausch von Buchstabensteinen":{
96 | "english":"Replacement of letter stones"
97 | },
98 | "Ein Spieler hat die Möglichkeit einen oder all seine Buchstabensteine auszutauschen, wenn er an der Reihe ist. Er legt die Buchstabensteine mit dem Buchstaben nach unten zur Seite und zieht die gleiche Anzahl neuer Buchstabensteine aus dem Beutel. Die zur Seite gelegten Buchstabensteine werden dann in den Beutel gelegt und mit den anderen Steinen gemischt. Er muss diese Runde dann aussetzen.":{
99 | "german":"Du hast die Möglichkeit einen oder alle Buchstabensteine auszutauschen. Wähle die zu tauschenden Buchstaben aus, die gleiche Anzahl neuer Buchstabensteine bekommst du aus den übrigen Buchstaben. Die abgelegten Buchstabensteine werden dann zurück zum Vorrat gegeben. Du musst diese Runde dann aussetzen.",
100 | "english":"You have the possibility to replace one or all of your letter stones when it is your turn. Select the letter stones to be abandoned. The same number of new letter stones will be drawn form the stash. The abandoned letter stones are then placed in the stash. You must then suspend this round."
101 | },
102 | "Du gewinnst.":{
103 | "english":"You win."
104 | },
105 | "Du verlierst.":{
106 | "english":"You loose."
107 | }
108 | };
109 |
--------------------------------------------------------------------------------
/multiplayer/game/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | Scrabble
11 |
12 |
13 |
14 |
77 |
78 |
79 |
80 |
81 |
82 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/multiplayer/game/game.js:
--------------------------------------------------------------------------------
1 | /**
2 |
3 | Copyright 2014-2018 David Edler
4 |
5 | Licensed under the Apache License, Version 2.0 (the "License");
6 | you may not use this file except in compliance with the License.
7 | You may obtain a copy of the License at
8 |
9 | http://www.apache.org/licenses/LICENSE-2.0
10 |
11 | Unless required by applicable law or agreed to in writing, software
12 | distributed under the License is distributed on an "AS IS" BASIS,
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | See the License for the specific language governing permissions and
15 | limitations under the License.
16 |
17 | **/
18 | var BOARD = null; // future pointer to dom element
19 | var BOARD_LETTERS = [];
20 | var TO_BE_PLAYED_BOARD_LETTER_INDEXES = [];
21 | var LETTERS_PLAYED_BY_OPPONENT_INDEXES = [];
22 |
23 | var OPPONENT_NAME;
24 |
25 | var LETTER_STASH_SIZE;
26 | var POINTS_PER_LETTER;
27 | var LANGUAGE_CONFIG;
28 |
29 | var IS_MY_MOVE;
30 | var IS_GAME_FINISHED;
31 |
32 | var PLAYER_ID = getUrlParameterByName('playerId');
33 | var PLAYER_LETTERS = [];
34 |
35 | var PLAYER_1_POINTS = 0;
36 | var PLAYER_2_POINTS = 0;
37 |
38 | const LANG_ENGLISH = 'english';
39 | const LANG_GERMAN = 'deutsch';
40 | const ENGLISH_CONFIG_URL = 'config/english.jsonp';
41 | const GERMAN_CONFIG_URL = 'config/german.jsonp';
42 | loadLanguageConfig();
43 |
44 | function getUrlParameterByName(name, url) {
45 | if (!url) url = window.location.href;
46 | name = name.replace(/[\[\]]/g, "\\$&");
47 | var regex = new RegExp("[?&]" + name + "(=([^]*)|&|#|$)"),
48 | results = regex.exec(url);
49 | if (!results) return null;
50 | if (!results[2]) return '';
51 | return decodeURIComponent(results[2].replace(/\+/g, " "));
52 | }
53 |
54 | function getConfigUrl() {
55 | var lang = getUrlParameterByName('lang');
56 |
57 | switch (lang) {
58 | case LANG_ENGLISH:
59 | return ENGLISH_CONFIG_URL;
60 | case LANG_GERMAN:
61 | default:
62 | return GERMAN_CONFIG_URL;
63 | }
64 | }
65 |
66 | function i18n(text) {
67 | var lang = getUrlParameterByName('lang') || LANG_GERMAN;
68 |
69 | if (TRANSLATION_MAP[text] && TRANSLATION_MAP[text][lang]) {
70 | return TRANSLATION_MAP[text][lang];
71 | }
72 |
73 | return text;
74 | }
75 |
76 | function loadLanguageConfig() {
77 | var request = new XMLHttpRequest();
78 | var configUrl = getConfigUrl();
79 | request.open("GET", configUrl, true);
80 | request.onreadystatechange = function()
81 | {
82 | if (request.readyState === 4) {
83 | LANGUAGE_CONFIG = JSON.parse(request.responseText);
84 | POINTS_PER_LETTER = LANGUAGE_CONFIG.POINTS_PER_LETTER;
85 | loadDictionary();
86 | }
87 | };
88 | request.send(null);
89 | }
90 |
91 | var DICTIONARY = [];
92 |
93 | function loadDictionary() {
94 | var request = new XMLHttpRequest();
95 | request.open("GET", LANGUAGE_CONFIG.DICTIONARY_URL, true);
96 | request.onreadystatechange = function()
97 | {
98 | if (request.readyState === 4) {
99 | DICTIONARY = request.responseText.replace("OE","Ö").replace("UE",'Ü').replace('AE','Ä').toUpperCase();
100 | initializeMultiplayer();
101 | }
102 | };
103 | request.send(null);
104 | }
105 |
106 | function initializeMultiplayer() {
107 | var request = new XMLHttpRequest();
108 | request.open("GET", '../2_initialize_game.php?playerId=' + PLAYER_ID, true);
109 | request.onreadystatechange = function()
110 | {
111 | if (request.readyState === 4) {
112 |
113 | GAME_STATE = JSON.parse(request.responseText);
114 | BOARD_LETTERS = GAME_STATE.boardLetters;
115 | OPPONENT_NAME = GAME_STATE.otherPlayersName;
116 | PLAYER_1_POINTS = GAME_STATE.playerScores.you;
117 | PLAYER_2_POINTS = GAME_STATE.playerScores.other;
118 | PLAYER_LETTERS = GAME_STATE.yourLetters;
119 | LETTER_STASH_SIZE = parseInt(GAME_STATE.stashSize);
120 | IS_MY_MOVE = GAME_STATE.yourTurn;
121 | startGame();
122 | }
123 | };
124 | request.send(null);
125 | }
126 |
127 | function sendSetTilesRequest(setLettersWithIndex, points, onDone) {
128 | var request = new XMLHttpRequest();
129 | request.open(
130 | "GET",
131 | '../3_set_tiles.php?playerId=' + PLAYER_ID + '&points=' + points + '&setLettersWithIndex=' + JSON.stringify(setLettersWithIndex),
132 | true
133 | );
134 | request.onreadystatechange = function()
135 | {
136 | if (request.readyState === 4) {
137 |
138 | GAME_STATE = JSON.parse(request.responseText);
139 | BOARD_LETTERS = GAME_STATE.boardLetters;
140 | PLAYER_LETTERS = GAME_STATE.yourLetters;
141 | PLAYER_1_POINTS = GAME_STATE.playerScores.you;
142 | PLAYER_2_POINTS = GAME_STATE.playerScores.other;
143 | LETTER_STASH_SIZE = parseInt(GAME_STATE.stashSize);
144 | IS_MY_MOVE = GAME_STATE.yourTurn;
145 |
146 | onDone();
147 |
148 | if (GAME_STATE.isFinished) {
149 | endGame();
150 | }
151 | }
152 | };
153 | request.send(null);
154 | }
155 |
156 | function sendGetOtherPlayersMoveRequest(onKeepWaiting, onDone) {
157 | var request = new XMLHttpRequest();
158 | request.open(
159 | "GET",
160 | '../4_get_other_players_move.php?playerId=' + PLAYER_ID,
161 | true
162 | );
163 | request.onreadystatechange = function()
164 | {
165 | if (request.readyState === 4) {
166 | GAME_STATE = JSON.parse(request.responseText);
167 | BOARD_LETTERS = GAME_STATE.boardLetters;
168 | PLAYER_1_POINTS = GAME_STATE.playerScores.you;
169 | PLAYER_2_POINTS = GAME_STATE.playerScores.other;
170 | LETTERS_PLAYED_BY_OPPONENT_INDEXES = GAME_STATE.recentSetIndexes;
171 | LETTER_STASH_SIZE = parseInt(GAME_STATE.stashSize);
172 | IS_MY_MOVE = GAME_STATE.yourTurn;
173 |
174 | if (IS_MY_MOVE) {
175 | onDone();
176 |
177 | if (GAME_STATE.isFinished) {
178 | endGame();
179 | }
180 | } else {
181 | onKeepWaiting();
182 | }
183 | }
184 | };
185 | request.send(null);
186 | }
187 |
188 | function sendPassRequest(droppedLetters, onDone) {
189 | var request = new XMLHttpRequest();
190 | request.open(
191 | "GET",
192 | '../5_skip_turn.php?playerId=' + PLAYER_ID + '&droppedLetters=' + JSON.stringify(droppedLetters),
193 | true
194 | );
195 | request.onreadystatechange = function()
196 | {
197 | if (request.readyState === 4) {
198 | GAME_STATE = JSON.parse(request.responseText);
199 | LETTER_STASH_SIZE = parseInt(GAME_STATE.stashSize);
200 | PLAYER_1_POINTS = GAME_STATE.playerScores.you;
201 | PLAYER_2_POINTS = GAME_STATE.playerScores.other;
202 | PLAYER_LETTERS = GAME_STATE.yourLetters;
203 | IS_MY_MOVE = GAME_STATE.yourTurn;
204 |
205 | onDone();
206 |
207 | if (GAME_STATE.isFinished) {
208 | endGame();
209 | }
210 | }
211 | };
212 | request.send(null);
213 | }
214 |
215 | function showLetterInput(elem) {
216 | // get current field
217 | var targetPosition = elem.srcElement.id.substring(1,elem.srcElement.id.length).split("_");
218 | var x = parseInt(targetPosition[0]) - 1;
219 | var y = parseInt(targetPosition[1]) - 1;
220 |
221 | // if there is already a active tile, remove it.
222 | if (elem.target.classList.contains('player_set_tile')) {
223 | var returnedIndex = x * 15 + y;
224 | var letter = BOARD_LETTERS[returnedIndex];
225 | BOARD_LETTERS[x*15+y] = "";
226 | TO_BE_PLAYED_BOARD_LETTER_INDEXES.splice(TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(returnedIndex), 1);
227 | PLAYER_LETTERS.push(letter);
228 | elem.target.classList.remove('player_set_tile');
229 | printPlayersLetters();
230 | printBoard();
231 | updatePlayButton();
232 | return;
233 | }
234 |
235 | // there is already a letter
236 | if (elem.target.innerHTML !== '') {
237 | return;
238 | }
239 |
240 | // mark target cell
241 | elem.srcElement.classList.add("input_here");
242 |
243 | // show the input layer
244 | var input_container = document.getElementById("input_container");
245 | input_container.style.padding= (elem.srcElement.offsetTop + 10) + " 0 0 " + (elem.srcElement.offsetLeft + 55);
246 | input_container.style.display= "block";
247 | input_container.innerHTML = i18n('Welchen Buchstaben möchtest du hier setzen?') + "
" + PLAYER_LETTERS.join("
") + "
";
248 |
249 | // append event listeners to input buttons
250 | var buttons=document.getElementsByClassName("input_letter");
251 | for (var i=0; i' + PLAYER_LETTERS[i] + '
' + POINTS_PER_LETTER[PLAYER_LETTERS[i]] + '
';
303 | }
304 | document.getElementById("player_1_letters").innerHTML = out;
305 | }
306 |
307 | function printBoard() {
308 | for (var i=0; i<15; i++) {
309 | for (var j=0; j<15; j++) {
310 | var field = BOARD.rows[i].cells[j];
311 | field.innerHTML=BOARD_LETTERS[i * 15 + j];
312 |
313 | if (BOARD_LETTERS[i * 15 + j] === '') {
314 | field.style.cursor = "pointer";
315 | } else {
316 | field.style.cursor = "auto";
317 | }
318 |
319 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(i * 15 + j) !== -1) {
320 | if (!field.classList.contains('player_set_tile')) {
321 | field.classList.add('player_set_tile');
322 | }
323 | field.style.cursor = "no-drop";
324 | } else {
325 | field.classList.remove('player_set_tile');
326 | }
327 |
328 | if (LETTERS_PLAYED_BY_OPPONENT_INDEXES.indexOf(i * 15 + j) !== -1) {
329 | if (!field.classList.contains('ki_set_tile')) {
330 | field.classList.add('ki_set_tile');
331 | }
332 | } else {
333 | field.classList.remove('ki_set_tile');
334 | }
335 | }
336 | }
337 |
338 | // score
339 | document.getElementById("player_1_points").innerHTML = PLAYER_1_POINTS.toString();
340 | document.getElementById("player_2_points").innerHTML = PLAYER_2_POINTS.toString();
341 |
342 | // remaining tiles
343 | document.getElementById("letters_left").innerHTML = LETTER_STASH_SIZE.toString();
344 | }
345 |
346 | function takeBackCurrentTiles() {
347 | for (var i=0; i 0) {
373 | h -=1;
374 | }
375 | //construct word
376 | var word_multiplier = 1;
377 | var letter_multiplier = 1;
378 | var word = BOARD_LETTERS[h];
379 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(h) !== -1) {
380 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("dl")) {
381 | letter_multiplier = 2;
382 | }
383 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("tl")) {
384 | letter_multiplier = 3;
385 | }
386 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("dw")) {
387 | word_multiplier *= 2;
388 | }
389 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("tw")) {
390 | word_multiplier *= 3;
391 | }
392 | }
393 | var points = letter_multiplier * POINTS_PER_LETTER[BOARD_LETTERS[h]];
394 | h++;
395 | while (BOARD_LETTERS[h] !== "" && (h % 15) !== 0) {
396 | letter_multiplier = 1;
397 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(h) !== -1) {
398 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("dl")) {
399 | letter_multiplier = 2;
400 | }
401 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("tl")) {
402 | letter_multiplier = 3;
403 | }
404 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("dw")) {
405 | word_multiplier *= 2;
406 | }
407 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("tw")) {
408 | word_multiplier *= 3;
409 | }
410 | }
411 | word = word.concat(BOARD_LETTERS[h]);
412 | points += letter_multiplier * POINTS_PER_LETTER[BOARD_LETTERS[h]];
413 | h+=1;
414 | }
415 | if (word.length > 1 && words.indexOf(word) === -1) {
416 | words.push(word);
417 | pointSum += points * word_multiplier;
418 | }
419 |
420 | /*
421 | * vertical words
422 | */
423 | // find highest letter
424 | var v=cur;
425 | while (BOARD_LETTERS[v-15] !== "" && v > 14) {
426 | v -= 15;
427 | }
428 | //construct word
429 | word = '';
430 | points = 0;
431 | word_multiplier = 1;
432 | while (BOARD_LETTERS[v] !== "" && v < 225) {
433 | letter_multiplier = 1;
434 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(v) !== -1) {
435 | if (document.getElementById("s"+ Math.floor(v/15+1) + "_" + Math.floor(v%15+1)).classList.contains("dl")) {
436 | letter_multiplier = 2;
437 | }
438 | if (document.getElementById("s"+ Math.floor(v/15+1) + "_" + Math.floor(v%15+1)).classList.contains("tl")) {
439 | letter_multiplier = 3;
440 | }
441 | if (document.getElementById("s"+ Math.floor(v/15+1) + "_" + Math.floor(v%15+1)).classList.contains("dw")) {
442 | word_multiplier *= 2;
443 | }
444 | if (document.getElementById("s"+ Math.floor(v/15+1) + "_" + Math.floor(v%15+1)).classList.contains("tw")) {
445 | word_multiplier *= 3;
446 | }
447 | }
448 | word = word.concat(BOARD_LETTERS[v]);
449 | points += letter_multiplier * POINTS_PER_LETTER[BOARD_LETTERS[v]];
450 | v += 15;
451 | }
452 | if (word.length > 1 && words.indexOf(word) === -1) {
453 | words.push(word);
454 | pointSum += points * word_multiplier;
455 | }
456 | }
457 | return [words, pointSum];
458 | }
459 |
460 | /**
461 | * is the current position of new letters valid?
462 | *
463 | * one new word set and no letters on random points of the board
464 | * new word is connected to old letters
465 | * or opening of the game and center field used
466 | **/
467 | function isLetterPositionValid() {
468 | var start = 225;
469 | var end = 0;
470 | for (i=0; i < TO_BE_PLAYED_BOARD_LETTER_INDEXES.length; i++) {
471 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES[i] < start) {
472 | start = TO_BE_PLAYED_BOARD_LETTER_INDEXES[i];
473 | }
474 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES[i] > end) {
475 | end = TO_BE_PLAYED_BOARD_LETTER_INDEXES[i];
476 | }
477 | }
478 |
479 | var lineEnd = Math.abs(14 - (start % 15)) + start;
480 | var isHorizontal = lineEnd >= end;
481 | var increment = isHorizontal ? 1 : 15;
482 |
483 | for (i=start; i 0 && isFieldWithLetter(right)) {
498 | return true;
499 | }
500 |
501 | var top = TO_BE_PLAYED_BOARD_LETTER_INDEXES[i]-15;
502 | if (top > 0 && isFieldWithLetter(top)) {
503 | return true;
504 | }
505 |
506 | var down = TO_BE_PLAYED_BOARD_LETTER_INDEXES[i]+15;
507 | if (down < 225 && isFieldWithLetter(down)) {
508 | return true;
509 | }
510 | }
511 |
512 | return wasBoardEmpty() && isCenterFieldUsed();
513 | }
514 |
515 | function wasBoardEmpty() {
516 | for (var i = 0; i < 225; i++) {
517 | if (BOARD_LETTERS[i] !== '' && TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(i) === -1) {
518 | return false;
519 | }
520 | }
521 |
522 | return true;
523 | }
524 |
525 | function isCenterFieldUsed() {
526 | return TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(112) !== -1;
527 | }
528 |
529 | function isFieldWithLetter(index) {
530 | return TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(index) === -1 && BOARD_LETTERS[index] !== '';
531 | }
532 |
533 | function checkValidStateAndCalculatePoints() {
534 | if (!isLetterPositionValid()) {
535 | return false;
536 | }
537 |
538 | var t = findWordsAndPointsByActiveLetters();
539 | var words = t[0];
540 | var points = t[1];
541 |
542 | if (words.length < 1) {
543 | return false;
544 | }
545 |
546 | for (var i=0; istart new game';
681 | document.getElementById("input_container").style.display= "block";
682 |
683 | var winText = i18n('Du gewinnst.');
684 | var looseText = i18n('Du verlierst.');
685 | var resultText = PLAYER_1_POINTS > PLAYER_2_POINTS ? winText : looseText;
686 |
687 | alert(
688 | i18n('Das Spiel ist aus.') + '\n' +
689 | resultText + '\n' +
690 | i18n("DU") + ": " + PLAYER_1_POINTS + ' ' + i18n("punkte") + '\n' +
691 | OPPONENT_NAME + ": " + PLAYER_2_POINTS + ' ' + i18n("punkte")
692 | );
693 | }
694 |
695 | function startGame() {
696 | BOARD = document.getElementById("board");
697 | // event handlers on board
698 | for (var i=0; i<15; i++) {
699 | for (var j=0; j<15; j++) {
700 | BOARD.rows[i].cells[j].onclick=showLetterInput;
701 | }
702 | }
703 |
704 | document.getElementById("move").disabled = true;
705 | document.getElementById("opponent_name").innerHTML = OPPONENT_NAME;
706 |
707 | printPlayersLetters();
708 | printBoard();
709 |
710 | if (!IS_MY_MOVE) {
711 | waitForOtherPlayersMove();
712 | }
713 | }
714 |
--------------------------------------------------------------------------------
/game.js:
--------------------------------------------------------------------------------
1 | /**
2 |
3 | Copyright 2014-2018 David Edler
4 |
5 | Licensed under the Apache License, Version 2.0 (the "License");
6 | you may not use this file except in compliance with the License.
7 | You may obtain a copy of the License at
8 |
9 | http://www.apache.org/licenses/LICENSE-2.0
10 |
11 | Unless required by applicable law or agreed to in writing, software
12 | distributed under the License is distributed on an "AS IS" BASIS,
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | See the License for the specific language governing permissions and
15 | limitations under the License.
16 |
17 | **/
18 | var BOARD = null; // future pointer to dom element
19 | var BOARD_LETTERS = [];
20 | var TO_BE_PLAYED_BOARD_LETTER_INDEXES = [];
21 | var LETTERS_PLAYED_BY_KI_INDEXES = [];
22 |
23 | var IS_GAME_FINISHED;
24 |
25 | var LETTER_STASH;
26 | var POINTS_PER_LETTER;
27 | var LANGUAGE_CONFIG;
28 | const LANG_ENGLISH = 'english';
29 | const LANG_GERMAN = 'german';
30 | const ENGLISH_CONFIG_URL = 'config/english.jsonp';
31 | const GERMAN_CONFIG_URL = 'config/german.jsonp';
32 | loadLanguageConfig();
33 |
34 | window.onload = function() {
35 | document.querySelectorAll('.tw').forEach(cell => {
36 | cell.title = i18n("Dreifacher Wortwert");
37 | });
38 | document.querySelectorAll('.dl').forEach(cell => {
39 | cell.title = i18n("Doppelter Buchstabenwert");
40 | });
41 | document.querySelectorAll('.dw').forEach(cell => {
42 | cell.title = i18n("Doppelter Wortwert");
43 | });
44 | document.querySelectorAll('.tl').forEach(cell => {
45 | cell.title = i18n("Dreifacher Buchstabenwert");
46 | });
47 | };
48 |
49 | function getUrlParameterByName(name, url) {
50 | if (!url) url = window.location.href;
51 | name = name.replace(/[\[\]]/g, "\\$&");
52 | var regex = new RegExp("[?&]" + name + "(=([^]*)|&|#|$)"),
53 | results = regex.exec(url);
54 | if (!results) return null;
55 | if (!results[2]) return '';
56 | return decodeURIComponent(results[2].replace(/\+/g, " "));
57 | }
58 |
59 | function getConfigUrl() {
60 | var lang = getUrlParameterByName('lang');
61 |
62 | switch (lang) {
63 | case LANG_ENGLISH:
64 | return ENGLISH_CONFIG_URL;
65 | case LANG_GERMAN:
66 | default:
67 | return GERMAN_CONFIG_URL;
68 | }
69 | }
70 |
71 | function i18n(text) {
72 | var lang = getUrlParameterByName('lang') || LANG_GERMAN;
73 |
74 | if (TRANSLATION_MAP[text] && TRANSLATION_MAP[text][lang]) {
75 | return TRANSLATION_MAP[text][lang];
76 | }
77 |
78 | return text;
79 | }
80 |
81 | function loadLanguageConfig() {
82 | var request = new XMLHttpRequest();
83 | var configUrl = getConfigUrl();
84 | request.open("GET", configUrl, true);
85 | request.onreadystatechange = function()
86 | {
87 | if (request.readyState === 4) {
88 | LANGUAGE_CONFIG = JSON.parse(request.responseText);
89 | LETTER_STASH = LANGUAGE_CONFIG.LETTER_STASH;
90 | POINTS_PER_LETTER = LANGUAGE_CONFIG.POINTS_PER_LETTER;
91 | loadDictionary();
92 | }
93 | };
94 | request.send(null);
95 | }
96 |
97 | var PLAYER_1_LETTERS = [];
98 | var PLAYER_1_POINTS = 0;
99 |
100 | var PLAYER_2_LETTERS = [];
101 | var PLAYER_2_POINTS = 0;
102 |
103 | var KI_INTELLIGENCE = 1;
104 | var KI_MAX_INTELLIGENCE = 0.2;
105 |
106 | var MAX_POINTS = 0;
107 | var MAX_RESULT = {};
108 |
109 | var BOTH_PLAYERS_PASS_COUNT = 0;
110 |
111 | var DICTIONARY = [];
112 |
113 | function loadDictionary() {
114 | var request = new XMLHttpRequest();
115 | request.open("GET", LANGUAGE_CONFIG.DICTIONARY_URL, true);
116 | request.onreadystatechange = function()
117 | {
118 | if (request.readyState === 4) {
119 | DICTIONARY = request.responseText.replace("OE","Ö").replace("UE",'Ü').replace('AE','Ä').toUpperCase();
120 | startGame();
121 | }
122 | };
123 | request.send(null);
124 | }
125 |
126 | function showLetterInput(elem) {
127 | // get current field
128 | var targetPosition = elem.srcElement.id.substring(1,elem.srcElement.id.length).split("_");
129 | var x = parseInt(targetPosition[0]) - 1;
130 | var y = parseInt(targetPosition[1]) - 1;
131 |
132 | // if there is already a active tile, remove it.
133 | if (elem.target.classList.contains('player_set_tile')) {
134 | var returnedIndex = x * 15 + y;
135 | var letter = BOARD_LETTERS[returnedIndex];
136 | BOARD_LETTERS[x*15+y] = "";
137 | TO_BE_PLAYED_BOARD_LETTER_INDEXES.splice(TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(returnedIndex), 1);
138 | PLAYER_1_LETTERS.push(letter);
139 | elem.target.classList.remove('player_set_tile');
140 | printPlayersLetters();
141 | printBoard();
142 | updatePlayButton();
143 | return;
144 | }
145 |
146 | // there is already a letter
147 | if (elem.target.innerHTML !== '') {
148 | return;
149 | }
150 |
151 | // mark target cell
152 | elem.srcElement.classList.add("input_here");
153 |
154 | // show the input layer
155 | var input_container = document.getElementById("input_container");
156 | input_container.style.padding= (elem.srcElement.offsetTop + 10) + " 0 0 " + (elem.srcElement.offsetLeft + 55);
157 | input_container.style.display= "block";
158 | input_container.innerHTML = i18n('Welchen Buchstaben möchtest du hier setzen?') + "
" + PLAYER_1_LETTERS.join("
") + "
";
159 |
160 | // append event listeners to input buttons
161 | var buttons=document.getElementsByClassName("input_letter");
162 | for (var i=0; i 0) {
220 | var i = seededRandom(0, LETTER_STASH.length);
221 | player_var.push(LETTER_STASH[i]);
222 | LETTER_STASH.splice(i,1);
223 | }
224 |
225 | if (player_var.length === 0) {
226 | endGame();
227 | }
228 | }
229 |
230 | function printPlayersLetters() {
231 | var out = "";
232 | for (var i=0; i' + PLAYER_1_LETTERS[i] + '
' + POINTS_PER_LETTER[PLAYER_1_LETTERS[i]] + '
';
234 | }
235 | document.getElementById("player_1_letters").innerHTML = out;
236 | }
237 |
238 | function printBoard() {
239 | for (var i=0; i<15; i++) {
240 | for (var j=0; j<15; j++) {
241 | var field = BOARD.rows[i].cells[j];
242 | field.innerHTML=BOARD_LETTERS[i * 15 + j];
243 |
244 | if (BOARD_LETTERS[i * 15 + j] === '') {
245 | field.style.cursor = "pointer";
246 | } else {
247 | field.style.cursor = "auto";
248 | }
249 |
250 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(i * 15 + j) !== -1) {
251 | if (!field.classList.contains('player_set_tile')) {
252 | field.classList.add('player_set_tile');
253 | }
254 | field.style.cursor = "no-drop";
255 | } else {
256 | field.classList.remove('player_set_tile');
257 | }
258 |
259 | if (LETTERS_PLAYED_BY_KI_INDEXES.indexOf(i * 15 + j) !== -1) {
260 | if (!field.classList.contains('ki_set_tile')) {
261 | field.classList.add('ki_set_tile');
262 | }
263 | } else {
264 | field.classList.remove('ki_set_tile');
265 | }
266 | }
267 | }
268 |
269 | // score
270 | document.getElementById("player_1_points").innerHTML = PLAYER_1_POINTS.toString();
271 | document.getElementById("player_2_points").innerHTML = PLAYER_2_POINTS.toString();
272 |
273 | // remaining tiles
274 | document.getElementById("letters_left").innerHTML = LETTER_STASH.length.toString();
275 | }
276 |
277 | function takeBackCurrentTiles() {
278 | for (var i=0; i KI_INTELLIGENCE) {
291 | return false;
292 | }
293 |
294 | return DICTIONARY.match("\n" + word + "\n") !== null;
295 | }
296 |
297 | function isWordStartInDictionary(word) {
298 | return DICTIONARY.match("\n" + word) !== null;
299 | }
300 |
301 | function findWordsAndPointsByActiveLetters() {
302 | var words = [];
303 | var pointSum = 0;
304 | for (var i=0; i < TO_BE_PLAYED_BOARD_LETTER_INDEXES.length; i++) {
305 | var cur = TO_BE_PLAYED_BOARD_LETTER_INDEXES[i];
306 | /*
307 | * horizontal words
308 | */
309 | // find leftest letter
310 | var h=cur;
311 | while (BOARD_LETTERS[h-1] !== "" && (h % 15) > 0) {
312 | h -=1;
313 | }
314 | //construct word
315 | var word_multiplier = 1;
316 | var letter_multiplier = 1;
317 | var word = BOARD_LETTERS[h];
318 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(h) !== -1) {
319 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("dl")) {
320 | letter_multiplier = 2;
321 | }
322 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("tl")) {
323 | letter_multiplier = 3;
324 | }
325 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("dw")) {
326 | word_multiplier *= 2;
327 | }
328 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("tw")) {
329 | word_multiplier *= 3;
330 | }
331 | }
332 | var points = letter_multiplier * POINTS_PER_LETTER[BOARD_LETTERS[h]];
333 | h++;
334 | while (BOARD_LETTERS[h] !== "" && (h % 15) !== 0) {
335 | letter_multiplier = 1;
336 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(h) !== -1) {
337 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("dl")) {
338 | letter_multiplier = 2;
339 | }
340 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("tl")) {
341 | letter_multiplier = 3;
342 | }
343 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("dw")) {
344 | word_multiplier *= 2;
345 | }
346 | if (document.getElementById("s"+ Math.floor(h/15+1) + "_" + Math.floor(h%15+1)).classList.contains("tw")) {
347 | word_multiplier *= 3;
348 | }
349 | }
350 | word = word.concat(BOARD_LETTERS[h]);
351 | points += letter_multiplier * POINTS_PER_LETTER[BOARD_LETTERS[h]];
352 | h+=1;
353 | }
354 | if (word.length > 1 && words.indexOf(word) === -1) {
355 | words.push(word);
356 | pointSum += points * word_multiplier;
357 | }
358 |
359 | /*
360 | * vertical words
361 | */
362 | // find highest letter
363 | var v=cur;
364 | while (BOARD_LETTERS[v-15] !== "" && v > 14) {
365 | v -= 15;
366 | }
367 | //construct word
368 | word = '';
369 | points = 0;
370 | word_multiplier = 1;
371 | while (BOARD_LETTERS[v] !== "" && v < 225) {
372 | letter_multiplier = 1;
373 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(v) !== -1) {
374 | if (document.getElementById("s"+ Math.floor(v/15+1) + "_" + Math.floor(v%15+1)).classList.contains("dl")) {
375 | letter_multiplier = 2;
376 | }
377 | if (document.getElementById("s"+ Math.floor(v/15+1) + "_" + Math.floor(v%15+1)).classList.contains("tl")) {
378 | letter_multiplier = 3;
379 | }
380 | if (document.getElementById("s"+ Math.floor(v/15+1) + "_" + Math.floor(v%15+1)).classList.contains("dw")) {
381 | word_multiplier *= 2;
382 | }
383 | if (document.getElementById("s"+ Math.floor(v/15+1) + "_" + Math.floor(v%15+1)).classList.contains("tw")) {
384 | word_multiplier *= 3;
385 | }
386 | }
387 | word = word.concat(BOARD_LETTERS[v]);
388 | points += letter_multiplier * POINTS_PER_LETTER[BOARD_LETTERS[v]];
389 | v += 15;
390 | }
391 | if (word.length > 1 && words.indexOf(word) === -1) {
392 | words.push(word);
393 | pointSum += points * word_multiplier;
394 | }
395 | }
396 | return [words, pointSum];
397 | }
398 |
399 | /**
400 | * is the current position of new letters valid?
401 | *
402 | * one new word set and no letters on random points of the board
403 | * new word is connected to old letters
404 | * or opening of the game and center field used
405 | **/
406 | function isLetterPositionValid() {
407 | var start = 225;
408 | var end = 0;
409 | for (i=0; i < TO_BE_PLAYED_BOARD_LETTER_INDEXES.length; i++) {
410 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES[i] < start) {
411 | start = TO_BE_PLAYED_BOARD_LETTER_INDEXES[i];
412 | }
413 | if (TO_BE_PLAYED_BOARD_LETTER_INDEXES[i] > end) {
414 | end = TO_BE_PLAYED_BOARD_LETTER_INDEXES[i];
415 | }
416 | }
417 |
418 | var lineEnd = Math.abs(14 - (start % 15)) + start;
419 | var isHorizontal = lineEnd >= end;
420 | var increment = isHorizontal ? 1 : 15;
421 |
422 | for (i=start; i 0 && isFieldWithLetter(right)) {
437 | return true;
438 | }
439 |
440 | var top = TO_BE_PLAYED_BOARD_LETTER_INDEXES[i]-15;
441 | if (top > 0 && isFieldWithLetter(top)) {
442 | return true;
443 | }
444 |
445 | var down = TO_BE_PLAYED_BOARD_LETTER_INDEXES[i]+15;
446 | if (down < 225 && isFieldWithLetter(down)) {
447 | return true;
448 | }
449 | }
450 |
451 | return wasBoardEmpty() && isCenterFieldUsed();
452 | }
453 |
454 | function wasBoardEmpty() {
455 | for (var i = 0; i < 225; i++) {
456 | if (BOARD_LETTERS[i] !== '' && TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(i) === -1) {
457 | return false;
458 | }
459 | }
460 |
461 | return true;
462 | }
463 |
464 | function isCenterFieldUsed() {
465 | return TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(112) !== -1;
466 | }
467 |
468 | function isFieldWithLetter(index) {
469 | return TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(index) === -1 && BOARD_LETTERS[index] !== '';
470 | }
471 |
472 | function checkValidStateAndCalculatePoints() {
473 | if (!isLetterPositionValid()) {
474 | return false;
475 | }
476 |
477 | var t = findWordsAndPointsByActiveLetters();
478 | var words = t[0];
479 | var points = t[1];
480 |
481 | if (words.length < 1) {
482 | return false;
483 | }
484 |
485 | for (var i=0; i= 4) {
596 | endGame();
597 | }
598 | }
599 |
600 | function endGame() {
601 | IS_GAME_FINISHED = true;
602 |
603 | for (var i = 0; i < PLAYER_1_LETTERS.length; i++) {
604 | var letter = PLAYER_1_LETTERS[i];
605 | PLAYER_1_POINTS -= POINTS_PER_LETTER[letter];
606 | }
607 |
608 | for (i = 0; i < PLAYER_2_LETTERS.length; i++) {
609 | letter = PLAYER_2_LETTERS[i];
610 | PLAYER_2_POINTS -= POINTS_PER_LETTER[letter];
611 | }
612 |
613 | document.getElementById("input_container").innerHTML='game over start new game';
614 | document.getElementById("input_container").style.display= "block";
615 |
616 | document.getElementById("move").disabled = true;
617 | document.getElementById('pass').disabled = true;
618 |
619 | var winText = i18n('Du gewinnst.');
620 | var looseText = i18n('Du verlierst.');
621 | var resultText = PLAYER_1_POINTS > PLAYER_2_POINTS ? winText : looseText;
622 |
623 | alert(
624 | i18n('Das Spiel ist aus.') + '\n' +
625 | resultText + '\n' +
626 | i18n("DU") + ": " + PLAYER_1_POINTS + ' ' + i18n("punkte") + '\n' +
627 | i18n("KI") + ": " + PLAYER_2_POINTS + ' ' + i18n("punkte")
628 | );
629 | }
630 |
631 | /**
632 | * pos: array of positions in game array to be filled with available letters
633 | * letters: array of available letters
634 | * result: object of indexes in game array and the letters to be set
635 | */
636 | function tryFreePositions(pos,letters,result) {
637 | var tryPos = pos.pop();
638 | TO_BE_PLAYED_BOARD_LETTER_INDEXES.push(tryPos);
639 |
640 | // try all letters available on current position
641 | for (var k = 0; k < letters.length; k++) {
642 | var tempLetter = letters.splice(k, 1)[0];
643 | BOARD_LETTERS[tryPos] = tempLetter;
644 | result[tryPos] = tempLetter;
645 |
646 | // more positions to fill, recurse
647 | if (pos.length > 0) {
648 |
649 | // recurse only if we have laid valid starts of words yet
650 | var recurse = true;
651 | var words = findWordsAndPointsByActiveLetters()[0];
652 |
653 | for (var i = 0; i < words.length; i++) {
654 | if (!isWordStartInDictionary(words[i])) {
655 | recurse = false;
656 | break;
657 | }
658 | }
659 | if (recurse) {
660 | tryFreePositions(pos, letters, result);
661 | }
662 | } else {
663 | var points = checkValidStateAndCalculatePoints();
664 |
665 | // store points
666 | // store position and letters in result
667 | if (points > MAX_POINTS) {
668 | MAX_POINTS = points;
669 | //copy by value
670 | MAX_RESULT = JSON.parse(JSON.stringify(result));
671 | }
672 | }
673 | BOARD_LETTERS[tryPos] = '';
674 | result[tryPos] = '';
675 | letters.insert(k, tempLetter);
676 | }
677 |
678 | pos.push(tryPos);
679 | TO_BE_PLAYED_BOARD_LETTER_INDEXES.splice(TO_BE_PLAYED_BOARD_LETTER_INDEXES.indexOf(tryPos), 1);
680 | }
681 |
682 | //fancy ki comes here
683 | function computerMove() {
684 | MAX_POINTS = 0;
685 | MAX_RESULT = {};
686 |
687 | // try all rows
688 | for (var row=0; row<15; row++) {
689 |
690 | // try all starting positions within a row
691 | for (var rowStart=0; rowStart < 14; rowStart++) {
692 |
693 | // beginning field of our word
694 | startPos = row*15 + rowStart;
695 |
696 | // the field left of our current word is not empty
697 | if (rowStart !== 0 && BOARD_LETTERS[rowStart-1] !== '') {
698 | continue;
699 | }
700 |
701 | // try all possible word lengths
702 | for (var wordLength=2; wordLength < 15 - rowStart; wordLength++) {
703 |
704 | // end field of our word
705 | var endPos = row*15 + rowStart + wordLength;
706 |
707 | // the field right of our current word is not empty
708 | if (endPos !== 15 && BOARD_LETTERS[endPos+1] !== '') {
709 | continue;
710 | }
711 |
712 | var free_letter_positions=[];
713 | var free_letter_count=0;
714 | var set_letter_count=0;
715 | for (i=startPos; i < endPos; i++) {
716 | if (BOARD_LETTERS[i] === '') {
717 | free_letter_positions[free_letter_count] = i;
718 | free_letter_count++;
719 | } else {
720 | set_letter_count++;
721 | }
722 | }
723 |
724 | // no letter set or
725 | // no free space to set a letter or
726 | // too many free spaces (should be up to number of tiles on player hand)
727 | if (set_letter_count === 0 || free_letter_count === 0 || free_letter_count > 3) {
728 | continue;
729 | }
730 |
731 | best_try = tryFreePositions(free_letter_positions, PLAYER_2_LETTERS, {});
732 | }
733 | }
734 | }
735 |
736 | // try all columns
737 | var best_try;
738 | for (var column = 0; column < 15; column++) {
739 |
740 | // the starting position inside the column
741 | for (var columnStart = 0; columnStart < 14; columnStart++) {
742 |
743 | // beginning field of our word
744 | var startPos = column + 15 * columnStart;
745 |
746 | // the field on top of our current word is not empty
747 | if (startPos > 14 && BOARD_LETTERS[startPos - 15] !== '') {
748 | continue;
749 | }
750 |
751 | // try all possible word lengths
752 | for (wordLength = 2; wordLength < 15 - columnStart; wordLength++) {
753 |
754 | // end field of our word
755 | endPos = startPos + (15 * wordLength);
756 |
757 | // the field below our current word is not empty
758 | if (endPos + 15 < 225 && BOARD_LETTERS[endPos + 15] !== '') {
759 | continue;
760 | }
761 |
762 | free_letter_positions = [];
763 | free_letter_count = 0;
764 | set_letter_count = 0;
765 | for (i = startPos; i < endPos; i += 15) {
766 | if (BOARD_LETTERS[i] === '') {
767 | free_letter_positions[free_letter_count] = i;
768 | free_letter_count++;
769 | } else {
770 | set_letter_count++;
771 | }
772 | }
773 |
774 | // no letter set or
775 | // no free space to set a letter or
776 | // too many free spaces (should be up to number of tiles on player hand)
777 | if (set_letter_count === 0 || free_letter_count === 0 || free_letter_count > 3) {
778 | continue;
779 | }
780 |
781 | best_try = tryFreePositions(free_letter_positions, PLAYER_2_LETTERS, {});
782 | }
783 | }
784 | }
785 |
786 | PLAYER_2_POINTS += MAX_POINTS;
787 |
788 | LETTERS_PLAYED_BY_KI_INDEXES = [];
789 | for (var i in MAX_RESULT) {
790 | LETTERS_PLAYED_BY_KI_INDEXES.push(parseInt(i));
791 | var letter_pos = PLAYER_2_LETTERS.indexOf(MAX_RESULT[i]);
792 | PLAYER_2_LETTERS.splice(letter_pos,1);
793 | BOARD_LETTERS[i] = MAX_RESULT[i];
794 | }
795 |
796 | TO_BE_PLAYED_BOARD_LETTER_INDEXES.length=0;
797 | drawTiles(PLAYER_2_LETTERS);
798 |
799 | if (MAX_POINTS === 0) {
800 | incrementAndCheckPassCount();
801 | } else {
802 | BOTH_PLAYERS_PASS_COUNT = 0;
803 | }
804 | printBoard();
805 | }
806 |
807 | function startGame() {
808 | BOARD = document.getElementById("board");
809 | // event handlers on board
810 | for (var i=0; i<15; i++) {
811 | for (var j=0; j<15; j++) {
812 | BOARD_LETTERS[i * 15 + j]='';
813 | BOARD.rows[i].cells[j].onclick=showLetterInput;
814 | }
815 | }
816 |
817 | document.getElementById("move").disabled = true;
818 |
819 | drawTiles(PLAYER_1_LETTERS);
820 | drawTiles(PLAYER_2_LETTERS);
821 | printPlayersLetters();
822 | printBoard();
823 | }
824 |
--------------------------------------------------------------------------------