Makes the game deterministic

This commit is contained in:
2023-06-06 15:54:47 -04:00
parent ef550e4cca
commit ebdaa7d830
4 changed files with 118 additions and 19 deletions

View File

@@ -9,6 +9,17 @@ function valid_moves($board, $history)
if (count($row) != 4) {
return false;
}
foreach ($row as $card) {
if (!isset($card["number"]) || !isset($card["color"])) {
return false;
}
if ($card["number"] > 6 || $card["number"] < 1) {
return false;
}
if (!in_array($card["color"], ["red", "green", "blue", "yellow", "brown"])) {
return false;
}
}
}
$rounds = pow(2, 16) - 1;
foreach ($history as $round) {

View File

@@ -6,6 +6,69 @@ const state = {
};
const history = [];
const arr = [];
function numsToCard(arr) {
let [a, b, c] = arr;
if (a <= b) {
b++;
}
if (a <= c) {
c++;
}
if (b <= c) {
c++;
}
return [
{ color: colors[Math.floor(a / 6)], number: (a % 6) + 1 },
{ color: colors[Math.floor(b / 6)], number: (b % 6) + 1 },
{ color: colors[Math.floor(c / 6)], number: (c % 6) + 1 },
];
}
let curr_seed = Math.floor(Date.now() / 1000 / 3600 / 24);
function prand(str) {
let h1 = 1779033703,
h2 = 3144134277,
h3 = 1013904242,
h4 = 2773480762;
for (let i = 0, k; i < str.length; i++) {
k = str.charCodeAt(i);
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
}
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
return [
(h1 ^ h2 ^ h3 ^ h4) >>> 0,
(h2 ^ h1) >>> 0,
(h3 ^ h1) >>> 0,
(h4 ^ h1) >>> 0,
];
}
function numToString(num) {
let ret = "";
while (num >= 0) {
if (num % 100 <= 61) {
ret = String.fromCharCode((num % 100) + 65) + ret;
}
num = Math.floor(num / 100) - 1;
}
return ret;
}
const upper_bound = prand(numToString(curr_seed))[1];
function adjust_seed(inxOfSquare, inxOfCardPlayed) {
sqArray = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53];
cArray = [59, 61, 67];
curr_seed *= sqArray[inxOfSquare] * cArray[inxOfCardPlayed];
curr_seed %= upper_bound;
curr_board = prand(numToString(curr_seed))[0];
let c1 = curr_board % 30;
let c2 = Math.floor(curr_board / 30) % 29;
let c3 = Math.floor(curr_board / 870) % 28;
return [c1, c2, c3];
}
function render_game(state) {
if (state.picked_board_card !== null && state.picked_hand_card !== null) {
$("#play").button("enable");
@@ -62,13 +125,16 @@ function cellClicked(i, j) {
}
render_game(state);
}
function giveHand() {
state.hand = [0, 0, 0].map(() => {
return {
number: Math.floor(Math.random() * 8) + 1,
color: colors[Math.floor(Math.random() * colors.length)],
};
});
function giveHand(indexOfSquare = -1, inxOfCardPlayed = -1) {
if (indexOfSquare != -1 && inxOfCardPlayed != -1) {
state.hand = numsToCard(adjust_seed(inxOfCardPlayed, inxOfCardPlayed));
} else {
let curr_board = prand(numToString(curr_seed))[0];
let c1 = curr_board % 30;
let c2 = Math.floor(curr_board / 30) % 29;
let c3 = Math.floor(curr_board / 870) % 28;
state.hand = numsToCard([c1, c2, c3]);
}
}
// Generate hand and empty board
for (let i = 0; i < rows; i++) {
@@ -76,7 +142,7 @@ for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
add.push(null);
// add.push({
// number: Math.floor(Math.random() * 8) + 1,
// number: Math.floor(Math.random() * 6) + 1,
// color: colors[Math.floor(Math.random() * colors.length)],
// });
}
@@ -95,9 +161,12 @@ $("document").ready(() => {
});
state.board[state.picked_board_card[0]][state.picked_board_card[1]] =
state.hand[state.picked_hand_card];
giveHand(
state.picked_board_card[0] * 4 + state.picked_board_card[1],
state.picked_hand_card
);
state.picked_hand_card = null;
state.picked_board_card = null;
giveHand();
render_game(state);
});
render_game(state);

View File

@@ -21,8 +21,27 @@
<h1>Klumpy</h1>
<h3>Rules</h3>
<?php
echo "<p>$DESCRIPTION <a href='/klumpy/leaderboard.php'>There is also a leaderboard you can look at here.</a></p>";
echo "<p>$DESCRIPTION</p>";
?>
<h3>Calculating Points</h3>
<ol>
<li>
Having “clumps” of cards of the same color. Each clump is worth its area squared in points (i.e one large clump is more valuable than two smaller ones)
</li>
<li>
Having a large chain of consecutive numbers. The numbers must be connected orthogonally (i.e diagonals don't count). Points will be awarded based on the length of this chain and the lowest number within in. (Note: While a chain that goes 1-2 may be worth more points than one that goes 4-5-6, your longest chain will be the only one scored. In case of a tie of length, the more valuable chain is score).
</li>
<li>
Having strict-increasing rows of numbers. At the end of the game, the number of strictly-increasing rows will be counted and scored according to the following: 1 row-10 points, 2-rows-25 points, 3 rows-45 points, 4-rows 70 points.
</li>
<li>
Having large numbers. The sum of every card will be totaled and divided by two (rounded up). This will then be added to your total score.
</li>
<li>
Having a variety of numbers. Players get the number of unique cards squared in points.
</li>
</ol>
<p><a href='/klumpy/leaderboard.php'>Click here for the leaderboard.</a></p>
<?php
if (!$USERNAME) {
echo "<h2>You must be logged in to submit your score to the leaderboard and share your games. <a href='/login.php'>Login here</a></h2>";
@@ -64,11 +83,11 @@
</tr>
</table>
<p>Total Score: <span id="score"></span></p>
<p>Color Clump Score<span title="All clumps of colors are added together by squaring the number of neighboring colors." style="cursor: help" class="help ui-icon ui-icon-info"></span>: <span id="clump_score"></span></p>
<p>Single Run Score<span title="The number of points based on the longest run of consecutive numbers in any orthogonal direction. Uses the following formula (9 - lowest_value) * (highest_value - lowest_value + 1)." style="cursor: help" class="help ui-icon ui-icon-info"></span>: <span id="single_run_score"></span></p>
<p>Increasing Row Score<span title="You get 10 points for every row where the numbers are strictly increasing." style="cursor: help" class="help ui-icon ui-icon-info"></span>: <span id="increasing_row_across_score"></span></p>
<p>Total Sum Score<span title="This is just a sum of all the numbers divided by 2." style="cursor: help" class="help ui-icon ui-icon-info"></span>: <span id="tot_sum_scores"></span></p>
<p>Unique Number Bonus<span title="A bonus of 50 points if you have all the numbers on the board" style="cursor: help" class="help ui-icon ui-icon-info"></span>: <span id="all_number_scores"></span></p>
<p>Color Clump Score: <span id="clump_score"></span></p>
<p>Single Run Score: <span id="single_run_score"></span></p>
<p>Increasing Row Score: <span id="increasing_row_across_score"></span></p>
<p>Total Sum Score: <span id="tot_sum_scores"></span></p>
<p>Unique Number Bonus: <span id="all_number_scores"></span></p>
<br>
<div id='winGame' class='popup'>
<div class='popup-content'>
@@ -84,7 +103,7 @@
</div>
<script src="render.js?v=1.0.1"></script>
<script src="score.js?v=1.0.0"></script>
<script src="index.js?v=1.2.0"></script>
<script src="index.js?v=1.3.0"></script>
</body>
</html>

View File

@@ -155,14 +155,14 @@ function single_run_score(object) {
}
function increasing_row_across(object) {
let ret = 0;
let add = 0;
let add = 5;
for (let i = 0; i < rows; i++) {
let curr_num = object[i][0] ? object[i][0].number : 0;
for (let j = 1; j < cols; j++) {
if (object[i][j] === null) {
if (j == cols - 1) {
ret += add;
add += 5;
ret += add;
}
continue;
}
@@ -170,8 +170,8 @@ function increasing_row_across(object) {
break;
}
if (j == cols - 1 && curr_num < object[i][j].number) {
ret += add;
add += 5;
ret += add;
}
curr_num = object[i][j].number;
}