Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple JS game of interconnected nodes - problems assigning player direction choice

    I'm developing a simple game that involves a system of interconnected nodes with unidirectional travel between nodes (similar to the circulation system!). The goal of the game is to get from a starting node to an ending node, which can be a variable number of nodes away.

    The program picks a random starting point, then randomly chooses one of its connecting nodes (cNodes) and pushes it onto a pathArray. A cNode is randomly chosen from this new node and it is pushed onto the pathArray. This continues for a designated number of turns, thus generating a pathArray (invisible to the player). The last element in the pathArray is the endNode and the goal of the puzzle.

    At each node the player is given two options of travel (though there may be more than two ways to go). One of these options MUST be the correct way if the player has not deviated from the path up until that point. If the player has deviated, this option can be any cNode. The other node is any cNode that does not lead to the endNode.

    The following code contains a simplified list of nodes that represents the content in my game. The function, however, is taken word for word. In this snippet, the pathArray & startNode have already been generated and I am trying to resolve how to assign "nodeChoice" as either the correct direction of travel (for a player on the correct path) or any random cNode (for a player who has deviated from the path). Keep in mind that the pathArray and cNodes lengths can be any size.

    Code:
    <script>
    
    //NODES:
    var nodeA = {name:"A"};
    var nodeB = {name:"B"};
    var nodeC = {name:"C"};
    var nodeD = {name:"D"};
    var nodeE = {name:"E"};
    var nodeF = {name:"F"};
    var nodeG = {name:"G"};
    var nodeH = {name:"H"};
    var nodeI = {name:"I"};
    var nodeJ = {name:"J"};
    var nodeK = {name:"K"};
    //An array of all nodes in the system:
    var systemArray = [nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, nodeG, nodeH, nodeI, nodeJ, nodeK];
    
    //Connecting Nodes (cNodes):
    //(uni-directional, but cyclical)
    nodeA.cNodes = [nodeB, nodeC];
    nodeB.cNodes = [nodeD, nodeE, nodeF];
    nodeC.cNodes = [nodeF, nodeG];
    nodeD.cNodes = [nodeI, nodeH];
    nodeE.cNodes = [nodeJ];
    nodeF.cNodes = [nodeK];
    nodeG.cNodes = [nodeK];
    nodeJ.cNodes = [nodeA];
    nodeK.cNodes = [nodeA];
    nodeI.cNodes = [nodeA];
    nodeH.cNodes = [nodeA];
    
    //The path chosen (generated from code not included here)
    var pathArray = [nodeA, nodeB, nodeE, nodeJ];
    
    //nodeChoice will represent a cNode from any given node
    var nodeChoice;
    
    //chooseNode is supposed to assign nodeChoice the next element in pathArray if the player on on the right path (if at nodeB, nodeChoice = nodeE).
    //However, if the user has taken a different path, its cNodes will not be in pathArray in which case a random cNode is assigned to nodeChoice
    function chooseNode(_node) {
    	//check each cNode to see if any are in pathArray
    	for (var j = 0; j < _node.cNodes.length; j++) {
    		//if a cNode is in pathArray, then we know to assign it nodeChoice...
    		if (_node.cNodes[j] in pathArray) {
    			nodeChoice = _node.cNodes[j];
    			console.log("choiceNode CORRECT: " + nodeChoice.name); //(for debugging purposes only)
    		}
    		//...otherwise don't do anything in this forLoop/ifStatement
    	};
    	//if by this point nodeChoice is still undefined, meaning none of the current node's cNodes are in pathArray, assign it any one of its cNodes.
    	if (nodeChoice == undefined) {
    		nodeChoice = _node.cNodes[Math.floor(Math.random()* _node.cNodes.length)];
    			console.log("choiceNode INCORRECT: " + nodeChoice.name);//(for debugging purposes only)
    	};	
    };
    
    //Runtime:
    
    chooseNode(nodeB);
    //Result should be only nodeE.name since nodeD is not in the pathArray...
    console.log(nodeChoice.name);
    
    </script>
    ...however, nodeChoice is assigned either D, E or F randomly and we are given the troubleshooting statement "choiceNode INCORRECT: D (or) E (or) F", indicating that the if-in statement is always ignored.

    I know that the if-in statement doesn't work but am not sure how else to write it so that each cNode is compared the each element in pathArray, both of which can be of variable lengths...

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,597
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    Don't see any "out" but to write a loop based compare.

    Find any match from set (array) 1 that is in any match from set 2.

    Code:
    function findMatch( arrA, arrB )
    {
        for ( var a = 0; a < arrA.length; ++a )
       {
            for ( var b = 0; b < arrB.length; ++b )
            {
                if ( arrA[a] == arrB[b] ) return arrA[a]; // ??? or do you want to just return arrA?
            }
        }
        return null;
    }
    Or something along those lines.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This works perfectly, thank you!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •