|
1 | 1 | '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++ |
11 | 11 | } |
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 |
17 | 17 | } |
18 | 18 | } |
19 | | - return ai_1 === 0 || ai_2 === 0; |
| 19 | + return ai_1 === 0 || ai_2 === 0 |
20 | 20 | } |
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 |
32 | 32 | } |
33 | 33 | } |
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 |
42 | 42 | } |
43 | 43 | } |
44 | | - return {moveAgain: move === ownStore, gameboard, gameboard}; |
| 44 | + return { moveAgain: move === ownStore, gameboard, gameboard } |
45 | 45 | } |
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] |
50 | 50 | } |
51 | | - return data; |
| 51 | + return data |
52 | 52 | } |
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 |
58 | 58 | } |
59 | | - participants.forEach(participant => { |
60 | | - participant.addScore(score[participant.team]); |
61 | | - }); |
| 59 | + participants.forEach((participant) => { |
| 60 | + participant.addScore(score[participant.team]) |
| 61 | + }) |
62 | 62 | } |
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 |
70 | 70 | } |
71 | 71 | } |
72 | | - while(match.gameboard[selectedMove] === 0){ // Validate |
73 | | - selectedMove++; |
| 72 | + while (match.gameboard[selectedMove] === 0) { // Validate |
| 73 | + selectedMove++ |
74 | 74 | } |
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() }) |
78 | 78 |
|
79 | 79 | // 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()) |
84 | 84 | } |
85 | 85 | } |
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()) |
90 | 90 | } |
91 | 91 | } |
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() |
98 | 98 | } |
99 | | - }else{ |
100 | | - callParticipant(match, aiIndex); |
| 99 | + } else { |
| 100 | + callParticipant(match, aiIndex) |
101 | 101 | } |
102 | | - }); |
| 102 | + }) |
103 | 103 | } |
104 | 104 | 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) |
109 | 109 | } |
110 | | - gameboard.push(0); |
| 110 | + gameboard.push(0) |
111 | 111 | } |
112 | 112 | let match = { |
113 | 113 | participants: null, |
114 | 114 | score: undefined, |
115 | 115 | 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) |
120 | 120 | } |
0 commit comments