|
Swap2 PenteX?
Posted:
Mar 5, 2024, 7:55 AM
|
|
I've heard good things about the newly released LMM Claude 3, so I decided to test it on some game code- 15x15 board Pente, where overlines are allowed, but don't win (allowing rare draw cases).
Basic game code:
class GameBoard: def __init__(self): self.board = [[' ' for _ in range(15)] for _ in range(15)] self.player = 'X' self.captured_pairs = {'X': 0, 'O': 0}
def is_valid_move(self, row, col): return 0 <= row < 15 and 0 <= col < 15 and self.board[row][col] == ' '
def make_move(self, row, col): self.board[row][col] = self.player self.check_captures(row, col) self.check_win(row, col) self.player = 'O' if self.player == 'X' else 'X'
def check_captures(self, row, col): player = self.player opponent = 'O' if player == 'X' else 'X'
# Check horizontal captures for i in range(col - 1, -1, -1): if self.board[row] == opponent: if i == 0 or self.board[row][i - 1] == player: break elif self.board[row] == ' ': break else: j = i + 1 while j < col and self.board[row][j] == opponent: j += 1 if j < col: continue self.capture_pairs(row, i, row, j - 1, player) break
for i in range(col + 1, 15): if self.board[row] == opponent: if i == 14 or self.board[row][i + 1] == player: break elif self.board[row] == ' ': break else: j = i - 1 while j >= 0 and self.board[row][j] == opponent: j -= 1 if j >= 0: continue self.capture_pairs(row, i, row, j + 1, player) break
# Check vertical captures for i in range(row - 1, -1, -1): if self.board[col] == opponent: if i == 0 or self.board[i - 1][col] == player: break elif self.board[col] == ' ': break else: j = i + 1 while j < row and self.board[j][col] == opponent: j += 1 if j < row: continue self.capture_pairs(i, col, j - 1, col, player) break
for i in range(row + 1, 15): if self.board[col] == opponent: if i == 14 or self.board[i + 1][col] == player: break elif self.board[col] == ' ': break else: j = i - 1 while j >= 0 and self.board[j][col] == opponent: j -= 1 if j >= 0: continue self.capture_pairs(j + 1, col, i, col, player) break
def capture_pairs(self, row1, col1, row2, col2, player): for i in range(row1, row2 + 1): for j in range(col1, col2 + 1): self.board[j] = ' ' self.captured_pairs[player] += (row2 - row1 + 1) // 2
def check_win(self, row, col): player = self.player opponent = 'O' if player == 'X' else 'X'
# Check horizontal win count = 1 for i in range(col - 1, -1, -1): if self.board[row] == player: count += 1 else: break
for i in range(col + 1, 15): if self.board[row] == player: count += 1 else: break
if count == 5: print(f"Player {player} wins!") exit()
# Check vertical win count = 1 for i in range(row - 1, -1, -1): if self.board[col] == player: count += 1 else: break
for i in range(row + 1, 15): if self.board[col] == player: count += 1 else: break
if count == 5: print(f"Player {player} wins!") exit()
# Check diagonal win (top-left to bottom-right) count = 1 i, j = row - 1, col - 1 while i >= 0 and j >= 0: if self.board[j] == player: count += 1 else: break i -= 1 j -= 1
i, j = row + 1, col + 1 while i < 15 and j < 15: if self.board[j] == player: count += 1 else: break i += 1 j += 1
if count == 5: print(f"Player {player} wins!") exit()
# Check diagonal win (top-right to bottom-left) count = 1 i, j = row - 1, col + 1 while i >= 0 and j < 15: if self.board[j] == player: count += 1 else: break i -= 1 j += 1
i, j = row + 1, col - 1 while i < 15 and j >= 0: if self.board[j] == player: count += 1 else: break i += 1 j -= 1
if count == 5: print(f"Player {player} wins!") exit()
# Check for draw if self.captured_pairs['X'] + self.captured_pairs['O'] == 112: print("Draw!") exit()
def print_board(self): print(" " + " ".join(map(str, [chr(i) for i in range(ord('a'), ord('o') + 1)]))) for i in range(15): row = [self.board[j] if self.board[j] != ' ' else '.' for j in range(15)] print(f"{i + 1:2d} {' '.join(row)}")
def play_game(): board = GameBoard() while True: board.print_board() player = 'X' if board.player == 'X' else 'O' row = int(input(f"Player {player}, enter row: ")) - 1 col = ord(input(f"Player {player}, enter column: ").lower()) - ord('a') if board.is_valid_move(row, col): board.make_move(row, col) else: print("Invalid move, try again.")
if __name__ == "__main__": play_game()
Swap2 opening code: class GameBoard: def __init__(self): self.board = [[' ' for _ in range(15)] for _ in range(15)] self.player = None self.captured_pairs = {'X': 0, 'O': 0}
def is_valid_move(self, row, col): return 0 <= row < 15 and 0 <= col < 15 and self.board[row][col] == ' '
def make_move(self, row, col): self.board[row][col] = self.player self.check_captures(row, col) self.check_win(row, col) self.player = 'O' if self.player == 'X' else 'X'
# ... (the rest of the GameBoard class remains the same)
def pie_rule_opening(): board = GameBoard() print("Pie rule opening:") print("Player X, place two X pieces and one O piece on the board.") while True: row1 = int(input("Enter row for X: ")) - 1 col1 = ord(input("Enter column for X: ").lower()) - ord('a') if board.is_valid_move(row1, col1): board.board[row1][col1] = 'X' break else: print("Invalid move, try again.")
while True: row2 = int(input("Enter row for X: ")) - 1 col2 = ord(input("Enter column for X: ").lower()) - ord('a') if board.is_valid_move(row2, col2): board.board[row2][col2] = 'X' break else: print("Invalid move, try again.")
while True: row3 = int(input("Enter row for O: ")) - 1 col3 = ord(input("Enter column for O: ").lower()) - ord('a') if board.is_valid_move(row3, col3): board.board[row3][col3] = 'O' break else: print("Invalid move, try again.")
board.print_board()
print("Player O, choose an option:") print("1. Place an O piece on the board and play as O.") print("2. Switch pieces and play as X.") print("3. Add one X and one O piece, and let X choose who plays first.")
choice = int(input("Enter your choice (1, 2, or 3): "))
if choice == 1: board.player = 'O' while True: row = int(input("Player O, enter row: ")) - 1 col = ord(input("Player O, enter column: ").lower()) - ord('a') if board.is_valid_move(row, col): board.make_move(row, col) break else: print("Invalid move, try again.") elif choice == 2: board.player = 'X' while True: row = int(input("Player O, enter row for O: ")) - 1 col = ord(input("Player O, enter column for O: ").lower()) - ord('a') if board.is_valid_move(row, col): board.board[row][col] = 'O' break else: print("Invalid move, try again.") elif choice == 3: while True: row1 = int(input("Player O, enter row for X: ")) - 1 col1 = ord(input("Player O, enter column for X: ").lower()) - ord('a') if board.is_valid_move(row1, col1): board.board[row1][col1] = 'X' break else: print("Invalid move, try again.")
while True: row2 = int(input("Player O, enter row for O: ")) - 1 col2 = ord(input("Player O, enter column for O: ").lower()) - ord('a') if board.is_valid_move(row2, col2): board.board[row2][col2] = 'O' break else: print("Invalid move, try again.")
board.print_board() player_choice = input("Player X, choose who plays first (X or O): ").upper() if player_choice == 'X': board.player = 'X' else: board.player = 'O' else: print("Invalid choice, starting a new game.") return play_game()
return board
def play_game(): board = pie_rule_opening() while True: board.print_board() player = board.player row = int(input(f"Player {player}, enter row: ")) - 1 col = ord(input(f"Player {player}, enter column: ").lower()) - ord('a') if board.is_valid_move(row, col): board.make_move(row, col) else: print("Invalid move, try again.")
if __name__ == "__main__": play_game()
Does it work? Dunno.
|
Retired from TB Pente, but still playing live games & exploring variants like D, poof and boat
|
|