I've progressed steadily on this issue, however I'm coming acrossed a problem in validating if a spot is clear or not... Perhaps my explanation isn't good so I'll just leave it in code:
func.CheckNear = function(field, pos)
local x, y = pos[1], pos[2];
local coordinates = {{x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}}
for key, array in next, coordinates do
local field = field[array[2]];
if field then
if (field[coordinates[1]]) then
if field[coordinates[1]] == "*" then
coordinates[key] = nil;
end;
else
coordinates[key] = nil;
end;
else
coordinates[key] = nil
end;
end;
return coordinates;
end;
Related
I know Lua remember value in the same field/function with the Same defined Variable. but I still lack the concept behind it. Let's say I'see Int Variable = 2 which can be modify if we iterate over it Int variable = 8
Same logic I was applying on the Code. I got the results, but I didn't understand the concept behind it.
You Can see in my Code I was saving some value in Var x and saveX
and I was doing saveX = x from my logic The value of x should be saved inside saveX instead it was saving the loop value as I'm applying over it. Why is that?
-- Graphical Representation of table1
--[[ { { {},{},{},{} },
{ {},{},{},{} },
{ {},{},{},{} },
{ {},{},{},{} } } ]]
_Gtable1 = {}
for y = 1, 4 do
table.insert(_Gtable1, {})
for x = 1, 4 do
table.insert(_Gtable1[y], {
x = (x - 1) * 10, --Coordinate value of X
y = (y - 1) * 10, --Coordinate value of Y
-- what if i want to save Coordinate value on another variable How should i do?
saveX = x, -- Saving Loop X
saveY = y, -- Saving Loo Y
t = "Check" -- to Debug
})
end
end
ti = _Gtable1
for y = 1, 4 do
for x = 1, 4 do
tim = ti[y][x]
-- print(tim.saveX) -- output 1, 2, 3, 4
print(tim.x) -- output 0, 10, 20, 30
end
end
I hope I understood the question:
Let's simplify the example:
local t = {
x = 10,
y = x,
}
You assume t.y is 10, because you declared it a line before that? But it is nil here, because x = 10, is no variable. It's part of the table constructor and can not be referenced in the same constructor. Check out https://www.lua.org/pil/3.6.html for more details.
After the constructor has finished, you can access that as t.x (not x!).
Now for your example, you need to move (x - 1) * 10 outside the constructor, e.g.: local tempX = (x - 1) * 10. Then use tempX inside the constructor. Or you can set your values individually, e.g. _Gtable1[y][x].saveX = _Gtable1[y][x].x. I would prefer the former.
I have the following functions:
P[t_] := P[t] = P[t-1] +a*ED[t-1];
ED[t_] := ED[t] = DF[t] + DC[t];
DF[t_] := DF[t] = b (F - P[t]);
DC[t_] := DC[t] = c (P[t] - F);
And the following parameters:
a=1;
c=0.2;
b = 0.75;
F=100;
In Mathematica I use the function "ListLinePlot" in order to plot P[t] and F:
ListLinePlot[{Table[P[t], {t, 0, 25}], Table[F, {t, 0, 25}]}, PlotStyle → {Black, Red},Frame → True, FrameLabel → {"time", "price"}, AspectRatio → 0.4, PlotRange → All]
How can I do this in wxMaxima? Is there a similar function or an alternative to ListLinePlot?
This is my attempt in wxMaxima:
P[t] := P[t-1] + a * ED[t-1];
ED[t] := DF[t] + DC[t];
DF[t] := b*[F-P[t]];
DC[t] := c*[P[t]-F];
a=1;
c=0.2;
b=0.75;
F=100;
And then I tried:
draw2d(points(P[t], [t,0,25]))
The plotted function should look like this:
OK, I've adapted the code you showed above. This works for me. I'm working with Maxima 5.44 on macOS.
P[t] := P[t-1] + a * ED[t-1];
ED[t] := DF[t] + DC[t];
DF[t] := b*(F-P[t]);
DC[t] := c*(P[t]-F);
a:1;
c:0.2;
b:0.75;
F:100;
P[0]: F + 1;
Pt_list: makelist (P[t], t, 0, 25);
load (draw);
set_draw_defaults (terminal = qt);
draw2d (points_joined = true, points(Pt_list));
Notes. (1) There needs to be a base case for the recursion on P. I put P[0]: F + 1. (2) Assignments are : instead of =. Note that x = y is a symbolic equation instead of an assignment. (3) Square brackets [ ] are only for subscripts and lists. Use parentheses ( ) for grouping expressions. (4) Syntax for draw2d is a little different, I fixed it up. (I put a default for terminal since the built-in value is incorrect for Maxima on macOS; if you are working on Linux or Windows, you can omit that.)
EDIT: Try this to draw a horizontal line as well.
draw2d (points_joined = true, points(Pt_list),
color = red, points([[0, F], [25, F]]),
yrange = [F - 1, P[0] + 1]);
I have an example of a code and not sure what way is the best to use.
For example I have
if (x = 1) and (y = 2) and (if abc = false then check if z = 3) then
begin
...
check only
if x = 1
if y = 2
if abc = false check z = 3. if abc = true then dont check z = 3
i am not sure if i am explaining the best but hopefuly people will understand.
I want to know if this is possible or the best way to do it. Keeping in mind that rather than in example where its x, y, z and abc. there can be more in my use.
I currently have structure as...which i dont think is practical, and think theres a better way but i am not sure
if (abc = false) then
begin
if (x = 1) and (y = 2) and (z = 3) then
begin
...
end
else
begin
if (x = 1) and (y = 2) then
begin
...
Thanks in advance
I think you're looking for or. Now you will check that x must be 1, y must be 2, and if abc is false, z must be 3.
If abc = true, z can still be three, but it won't be checked.
Note that I just wrote abc instead of abc = true. Since it's a Boolean (true/false) already, that's allowed.
Also note how the operations are grouped using parentheses. The total sub-expression abc or (z=3) must return true for the total expression to return true.
Furthermore the sequence of the terms is significant - they are evaluated left-to-right. If the term (abc or (z=3)) is replaced by the logically-equivalent term ((z=3) or abc) then z=3 will be evaluated.
if (x = 1) and (y = 2) and (abc or (z = 3)) then
// Your magic goes here
Test program body to prove sequence is important
function z : Integer;
begin
writeln('Z being evaluated');
result := x + y;
end;
begin
x := 1;y := 2;
abc := true;
if (x=1) and (y=2) and (abc or (z=3)) then
writeln ('evaluated true')
else
writeln ('evaluated false');
writeln('done');
readln;
end.
Neither of your code samples compile, because neither is using the proper syntax.
This should get you started:
if (x = 1) and (y = 2) then
begin
if (abc) then
// Handle abc = True
else
begin
if (z = 3) then
// Handle abc = false and z = 3
else
// Handle abc = false and z <> 3
end;
end;
this is a decode function for hexadecimal value,
I tried to figure out the encode function for it, but no luck.
function dtwin(flg: Integer): Integer;
var i:integer;
ner,yrd, yrv :Cardinal;
unr :Int64;
begin
ner := 1;
unr := flg;
yrd := $2E8CFFB0;
yrv := $0C8CFFF0;
for i := 1 to 32 do
begin
if (yrv and 1) <> 0 then
begin
ner := ((ner * unr) mod (yrd));
end;
unr := ((unr * unr) mod (yrd));
yrv := (yrv shr 1) and $7FFFFFFF;
end;
Result := ner;
end;
The short answer is: It can't be done.
Please read:
http://en.wikipedia.org/wiki/Modular_exponentiation
Let b = flg
Let e = $0C8CFFF0 = 210567152
Let m = $2E8CFFB0 = 780992432
Then this function is calculating ( b ^ e ) mod m
To reverse it, we need to find the multiplicative inverse of e mod m.
I tried using WolframAlpha.
http://www.wolframalpha.com/input/?i=multiplicative+inverse+of+210567152+mod+780992432
The result it gives is:
(210567152 is not invertible modulo 780992432)
The reason is that e and m are not co-prime. They are both divisible by two.
From this we can conclude that there is no way to reverse this function because there are colissions.
As an example:
dtwin(60) = dtwin(2326) = 62188800
What should the reverse function return when called with the parameter 62188800?
Should it return 60 or 2326?
Here are some more examples of collisions:
dtwin(658) = dtwin(1300) = 682595280
dtwin(60) = dtwin(2326) = 62188800
dtwin(1316) = dtwin(2600) = 76519712
dtwin(2312) = dtwin(3522) = 317601904
dtwin(1974) = dtwin(3900) = 52357088
dtwin(120) = dtwin(4652) = 144155936
dtwin(2632) = dtwin(5200) = 679101872
dtwin(3290) = dtwin(6500) = 322955216
dtwin(3989) = dtwin(6725) = 301338273
dtwin(180) = dtwin(6978) = 628048624
dtwin(4624) = dtwin(7044) = 435300992
dtwin(5080) = dtwin(7658) = 2152880
dtwin(3948) = dtwin(7800) = 682904608
dtwin(2685) = dtwin(8183) = 461799889
dtwin(2461) = dtwin(8951) = 170465
dtwin(4606) = dtwin(9100) = 138445536
dtwin(240) = dtwin(9304) = 231258592
dtwin(4741) = dtwin(9603) = 586985553
dtwin(6117) = dtwin(9923) = 277591073
To generate results that could be useful for encryption, you can generate numbers as follows.
I won't go into details here about how this works. You can Google public key cryptography if you need to know more.
Select P and Q that are prime.
Compute N = P * Q
Compute T = (P-1) * (Q-1) This is called the totient.
Select E that is coprime to N and T.
Select D that is the multiplicative inverse of E mod T.
Your modulus is N. The two exponents are E and D.
To encrypt A, calculate B = ( A ^ E ) mod N
To decrypt B, calculate A = ( B ^ D ) mod N
Note that in real-world encryption, these values typically have hundreds or thousands of digits.
Here are some results that are in the order of magnitude of your examples:
N = 590108483 =$232C5743
E = 547145911 =$209CC8B7
D = 507147559 =$1E3A7527
N = 763464677 =$2D818BE5
E = 545809367 =$208863D7
D = 622691303 =$251D83E7
N = 948703211 =$388C0FEB
E = 885205759 =$34C32AFF
D = 893844127 =$3546FA9F
N = 897918037 =$35852455
E = 894567871 =$355205BF
D = 539129719 =$20227777
N = 754905647 =$2CFEF22F
E = 540902531 =$203D8483
D = 534729131 =$1FDF51AB
I want to make some sort of a Vector3 library for Lua which could let you make simple 3D position operations with simple syntax. I'll mention that I'm using Luaj for running Lua code for Java manipulation.
Here's my beginning code:
Vector3 = {
new = function (x1, y1, z1)
return {x = x1, y = y1, z = z1}
end
}
Position1 = Vector3.new(1, 5, 8)
Position2 = Vector3.new(4, 7, 2)
And here's what I want to be able to happen:
Subtraction = Position1 - Position2
print(Subtraction.x, Subtraction.y, Subtraction.z) -- prints "-3, -2, 6"
Any idea on making EXACT code to work?
This is what metatables and metamethods are for. You should have a read in the documentation.
Basically, they let you redefine what operators (and some other things) do on your values. What you want right now is to define the __sub metamethod, which defines how to handle the - operator. I guess in the future you'll want to redefine the other metamethods as well.
First, define a subtraction function in your Vector3 "class" that takes two vectors:
function Vector3.subtract(u,v)
return Vector3.new(u.x - v.x, u.y - v.y, u.z - v.z)
end
Then create let Vector3 know the metatable it should give all vectors:
Vector3.mt = {__sub = Vector3.subtract}
And when you create a new vector:
new = function (x1, y1, z1)
local vec = {x = x1, y = y1, z = z1}
setmetatable(vec, Vector3.mt)
return vec
end
You could also make the metatable (mt) a local variable inside your new function - that would prevent external code from messing with the metatable (as it would be only accessible by your new function). However, having it inside Vector3 allows you to check against usages like v - "string":
function Vector3.subtract(u,v)
if getmetatable(u) ~= Vector3.mt or
getmetatable(v) ~= Vector3.mt then
error("Only vectors can be subtracted from vectors", 2)
end
return Vector3.new(u.x - v.x, u.y - v.y, u.z - v.z)
end
You could do something like this:
Vector3 = {}
mt = {}
function Vector3:new(_x, _y, _z)
return setmetatable({
x = _x or 0,
y = _y or 0,
z = _z or 0
}, mt)
end
mt.__sub = function(v1, v2) return Vector3:new(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z) end
mt.__tostring = function(v) return "Vector3=(" .. v.x .. "," .. v.y .. "," .. v.z .. ")" end
mt.__index = Vector3 -- redirect queries to the Vector3 table
-- test Vector3
Position1 = Vector3:new(1, 5, 8)
Position2 = Vector3:new(4, 7, 2)
Sub = Position1 - Position2
print(Sub)
which would print:
Vector3=(-3,-2,6)
More on Lua & OO, see: http://lua-users.org/wiki/ObjectOrientationTutorial