8000 added chess game with readme file · d-coder111/SpectrumOfPython@56164d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 56164d3

Browse files
committed
added chess game with readme file
1 parent be67753 commit 56164d3

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

chess/code.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Chess game with basic piece movement logic
2+
3+
class Piece:
4+
def __init__(self, color):
5+
self.color = color
6+
7+
def is_valid_move(self, start_pos, end_pos, board):
8+
raise NotImplementedError("This method should be overridden by subclasses")
9+
10+
11+
class Pawn(Piece):
12+
def is_valid_move(self, start_pos, end_pos, board):
13+
row_start, col_start = start_pos
14+
row_end, col_end = end_pos
15+
16+
if self.color == 'white':
17+
if row_start == 6 and row_start - row_end == 2 and col_start == col_end:
18+
return True
19+
if row_start - row_end == 1 and col_start == col_end:
20+
return True
21+
else: # black
22+
if row_start == 1 and row_end - row_start == 2 and col_start == col_end:
23+
return True
24+
if row_end - row_start == 1 and col_start == col_end:
25+
return True
26+
return False
27+
28+
29+
class Rook(Piece):
30+
def is_valid_move(self, start_pos, end_pos, board):
31+
row_start, col_start = start_pos
32+
row_end, col_end = end_pos
33+
return row_start == row_end or col_start == col_end
34+
35+
36+
class Knight(Piece):
37+
def is_valid_move(self, start_pos, end_pos, board):
38+
row_start, col_start = start_pos
39+
row_end, col_end = end_pos
40+
row_diff = abs(row_start - row_end)
41+
col_diff = abs(col_start - col_end)
42+
return (row_diff == 2 and col_diff == 1) or (row_diff == 1 and col_diff == 2)
43+
44+
45+
class Bishop(Piece):
46+
def is_valid_move(self, start_pos, end_pos, board):
47+
row_start, col_start = start_pos
48+
row_end, col_end = end_pos
49+
return abs(row_start - row_end) == abs(col_start - col_end)
50+
51+
52+
class Queen(Piece):
53+
def is_valid_move(self, start_pos, end_pos, board):
54+
row_start, col_start = start_pos
55+
row_end, col_end = end_pos
56+
return abs(row_start - row_end) == abs(col_start - col_end) or row_start == row_end or col_start == col_end
57+
58+
59+
class King(Piece):
60+
def is_valid_move(self, start_pos, end_pos, board):
61+
row_start, col_start = start_pos
62+
row_end, col_end = end_pos
63+
return abs(row_start - row_end) <= 1 and abs(col_start - col_end) <= 1
64+
65+
66+
class Board:
67+
def __init__(self):
68+
self.board = self.setup_board()
69+
70+
def setup_board(self):
71+
board = [[None for _ in range(8)] for _ in range(8)]
72+
73+
for i in range(8):
74+
board[1][i] = Pawn('black')
75+
board[6][i] = Pawn('white')
76+
77+
board[0][0] = board[0][7] = Rook('black')
78+
board[7][0] = board[7][7] = Rook('white')
79+
80+
board[0][1] = board[0][6] = Knight('black')
81+
board[7][1] = board[7][6] = Knight('white')
82+
83+
board[0][2] = board[0][5] = Bishop('black')
84+
board[7][2] = board[7][5] = Bishop('white')
85+
86+
board[0][3] = Queen('black')
87+
board[7][3] = Queen('white')
88+
89+
board[0][4] = King('black')
90+
board[7][4] = King('white')
91+
92+
return board
93+
94+
def display(self):
95+
for row in self.board:
96+
row_display = []
97+
for piece in row:
98+
if piece is None:
99+
row_display.append(".")
100+
else:
101+
row_display.append(piece.__class__.__name__[0] + ('w' if piece.color == 'white' else 'b'))
102+
print(" ".join(row_display))
103+
print()
104+
105+
def move_piece(self, start_pos, end_pos):
106+
piece = self.get_piece(start_pos)
107+
if piece is not None and piece.is_valid_move(start_pos, end_pos, self.board):
108+
self.board[end_pos[0]][end_pos[1]] = piece
109+
self.board[start_pos[0]][start_pos[1]] = None
110+
return True
111+
return False
112+
113+
def get_piece(self, position):
114+
row, col = position
115+
return self.board[row][col]
116+
117+
118+
class Game:
119+
def __init__(self):
120+
self.board = Board()
121+
self.current_turn = 'white'
122+
123+
def switch_turn(self):
124+
self.current_turn = 'black' if self.current_turn == 'white' else 'white'
125+
126+
def play(self):
127+
while True:
128+
self.board.display()
129+
print(f"{self.current_turn.capitalize()}'s turn")
130+
131+
try:
132+
start = input("Enter start position (e.g., 6 0 for row 6, column 0): ").split()
133+
end = input("Enter end position (e.g., 5 0 for row 5, column 0): ").split()
134+
start_pos = (int(start[0]), int(start[1]))
135+
end_pos = (int(end[0]), int(end[1]))
136+
137+
piece = self.board.get_piece(start_pos)
138+
139+
if piece is None or piece.color != self.current_turn:
140+
print("Invalid move! Try again.")
141+
continue
142+
143+
if self.board.move_piece(start_pos, end_pos):
144+
self.switch_turn()
145+
else:
146+
print("Invalid move! Try again.")
147+
148+
except (ValueError, IndexError):
149+
print("Invalid input! Please use the correct format (e.g., 6 0 for row 6, column 0).")
150+
151+
152+
if __name__ == "__main__":
153+
game = Game()
154+
game.play()

chess/readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Python Chess Game
2+
3+
## Description
4+
This is a simple, text-based chess game implemented in Python. The game allows two players to take turns moving chess pieces on an 8x8 chessboard. The chess rules for movement (Pawn, Rook, Knight, Bishop, Queen, King) are implemented. This version does not include advanced features like castling, check, or checkmate detection.
5+
6+
## Features
7+
- All chess pieces with basic movement rules (Pawn, Rook, Knight, Bishop, Queen, King).
8+
- A simple turn-based system for two players.
9+
- Text-based display of the board.
10+
- Input validation for moves.
11+
12+
## Requirements
13+
- Python 3.x
14+
15+
## How to Run the Game
16+
1. Clone or download this repository.
17+
2. Open a terminal or command prompt and navigate to the folder where the script is located.
18+
3. Run the game by executing the following command:
19+
```bash
20+
python chess_game.py

0 commit comments

Comments
 (0)
0