SebLague / Chess-Challenge

Create your own tiny chess bot!
https://www.youtube.com/watch?v=Ne40a5LkK6A
MIT License
1.77k stars 1.06k forks source link

Run codes from strings in C# #487

Closed Gourab-Ghosh closed 1 year ago

Gourab-Ghosh commented 1 year ago

I have already completed the code of my mini bot. But I wanted to test if I could compress my code even further with the help of some strings. So I wanted to know if it is possible in C# to save a code inside a string and run it.

For example let the function is called Eval. So I want the following code to run properly.

Move bestMove;
public Move Think(Board board, Timer timer) {
    code = "void CalculateBestMove() { // Some code which edits the bestMove variable }"
    Eval(code);
    return bestMove
}

Thank you.

ryanheath commented 1 year ago

No that is not allowed.

Look for removing unneeded (squirly) brackets, repeated code/variables, mabye even smaller types, not everything needs to be an int.

Gourab-Ghosh commented 1 year ago

@ryanheath by saying this is not allowed, did you mean this is not possible in C# or not allowed in the rules? Because it is mentioned in #224 that adding code via string is allowed.

mcthouacbb commented 1 year ago

Adding code via ulongs is allowed, adding it via strings is not allowed besides, strings count as 1 token per character so they are extremely inefficient

ryanheath commented 1 year ago

Running other code than what is already compiled is not allowed. Try to use strings to compress used ulong tables is allowed, though I dont think you will gain much, if not anything.

Gourab-Ghosh commented 1 year ago

Adding code via ulongs is allowed, adding it via strings is not allowed besides, strings count as 1 token per character so they are extremely inefficient

I am exactly trying to do that. I know at this point how to store code into ulongs and how to generate code from those ulongs. But once I get the code I don't know how to make use of it. That is what I am asking, what to do with the string of code now? Is this string even useful in any way? I want to write my whole code in ulongs and then run it. Is it possible?

Gourab-Ghosh commented 1 year ago

Running other code than what is already compiled is not allowed. Try to use strings to compress used ulong tables is allowed, though I dont think you will gain much, if not anything.

Thanks for your help. Although I am more interested to store code in ulongs which I just explained in the previous comment, still I am interested to know how to compress ulongs through string. Can you show any little sample code for this?

ryanheath commented 1 year ago

If I understand you correctly, what you want is not allowed. Do you want to execute code based on the content of the string? That is not allowed.

224 allows simple values to be store in strings, but those values may not be use as executable code.

Gourab-Ghosh commented 1 year ago

If I understand you correctly, what you want is not allowed. Do you want to execute code based on the content of the string? That is not allowed.

224 allows simple values to be store in strings, but those values may not be use as executable code.

Exactly I wanted to do that only. But after this conversation I don't think it is possible. Storing information into a string will definitely take more tokens as strings are so expensive and I am not planning to do that. Anyways thank you for your awesome support.