[Seed7-users] Help in converting startrek to Python
Interpreter and compiler for the Seed7 programming language.
Brought to you by:
thomas_mertes
|
From: Joseph K. <joe...@gm...> - 2017-03-20 08:15:52
|
Hello there.
I am looking at returinng to University to do a B.A Music or a B.Sc
Computer Science (or both - I'm not sure yet). In order to get a headstart
on the B.Sc, I read the syllabus and saw the introductory programming paper
was a Python-based one, so I thought I'd have a go at writing some Python.
I thought what would be easiest given that I haven't really programmed for
20 odd years was to find an existing bit of code and convert it. In a
completely unrelated search, I found the Seed7 listing for the classic Star
Trek game, and I thought that would be a fun thing to work with.
To my satisfaction and mild surprise, I have got nearly all of it working
and behaving like it should (I'm using the 'global' declaration in Python,
which it seems would normally get me burnt at the stake, but for what I
need, it works, so I'm at this point not too concerned).
The only thing I can't quite get working is the movement / torpedo code,
which according to what I have read, should just work.
The Seed7 code:
row := sectorRow;
column := sectorColumn;
x1 := flt(column) + 0.5;
y1 := flt(row) + 0.5;
angle := (course - 1.0) * 0.785398;
delta_x := cos(angle);
delta_y := -sin(angle);
inquad := TRUE;
blocked := FALSE;
number := 1;
while number <= distance do
y1 := y1 + delta_y;
x1 := x1 + delta_x;
row := trunc(y1);
column := trunc(x1);
if column < 1 or column > 8 or row < 1 or row > 8 then
inquad := FALSE;
number := distance;
else
if sect[row][column] <> 1 then (* Object blocking move *)
blocked := TRUE;
number := distance;
end if;
end if;
incr(number);
and my code:
num = 0
inQuad = False
blocked = False
x1 = 0.0
y1 = 0.0
row = 0
column = 0
angle = 0.0
deltaX = 0.0
deltaY = 0.0
row = sectorRow
column = sectorColumn
x1 = float(column) + 0.5
y1 = float(row) + 0.5
angle = (course - 1.0) * 0.785398
deltaX = math.cos(angle)
deltaY = -math.sin(angle)
inQuad = True
blocked = False
num = 1
while num <= distance:
y1 += deltaY
x1 += deltaX
row = int(round(y1))
column = int(round(x1))
if column < 0 or column > 7 or row < 0 or row > 7:
inQuad = False
num = distance
else:
if sect[row][column] != 1:
blocked = True
num = distance
num += 1
The sin and cos functions according to Seed7 and Python should output the
same numbers, and I think the inputs are the same, but when I try to move,
it doesn't correspond to what the help text says to expect.
The only thing I can think of is I'm not using incr() at the end (I'm just
going num += 1), and that's putting it all wrong, but I'm not sure.
Can anybody who knows Seed7 and a bit of Python give me some advice? I'm
just about there in terms of making it work, and this is the last real
hurdle.
Any help would greatly appreciated.
Joseph Karl.
|