How to resolve the algorithm 24 game/Solve step by step in the JavaScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 24 game/Solve step by step in the JavaScript programming language

Table of Contents

Problem Statement

Write a program that takes four digits, either from user input or by random generation, and computes arithmetic expressions following the rules of the 24 game. Show examples of solutions generated by the program.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 24 game/Solve step by step in the JavaScript programming language

This JavaScript code is designed to solve the 24-game puzzle, which involves using mathematical operators and parentheses to combine four numbers (typically digits) to obtain a value of 24. Here's a detailed explanation of the code:

  1. Initialization:

    • ar: An array to store the four numbers.
    • order: An array representing the order in which the numbers in ar will be used in calculations.
    • op: An array to store the operators (+-*/) corresponding to the three positions between the four numbers.
    • val: An array to store intermediate or final values during calculations.
  2. Constant: NOVAL is set to 9999, which is used as a placeholder for empty values in val.

  3. rnd(n): A helper function that generates a random integer between 0 and n-1.

  4. say(s): A helper function that prints the string s to the browser console or a text file (depending on the execution environment).

  5. getvalue(x, dir): This function extracts a non-NOVAL value from the val array. It starts at index x and searches in the given dir (1 for right, -1 for left) until a valid value is found. The found value is removed from val and returned.

  6. calc(): This function performs the calculations based on the current ar, order, and op arrays. It iterates over the three positions between the four numbers, performing the corresponding mathematical operation (addition, subtraction, multiplication, or division) using the getvalue function to retrieve values. The result is stored back in the val array.

  7. shuffle(s, n): A helper function that shuffles the elements of an array or string s of length n. It does this by randomly swapping elements within the array.

  8. parenth(n): A helper function that adds a specified number of open or close parentheses to the out string. It is used to handle parentheses in the final solution.

  9. getpriority(x): This function retrieves the priority of the number at position x in the order array, where 3 represents the highest priority and 1 the lowest.

  10. showsolution(): This function generates the final solution string out by combining the numbers in ar, operators in oper, and appropriate parentheses. It uses the getpriority function to determine the placement of parentheses based on operator priority.

  11. solve24(s): This function takes a string s containing four digits and attempts to solve the 24-game puzzle using the specified numbers. It generates random combinations of operator and number orders, performs calculations using calc(), and checks if the result is 24. If a solution is found, it calls showsolution() to display it.

  12. Main Function: The code calls solve24 with the strings "1234", "6789", and "1127" to solve the 24-game puzzle with these sets of numbers.

Source code in the javascript programming language

var ar=[],order=[0,1,2],op=[],val=[];
var NOVAL=9999,oper="+-*/",out;

function rnd(n){return Math.floor(Math.random()*n)}

function say(s){
 try{document.write(s+"<br>")}
 catch(e){WScript.Echo(s)}
}

function getvalue(x,dir){
 var r=NOVAL;
 if(dir>0)++x;
 while(1){
  if(val[x]!=NOVAL){
   r=val[x];
   val[x]=NOVAL;
   break;
  }
  x+=dir;
 }
 return r*1;
}

function calc(){
 var c=0,l,r,x;
 val=ar.join('/').split('/');
 while(c<3){
  x=order[c];
  l=getvalue(x,-1);
  r=getvalue(x,1);
  switch(op[x]){
   case 0:val[x]=l+r;break;
   case 1:val[x]=l-r;break;
   case 2:val[x]=l*r;break;
   case 3:
   if(!r||l%r)return 0;
   val[x]=l/r;
  }
  ++c;
 }
 return getvalue(-1,1);
}

function shuffle(s,n){
 var x=n,p=eval(s),r,t;
 while(x--){
  r=rnd(n);
  t=p[x];
  p[x]=p[r];
  p[r]=t;
 }
}

function parenth(n){
 while(n>0)--n,out+='(';
 while(n<0)++n,out+=')';
}

function getpriority(x){
 for(var z=3;z--;)if(order[z]==x)return 3-z;
 return 0;
}

function showsolution(){
 var x=0,p=0,lp=0,v=0;
 while(x<4){
  if(x<3){
   lp=p;
   p=getpriority(x);
   v=p-lp;
   if(v>0)parenth(v);
  }
  out+=ar[x];
  if(x<3){
   if(v<0)parenth(v);
   out+=oper.charAt(op[x]);
  }
  ++x;
 }
 parenth(-p);
 say(out);
}

function solve24(s){
 var z=4,r;
 while(z--)ar[z]=s.charCodeAt(z)-48;
 out="";
 for(z=100000;z--;){
  r=rnd(256);
  op[0]=r&3;
  op[1]=(r>>2)&3;
  op[2]=(r>>4)&3;
  shuffle("ar",4);
  shuffle("order",3);
  if(calc()!=24)continue;
  showsolution();
  break;
 }
}

solve24("1234");
solve24("6789");
solve24("1127");


  

You may also check:How to resolve the algorithm Smith numbers step by step in the Action! programming language
You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the Action! programming language
You may also check:How to resolve the algorithm Morse code step by step in the Wren programming language
You may also check:How to resolve the algorithm Determine if only one instance is running step by step in the Rust programming language
You may also check:How to resolve the algorithm Draw a clock step by step in the Ada programming language