Skip to content

Commit df4f71d

Browse files
Saubhagya SharmaSaubhagya Sharma
authored andcommitted
Add Chess Game To Python/Games
1 parent 10a0de1 commit df4f71d

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

Python/Games/Chess.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
def create_board():
2+
# Set up chess board as an 8x8 array
3+
board = [
4+
["r", "n", "b", "q", "k", "b", "n", "r"],
5+
["p", "p", "p", "p", "p", "p", "p", "p"],
6+
[" ", " ", " ", " ", " ", " ", " ", " "],
7+
[" ", " ", " ", " ", " ", " ", " ", " "],
8+
[" ", " ", " ", " ", " ", " ", " ", " "],
9+
[" ", " ", " ", " ", " ", " ", " ", " "],
10+
["P", "P", "P", "P", "P", "P", "P", "P"],
11+
["R", "N", "B", "Q", "K", "B", "N", "R"]
12+
]
13+
return board
14+
15+
def print_board(board):
16+
print("\n a b c d e f g h")
17+
print(" -----------------")
18+
for i, row in enumerate(board):
19+
print(str(8-i) + "|", end=" ")
20+
for piece in row:
21+
print(piece, end=" ")
22+
print("|" + str(8-i))
23+
print(" -----------------")
24+
print(" a b c d e f g h\n")
25+
26+
def parse_move(move):
27+
# Example move: e2e4
28+
if len(move) != 4:
29+
return None
30+
cols = {"a":0,"b":1,"c":2,"d":3,"e":4,"f":5,"g":6,"h":7}
31+
try:
32+
from_col, from_row, to_col, to_row = move[0], int(move[1]), move[2], int(move[3])
33+
return 8-from_row, cols[from_col], 8-to_row, cols[to_col]
34+
except (KeyError, ValueError):
35+
return None
36+
37+
def valid_move(board, fr, fc, tr, tc, turn):
38+
piece = board[fr][fc]
39+
dest = board[tr][tc]
40+
if piece == " " or (turn == "white" and piece.islower()) or (turn == "black" and piece.isupper()):
41+
return False
42+
# Basic move validation: allow moving to empty square or capturing opponent
43+
if turn == "white" and dest.isupper():
44+
return False
45+
if turn == "black" and dest.islower():
46+
return False
47+
return True
48+
49+
def make_move(board, fr, fc, tr, tc):
50+
board[tr][tc] = board[fr][fc]
51+
board[fr][fc] = " "
52+
53+
def main():
54+
board = create_board()
55+
print("Simple Chess Game")
56+
print_board(board)
57+
turn = "white"
58+
while True:
59+
move = input(f"{turn.capitalize()} move (e.g. e2e4): ").lower().strip()
60+
if move == "exit":
61+
print("Game exited.")
62+
break
63+
pos = parse_move(move)
64+
if not pos:
65+
print("Invalid input! Format: e2e4")
66+
continue
67+
fr, fc, tr, tc = pos
68+
if valid_move(board, fr, fc, tr, tc, turn):
69+
make_move(board, fr, fc, tr, tc)
70+
print_board(board)
71+
# Basic checkmate detection (ends when king captured)
72+
pieces = sum(row for row in board)
73+
if "k" not in pieces:
74+
print("White wins! (Black king captured)")
75+
break
76+
if "K" not in pieces:
77+
print("Black wins! (White king captured)")
78+
break
79+
turn = "black" if turn == "white" else "white"
80+
else:
81+
print("Invalid move or move not allowed!")
82+
83+
if __name__ == "__main__":
84+
main()

0 commit comments

Comments
 (0)