Hacker game Follow-up: Starting from Goal games

Source: Internet
Author: User

Hacker game Follow-up: Starting from Goal games

A few days ago, I saw a simple and beautiful hacker programming game, so I almost chewed it ~, I also gave it to my colleagues. The code is very simple, but the idea is not clear, it is difficult to write concise code. I want to clarify the requirements and split the big issues into small ones.

(For more information, see the original document.Http://www.bkjia.com/Article/201409/338378.html)

I. Preliminary viewing rules

 

1. Rules are to encourage you to break the rules, and to be smart! 2. During program execution, the program should be printed with enough 'O' "goal" to display program functions. 3. Code g ()() ('al') must appear in the source code. I. g () ('al') cannot be a string; ii. 'al' must be a string or an equivalent type in other languages. you can create your own set of language standard methods (for example, C language must be used ", ruby can use either of the "or"; iii. g () ('al') must be a valid right value in your language (if applicable) 4, g () ('al') may not be able to output such a string, if you really cannot output a string in your language, you should submit a principle explanation: Why cannot it print an acceptable string! 5. You must be able to insert any number () for calling without modifying your scheme. If you have to modify the original scheme, it will fail. 6. Oh, remind me that g ('al') must return "gal ".

Note: If you want to test your skills, you can skip this step and write a piece of code that can be run before further analysis.

This rule looks a bit complicated. Let's take a look at the rule and think about what the game is about to do.

Because games require implementation in different languages, supporting () calls may be different in different languages. For example, C ++ may be a function imitation or constructor. The following functions are used to indicate the objects that can be called.

1. Write a function named g. 2. The g function must have two reloads: I. if one parameter exists, g + o + parameter ii is returned. if no parameter exists, a function similar to o is returned. The difference is that when one parameter is included, two o iii is added. if one parameter exists, g + parameter iv is returned. if there is no parameter, a function is returned. I call it an o function. The o function also requires two reloads ,... And so on.

Note: If you do not have the idea to try your skills, you are advised not to waste time reading it again.

Ii. Test the knife

Due to recent beginnersLuaThe lua function is first-class, so my intuition is that lua can solve this problem very easily, so the following is the implementation of the lua code. Other functions are first-class or languages that support closures, and can be easily transplanted or rewritten.

0 × 001: First Attempt

The problem is a bit complicated. Let's divide the problem into two parts:

1. constraint rules: various odd call Methods 2. Expected results: generate "g" + "o" * n + "al", such as gal, goal, and gooal.

In this case, it seems that the 1st parts are too difficult. We try to leave 1st parts alone and only implement the 2nd requirements.

 

 

The result is the same as expected. Now, you only need to modify the value of n to obtain the goal of n o.

0 × 002: Second Attempt

It seems that the progress is quite smooth. Let's continue. Based on the 1st-part rule, we need to write a g function that supports two types of overloading and returns different values (string/o function ), the returned o functions are similar. They support two types of overloading and return different values (strings/functions ). So, can o be g? Suppose this is feasible first. Let's try it:

Now let's analyze the reloads in two cases:

1. If one parameter exists, the return value is 'G' + 'O' * (number of calls to g-1) + parameter. 2. If no parameter exists, the return value is g.

 

 

We can see that we use a closure to easily solve this problem. When g is called with no parameters, o adds a 'O '.

0 × 003: the third attempt

The above code looks perfect, but g () ('al') cannot be called repeatedly, because g shares the same o variable. But this is easy to solve.

 

Yes. Just reset o before the final result is returned. This is the legendary "recursive ending condition ".

0 × 004: 4th attempts

If the above Code seems to be completely compliant with the game rules, or even meet the rules we have added (because the rules do not have requirements, they can be called repeatedly ). Now that a rule has been added, let's try again, that is, when g does not call g ('al'), for example, g ()(), call g () ('al') later and output gooal correctly.

To meet this condition, we can only let g return another function and then implement a real closure.

 

Iii. Conclusion

At this point, I think it is enough. It is time to end .. I don't know whether the problem is clearly described. What I want to talk about is that if we understand the requirements clearly, we can split the problem into small modules to break it down one by one. In the end, you can even find vulnerabilities in the Rules, strengthen the rules, and play with the rules.

Xiaobian: thanks to the enthusiastic participation of freebuffer, we look forward to more!

[Authorized by Jennal and released several times in the early morning by FreeBuf editor. The source address is http://jennal.com/2014/09/23/from goalgame.]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.