import random
class Checkerboard:
def __init__(self):
self.board = [[' ' for _ in range(8)] for _ in range(8)]
self.initialize_board()
def initialize_board(self):
for i in range(3):
for j in range(0, 8, 2):
self.board[i][j] = 'b'
self.board[i][j + 1] = 'w'
for i in range(5, 8):
for j in range(1, 7, 2):
self.board[i][j] = 'w'
self.board[i][j + 1] = 'b'
def print_board(self):
for row in self.board:
print(' '.join(row))
def valid_moves(self, piece):
moves = []
if piece == 'b':
direction = -1
else:
direction = 1
for i in range(8):
for j in range(8):
if self.board[i][j] == piece:
if i + direction < 8 and self.board[i + direction][j + 1] == ' ':
moves.append((i, j, i + direction, j + 1))
if i + direction < 8 and j - 1 >= 0 and self.board[i + direction][j - 1] == 'w':
moves.append((i, j, i + direction, j - 1))
if i + direction < 8 and j + 1 < 8 and self.board[i + direction][j + 1] == 'w':
moves.append((i, j, i + direction, j + 1))
if i + direction < 8 and j - 1 >= 0 and self.board[i + direction][j - 1] == 'b':
moves.append((i, j, i + direction, j - 1))
return moves
def make_move(self, start, end):
x1, y1, x2, y2 = start
self.board[x1][y1], self.board[x2][y2] = self.board[x2][y2], self.board[x1][y1]
if abs(x1 - x2) == 2:
mid = (x1 + x2) // 2
self.board[mid][(y1 + y2) // 2] = ' '
def check_winner(self):
if len(self.valid_moves('b')) == 0:
return 'w'
elif len(self.valid_moves('w')) == 0:
return 'b'
else:
return None
def play_game(player1, player2):
board = Checkerboard()
while True:
board.print_board()
print("Player 1's turn")
move = player1.get_move(board)
if move is not None:
board.make_move(*move)
winner = board.check_winner()
if winner is not None:
print(f"Player {winner} wins!")
break
board.print_board()
print("Player 2's turn")
move = player2.get_move(board)
if move is not None:
board.make_move(*move)
winner = board.check_winner()
if winner is not None:
print(f"Player {winner} wins!")
break
class HumanPlayer:
def get_move(self, board):
while True:
start = input("Enter the starting position (row col): ")
start = list(map(int, start.split()))
end = input("Enter the ending position (row col): ")
end = list(map(int, end.split()))
if (start[0], start[1]) in board.valid_moves('b') or (start[0], start[1]) in board.valid_mov
To embed this program on your website, copy the following code and paste it into your website's HTML: