MasterMind Game Algorithm Problem

Κώδικας, πληροφορίες, ερωτήσεις και απαντήσεις σχετικές με την JavaScript.

Συντονιστές: WebDev Moderators, Super-Moderators

Απάντηση
rickinio
Δημοσιεύσεις: 1
Εγγραφή: 27 Ιούλ 2012 17:48

MasterMind Game Algorithm Problem

Δημοσίευση από rickinio » 30 Ιούλ 2012 09:39

Hello Please find bellow the code that i use to make a MasterMind like game.
For now people can see the PC selection so i can check if my algorith works.
Further more colors are only as visual part and don't have any functionality in the code.

Κώδικας: Επιλογή όλων

<!DOCTYPE html>
<html>
<head>
<script src="https&#58;//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<style>
</style>
</head>
<body>
<div id="firstcolor" name="firstcolor">
<script>
var x1 = Math.floor&#40;&#40;Math.random&#40;&#41;*4&#41;+1&#41;; // here PC selects the first random number &#40;color&#41;
document.write&#40;x1&#41;;
if&#40;x1==1&#41;&#123;
$&#40;"#firstcolor"&#41;.css&#40;&#123;'background-color'&#58; 'red'&#125;&#41;;
&#125;
else if &#40;x1==2&#41;&#123;
$&#40;"#firstcolor"&#41;.css&#40;&#123;'background-color'&#58; 'yellow'&#125;&#41;;
&#125;
else if &#40;x1==3&#41;&#123;
$&#40;"#firstcolor"&#41;.css&#40;&#123;'background-color'&#58; 'blue'&#125;&#41;;
&#125;
else if &#40;x1==4&#41;&#123;
$&#40;"#firstcolor"&#41;.css&#40;&#123;'background-color'&#58; 'green'&#125;&#41;;
&#125;
</script>
</div>
<div id="secondcolor">
<script>
var x2 = Math.floor&#40;&#40;Math.random&#40;&#41;*4&#41;+1&#41;;  // here PC selects the second random number &#40;color&#41;
document.write&#40;x2&#41;;
if&#40;x2==1&#41;&#123;
$&#40;"#secondcolor"&#41;.css&#40;&#123;'background-color'&#58; 'red'&#125;&#41;;
&#125;
else if &#40;x2==2&#41;&#123;
$&#40;"#secondcolor"&#41;.css&#40;&#123;'background-color'&#58; 'yellow'&#125;&#41;;
&#125;
else if &#40;x2==3&#41;&#123;
$&#40;"#secondcolor"&#41;.css&#40;&#123;'background-color'&#58; 'blue'&#125;&#41;;
&#125;
else if &#40;x2==4&#41;&#123;
$&#40;"#secondcolor"&#41;.css&#40;&#123;'background-color'&#58; 'green'&#125;&#41;;
&#125;
</script>
</div>
<div id="thirdcolor">
<script>
var x3 = Math.floor&#40;&#40;Math.random&#40;&#41;*4&#41;+1&#41;;  // here PC selects the third random number &#40;color&#41;
document.write&#40;x3&#41;;
if&#40;x3==1&#41;&#123;
$&#40;"#thirdcolor"&#41;.css&#40;&#123;'background-color'&#58; 'red'&#125;&#41;;
&#125;
else if &#40;x3==2&#41;&#123;
$&#40;"#thirdcolor"&#41;.css&#40;&#123;'background-color'&#58; 'yellow'&#125;&#41;;
&#125;
else if &#40;x3==3&#41;&#123;
$&#40;"#thirdcolor"&#41;.css&#40;&#123;'background-color'&#58; 'blue'&#125;&#41;;
&#125;
else if &#40;x3==4&#41;&#123;
$&#40;"#thirdcolor"&#41;.css&#40;&#123;'background-color'&#58; 'green'&#125;&#41;;
&#125;
</script>
</div>
<div id="fourthcolor">
<script>
var x4 = Math.floor&#40;&#40;Math.random&#40;&#41;*4&#41;+1&#41;;  // here PC selects the fourth random number &#40;color&#41;
document.write&#40;x4&#41;;
if&#40;x4==1&#41;&#123;
$&#40;"#fourthcolor"&#41;.css&#40;&#123;'background-color'&#58; 'red'&#125;&#41;;
&#125;
else if &#40;x4==2&#41;&#123;
$&#40;"#fourthcolor"&#41;.css&#40;&#123;'background-color'&#58; 'yellow'&#125;&#41;;
&#125;
else if &#40;x4==3&#41;&#123;
$&#40;"#fourthcolor"&#41;.css&#40;&#123;'background-color'&#58; 'blue'&#125;&#41;;
&#125;
else if &#40;x4==4&#41;&#123;
$&#40;"#fourthcolor"&#41;.css&#40;&#123;'background-color'&#58; 'green'&#125;&#41;;
&#125;
</script>
</div>
<form name="myForm" action="" method="post" onsubmit="return false">
<select id="firstselection" name="first" onchange="saveSelections&#40;this&#41;">
  <option >Choose</option>
  <option >1</option>
  <option >2</option>
  <option >3</option>
  <option >4</option>
  </select>
<select id="secondselection" name="second" onchange="saveSelections&#40;this&#41;">
  <option >Choose</option>
  <option >1</option>
  <option >2</option>
  <option >3</option>
  <option >4</option>
</select>
<select id="thirdselection" name="third" onchange="saveSelections&#40;this&#41;">
  <option >Choose</option>
  <option >1</option>
  <option >2</option>
  <option >3</option>
  <option >4</option>
</select>
<select id="fourthselection" name="forth" onchange="saveSelections&#40;this&#41;">
  <option >Choose</option>
  <option >1</option>
  <option >2</option>
  <option >3</option>
  <option >4</option>
</select>
<button onclick="showSelections&#40;&#41;">Show choices</button>
</form>
<script>
var choices = &#91;&#93;; // in this array i save the options that the player choosed from the drop down lists
function saveSelections&#40;info&#41; &#123; // that's the function that stores the options that the player did did
 if &#40;info.value != ''&#41; &#123;
   choices.push&#40;info.options&#91;info.selectedIndex&#93;.text&#41;;
 &#125;
&#125;
function showSelections&#40;&#41;&#123;
var matched =&#91;&#93;; // In this array i store the selections of the player that are correct numbers &#40;colors&#41; and are in the correct positions.
var colors = &#91;x1,x2,x3,x4&#93;; // In this array i store the random number &#40;color&#41; selections that the PC did.
var a=0; // Variable "a" is a variable that counts how many numbers are correct but in the wrong place.
for &#40;var i=0 ; i < choices.length ; i++&#41; // with this for loop i check "choices array" with "colors array" position by position 
if &#40;choices&#91;i&#93; == colors&#91;i&#93;&#41;&#123; // and if their value match 
matched.push&#40;choices&#91;i&#93;&#41;; // i populate the "matched array" with this value otherwise i populate the matched array with the value "zero"
choices&#91;i&#93;=0 // Also i change the value of "color and choices array" to zero for these positions so i know and won't check these positions again.
colors&#91;i&#93;=0&#125; //
else if &#40;choices&#91;i&#93; != colors&#91;i&#93;&#41;&#123; // here as i said if the values of "choices array" and "colors array" don't match i put a value "zero" in this position
matched.push&#40;0&#41;;&#125; // by doing this i can count later on how many values in this array are NOT "zero" and get the number of correct number &#40;colors&#41; in the correct position.
for &#40; var j=0 ; j<choices.length ; j++&#41; // with this for loop i can check which selected number &#40;colors&#41; are correct but in the wrong place
if &#40;&#40;choices&#91;j&#93; != 0&#41; && &#40;colors.indexOf&#40;choices&#91;j&#93;&#41; != -1&#41;&#41;&#123; // if this position has NOT value "zero AND the value of choices is contained in the colors array
var a =a+1; // THEN the variable "a" is increased by +1
colors&#91;colors.indexOf&#40;choices&#91;j&#93;&#41;&#93;=0;&#125; // and the position that was found in "colors array" is getting value "zero" so i won't count this position again.
alert&#40;"matched "+matched&#41;; // pop ups an alert with the "matched array"
alert&#40;"variable a "+a&#41;; // pop ups an alert with the count of "a" variable
&#125;;
</script>
 </body>
</html>
My main problem is that my algorith populates the matched array correctly but when it's time to count variable "a" it always brings me "zero"

I have tried to explain with details what every part of my code does but if you have further questions please don't hesitate to post.

Thanks in advance for your help!

Απάντηση

Επιστροφή στο “JavaScript και Frameworks”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες