main.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. console.log('JavaScript Beginner Projects: Rock Paper Scissors Lizard Spock!');
  2. window.addEventListener('DOMContentLoaded', () => {
  3. _Setup();
  4. });
  5. // The general idea here is that these represent all the possible ways to
  6. // win for a given choice. So, for example, if you were to lookup "scissors",
  7. // you can see that it cuts paper and decapitates lizard.
  8. const _SHAPES = {
  9. scissors: {paper: 'cuts', lizard: 'decapitates'},
  10. paper: {rock: 'covers', spock: 'disproves'},
  11. rock: {lizard: 'crushes', scissors: 'crushes'},
  12. lizard: {spock: 'poisons', paper: 'eats'},
  13. spock: {scissors: 'smashes', rock: 'vaporizes'},
  14. };
  15. let _AIChoice = null;
  16. let _PlayerChoice = null;
  17. let _Score = [0, 0];
  18. // This sets up the choices by creating elements programmatically, and then
  19. // assigning the images to them and setting the onclick handler to handle
  20. // player choices.
  21. function _Setup() {
  22. for (const k in _SHAPES) {
  23. const div1 = document.createElement('div');
  24. const div2 = document.createElement('div');
  25. const img = document.createElement('img');
  26. img.src = k + '.png';
  27. div1.id = k;
  28. div1.className = 'base unselected';
  29. div1.appendChild(img);
  30. div1.onclick = (e) => {
  31. _Select(e);
  32. };
  33. div2.id = 'ai-' + k;
  34. div2.className = 'base unselected';
  35. div2.appendChild(div1);
  36. document.getElementById('selection').appendChild(div2);
  37. }
  38. _UpdateScore();
  39. }
  40. function _UpdateScore() {
  41. document.getElementById('score').innerText = _Score[0] + ':' + _Score[1];
  42. }
  43. // This is called when a player selects one of the choices, ie. rock, paper, etc...
  44. function _Select(evt) {
  45. for (const k in _SHAPES) {
  46. const node = document.getElementById(k);
  47. node.className = "base unselected";
  48. }
  49. evt.currentTarget.className = "base selected";
  50. _PlayerChoice = evt.currentTarget.id;
  51. _AIChoose(50);
  52. }
  53. function _AIChoose(delay) {
  54. for (const k in _SHAPES) {
  55. const node = document.getElementById(k);
  56. node.parentNode.className = "base unselected";
  57. }
  58. const possibleSelections = Object.keys(_SHAPES);
  59. const roll = Math.floor(Math.random() * possibleSelections.length);
  60. const choice = possibleSelections[roll];
  61. const node = document.getElementById(choice);
  62. node.parentNode.className = "base aiSelected";
  63. _AIChoice = choice;
  64. if (delay < 200) {
  65. setTimeout(() => {
  66. _AIChoose(delay + 5);
  67. }, delay);
  68. } else {
  69. _ResolveWinner();
  70. }
  71. }
  72. function _ResolveWinner() {
  73. let desc = '';
  74. if (_AIChoice == _PlayerChoice) {
  75. desc = 'Tie game.'
  76. } else if (_AIChoice in _SHAPES[_PlayerChoice]) {
  77. desc = 'Player Wins: ';
  78. desc += (_PlayerChoice + ' ' + _SHAPES[_PlayerChoice][_AIChoice] +
  79. ' ' + _AIChoice + '.');
  80. _Score[0] += 1;
  81. } else {
  82. desc = 'AI Wins: ';
  83. desc += (_AIChoice + ' ' + _SHAPES[_AIChoice][_PlayerChoice] +
  84. ' ' + _PlayerChoice + '.');
  85. _Score[1] += 1;
  86. }
  87. document.getElementById('description').innerText = desc;
  88. _UpdateScore();
  89. }
  90. function _IsWinner(player1, player2) {
  91. return player2 in _CHOICES[player1];
  92. }