Skip to content

Commit 4922087

Browse files
authored
Merge pull request #12 from AlgorithmicGames/reboot
Reboot
2 parents 26b9c85 + b7ac4ad commit 4922087

5 files changed

Lines changed: 233 additions & 221 deletions

File tree

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Kalaha-Arena
2-
Gathers and manage all [Kalaha](https://en.wikipedia.org/wiki/Kalah) (a.k.s. Mancala) participants.
3-
Click image below to join the official Discord channel.
2+
3+
Gathers and manage all [Kalaha](https://en.wikipedia.org/wiki/Kalah) (a.k.s. Mancala) participants. Click image below to join the official Discord channel.
44
<br>[![Discord banner2](https://discord.com/api/guilds/765291928454823936/widget.png?style=banner2)](https://discord.gg/fxgQqacSgG)
55

66
## Rules
7-
You will receive an array that represents where the pellets are located. The first half is your side and the later is the oponents. The store is located last in each half.
8-
The `tick`-counter starts at 0 and raised 1 after a choise is made.
9-
The `switch`-counter starts at 0 and raised 1 when the player is swaped.
7+
8+
You will receive an array that represents where the pellets are located. The first half is your side and the later is the oponents. The store is located last in each half. The `tick`-counter starts at 0 and raised 1 after a choise is made. The `switch`-counter starts at 0 and raised 1 when the player is swaped.
109

1110
### Rules violation
11+
1212
If response violate any of this, the respons will be replaced with taking the first valid keep farest from the store.
13+
1314
- Caught exceeding the time limit (`executionSteps`) for tick execution defined by the tournament.
1415
- Trying to movie pellets from the opponent's side or the stores.

arena.js

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,120 @@
11
'use strict'
2-
function isGameFinished(gameboard){
3-
let ai_1 = 0;
4-
let ai_2 = 0;
5-
let length = gameboard.length;
6-
let lengthHalf = (length/2)-1;
7-
length--;
8-
for(let i = 0; i < length; i++){
9-
if(i === lengthHalf){
10-
i++;
2+
function isGameFinished(gameboard) {
3+
let ai_1 = 0
4+
let ai_2 = 0
5+
let length = gameboard.length
6+
let lengthHalf = (length / 2) - 1
7+
length--
8+
for (let i = 0; i < length; i++) {
9+
if (i === lengthHalf) {
10+
i++
1111
}
12-
let score = gameboard[i];
13-
if(i < lengthHalf){
14-
ai_1 += score;
15-
}else{
16-
ai_2 += score;
12+
let score = gameboard[i]
13+
if (i < lengthHalf) {
14+
ai_1 += score
15+
} else {
16+
ai_2 += score
1717
}
1818
}
19-
return ai_1 === 0 || ai_2 === 0;
19+
return ai_1 === 0 || ai_2 === 0
2020
}
21-
function doMove(gameboard, move, rules){
22-
let size = gameboard.length;
23-
let steps = gameboard[move];
24-
let ownStore = (size/2)-1;
25-
gameboard[move] = 0;
26-
while(0 < steps){
27-
move++;
28-
move %= size;
29-
if(move < size-1){ // If on last, do not add. Is opposite goal.
30-
steps--;
31-
gameboard[move] += 1;
21+
function doMove(gameboard, move, rules) {
22+
let size = gameboard.length
23+
let steps = gameboard[move]
24+
let ownStore = (size / 2) - 1
25+
gameboard[move] = 0
26+
while (0 < steps) {
27+
move++
28+
move %= size
29+
if (move < size - 1) { // If on last, do not add. Is opposite goal.
30+
steps--
31+
gameboard[move] += 1
3232
}
3333
}
34-
if(rules.empty_capture){
35-
if(move < ownStore && 1 === gameboard[move]){
36-
let score = gameboard[move];
37-
gameboard[move] = 0;
38-
let oppositeSide = size - move - 2;
39-
score += gameboard[oppositeSide];
40-
gameboard[oppositeSide] = 0;
41-
gameboard[ownStore] += score;
34+
if (rules.empty_capture) {
35+
if (move < ownStore && 1 === gameboard[move]) {
36+
let score = gameboard[move]
37+
gameboard[move] = 0
38+
let oppositeSide = size - move - 2
39+
score += gameboard[oppositeSide]
40+
gameboard[oppositeSide] = 0
41+
gameboard[ownStore] += score
4242
}
4343
}
44-
return {moveAgain: move === ownStore, gameboard, gameboard};
44+
return { moveAgain: move === ownStore, gameboard, gameboard }
4545
}
46-
function sumBoard(gameboard){
47-
let data = [0,0];
48-
for(let i = 0; i < gameboard.length; i++){
49-
data[i < gameboard.length/2 ? 0 : 1] += gameboard[i];
46+
function sumBoard(gameboard) {
47+
let data = [0, 0]
48+
for (let i = 0; i < gameboard.length; i++) {
49+
data[i < gameboard.length / 2 ? 0 : 1] += gameboard[i]
5050
}
51-
return data;
51+
return data
5252
}
53-
function sumScore(score, gameboardLength, startValue, participants){
54-
let boardValue = gameboardLength*startValue;
55-
let errorFound = boardValue != score[0] + score[1];
56-
if(errorFound){
57-
return null;
53+
function sumScore(score, gameboardLength, startValue, participants) {
54+
let boardValue = gameboardLength * startValue
55+
let errorFound = boardValue != score[0] + score[1]
56+
if (errorFound) {
57+
return null
5858
}
59-
participants.forEach(participant => {
60-
participant.addScore(score[participant.team]);
61-
});
59+
participants.forEach((participant) => {
60+
participant.addScore(score[participant.team])
61+
})
6262
}
63-
function callParticipant(match, aiIndex){
64-
let participant = match.participants.get(aiIndex%2, 0);
65-
participant.postMessage(match.gameboard).then(response => {
66-
let selectedMove = 0;
67-
if(response.message){
68-
if(0 <= response.message.data && response.message.data < match.gameboard.length/2 && 0 < match.gameboard[response.message.data]){ // Check if legal move.
69-
selectedMove = response.message.data;
63+
function callParticipant(match, aiIndex) {
64+
let participant = match.participants.get(aiIndex % 2, 0)
65+
participant.postMessage(match.gameboard).then((response) => {
66+
let selectedMove = 0
67+
if (response.message) {
68+
if (0 <= response.message.data && response.message.data < match.gameboard.length / 2 && 0 < match.gameboard[response.message.data]) { // Check if legal move.
69+
selectedMove = response.message.data
7070
}
7171
}
72-
while(match.gameboard[selectedMove] === 0){ // Validate
73-
selectedMove++;
72+
while (match.gameboard[selectedMove] === 0) { // Validate
73+
selectedMove++
7474
}
75-
let moveData = doMove(match.gameboard, selectedMove, match.settings.rules);
76-
match.gameboard = moveData.gameboard;
77-
ArenaHelper.log('tick', {mover: participant.name, gameboard: match.gameboard.slice()});
75+
let moveData = doMove(match.gameboard, selectedMove, match.settings.rules)
76+
match.gameboard = moveData.gameboard
77+
ArenaHelper.log('tick', { mover: participant.name, gameboard: match.gameboard.slice() })
7878

7979
// Switch AI
80-
if(!moveData.moveAgain){
81-
aiIndex++;
82-
for(let i=0; i < match.gameboard.length/2; i++){
83-
match.gameboard.push(match.gameboard.shift());
80+
if (!moveData.moveAgain) {
81+
aiIndex++
82+
for (let i = 0; i < match.gameboard.length / 2; i++) {
83+
match.gameboard.push(match.gameboard.shift())
8484
}
8585
}
86-
if(isGameFinished(match.gameboard)){
87-
if(aiIndex%2){
88-
for(let i=0; i < match.gameboard.length/2; i++){
89-
match.gameboard.push(match.gameboard.shift());
86+
if (isGameFinished(match.gameboard)) {
87+
if (aiIndex % 2) {
88+
for (let i = 0; i < match.gameboard.length / 2; i++) {
89+
match.gameboard.push(match.gameboard.shift())
9090
}
9191
}
92-
match.participants.terminateAllWorkers();
93-
let score = sumScore(sumBoard(match.gameboard), match.gameboard.length-2, match.settings.gameboard.startValue, match.participants);
94-
if(score === null){
95-
ArenaHelper.postAbort(participant, 'General error - Illegal final score.');
96-
}else{
97-
ArenaHelper.postDone();
92+
match.participants.terminateAllWorkers()
93+
let score = sumScore(sumBoard(match.gameboard), match.gameboard.length - 2, match.settings.gameboard.startValue, match.participants)
94+
if (score === null) {
95+
ArenaHelper.postAbort(participant, 'General error - Illegal final score.')
96+
} else {
97+
ArenaHelper.postDone()
9898
}
99-
}else{
100-
callParticipant(match, aiIndex);
99+
} else {
100+
callParticipant(match, aiIndex)
101101
}
102-
});
102+
})
103103
}
104104
ArenaHelper.init = (participants, settings) => {
105-
let gameboard = [];
106-
for(let i = 0; i < 2; i++){
107-
for(let n=0; n < settings.gameboard.boardLength; n++){
108-
gameboard.push(settings.gameboard.startValue);
105+
let gameboard = []
106+
for (let i = 0; i < 2; i++) {
107+
for (let n = 0; n < settings.gameboard.boardLength; n++) {
108+
gameboard.push(settings.gameboard.startValue)
109109
}
110-
gameboard.push(0);
110+
gameboard.push(0)
111111
}
112112
let match = {
113113
participants: null,
114114
score: undefined,
115115
gameboard: gameboard,
116-
settings: settings
117-
};
118-
match.participants = participants;
119-
callParticipant(match, 0);
116+
settings: settings,
117+
}
118+
match.participants = participants
119+
callParticipant(match, 0)
120120
}

properties.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"header": {
33
"limits": {
4-
"participants": {"min": 2, "max": 2},
5-
"teams": {"min": 2, "max": 2},
6-
"participantsPerTeam": {"min": 1, "max": 1}
4+
"participants": { "min": 2, "max": 2 },
5+
"teams": { "min": 2, "max": 2 },
6+
"participantsPerTeam": { "min": 1, "max": 1 }
77
},
88
"replay": "/replay/"
99
},
@@ -12,8 +12,8 @@
1212
"boardLength": 6,
1313
"startValue": 4,
1414
"_meta": {
15-
"boardLength": {"min": 1, "max": null},
16-
"startValue": {"min": 1, "max": null}
15+
"boardLength": { "min": 1, "max": null },
16+
"startValue": { "min": 1, "max": null }
1717
}
1818
},
1919
"rules": {

replay/index.html

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Replay</title>
6-
<script async src="https://www.googletagmanager.com/gtag/js?id=G-TJFMG29186"></script><script>window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', 'G-TJFMG29186');</script>
6+
<script async src="https://www.googletagmanager.com/gtag/js?id=G-TJFMG29186"></script>
7+
<script>
8+
window.dataLayer = window.dataLayer || []
9+
function gtag() {
10+
dataLayer.push(arguments)
11+
}
12+
gtag('js', new Date())
13+
gtag('config', 'G-TJFMG29186')
14+
</script>
715
<meta content="width=device-width, initial-scale=1.0" name="viewport">
816
<meta content="yes" name="mobile-web-app-capable">
917
<style>
10-
#gamebord-wrapper {
18+
#gameboard-wrapper {
1119
text-align: center;
1220
}
1321
#secund-player {
@@ -19,7 +27,7 @@
1927
text-align: right;
2028
}
2129

22-
#gamebord {
30+
#gameboard {
2331
cursor: default;
2432
user-select: none;
2533
display: inline-block;
@@ -41,14 +49,14 @@
4149
justify-content: center;
4250
align-items: center;
4351
}
44-
#score-board{
52+
#score-board {
4553
border: 1px solid black;
46-
background-color: rgba(0,0,0,.9);
54+
background-color: rgba(0, 0, 0, 0.9);
4755
border-radius: 25px;
4856
padding: 15px;
4957
text-align: left;
5058
}
51-
#score-board tr>th:not(:first-child), #score-board tr>td:not(:first-child) {
59+
#score-board tr > th:not(:first-child), #score-board tr > td:not(:first-child) {
5260
padding-left: 1em;
5361
}
5462

@@ -68,7 +76,7 @@
6876
display: flex;
6977
}
7078

71-
#column-up>.square, #column-down>.square {
79+
#column-up > .square, #column-down > .square {
7280
transform: translate(0%, 50%);
7381
display: inline-grid;
7482
}
@@ -81,13 +89,13 @@
8189
margin: 1px;
8290
}
8391
</style>
84-
<script src="https://Algorithmic.Games/ReplayHelper.js"></script>
92+
<script src="/ReplayHelper.js"></script>
8593
<script src="index.js"></script>
8694
</head>
8795
<body onload="a()">
88-
<div id="gamebord-wrapper">
96+
<div id="gameboard-wrapper">
8997
<span id="secund-player"></span>
90-
<div id="gamebord">
98+
<div id="gameboard">
9199
<div class="column" id="column-up">
92100
<!-- Square -->
93101
</div>
@@ -106,7 +114,7 @@
106114
<!-- Square -->
107115
</div>
108116
</div>
109-
<div id="score-board-outer-wrapper" style="display: none;">
117+
<div id="score-board-outer-wrapper" style="display: none">
110118
<div id="score-board-inner-wrapper">
111119
<div id="score-board"></div>
112120
</div>

0 commit comments

Comments
 (0)