The Python code below is for a basic chess game. The user can select the number of players (1 or 2) and the chessboard size (8 or 16). The computer randomly generates a chess position and the user has the option to make a move or accept the move. If the user accepts the move, the computer randomly generates a new position and the process continues until a winner is determined.
The winner is determined by which player has the most pieces (rooks, bishops, knights and pawns) captured after a certain amount of turns.
The Python code is for 1 player and a 8x8 chessboard.
from random import shuffle
set up the chessboard and the players
N = 8 # number of squares on a chessboard
P = 1 # number of players
white = 'R', 'B', 'N', 'P' # pawns are represented by P
black = 'q', 'b', 'n', 'p' # knights are represented by N
initialize the state to undefined (an invalid position)
def position():
return 'undef'
generate a random chess position
def position_random():
return''.join(random.choice(white) for _ in range(N)) +'' +
''.join(random.choice(black) for _ in range(N))
while True:
# get user input to play or stop the game
play = input('play or stop?\n').lower().strip()
if play == 'play':
move = input('enter move: ').lower()
if move in position_random():
accepted_move = input('do you accept the move? y/n').lower()
if accepted_move == 'y':
position = position_random()
while position == 'undef':
position = position_random()
print('player %s won!' % (white if P == 1 else black),
'', end='')
print('the game was a %s draw' % (white if P == 1 else black),
'', end='')
print('the computer generated %s moves' % (', '.join(shuffle(position))), end='')
print('player %s won by capturing %s' % (white if P == 1 else black, ', '.join(chunks.values())), end='')
else:
print('that is a bad move')
print('enter play again')
else:
print('that is an invalid move')
elif play =='stop':
print('game over')
break
else:
print('that is not a valid input')
print('enter play or stop')
# test the user input
print(position_random() == 'undef')
print('you should play this game again')
print('enter play or stop')
# ask for another move
print('enter another move')
check to see if a player won
def winner(state):
''' return win or loss for each player, or 'tie' if both players have the same number of pieces captured '''
print('player %s has %s pieces captured' % (white if P == 1 else black, sum(chunks.values())))
if sum(chunks.values()) > N//4:
return 'win'
elif sum(chunks.values()) < N//4:
return 'loss'
else:
return 'tie'
determine who won the game
def who_won(state):
if winner(state) == 'win' or winner(state) == 'tie':
print('player %s won' % (white if P == 1 else black))
elif winner(state) == 'loss':
print('player %s lost' % (white if P == 1 else black))
check to see if the game has ended in a draw
def did_game_end_draw():
''' return true if the game ended in a draw, false otherwise '''
return sum(chunks.values()) == N//4
determine if the game has ended in a draw
def game_ended_draw(state):
if did_game_end_draw():
print('the game ended in a draw')
else:
print('the game was not a draw')
determine if a player has won
def player_won(state):
if white if P == 1 else black in winner(state):
print('%s