[phpBB Debug] PHP Warning: in file [ROOT]/ext/lmdi/autolinks/event/listener.php on line 237: Undefined array key 120
[phpBB Debug] PHP Warning: in file [ROOT]/ext/lmdi/autolinks/event/listener.php on line 237: Trying to access array offset on value of type null
Answer Happy • PA 5. Chess Topics 1. Classes 2. Methods 3. Data Collections: Lists, Tuples, and Dictionaries 4. String Manipulation Ins
Page 1 of 1

PA 5. Chess Topics 1. Classes 2. Methods 3. Data Collections: Lists, Tuples, and Dictionaries 4. String Manipulation Ins

Posted: Mon Jun 06, 2022 4:52 pm
by answerhappygod
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 1
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 1 (26.51 KiB) Viewed 42 times
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 2
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 2 (49.19 KiB) Viewed 42 times
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 3
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 3 (30.03 KiB) Viewed 42 times
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 4
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 4 (17.13 KiB) Viewed 42 times
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 5
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 5 (30.2 KiB) Viewed 42 times
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 6
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 6 (32.06 KiB) Viewed 42 times
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 7
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 7 (59.55 KiB) Viewed 42 times
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 8
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 8 (34.74 KiB) Viewed 42 times
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 9
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 9 (23.34 KiB) Viewed 42 times
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 10
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 10 (22.23 KiB) Viewed 42 times
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 11
Pa 5 Chess Topics 1 Classes 2 Methods 3 Data Collections Lists Tuples And Dictionaries 4 String Manipulation Ins 11 (21.04 KiB) Viewed 42 times
This is a python assignment about chess
PA 5. Chess Topics 1. Classes 2. Methods 3. Data Collections: Lists, Tuples, and Dictionaries 4. String Manipulation Instructions (You Must Read Everything!!!) Chess Objective: practicing with classes, instance methods, data collections, loops, if and elif statements, and string methods Description In this assignment you will write a program that shows the valid moves of chess pieces. Your program will draw a board with 64 squares using the traditional layout, next ask the user to choose a move, and then, depending on the user's choice, redraw the board with the selected chess piece and its valid moves. Please see the examples of valid moves of chess pieces and the traditional chess board layout below:
876 3 21 00 6 Moves of the king abcdefgh a $ b Moves of a queen abcdefgh L 87 6 5 4 87 65 54 4 765 43 1 321 321 Moves of a rook abcdefgh a Moves of a knight abcdefgh d b 4 f h 29 48 9 5 43 3 2 32 Moves of a bishop abcdefgh gh Moves of a pawn abcdefgh X X 8 abc d X T X h 896 SYM NY 3 2 1 7 1010
At the beginning, your program should draw an empty chess board and prompt the user to enter a move: Welcome to the Chess Game! bc de f sh 11 7| | | | | I 17 6| | | | 1116 +* 5|||| 41 | 1 21 | 1 11111 [1 +* abcde gh Enter a chess piece and its position or type x to exit: A move is represented as a three-letter string where the first letter corresponds to a chess piece initial: K stands for king. Q-queen, B-bishop, N-knight, and R-rook (a chariot). Pawns do not have initials, and you will not be tested on their implementation. The next letter in the move is the position on the board that corresponds to one of eight columns (called files). The columns (files) are labeled as a, b, c, d, e, f. g. h. The third symbol is a digit that corresponds to one of eight rows (called ranks). The rows (ranks) are labeled as 1, 2. 3, 4, 5, 6, 7, 8. For example, if the user enters Re6, your program should generate the following output where R is the initial for a rook, and all possible moves of the rook are marked by x:
a b c de gh ||||×||||8 II |||7 + 6 x | x xx | x 16 ++ $|||||*||||5 +* 4|||||||||4 +-- 31 | || |×||||3 | | 12 21|||1x| 1||1|1x| | | | +++---+-- abcdefgh Enter a chess piece and its position or type X to exit: If the user enters Kd5, your program should produce the following output where K is the initial for the king. and all squares where the king can move are marked with x: 71
abcdefgh 8|||||||||8 7||||||||17 +-+-+ 61 | |*|*|*| | | 16 +-+-+---- 51 ||×|*|*||||5 +-+--- 4| | |×|×|x | | | 14 +-+- 3|||||||| 13 + 2|||||||||2 +--- 1||||||||1 +-+-+---- +---+ abcdefgh Enter a chess piece and its position or type x to exit: If the user does not enter a valid move that is included in all possible combinations of chess piece initials (K, Q. B, N, R), files (a, b, c, d, e, f, g, h), and ranks (1, 2, 3, 4, 5, 6, 7, 8), then the program should reprint the prompt: Enter a chess piece and its position or type x to exit: If the user enters X (or x), then the program should print 'Goodbye!' and terminate: Goodbye! NOTE: The user can use either upper or lowercase letters, for example X or x should result in the termination of the program, Ke3, KE3, kE3, or ke3 means the same move.
Programming Approaches In this assignment, you need to create a class Board and a class for each chess piece: King, Queen, Bishop, Knight, and Rook. They all should be located in a module called chess.py. All chess piece classes should be derived from the superclass Chess Piece given to you. Your class Board should have four methods init, empty, set, and draw. Three of the methods are already implemented for you. You only need to implement the method draw() that should print the board layout and label squares accordingly to the current chess piece positioned on the board. You can type or copy and paste the following code into your file chess.py. The given class Board has only one attribute that is also called board, and it is implemented as a dictionary. The method empty generates an empty board (a dictionary with keys corresponding to chess board squares, and their values are white spaces (). The method set() updates the label of a given square by assigning the dictionary key value to its new value. class Board: definit_(self): self.board () self.empty() def empty(self): for col in abcdefgh": for row in '12345678': self.board[col+row] = def set (self, pos, piece): #pos is a square label (a1, a2,, h8) if pos in self.board.keys(): self.board[pos] piece. def draw(self):
- The class Chess_Piece has four methods init, get_index, get_name, and moves. The constructor method init() creates an object with attributes position and color and updates the board by placing the chess piece on the board at a given position. The method get_name() should return the initial of the chess piece, and it is not implemented in the superclass. However, you need to implement it in all subclasses, for example, the method in a King class should return 'K', in a Rook class 'R', etc. The method get_index() converts a position (that is used as a key in the board dictionary) into a tuple of indexes where the first index is a column number, and the second index is a row number). It is useful to use indexes (integers) instead of strings to calculate possible moves of a chess piece because we can use addition and subtraction operations on indexes. For example, to get a move of a knight located at e3, we can get its position as indexes: 'e' has index 4, and '3' has index 2. Then, we can add 2 to the index of 'e' and 1 to the index of '3' and get a new position (6, 3) that corresponds to g4. The method moves() is not implemented. It is unique for each class, and it should label the board with the corresponding letters and 'x's. Your task is to find how to calculate indexes of possible moves. Hints: You can traverse squares on the board and check if a square has indexes that correspond to a possible move. A possible move for a rook is any square that has the same row or column index as the current position of the rook. A possible move for the king is a square that is one distance away from the king's current position. A possible move for a knight is a square that is two distances away in one direction and one distance away in another direction. class Chess_Piece: definit_(self, board, pos, color='white'): self.position= self.get_index (pos) self.color color. board.set(pos, self.get_name()) def get_index(self, pos): return ('abcdefgh'.index (pos[e]), '12345678'.index(pos[1])) def get_name(self): pass def moves(self, board): pass
Testing and Evaluation Scripts To test you program first run it in IDLE shell. If it runs correctly, test it in a terminal (or the bash shell on the Unix server). You need to have all files (chess.py, eval_pa5, ex1, ex2, ex1 out, and ex2.out) in one working directory (such as CSE20/PA5). You can download them from Files/Scripts/PAS. Then type in the shell (terminal) the following commands: python3 chess.py< ex1 python3 chess.py < ex2 All lines of the output should be printed on the separate lines as shown in the files ex1.out and ex2.out. So. do not forget to add '\n' to the end of each input statement! After that you can test your program with the evaluation script. The evaluation script and supplementary data files can be downloaded from Files/Scripts/PA5 on Canvas. You can run the evaluation script using the following command: sh eval_pa5 The example of the output in the IDLE shell is shown below (the input is the same as in the ex1 file and output is very similar to ex1.out, the only difference is the absence of input data). Remember extra newlines are not shown and should not matter! Welcome to the Chess Game! b c d e fsh S + 41 31 | 1 21 11 18
Welcome to the Chess Game! abcdefgh 8||||||||| 7||||||||17 6|||||||||6 |||5 41 || 31 ||||||||3 2|||||||||2 1||||||||11 +++++++ abcdefgh Enter a chess piece and its position or type x to exit: Kfs abcdefgh ++ 8|||||11|18 +-+ 7| | || | || | 17 ++++ 6|||||×|×|×| 16 5|||||*|*|×| 15 41 || Ixxx | 14 3|||||||||3 2|||||||||2 + 1|||||||||₁ ++++ abcdefgh Enter a chess piece and its position or type x to exit: Rd5
Enter a chess piece and its position or type x to exiti Rds 1* 15 41 11 111 11 ***** b cd . f gh Enter a chess piece and its position or type X to exit Nds f ch BE 1 71 +--- SINI 4x4 41 1x1 31 21 1 1| a b cdefgh Enter a chess piece and its position or type x to axit: Bd4 a de f 10
Enter a chess piece and its position or type x to exit: Bd4 abcd ch 811 IN 4. 611*11 51 | 1x11x1 4|||||| *** 31 | 1×1 | 1113 21 |×||| **** 1|×|||I [*||1 abcdefgh Enter a chess piece and its position or type x to exit: Qde abcdefgh 8 | 1x1 71 x | 11x17 61 |x| +++ |×|||6 51 101x1x1x1x 14 4 xx +---+- 31 | *** *||| |3 21 |×| 1| x | |×| | |×| 11 fgh b c d Enter a chess piece and its position or type x to exit: X Goodbye!