http://wiki.math.bme.hu/history/Informatics2-2019/Lab08?feed=atomInformatics2-2019/Lab08 - Laptörténet2020-11-30T01:51:23ZAz oldal laptörténete a wikibenMediaWiki 1.18.1http://wiki.math.bme.hu/index.php?title=Informatics2-2019/Lab08&diff=13896&oldid=prevGaebor: Új oldal, tartalma: „previous up next = Exercises = Work in Spyder! == Shapes on a canvas == === Shapes …”2019-04-03T07:35:27Z<p>Új oldal, tartalma: „<a href="/edit/Informatics2-2019/Lab07?redlink=1" class="new" title="Informatics2-2019/Lab07 (a lap nem létezik)">previous</a> <a href="/view/Informatics2-2019#Labs" title="Informatics2-2019">up</a> <a href="/view/Informatics2-2019/Lab09" title="Informatics2-2019/Lab09">next</a> = Exercises = Work in Spyder! == Shapes on a canvas == === Shapes …”</p>
<p><b>Új lap</b></p><div>[[Informatics2-2019/Lab07|previous]] [[Informatics2-2019#Labs|up]] [[Informatics2-2019/Lab09|next]]<br />
<br />
= Exercises =<br />
Work in Spyder!<br />
<br />
== Shapes on a canvas ==<br />
=== Shapes ===<br />
Write a class called '''Shape'''. <br />
*Let it have two members: '''x''' and '''y''', the coordinates of the shape on the plane (center of mass). <br />
*Define a '''move''' method, with one parameter '''v''': a list of length 2, a vector to translate the shape with. After this method the coordinates should be changed.<br />
<br />
Define the following classes as children of '''Shape''':<br />
* '''Ellipse''' with additional parameters (except the ''(x, y)'' coordinates): '''a''' and '''b''' the ''x'' and ''y'' axes radii<br />
* '''Rectangle''' with additional parameters (except the ''(x, y)'' coordinates) '''a''' and '''b''' the length of the sides<br />
<br />
Write an '''area''' method for both, which calculates the area!<br />
<br />
Define an '''equation''' method for printing the equation of the '''Ellipse'''! Something like:<br />
<br />
((x-1)/2)^2 + ((y-2)/3)^2 = 1<br />
<br />
=== Inheritance and constructors ===<br />
If the child class (e.g. Ellipse) you want to call the parents class' constructor then you have two ways:<br />
<br />
class B(A):<br />
def __init__(self, x, y, a, b):<br />
A.__init__(self, x, y)<br />
# OR<br />
super(B, self).__init__(x, y)<br />
<br />
The first explicitly calls the parents <tt>__init__</tt><br />
<br />
A.__init__(self, x, y)<br />
<br />
The second one calls the parent of '''B''' which happens to be '''A''':<br />
<br />
super(B, self).__init__(x, y)<br />
<br />
=== Canvas ===<br />
Define a class called '''Canvas''' (vászon) <br />
* Its only member variable should be a list of Shapes: '''shapes'''. This stores several Shape objects.<br />
* Define an '''add''' method which adds a new shape to the canvas!<br />
* Make this class '''iterable'''! Write the '''__iter__(self)''' and the '''next(self)''' methods, as seen on the lecture.<br />
* Define a '''crop''' method in the following way.<br />
** Its two parameters should be two coordinates: a top-left corner and a bottom-right corner.<br />
** The function should return the list of Shapes which entirely fit in that rectangle.<br />
** For this it is best to implement a '''box()''' method on both '''Ellipse''' and '''Rectangle''' which returns a bounding box of the shape:<br />
a list of two coordinates, the top-left corner and a bottom-right corner of the object.<br />
<br />
<br />
== Iterables ==<br />
<br />
Write an iterable class like '''range''', but without returning a whole list, but storing only the actual element.<br />
<br />
class Range(object):<br />
def __init__( ... ):<br />
...<br />
def __iter__( ... ):<br />
...<br />
def next( ... ):<br />
...<br />
* Its constructor should have one parameter: a number or a string. The iteration should go up that number, from 0 with 1 steps.<br />
* If the number is not positive, then the iteration should take 0 steps.<br />
* If you get a string then try to convert it to a number. It it cannot be converted, then raise a '''ValueError''' exception.<br />
** If it is a valid integer, then calculate with that.<br />
* If you get the string '''"inf"''' then make the iteration go endless (infinite loop)!<br />
<br />
== Chess==<br />
Define a class called '''Piece'''. This will represent a chess piece (a figure on the chessboard).<br />
Store its position and its color (Black/White) and its '''__str__''' function should write the position like A2, G3 etc.<br />
Store the position as integers.<br />
<br />
* Define child classes '''King''' and '''Pawn'''!<br />
* Implement their '''__str__''' method which also prints the type of the figure. (You can implement it in the parent class, too)<br />
** Look the unicode characters of the figures: [https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode]<br />
** Or their [https://en.wikipedia.org/w/index.php?title=Algebraic_notation_(chess)#Piece_names_in_various_languages one letter names]<br />
* Every child class should have a '''.move(pos)''' method, where '''pos''' is a string like A3, G2 etc. Move the piece to that position if it is accepted by the chess rules. If the move was successful return '''True''' otherwise '''False'''!<br />
* Define a '''PieceMoveError''' exception. If the move is not allowed, then raise this exception!<br />
<br />
Implement the '''Board''' class. Store the list of figures.<br />
* Make an '''add''' method which puts a figure on the table.<br />
* Implement a '''move(pos1, pos2)''' method in '''Board''' which moves a figure from one position to the other, if the move is allowed (handle the '''PieceMoveError''' exception).<br />
** Mind that there may be an other figure on the '''pos2''' position. If the target position is occupied with the same player's figure, then the move is not allowed. If the target is the other player's figure, then remove their figure and replace it with the new figure (its a ''capture'').<br />
** Its easier if you implement an '''.occupied(pos)''' method first, which tells whether a given position is occupied or not.<br />
* Implement the other pieces: '''Knight, Rook, Bishop''' and '''Queen'''!<br />
** Start with the '''Rook''', its move is the simplest. Mind that the figure cannot pass through existing figures!<br />
** Implement the '''Knight''' class! The Knight and Queen can jump through other figures, so its not a problem for them.<br />
** After the bishop its not hard to implement '''Queen'''.<br />
* Define a '''check''' method in Board. Return the color of the player who's King is in a chess position, or empty string of there is no chess position!<br />
** Modify the Board's move method not to allow self-checks (when the active player makes its own King into a chess)!<br />
* Implement the '''__str__''' method of '''Board'''!<br />
<br />
After all these, implement the '''start''' method which assigns the board to a starting position.<br />
<br />
=== CLI ===<br />
* Make a chess game in command line: read the moves of the players with <tt>raw_input</tt> and print the board after each step. The white player starts and mind that the same player cannot move twice.<br />
** It's better to use [https://en.wikipedia.org/wiki/Algebraic_notation_(chess) algebraic notation] (Bf5, Qc3, Ne2, Kcd4, Kxd5 etc.) <br />
* Try it from command line<br />
<br />
[[Informatics2-2019/Lab07|previous]] [[Informatics2-2019#Labs|up]] [[Informatics2-2019/Lab09|next]]</div>Gaebor