I tested my code again. This time comparing the outputs of the two ways.
Turn out the “+” operator works but not in a way I thought. I expected to add x to x, and y to y. However, the “+” operator just appends the numbers to the existing tuple and makes it longer.
At the time of solving the Lab, I did not know that this was not what expected. It shows a path and collection of movements. After solving the lab, NOW i know it was not a valid answer.
The source code is here( @sanity ):
from abc import ABC, abstractmethod
import random
class Player(ABC):
def __init__(self):
self.moves= []
self.position=(0,0)
self.path =[self.position]
def make_move(self):
print("\n_______________________\n Moves:",self.moves)
qq = random.choice(self.moves)
#self.position +=qq # Line 13
self.position = (qq[0]+self.position[0], qq[1]+self.position[1]) #Line 14
#print('self.position=',self.position)
self.path.append(self.position)
return self.position
@abstractmethod
def level_up(self):
pass
class Pawn(Player):
def __init__(self):
super().__init__()
self.moves += [(0,1), (1,0),(-1,0),(0,-1)]
def level_up(self):
self.moves +=[(1,1), (1,-1),(-1,1),(-1,-1)]
#if __name__ == '__main()__':
pawn = Pawn()
print(type(Player))
print("\n\n", type(pawn),'\n\n')
print(" pawn.make_move() => ",pawn.make_move())
print(" pawn.make_move() => ",pawn.make_move())
print(" pawn.make_move() => ",pawn.make_move())
print(" pawn.make_move() => ",pawn.make_move())
print('=====finished=====')
If I run it with “Line 14” and comment out “Line 13” the output is:
= RESTART: C:/Users/A/Forum, Build a Player Interface.py
<class 'abc.ABCMeta'>
<class '__main__.Pawn'>
_______________________
Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
pawn.make_move() => (-1, 0)
_______________________
Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
pawn.make_move() => (-1, 1)
_______________________
Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
pawn.make_move() => (0, 1)
_______________________
Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
pawn.make_move() => (-1, 1)
=====finished=====
Which is acceptable.
If I run it with “Line 13” and comment out “Line 14” the output is:
= RESTART: C:/Users/A/Forum, Build a Player Interface.py
<class 'abc.ABCMeta'>
<class '__main__.Pawn'>
_______________________
Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
pawn.make_move() => (0, 0, 1, 0)
_______________________
Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
pawn.make_move() => (0, 0, 1, 0, 0, 1)
_______________________
Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
pawn.make_move() => (0, 0, 1, 0, 0, 1, 0, -1)
_______________________
Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
pawn.make_move() => (0, 0, 1, 0, 0, 1, 0, -1, 0, 1)
=====finished=====
Which was not desired.
I consider this topic is to be closed and I am thankful for the help.
@ILM I expected the test system to check my code as a whole in Lab. In Workshop lessons I would accept your idea. But in a Lab I can write anything and the overall output of a function is examined. If I am wrong, would you please explain more?