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…”) |
|||
3. sor: | 3. sor: | ||
= Lab No.7 = | = Lab No.7 = | ||
== Chess== | == 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: [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. | ||
+ | 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! | ||
+ | |||
Legyen most egy '''Board''' osztályunk! Két listát tároljunk: '''white''' és '''black''', a játékosok bábuival! | 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! | * 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 lap 2018. április 4., 15:42-kori változata
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!
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.
CLI
- Játszunk a sakk-kal úgy, hogy raw_input-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.
- Ehhez érdemes implementálni az algebrai notációt (Bf5, Qc3, Ne2, Kcd4, Kxd5 stb.) és lépjünk eszerint!
- Minden lépés után írjuk ki a táblát (ha megírtuk a __repr__-et akkor ez csak egy print).
- Próbáljuk ki parancssorból.