-5

i am developing a kid´s game.

It consists in

  • 3 Tags
  • 6 sacks

enter image description here

Each sack has one secret fruit inside:Bananas or Tomatoes The first step is to reveal one sack, for example imagine we reveal sack 2 and it is a T(Tomato)

enter image description here

Now the rule is that you have to assign Tags to Sacks , following the rule that no of to sacks has above a correct Tag

So we start to assign tags to sacks.

enter image description here

enter image description here

And finally:

enter image description here

I don´t know how to develop a recursive function that checks the validity of each movement. I have tried a lot of functions but it ´s becoming impossible. I think the best option is to create

  • Recursive function
  • Checks if any other Tag could be a possible solution of the sacks revealed.If it finds another tag that fit in the sacks (with future solution possible of course) is Game Over.

Which is the pseudo code of this function?Or you can imagine another way to develop this code?? I am really stuck and I ask here because you are awesome

Don´t forget that the game is looking for any other possible movement to return game over, he wants you to fail!

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
X.Otano
  • 2,079
  • 1
  • 22
  • 40

3 Answers3

1

Here I've got one possible solution without recursion. I know that you requested pseudo code, but I did it in JAVA just to make sure that is working. At least it will give you another direction where to look at.

Let's assume that you have an array that represent all your possible combinations, e.g:

  • "00" => Apple-Apple
  • "01" => Apple-Banana
  • "10" => Banana-Apple
  • "11" => Banana-Banana

So, let's do the validation to find out if the MOVEMENT is valid.

 public class TestCode {

    public static String[] stuff = new String[] { "00", "01", "10", "11" };
    public static Boolean isMySearchValid = false;

    public static void main(String[] args) {

        String first = "01"; // my first guess was "01" which is Apple-Banana

        isMySearchValid = isValid(first, "11"); // find out if 10 is valid and
                                                // also has not been taken
                                                // already
        System.out.println(isMySearchValid);
    }

    public static Boolean isValid(final String firstGuess, final String newGuess) {
        Boolean response = false;

        if (firstGuess == newGuess) {
            response = false; // Game Over, because the newGuess was already
                                // taken
            //System.out.println(response + " 1");
        } else {
            for (int i = 0; i < stuff.length; i++) {
                if (!stuff[i].contains(firstGuess)) {
                    if (stuff[i].contains(newGuess)) {
                        response = true;
                        System.out.println("VALID: because was found available in emelement : " + i);
                        break;
                    }

                }
            }
        }

        return response;
    }

}

2nd Approach: (Pseudo Code)

#define VALID_MOVE = 1;
#define TAG_ALREADY_EXIST = 2;
#define NOT_FOUND_OR_NOT_VALID = 3;


int isTagValid(sring[] ArrayOfTags, string tagToFind) {
    string[] tempArray = NULL;
    int response;

    if(tempArray != NULL) {
        foreach(string str in tempArray) {
            if (str == tagToFind)  {                
                response = TAG_ALREADY_EXIST;
            }
        }
    } else {
        foreach(string str in ArrayOfTags) {
            if (str == tagToFind) {
                tempArray.add(tagToFind);
                response = VALID_MOVE;
            } else {
                response = NOT_FOUND_OR_NOT_VALID;
            }
        }       

    }
    return response;
}
Chale CS
  • 353
  • 4
  • 15
  • thats not enough. The game is looking for the game over every round. This code assumes that the game once started , sets the sacks fruits. But the game is assigning them (the fruits) dinamically, so if there is a possible game over situation it whill choose this one. – X.Otano Jan 29 '15 at 07:02
  • How about this second approach? – Chale CS Jan 29 '15 at 16:26
  • You requested a pseudo-code to find out if the "move" is valid or not, regardless if the game has started or not. So, I gave you the solution in the 2nd approach, and your reply is "read the comment". You need to be more specific in your request in order to understand you. – Chale CS Jan 29 '15 at 19:38
  • Ok, you qre right,my english is not good enough to explain better,but i will give you the bounty as you take your effort – X.Otano Jan 29 '15 at 22:32
0

I'm going to assume that you have a fixed, small set of fruits, each with an associated id. The simplest way to implement this would be an enum:

enum Fruit {
    APPLE,
    PEAR,
    BANANA
};

This way, APPLE has the value 0, PEAR is 1, and BANANA is 2. Now you can represent any sack pair or card by an array telling how many occurrences there is of each fruit (each element would tell the number of occurrences of the fruit that corresponds to the index). For example, a card with two apples would be {2, 0, 0}, and a sack pair with apples and bananas would be {1, 0, 1}. For each sack pair, you would also keep track of the "visible" fruits: if the aforementioned sack pair hasn't been opened, we have {0, 0, 0}, and when we have only revealed the apples, we would have {1, 0, 0}.

Now, all you need to do to figure out whether a given card is compatible with what we know about a given sack pair is to check each pair of elements and see if all elements from the sack pair's array is less than or equal to the corresponding element of the card's array. No recursion needed:

bool isCompatible(const vector<int> & card, const vector<int> & sack) {
    for (int i = 0; i < card.size(); i++) {
         if (card[i] < sack[i])
              return false;
    }
    return true;
}

bool isLegalMove(const vector<int> & movedCard, const vector<vector<int> > & otherCards, const vector<int> & sack) {
    if (!isCompatible(movedCard, sack))
        return false;
    for (int i = 0; i < otherCards.size(); i++) {
        if (isCompatible(otherCards[i], sack))
            return false;
    }
    return true;
}

Edit: I still don't quite understand the mechanics of your game, but here's the general approach for simulating possible game moves until the game ends. The code assumes that any move will always make the game progress closer to the end, i.e. that it's not possible to go around in circles (if it were, you'd need additional code to keep track of which game states you've tried out).

I'm assuming that all of your game state is wrapped up in a class called GameState, and that a move can be represented by Move. In your case, you might have two subclasses of Move, namely OpenSack (containing the sack index) and MoveCard (containing the original and final positions of the card being moved). You need a function vector<Move *> generateMoves(GameState &) that can generate the moves that are possible in a given game state (note that if the game state is a "game over", this function should return an empty vector). Move needs an abstract function GameState perform(const GameState &), to be implemented by each subclass, that will actually perform the move and create a new GameState (rather than modifying the existing state) that represents the game state after the move. (In a game with a larger state, it would be better to actually modify the state, and to have a reverse() method that would undo the effects of a move.) Then, the simulation can be implemented like this:

bool isSolvable(const GameState & currentState) {
    if (currentState.isValidFinalState())
        return true;
    vector<Move *> moves = generateMoves(currentState);
    for (int i = 0; i < moves.size(); i++) {
         GameState stateAfterMove = moves[i]->perform(currentState);
         if (isSolvable(stateAfterMove))
             return true;
    }
    return false;
}

This will play out all possible moves (this is feasible only because it's such a "small" game), and if any sequence of moves leads to a valid solution, the function will return true.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • The problem is not how to develop the game.I have the states as enums as you say.the problem is how to check if a movement is valid .In case there are two possible movements after a sack revelation is game over – X.Otano Jan 21 '15 at 22:00
  • One movement is valid if it satisfies the rule of "no flag is in good position" and the flags has the same fruits that are in the sacks – X.Otano Jan 21 '15 at 22:01
  • @Badulake: I'm confused by exactly what a "movement" is. Is it the action of placing a card in front of a sack pair, or opening a sack, or either? – Aasmund Eldhuset Jan 21 '15 at 22:42
  • Place a card over the sack – X.Otano Jan 21 '15 at 22:44
  • @Badulake: And at the time a card is placed, you wish to check that that card matches what we currently see of the sack contents, and that no other cards do? – Aasmund Eldhuset Jan 21 '15 at 23:08
  • Thats it, in case another matches it s game over because the game is looking you to show you in the sacks another alternative – X.Otano Jan 21 '15 at 23:12
  • @Badulake: Then, I do believe I gave the approach in my second-to-last sentence, but I've expanded on it now. – Aasmund Eldhuset Jan 21 '15 at 23:54
  • Is not enough. With your code It could be the situation that you assign a card to to a sack, and it is a ilegal move because there ´s another card that fits in the sacks but will not fit in the game ending. You allways have to be able to solve the game in the future after a card is assigned, so i think the better is to develop a recursive function that checks the others card fits, and there is a way to complete the game succesfully (if you can´t get a "game success" is not a valid solution neither) .If you need and example of this tell me – X.Otano Jan 22 '15 at 07:02
  • @Badulake: Sorry about the delay; I've been very busy lately. See my update. – Aasmund Eldhuset Feb 05 '15 at 20:58
0

Notice that if you proposes another card to a sack as a gameover situation, the next movements should be able to solve the game succesfully. Is not enough to check the validity of 1 movement, you need to check until the game ends.

enter image description here

You assign the 3rd card (Banana| Tomato) to the 3-4 sacks cause is the only one which satisfies the 2 rules (fruits are the same in sacks/card & card not above sacks).L

Later, we do another move .We move the 1st card (Tomato| Tomato to the 3rd sack because we have to satisfy that the rule of "correct card is never above sacks". If the algorithm is one step only checking, it will says that the correct one is the other possibility=>card 2 (Banana|Banana), so it will show us game over. But what the algorithm doesn´t know is that the game became unsolvable because we have a card above the only sack pending to solve (Tomato|Tomato)

enter image description here

X.Otano
  • 2,079
  • 1
  • 22
  • 40