Skip to content

Commit 2940195

Browse files
Changes Done
I have made all the requested changes. @prathimacode-hub
1 parent 30f6f90 commit 2940195

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

Diff for: PyGamesScripts/Chess Game/README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44
#### In this game, the whole board can be controlled using simple algebraic commands,
55
For example:
66
>>>e4 e7
7-
This will move the pawn present on the e4 position to the e7 position. The program will evaluate constantly, whether its a valid move or not.
7+
This will move the pawn present on the e4 position to the e7 position. The program will evaluate constantly, whether its a valid move or not.
8+
9+
CONVENTIONS: Positions are row-column based, both are numbers from the bottom left. This corresponds to the alpha-number system in traditional chess while being computationally useful. They are specified as tuples
10+
11+
Game class contains the following members and methods:
12+
> Two arrays of pieces for each player.
13+
> 8x8 piece array with references to these pieces.
14+
> A parse function, which turns the input from the user into a list of two tuples denoting start and end points.
15+
> A checkmateExists function which checks if either players are in checkmate.
16+
> A checkExists function which checks if either players are in check.
17+
> A main loop, which takes input, runs it through the parser, asks the piece if the move is valid, and moves the piece if it is. If the move conflicts with another piece, that piece is removed. ischeck(mate) is run, and if there is a checkmate, the game prints a message as to who wins.
818
919
General Chess Rules
1020
White is always first to move and players take turns alternately moving one piece at a time. A piece may be moved to another position or may capture an opponent´s piece, replacing on its square. With the exception of the knight, a piece may not move over or through any of the other pieces. When a king is threatened with capture (but can protect himself or escape), it´s called check. If a king is in check, then the player must make a move that eliminates the threat of capture and cannot leave the king in check. Checkmate happens when a king is placed in check and there is no legal/valid move to escape. Checkmate ends the game and the side whose king was checkmated looses.
@@ -18,10 +28,11 @@ White is always first to move and players take turns alternately moving one piec
1828

1929
5. Type this command to run the code
2030

21-
chessGame.py
31+
chess_game.py
2232

2333
Have fun!!
2434

2535
## Output
2636
<img align="center" alt="output" src="Images/img.jpg"/>
2737

38+
Creator: [Sahil Singh](https://github.com/sahilsingh2402)

Diff for: PyGamesScripts/Chess Game/chessGame.py renamed to PyGamesScripts/Chess Game/chess_game.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,21 @@
22
WHITE = "white"
33
BLACK = "black"
44

5+
"""
6+
CONVENTIONS: Positions are row-column based, both are numbers from the bottom left. This corresponds to the alpha-number system in traditional chess while being computationally useful. They are specified as tuples
7+
8+
Game class contains the following members and methods:
9+
> Two arrays of pieces for each player.
10+
> 8x8 piece array with references to these pieces.
11+
> A parse function, which turns the input from the user into a list of two tuples denoting start and end points.
12+
> A checkmateExists function which checks if either players are in checkmate.
13+
> A checkExists function which checks if either players are in check.
14+
> A main loop, which takes input, runs it through the parser, asks the piece if the move is valid, and moves the piece if it is. If the move conflicts with another piece, that piece is removed. ischeck(mate) is run, and if there is a checkmate, the game prints a message as to who wins.
15+
"""
16+
517

618
class Game:
19+
# I've decided since the number of pieces is capped but the type of pieces is not (pawn transformations), I've already coded much of the modularity to support just using a dictionary of pieces.
720
def __init__(self):
821
self.playersturn = BLACK
922
self.message = "This is where prompts will go"
@@ -64,7 +77,7 @@ def main(self):
6477
self.message = "There is no piece in that space"
6578

6679
def isCheck(self):
67-
# ascertain where the kings are, check all pieces of opposing color against those kings, then if either get hit, check if its checkmate
80+
# As certain where the kings are, check all pieces of opposing color against those kings, then if either get hit, check if its checkmate
6881
king = King
6982
kingDict = {}
7083
pieceDict = {BLACK: [], WHITE: []}
@@ -80,7 +93,7 @@ def isCheck(self):
8093
self.message = "Black player is in check"
8194

8295
def canSeeKing(self, kingpos, piecelist):
83-
# checks if any pieces in piece list (which is an array of (piece,position) tuples) can see the king in kingpos
96+
# Checks if any pieces in piece list (which is an array of (piece,position) tuples) can see the king in kingpos.
8497
for piece, position in piecelist:
8598
if piece.isValid(position, kingpos, piece.Color, self.gameboard):
8699
return True
@@ -210,7 +223,7 @@ class Pawn(Piece):
210223
def __init__(self, color, name, direction):
211224
self.name = name
212225
self.Color = color
213-
# of course, the smallest piece is the hardest to code. direction should be either 1 or -1, should be -1 if the pawn is traveling "backwards"
226+
# Direction should be either 1 or -1, should be -1 if the pawn is traveling "backwards"
214227
self.direction = direction
215228

216229
def availableMoves(self, x, y, gameboard, Color=None):
@@ -230,4 +243,5 @@ def availableMoves(self, x, y, gameboard, Color=None):
230243
uniDict = {WHITE: {Pawn: "♙", Rook: "♖", Knight: "♘", Bishop: "♗", King: "♔", Queen: "♕"},
231244
BLACK: {Pawn: "♟", Rook: "♜", Knight: "♞", Bishop: "♝", King: "♚", Queen: "♛"}}
232245

233-
Game()
246+
if __name__ == "__main__":
247+
Game()

Diff for: PyGamesScripts/Chess Game/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
itertools==8.8.0

0 commit comments

Comments
 (0)