Informatics2-2018/Lab08

A MathWikiből
(Változatok közti eltérés)
(Új oldal, tartalma: „previous up next = Lab No.7 = == Chess== Definiáljuk a '''Piece''' osztályt. Ez r…”)
 
a
 
(egy szerkesztő 3 közbeeső változata nincs mutatva)
3. sor: 3. sor:
 
= Lab No.7 =  
 
= Lab No.7 =  
 
== Chess==
 
== Chess==
Definiáljuk a '''Piece''' osztályt. Ez reprezentál egy sakkbábut, tároljuk a pozícióját a táblán két koodinátával, a színét (black/white), illetve a '''__repr__''' írja ki, hogy hol áll (A2, G3 etc.)!
+
Define a class called '''Piece'''. This will represent a chess piece (a figure on the chessboard).
* Definiáljuk a bábu leszármazottjaként a '''King''' és a '''Pawn''' osztályokat!
+
Store its position and its color (Black/White) and its '''__repr__''' function should write the position like A2, G3 etc.
* Legyen a leszármazottaknak (King, Pawn) is '''__repr__''' függvénye úgy, hogy az már a figura típusát is kiírja. (Nem muszáj a leszármazott osztályban megvalósítani, lehet az ősosztályban is)
+
Store the position as integers.
** Érdemes megnézni a sakk figurák unicode kódját: [https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode]
+
* Minden leszármazottnak legyen egy '''.move(pos)''' metódusa, ahol a '''pos''' egy sztring (A3, G2 etc.)! Mozgassuk el a bábut, ha szabályos a lépés! Ha a lépés szabályos volt, térjünk vissza '''True'''-val, egyébként '''False'''-szal!
+
* Definiáljuk a '''PieceMoveError''' osztályt. Ha szabálytalan a lépés, dobjunk egy ilyen exceptiont és kezeljük le!
+
Legyen most egy '''Board''' osztályunk! Két listát tároljunk: '''white''' és '''black''', a játékosok bábuival!
+
* 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!
+
** 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!
+
** Írjuk meg a '''Knight, Rook, Bishop''' és '''Queen''' osztályokat!
+
*** 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!
+
*** 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.
+
*** A '''Bishop''' után a '''Queen''' lépését könnyű összerakni.
+
** Oldjuk meg, hogy a '''Pawn''' ne állhasson az ellenfél alapvonalán!
+
* A '''Board''' osztályon belül definiáljunk egy '''check''' metódust. Térjen vissza azzal a színnel, amelyik király sakkban áll!
+
** 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!
+
* Legyen '''__repr__''' függvénye a '''Board'''-nak!
+
  
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.
+
* 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: [https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode]
 +
** Or their [https://en.wikipedia.org/w/index.php?title=Algebraic_notation_(chess)#Piece_names_in_various_languages one letter names]
 +
* 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 the color of the player who's King is in a chess position, or empty string of there is no chess position!
 +
** 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 ===
 
=== 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 jelenlegi, 2018. április 11., 11:24-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)
  • 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 the color of the player who's King is in a chess position, or empty string of there is no chess position!
    • 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