Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -19,28 +19,16 @@ SHIPS = [
|
|
| 19 |
{"name": "Destroyer", "size": 2, "symbol": ":speedboat:"}
|
| 20 |
]
|
| 21 |
|
| 22 |
-
# Initialize difficulty in session state if not present
|
| 23 |
-
if 'difficulty' not in st.session_state:
|
| 24 |
-
st.session_state.difficulty = "Beginner"
|
| 25 |
-
|
| 26 |
-
# Add difficulty selector
|
| 27 |
-
st.session_state.difficulty = st.selectbox(
|
| 28 |
-
"Select Difficulty",
|
| 29 |
-
["Beginner", "Expert"],
|
| 30 |
-
index=0 if st.session_state.difficulty == "Beginner" else 1
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
# MongoDB Atlas Connection
|
| 34 |
client = MongoClient(os.environ.get('MONGODB_ATLAS_URI'))
|
| 35 |
db = client['battleship']
|
| 36 |
games = db['games']
|
| 37 |
|
| 38 |
-
region = 'us-east-1' if st.session_state.difficulty == "Begginer" else 'us-west-2'
|
| 39 |
# AWS Bedrock Client Setup
|
| 40 |
bedrock_runtime = boto3.client('bedrock-runtime',
|
| 41 |
aws_access_key_id=os.environ.get('AWS_ACCESS_KEY'),
|
| 42 |
aws_secret_access_key=os.environ.get('AWS_SECRET_KEY'),
|
| 43 |
-
region_name=
|
| 44 |
|
| 45 |
def get_bedrock_claude_move(board, openent_moves=None):
|
| 46 |
"""
|
|
@@ -79,6 +67,47 @@ def create_empty_board():
|
|
| 79 |
"""Create an empty game board."""
|
| 80 |
return [[EMPTY for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
def place_ships_randomly(board):
|
| 83 |
"""Place ships randomly on the board."""
|
| 84 |
for ship in SHIPS:
|
|
@@ -91,17 +120,6 @@ def place_ships_randomly(board):
|
|
| 91 |
break
|
| 92 |
return board
|
| 93 |
|
| 94 |
-
def can_place_ship(board, row, col, size, direction):
|
| 95 |
-
"""Check if a ship can be placed at the given position."""
|
| 96 |
-
if direction == 'horizontal':
|
| 97 |
-
if col + size > BOARD_SIZE:
|
| 98 |
-
return False
|
| 99 |
-
return all(board[row][col+i] == EMPTY for i in range(size))
|
| 100 |
-
else:
|
| 101 |
-
if row + size > BOARD_SIZE:
|
| 102 |
-
return False
|
| 103 |
-
return all(board[row+i][col] == EMPTY for i in range(size))
|
| 104 |
-
|
| 105 |
def place_ship(board, row, col, size, direction, symbol):
|
| 106 |
"""Place a ship on the board."""
|
| 107 |
if direction == 'horizontal':
|
|
@@ -113,6 +131,9 @@ def place_ship(board, row, col, size, direction, symbol):
|
|
| 113 |
|
| 114 |
def initialize_game():
|
| 115 |
"""Initialize the game state."""
|
|
|
|
|
|
|
|
|
|
| 116 |
if 'game_state' not in st.session_state:
|
| 117 |
st.session_state.game_state = {
|
| 118 |
'player_board': place_ships_randomly(create_empty_board()),
|
|
@@ -227,9 +248,18 @@ def opponent_turn():
|
|
| 227 |
|
| 228 |
def main():
|
| 229 |
"""Main function to run the Battleship game."""
|
| 230 |
-
st.title('Battleship Game
|
| 231 |
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
initialize_game()
|
| 235 |
if st.session_state.game_state['game_over']:
|
|
|
|
| 19 |
{"name": "Destroyer", "size": 2, "symbol": ":speedboat:"}
|
| 20 |
]
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# MongoDB Atlas Connection
|
| 23 |
client = MongoClient(os.environ.get('MONGODB_ATLAS_URI'))
|
| 24 |
db = client['battleship']
|
| 25 |
games = db['games']
|
| 26 |
|
|
|
|
| 27 |
# AWS Bedrock Client Setup
|
| 28 |
bedrock_runtime = boto3.client('bedrock-runtime',
|
| 29 |
aws_access_key_id=os.environ.get('AWS_ACCESS_KEY'),
|
| 30 |
aws_secret_access_key=os.environ.get('AWS_SECRET_KEY'),
|
| 31 |
+
region_name="us-west-2")
|
| 32 |
|
| 33 |
def get_bedrock_claude_move(board, openent_moves=None):
|
| 34 |
"""
|
|
|
|
| 67 |
"""Create an empty game board."""
|
| 68 |
return [[EMPTY for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
|
| 69 |
|
| 70 |
+
def is_valid_position(row, col):
|
| 71 |
+
"""Check if a position is within the board boundaries."""
|
| 72 |
+
return 0 <= row < BOARD_SIZE and 0 <= col < BOARD_SIZE
|
| 73 |
+
|
| 74 |
+
def check_adjacent_cells(board, row, col):
|
| 75 |
+
"""Check if any adjacent cells (including diagonals) contain a ship."""
|
| 76 |
+
for i in range(-1, 2):
|
| 77 |
+
for j in range(-1, 2):
|
| 78 |
+
if i == 0 and j == 0:
|
| 79 |
+
continue
|
| 80 |
+
new_row, new_col = row + i, col + j
|
| 81 |
+
if is_valid_position(new_row, new_col) and board[new_row][new_col] != EMPTY:
|
| 82 |
+
return False
|
| 83 |
+
return True
|
| 84 |
+
|
| 85 |
+
def can_place_ship(board, row, col, size, direction):
|
| 86 |
+
"""Check if a ship can be placed at the given position."""
|
| 87 |
+
if direction == 'horizontal':
|
| 88 |
+
if col + size > BOARD_SIZE:
|
| 89 |
+
return False
|
| 90 |
+
# Check the ship's cells and their adjacent cells
|
| 91 |
+
for i in range(-1, size + 1):
|
| 92 |
+
for j in range(-1, 2):
|
| 93 |
+
check_row = row + j
|
| 94 |
+
check_col = col + i
|
| 95 |
+
if is_valid_position(check_row, check_col):
|
| 96 |
+
if board[check_row][check_col] != EMPTY:
|
| 97 |
+
return False
|
| 98 |
+
else: # vertical
|
| 99 |
+
if row + size > BOARD_SIZE:
|
| 100 |
+
return False
|
| 101 |
+
# Check the ship's cells and their adjacent cells
|
| 102 |
+
for i in range(-1, 2):
|
| 103 |
+
for j in range(-1, size + 1):
|
| 104 |
+
check_row = row + j
|
| 105 |
+
check_col = col + i
|
| 106 |
+
if is_valid_position(check_row, check_col):
|
| 107 |
+
if board[check_row][check_col] != EMPTY:
|
| 108 |
+
return False
|
| 109 |
+
return True
|
| 110 |
+
|
| 111 |
def place_ships_randomly(board):
|
| 112 |
"""Place ships randomly on the board."""
|
| 113 |
for ship in SHIPS:
|
|
|
|
| 120 |
break
|
| 121 |
return board
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
def place_ship(board, row, col, size, direction, symbol):
|
| 124 |
"""Place a ship on the board."""
|
| 125 |
if direction == 'horizontal':
|
|
|
|
| 131 |
|
| 132 |
def initialize_game():
|
| 133 |
"""Initialize the game state."""
|
| 134 |
+
if 'difficulty' not in st.session_state:
|
| 135 |
+
st.session_state.difficulty = "Beginner"
|
| 136 |
+
|
| 137 |
if 'game_state' not in st.session_state:
|
| 138 |
st.session_state.game_state = {
|
| 139 |
'player_board': place_ships_randomly(create_empty_board()),
|
|
|
|
| 248 |
|
| 249 |
def main():
|
| 250 |
"""Main function to run the Battleship game."""
|
| 251 |
+
st.title('Battleship Game')
|
| 252 |
|
| 253 |
+
# Initialize difficulty in session state if not present
|
| 254 |
+
if 'difficulty' not in st.session_state:
|
| 255 |
+
st.session_state.difficulty = "Beginner"
|
| 256 |
+
|
| 257 |
+
# Add difficulty selector
|
| 258 |
+
st.session_state.difficulty = st.selectbox(
|
| 259 |
+
"Select Difficulty",
|
| 260 |
+
["Beginner", "Expert"],
|
| 261 |
+
index=0 if st.session_state.difficulty == "Beginner" else 1
|
| 262 |
+
)
|
| 263 |
|
| 264 |
initialize_game()
|
| 265 |
if st.session_state.game_state['game_over']:
|