Description

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:

chess.jpg

At the beginning, your program should draw an empty chess board and prompt the user to enter a move:

Welcome to the Chess Game!
a b c d e f g h
+---+---+---+---+---+---+---+---+
8| | | | | | | | |8
+---+---+---+---+---+---+---+---+
7| | | | | | | | |7
+---+---+---+---+---+---+---+---+
6| | | | | | | | |6
+---+---+---+---+---+---+---+---+
5| | | | | | | | |5
+---+---+---+---+---+---+---+---+
4| | | | | | | | |4
+---+---+---+---+---+---+---+---+
3| | | | | | | | |3
+---+---+---+---+---+---+---+---+
2| | | | | | | | |2
+---+---+---+---+---+---+---+---+
1| | | | | | | | |1
+---+---+---+---+---+---+---+---+
a b c d e f g h
Enter a chess piece and its position or type X to exit:

A move is represented as a three-character string where the first character corresponds to a letter, 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 character 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 character 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   d   e   f   g   h
+---+---+---+---+---+---+---+---+
8| | | | | x | | | |8
+---+---+---+---+---+---+---+---+
7| | | | | x | | | |7
+---+---+---+---+---+---+---+---+
6| x | x | x | x | R | x | x | x |6
+---+---+---+---+---+---+---+---+
5| | | | | x | | | |5
+---+---+---+---+---+---+---+---+
4| | | | | x | | | |4
+---+---+---+---+---+---+---+---+
3| | | | | x | | | |3
+---+---+---+---+---+---+---+---+
2| | | | | x | | | |2
+---+---+---+---+---+---+---+---+
1| | | | | x | | | |1
+---+---+---+---+---+---+---+---+
a b c d e f g h
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:

   a   b   c   d   e   f   g   h
+---+---+---+---+---+---+---+---+
8| | | | | | | | |8
+---+---+---+---+---+---+---+---+
7| | | | | | | | |7
+---+---+---+---+---+---+---+---+
6| | | x | x | x | | | |6
+---+---+---+---+---+---+---+---+
5| | | x | K | x | | | |5
+---+---+---+---+---+---+---+---+
4| | | x | x | x | | | |4
+---+---+---+---+---+---+---+---+
3| | | | | | | | |3
+---+---+---+---+---+---+---+---+
2| | | | | | | | |2
+---+---+---+---+---+---+---+---+
1| | | | | | | | |1
+---+---+---+---+---+---+---+---+
a b c d e f g h
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:
def __init__(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)
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:
def __init__(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[0]), '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/PA5. 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!
a b c d e f g h
+---+---+---+---+---+---+---+---+
8| | | | | | | | |8
+---+---+---+---+---+---+---+---+
7| | | | | | | | |7
+---+---+---+---+---+---+---+---+
6| | | | | | | | |6
+---+---+---+---+---+---+---+---+
5| | | | | | | | |5
+---+---+---+---+---+---+---+---+
4| | | | | | | | |4
+---+---+---+---+---+---+---+---+
3| | | | | | | | |3
+---+---+---+---+---+---+---+---+
2| | | | | | | | |2
+---+---+---+---+---+---+---+---+
1| | | | | | | | |1
+---+---+---+---+---+---+---+---+
a b c d e f g h
Enter a chess piece and its position or type X to exit:
Kf5
a b c d e f g h
+---+---+---+---+---+---+---+---+
8| | | | | | | | |8
+---+---+---+---+---+---+---+---+
7| | | | | | | | |7
+---+---+---+---+---+---+---+---+
6| | | | | x | x | x | |6
+---+---+---+---+---+---+---+---+
5| | | | | x | K | x | |5
+---+---+---+---+---+---+---+---+
4| | | | | x | x | x | |4
+---+---+---+---+---+---+---+---+
3| | | | | | | | |3
+---+---+---+---+---+---+---+---+
2| | | | | | | | |2
+---+---+---+---+---+---+---+---+
1| | | | | | | | |1
+---+---+---+---+---+---+---+---+
a b c d e f g h
Enter a chess piece and its position or type X to exit:
Rd5
a b c d e f g h
+---+---+---+---+---+---+---+---+
8| | | | x | | | | |8
+---+---+---+---+---+---+---+---+
7| | | | x | | | | |7
+---+---+---+---+---+---+---+---+
6| | | | x | | | | |6
+---+---+---+---+---+---+---+---+
5| x | x | x | R | x | x | x | x |5
+---+---+---+---+---+---+---+---+
4| | | | x | | | | |4
+---+---+---+---+---+---+---+---+
3| | | | x | | | | |3
+---+---+---+---+---+---+---+---+
2| | | | x | | | | |2
+---+---+---+---+---+---+---+---+
1| | | | x | | | | |1
+---+---+---+---+---+---+---+---+
a b c d e f g h
Enter a chess piece and its position or type X to exit:
Nd5
a b c d e f g h
+---+---+---+---+---+---+---+---+
8| | | | | | | | |8
+---+---+---+---+---+---+---+---+
7| | | x | | x | | | |7
+---+---+---+---+---+---+---+---+
6| | x | | | | x | | |6
+---+---+---+---+---+---+---+---+
5| | | | N | | | | |5
+---+---+---+---+---+---+---+---+
4| | x | | | | x | | |4
+---+---+---+---+---+---+---+---+
3| | | x | | x | | | |3
+---+---+---+---+---+---+---+---+
2| | | | | | | | |2
+---+---+---+---+---+---+---+---+
1| | | | | | | | |1
+---+---+---+---+---+---+---+---+
a b c d e f g h
Enter a chess piece and its position or type X to exit:
Bd4
a b c d e f g h
+---+---+---+---+---+---+---+---+
8| | | | | | | | x |8
+---+---+---+---+---+---+---+---+
7| x | | | | | | x | |7
+---+---+---+---+---+---+---+---+
6| | x | | | | x | | |6
+---+---+---+---+---+---+---+---+
5| | | x | | x | | | |5
+---+---+---+---+---+---+---+---+
4| | | | B | | | | |4
+---+---+---+---+---+---+---+---+
3| | | x | | x | | | |3
+---+---+---+---+---+---+---+---+
2| | x | | | | x | | |2
+---+---+---+---+---+---+---+---+
1| x | | | | | | x | |1
+---+---+---+---+---+---+---+---+
a b c d e f g h
Enter a chess piece and its position or type X to exit:
Qd4
a b c d e f g h
+---+---+---+---+---+---+---+---+
8| | | | x | | | | x |8
+---+---+---+---+---+---+---+---+
7| x | | | x | | | x | |7
+---+---+---+---+---+---+---+---+
6| | x | | x | | x | | |6
+---+---+---+---+---+---+---+---+
5| | | x | x | x | | | |5
+---+---+---+---+---+---+---+---+
4| x | x | x | Q | x | x | x | x |4
+---+---+---+---+---+---+---+---+
3| | | x | x | x | | | |3
+---+---+---+---+---+---+---+---+
2| | x | | x | | x | | |2
+---+---+---+---+---+---+---+---+
1| x | | | x | | | x | |1
+---+---+---+---+---+---+---+---+
a b c d e f g h
Enter a chess piece and its position or type X to exit:
X
Goodbye!

Grading Rubric

You can start to work on your program without using eval_pa5 script. However, when you are done writing your program, you may need to use the eval_pa5 script to evaluate your program specifications and performance. All files, including a template chess.py, eval_pa5, and supplementary files are located in the Files/ Scripts/ PA5 folder on Canvas.

Criteria

Points 60

Excellent

Good

Satisfactory

Unsatisfactory

Submitted on time

60 possible points

yes

no

Runs without errors

12

yes

runs with simple input 6 points

no

Produces right output

12

yes

minor format mismatch

10 points

partially correct output

6 points

no

Has a comment block

3

yes

minor format mismatch

3-4 points

format mismatch

1-2 points

no

file named correctly

3

yes

no

has 5 chess piece classes

10

yes

1 is missing or incorrectly implemented

8 points

2-3 are missing or incorrectly implemented

4-6 points

no

has a moves method for each chess piece class

10

5 correctly implemented

10 points

1 is missing or incorrectly implemented

8 points

2-3 are missing or incorrectly implemented

4-6 points

no

has a board class

2

yes

no

has a draw method

4

correctly implemented 4 points

no

Uses a dictionary

1

yes

no

Uses a string method: format, lower, upper, isalpha, or join

1

yes

no

Uses a while loop

1

yes

no

Uses elif

1

yes

no