Pārlūkot izejas kodu

Initial commit.

simondevyoutube 5 gadi atpakaļ
vecāks
revīzija
6334689bfe
8 mainītis faili ar 204 papildinājumiem un 0 dzēšanām
  1. 75 0
      base.css
  2. 19 0
      index.html
  3. BIN
      lizard.png
  4. 110 0
      main.js
  5. BIN
      paper.png
  6. BIN
      rock.png
  7. BIN
      scissors.png
  8. BIN
      spock.png

+ 75 - 0
base.css

@@ -0,0 +1,75 @@
+#score {
+  border: 6px solid white;
+  border-radius: 10px;
+  width: 200px;
+  height: 100px;
+  display: flex;
+  align-items: center;
+  justify-content: center;
+  color: white;
+  font-size: 4em;
+  text-shadow: 2px 2px 2px black;
+  margin: 1em;
+}
+
+#description {
+  font-size: 5em;
+  color: white;
+  height: 150px;
+  text-shadow: 2px 2px 2px black;
+}
+
+.header {
+  font-size: 3em;
+  color: white;
+  background: #404040;
+  text-align: center;
+  height: 2.5em;
+  text-shadow: 4px 4px 4px black;
+  display: flex;
+  justify-content: center;
+  align-items: center;
+}
+
+.container {
+  display: flex;
+  justify-content: center;
+  align-items: center;
+  flex-direction: column;
+}
+
+.selection {
+  display: flex;
+  justify-content: center;
+}
+
+.selection > div {
+  margin: 1em;
+}
+
+img {
+  margin: 0.5em;
+  height: 196px;
+}
+
+.base {
+  border-radius: 50%;
+  padding: 0.25em;
+}
+
+.unselected {
+  border: 8px solid white;
+}
+
+.selected {
+  border: 8px solid green;
+}
+
+.aiSelected {
+  border: 8px solid red;
+}
+
+body {
+  background: #202020;
+  margin: 0;
+}

+ 19 - 0
index.html

@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Javascript Beginner Projects: Rock Paper Scissors</title>
+  <link rel="stylesheet" type="text/css" href="base.css">
+</head>
+<body>
+  <div class="header">
+    <span>Rock Paper Scissors Lizard Spock!</span>
+  </div>
+  <div class="container">
+    <div id="score"></div>
+    <div id="description"></div>
+    <div id="selection" class="selection"></div>
+  </div>
+  <script src="main.js" type="module">
+  </script>
+</body>
+</html>

BIN
lizard.png


+ 110 - 0
main.js

@@ -0,0 +1,110 @@
+console.log('JavaScript Beginner Projects: Rock Paper Scissors Lizard Spock!');
+
+window.addEventListener('DOMContentLoaded', () => {
+  _Setup();
+});
+
+// The general idea here is that these represent all the possible ways to
+// win for a given choice. So, for example, if you were to lookup "scissors",
+// you can see that it cuts paper and decapitates lizard.
+const _SHAPES = {
+  scissors: {paper: 'cuts', lizard: 'decapitates'},
+  paper: {rock: 'covers', spock: 'disproves'},
+  rock: {lizard: 'crushes', scissors: 'crushes'},
+  lizard: {spock: 'poisons', paper: 'eats'},
+  spock: {scissors: 'smashes', rock: 'vaporizes'},
+};
+
+let _AIChoice = null;
+let _PlayerChoice = null;
+let _Score = [0, 0];
+
+
+// This sets up the choices by creating elements programmatically, and then
+// assigning the images to them and setting the onclick handler to handle
+// player choices.
+function _Setup() {
+  for (const k in _SHAPES) {
+    const div1 = document.createElement('div');
+    const div2 = document.createElement('div');
+    const img = document.createElement('img');
+    img.src = k + '.png';
+    div1.id = k;
+    div1.className = 'base unselected';
+    div1.appendChild(img);
+    div1.onclick = (e) => {
+      _Select(e);
+    };
+
+    div2.id = 'ai-' + k;
+    div2.className = 'base unselected';
+    div2.appendChild(div1);
+
+    document.getElementById('selection').appendChild(div2);
+  }
+  _UpdateScore();
+}
+
+function _UpdateScore() {
+  document.getElementById('score').innerText = _Score[0] + ':' + _Score[1];
+}
+
+// This is called when a player selects one of the choices, ie. rock, paper, etc...
+function _Select(evt) {
+  for (const k in _SHAPES) {
+    const node = document.getElementById(k);
+    node.className = "base unselected";
+  }
+  evt.currentTarget.className = "base selected";
+
+  _PlayerChoice = evt.currentTarget.id;
+
+  _AIChoose(50);
+}
+
+function _AIChoose(delay) {
+  for (const k in _SHAPES) {
+    const node = document.getElementById(k);
+    node.parentNode.className = "base unselected";
+  }
+
+  const possibleSelections = Object.keys(_SHAPES);
+  const roll = Math.floor(Math.random() * possibleSelections.length);
+
+  const choice = possibleSelections[roll];
+  const node = document.getElementById(choice);
+  node.parentNode.className = "base aiSelected";
+  _AIChoice = choice;
+
+  if (delay < 200) {
+    setTimeout(() => {
+      _AIChoose(delay + 5);
+    }, delay);
+  } else {
+    _ResolveWinner();
+  }
+}
+
+function _ResolveWinner() {
+  let desc = '';
+  if (_AIChoice == _PlayerChoice) {
+    desc = 'Tie game.'
+  } else if (_AIChoice in _SHAPES[_PlayerChoice]) {
+    desc = 'Player Wins: ';
+    desc += (_PlayerChoice + ' ' + _SHAPES[_PlayerChoice][_AIChoice] +
+        ' ' + _AIChoice + '.');
+    _Score[0] += 1;
+  } else {
+    desc = 'AI Wins: ';
+    desc += (_AIChoice + ' ' + _SHAPES[_AIChoice][_PlayerChoice] +
+        ' ' + _PlayerChoice + '.');
+    _Score[1] += 1;
+  }
+  document.getElementById('description').innerText = desc;
+  _UpdateScore();
+}
+
+
+function _IsWinner(player1, player2) {
+  return player2 in _CHOICES[player1];
+}

BIN
paper.png


BIN
rock.png


BIN
scissors.png


BIN
spock.png