بتل کد Battle Codeدارد باز می‌شود…

جلسه‌ی 38: شانس Chance

برنامه‌هایت تا امروز همیشه همان کار را می‌کردند. در این جلسه با Math.random و Math.floor شانس را وارد برنامه می‌کنی: یک تاس، یک رنگ تصادفی، یک لامپ تصادفی از ریسه و یک بازی حدس عدد.

در این جلسه یاد می‌گیری

درس

Math.random() هر بار یک عدد تازه می‌دهد: عددی اعشاری از 0 تا نزدیک 1 (خود 1 هیچ‌وقت نمی‌آید). دکمه را چند بار بزن و عددها را ببین. داخل پرانتزش چیزی نوشته نمی‌شود.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
</style>

<button id="go" onclick="press()">Random</button>
<p id="msg"></p>

<script>
let line = document.getElementById("msg");

function press() {
  line.textContent = Math.random();
}
</script>

عدد اعشاری به درد تاس نمی‌خورد. پس سه قدم برمی‌داریم. Math.random() * 6 عددی از 0 تا نزدیک 6 می‌دهد، مثل 3.72. Math.floor(...) اعشارش را دور می‌ریزد: 3. حالا عددمان از 0 تا 5 است؛ با + 1 می‌شود 1 تا 6. این یک تاس است!

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.board { font-size: 40px; width: 200px; margin: 12px auto; padding: 10px; border-radius: 12px; background: #e2e8f0; }
</style>

<div id="board" class="board">0</div>
<button id="roll" onclick="roll()">Roll</button>

<script>
let board = document.getElementById("board");

function roll() {
  let n = Math.floor(Math.random() * 6) + 1;
  board.textContent = n;
}
</script>

فرمول را یاد بگیر: Math.floor(Math.random() * n) یک عدد صحیح از 0 تا n - 1 می‌دهد. این دقیقاً همان شماره‌های خانه‌های یک آرایه‌ی n تایی است! پس برای برداشتن یک رنگ تصادفی، n را colors.length می‌گذاریم و اینجا + 1 لازم نیست.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.lamp { font-size: 80px; width: 130px; margin: 12px auto; border-radius: 24px; background: #334155; filter: grayscale(1); opacity: 0.5; }
.lamp.on { background: #fde047; filter: none; opacity: 1; }
</style>

<div id="lamp" class="lamp on">💡</div>
<button id="go" onclick="press()">Colour</button>
<p id="msg"></p>

<script>
let colors = ["red", "gold", "limegreen", "skyblue"];
let lamp = document.getElementById("lamp");
let line = document.getElementById("msg");

function press() {
  let i = Math.floor(Math.random() * colors.length);
  lamp.style.backgroundColor = colors[i];
  line.textContent = colors[i];
}
</script>

همین فرمول روی ریسه هم کار می‌کند. bulbs فهرست همه‌ی لامپ‌هاست و bulbs[i] یکی از آن‌ها. هر بار که دکمه را می‌زنی، یک لامپ تصادفی روشن یا خاموش می‌شود.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.lights { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin: 12px auto; }
.bulb { width: 34px; height: 34px; border-radius: 50%; background: #64748b; }
.bulb.on { background: #fde047; }
</style>

<div id="lights" class="lights">
  <span class="bulb"></span>
  <span class="bulb"></span>
  <span class="bulb"></span>
  <span class="bulb"></span>
  <span class="bulb"></span>
  <span class="bulb"></span>
  <span class="bulb"></span>
  <span class="bulb"></span>
</div>
<button id="go" onclick="press()">Random bulb</button>

<script>
let bulbs = document.querySelectorAll(".bulb");

function press() {
  let i = Math.floor(Math.random() * bulbs.length);
  bulbs[i].classList.toggle("on");
}
</script>

با if می‌توانی شانس بسازی. Math.random() < 0.5 نصف وقت‌ها درست است، مثل شیر یا خط. Math.random() < 0.2 فقط حدود ۲۰ بار از ۱۰۰ بار درست است. این لامپ با هر کلیک با شانس نصف‌نصف روشن یا خاموش می‌شود.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.lamp { font-size: 80px; width: 130px; margin: 12px auto; border-radius: 24px; background: #334155; filter: grayscale(1); opacity: 0.5; }
.lamp.on { background: #fde047; filter: none; opacity: 1; }
</style>

<div id="lamp" class="lamp">💡</div>
<button id="go" onclick="press()">Try</button>
<p id="msg"></p>

<script>
let lamp = document.getElementById("lamp");
let line = document.getElementById("msg");

function press() {
  if (Math.random() < 0.5) {
    lamp.classList.add("on");
    line.textContent = "On";
  } else {
    lamp.classList.remove("on");
    line.textContent = "Off";
  }
}
</script>

سه اشتباه رایج. یک: فراموش کردن Math.floor؛ آن‌وقت تاس 3.72 می‌آید. دو: جای غلط پرانتز: Math.floor(Math.random()) * 6 همیشه 0 است، چون اول عدد کوچک‌تر از یک گرد می‌شود و صفر می‌شود. ضرب باید داخل پرانتز floor باشد. سه: + 1 برای تاس لازم است (۱ تا ۶)، ولی برای شماره‌ی خانه‌ی آرایه نه (چون از صفر شروع می‌شود).

همین در پایتون: در پایتون اول import random می‌نویسی. تاس می‌شود random.randint(1, 6) و یک خانه‌ی تصادفی از فهرست می‌شود random.choice(colors). پایتون فرمول را خودش پشت صحنه حساب می‌کند؛ در جاوااسکریپت خودت می‌نویسی‌اش.

مثال‌های این جلسه

چالش‌ها

  1. Roll the Dice (آسان)
  2. Heads or Tails (آسان)
  3. A Random Colour (متوسط)
  4. One Random Bulb (متوسط)
  5. Guess the Number (سخت)
  6. Find the Bugs (سخت)

بیشتر بخوان

← جلسه‌ی 37: یادت بماند جلسه‌ی 39: وقتی کار خراب می‌شود →