Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Starter Chess Program by @TokyoEdTech
- # Work in Progress
- import os
- import platform
- import time
- def clear_screen():
- if platform.system() == "Windows":
- os.system("cls")
- else:
- os.system("clear")
- def print_header():
- print(" Simple Chess by @TokyoEdTech")
- print(" Sample Move: a3 a5\n")
- class Board():
- def __init__(self):
- self.board = []
- self.board.append(["r", "k", "b", "q", "k", "b", "k", "r"])
- self.board.append(["p", "p", "p", "p", "p", "p", "p", "p"])
- for row in range(4):
- self.board.append([" ", " ", " ", " ", " ", " ", " ", " "])
- self.board.append(["P", "P", "P", "P", "P", "P", "P", "P"])
- self.board.append(["R", "K", "B", "Q", "K", "B", "K", "R"])
- def move_piece(self, move):
- # Strip extra spaces at start and end
- move = move.strip()
- # Make sure length is 5
- if len(move) != 5:
- return False
- # Make sure middle is a space
- if move[2] != " ":
- return False
- try:
- start_col = move[0]
- start_col = self.convert_col_to_number(start_col) - 1
- start_row = 8 - int(move[1])
- end_col = move[3]
- end_col = self.convert_col_to_number(end_col) - 1
- end_row = 8 - int(move[4])
- # Check for legal move
- print(self.get_legal_moves(start_row, start_col))
- self.board[end_row][end_col] = self.board[start_row][start_col]
- self.board[start_row][start_col] = " "
- return True
- except:
- return False
- def show_board(self):
- print(" A B C D E F G H")
- print(" ---------------------------------")
- row_number = 8
- for row in self.board:
- print(str(row_number) + " | ", end="")
- for i in range(len(row)):
- print(row[i] + " | ", end="")
- print(str(row_number))
- print(" ---------------------------------")
- row_number -= 1
- print(" A B C D E F G H")
- def convert_col_to_number(self, col):
- col = col.upper()
- if col == "A":
- return 1
- if col == "B":
- return 2
- if col == "C":
- return 3
- if col == "D":
- return 4
- if col == "E":
- return 5
- if col == "F":
- return 6
- if col == "G":
- return 7
- if col == "H":
- return 8
- def calculate_board(self):
- values = {"P":1, "p":1, "R":3, "r":3, "K":5, "k":5, "B":7, "b":7, "Q":14, "q":14, "K":50, "k":50}
- white_total = 0
- black_total = 0
- for row in self.board:
- for piece in row:
- if piece in ["P", "R", "K", "B", "Q", "K"]:
- white_total += values[piece]
- elif piece in ["p", "r", "k", "b", "q", "k"]:
- black_total += values[piece]
- return (white_total, black_total)
- def get_legal_moves(self, row, col):
- moves = []
- piece = self.board[row][col]
- # Pawns
- if piece == "p":
- if row == 6:
- moves.append((row-2, col))
- moves.append((row-1, col))
- elif piece == "P":
- if row == 1:
- moves.append((row+2, col))
- moves.append((row+1, col))
- # Return moves
- return moves
- clear_screen()
- print_header()
- board = Board()
- board.show_board()
- game_on = True
- while game_on:
- # Get White Move
- legal = False
- while not legal:
- move = input("\nWHITE MOVE: ")
- legal = board.move_piece(move)
- if not legal:
- print("ILLEGAL MOVE")
- time.sleep(1)
- clear_screen()
- print_header()
- board.show_board()
- legal = False
- while not legal:
- move = input("BLACK MOVE: ")
- legal = board.move_piece(move)
- if not legal:
- print("ILLEGAL MOVE")
- time.sleep(1)
- clear_screen()
- print_header()
- board.show_board()
- values = board.calculate_board()
- print("WHITE: {} BLACK: {}".format(values[0], values[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement