Informatics2-2018/Lab08

A MathWikiből
(Változatok közti eltérés)
10. sor: 10. sor:
 
* Implement their '''__repr__''' method which also prints the type of the figure. (You can implement it in the parent class, too)
 
* Implement their '''__repr__''' method which also prints the type of the figure. (You can implement it in the parent class, too)
 
** Look the unicode characters of the figures: [https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode]
 
** Look the unicode characters of the figures: [https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode]
* Every child class should have a '''.move(pos)''' method, where '''pos''' is a string like A3, G2 etc.
+
* 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'''!
Move the piece to that position if it is accepted by the chess rules. If the move was successful return '''True''' otherwise '''False'''!
+
 
* Define a '''PieceMoveError''' exception. If the move is not allowed, then raise this exception!
 
* Define a '''PieceMoveError''' exception. If the move is not allowed, then raise this exception!
  
Legyen most egy '''Board''' osztályunk! Két listát tároljunk: '''white''' és '''black''', a játékosok bábuival!
+
Implement the '''Board''' class. Store the list of figures.
* Legyen a '''Board''' osztálynak egy '''move(player, pos1, pos2)''' metódusa, ami a '''pos1''' pozícióban álló bábut a '''pos2''' helyre mozgatja, ha a lépés szabályos! Ehhez definiáljuk át a '''King''' és a '''Pawn''' osztályok '''move''' metódusát, hogy paraméterül kapjon egy '''board''' változót, ami a tábla!
+
* Make an '''add''' method which puts a figure on the table.
** A normál szabályok mellett vegyük figyelembe, hogy áll-e ott más bábu! Ha az a bábu sajátunk, a lépés szabálytalan, ha az ellenfél bábuja, akkor távolítsuk el a pályáról, hiszen leütöttük!
+
* 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).
** Írjuk meg a '''Knight, Rook, Bishop''' és '''Queen''' osztályokat!
+
** 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'').
*** Kezdjük a '''Rook'''-kal, ez a legegyszerűbb! Ügyeljünk rá, hogy a ne lépjünk át másik bábun, hiszen ez szabálytalan!
+
** Its easier if you implement an '''.occupied(pos)''' method first, which tells whether a given position is occupied or not.
*** Másodikként a '''Knight''' osztályt írjuk meg! A ló ugorhat, tehát a szabályok írásánál ezt nem kell figyelembe venni.
+
* Implement the other pieces: '''Knight, Rook, Bishop''' and '''Queen'''!
*** A '''Bishop''' után a '''Queen''' lépését könnyű összerakni.
+
** Start with the '''Rook''', its move is the simplest. Mind that the figure cannot pass through existing figures!
** Oldjuk meg, hogy a '''Pawn''' ne állhasson az ellenfél alapvonalán!
+
** Implement the '''Knight''' class! The Knight and Queen can jump through other figures, so its not a problem for them.
* A '''Board''' osztályon belül definiáljunk egy '''check''' metódust. Térjen vissza azzal a színnel, amelyik király sakkban áll!
+
** After the bishop its not hard to implement '''Queen'''.
** Definiáljuk át a lépést, hogy az csak akkor legyen szabályos, ha a lépő játékos királya nem áll sakkban a lépés után!
+
* Define a '''check''' method in Board. Return whether there is a ''chess'' or not!
* Legyen '''__repr__''' függvénye a '''Board'''-nak!
+
** Modify the Board's move method not to allow self-checks (when the active player makes its own King into a chess)!
 +
* Implement the '''__repr__''' method of '''Board'''!
  
Miután mindez megvan, közel állunk ahhoz, hogy tudjunk sakkozni. Definiáljuk a '''start''' metódust, ami kezdőállípotba teszi a táblát.
+
After all these, implement the '''start''' method which assigns the board to a starting position.
  
 
=== CLI ===
 
=== CLI ===
* Játszunk a sakk-kal úgy, hogy <tt>raw_input</tt>-tal kérünk a felhasználótól bemenetet, egyszer a világostól, egyszer a sötéttől és aszerint lép a tábla.
+
* 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.
** Ehhez érdemes implementálni az [https://en.wikipedia.org/wiki/Algebraic_notation_(chess) algebrai notációt] (Bf5, Qc3, Ne2, Kcd4, Kxd5 stb.) és lépjünk eszerint!
+
** It's better to use [https://en.wikipedia.org/wiki/Algebraic_notation_(chess) algebraic notation] (Bf5, Qc3, Ne2, Kcd4, Kxd5 etc.)  
* Minden lépés után írjuk ki a táblát (ha megírtuk a <tt>__repr__</tt>-et akkor ez csak egy <tt>print</tt>).
+
* Try it from command line
* Próbáljuk ki parancssorból.
+
  
 
[[Informatics2-2018/Lab07|previous]] [[Informatics2-2018#Labs|up]] [[Informatics2-2018/Lab09|next]]
 
[[Informatics2-2018/Lab07|previous]] [[Informatics2-2018#Labs|up]] [[Informatics2-2018/Lab09|next]]

A lap 2018. április 4., 15:00-kori változata

previous up next

Lab No.7

Chess

Define a class called Piece. This will represent a chess piece (a figure on the chessboard). Store its position and its color (Black/White) and its __repr__ function should write the position like A2, G3 etc. Store the position as integers.

  • Define child classes King and Pawn!
  • Implement their __repr__ method which also prints the type of the figure. (You can implement it in the parent class, too)
    • Look the unicode characters of the figures: [1]
  • 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!
  • Define a PieceMoveError exception. If the move is not allowed, then raise this exception!

Implement the Board class. Store the list of figures.

  • Make an add method which puts a figure on the table.
  • 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).
    • 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).
    • Its easier if you implement an .occupied(pos) method first, which tells whether a given position is occupied or not.
  • Implement the other pieces: Knight, Rook, Bishop and Queen!
    • Start with the Rook, its move is the simplest. Mind that the figure cannot pass through existing figures!
    • Implement the Knight class! The Knight and Queen can jump through other figures, so its not a problem for them.
    • After the bishop its not hard to implement Queen.
  • Define a check method in Board. Return whether there is a chess or not!
    • Modify the Board's move method not to allow self-checks (when the active player makes its own King into a chess)!
  • Implement the __repr__ method of Board!

After all these, implement the start method which assigns the board to a starting position.

CLI

  • Make a chess game in command line: read the moves of the players with raw_input and print the board after each step. The white player starts and mind that the same player cannot move twice.
  • Try it from command line

previous up next

Személyes eszközök