awacke1 commited on
Commit
690a471
โ€ข
1 Parent(s): 61f9d9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -115
app.py CHANGED
@@ -1,116 +1,122 @@
1
  import streamlit as st
2
- import numpy as np
3
- import time
4
-
5
- # Define game board size
6
- BOARD_HEIGHT = 20
7
- BOARD_WIDTH = 10
8
-
9
- # Define shapes of tetrominoes
10
- SHAPES = [
11
- np.array([
12
- [1, 1],
13
- [1, 1]
14
- ]),
15
-
16
- np.array([
17
- [1, 1, 1],
18
- [0, 1, 0]
19
- ]),
20
-
21
- np.array([
22
- [1, 1, 1],
23
- [1, 0, 0]
24
- ]),
25
-
26
- np.array([
27
- [1, 1, 1],
28
- [0, 0, 1]
29
- ]),
30
-
31
- np.array([
32
- [1, 1, 0],
33
- [0, 1, 1]
34
- ]),
35
-
36
- np.array([
37
- [0, 1, 1],
38
- [1, 1, 0]
39
- ]),
40
-
41
- np.array([
42
- [1, 1, 1, 1]
43
- ])
44
- ]
45
-
46
- # Define colors for each tetromino
47
- COLORS = [
48
- "yellow",
49
- "orange",
50
- "cyan",
51
- "blue",
52
- "purple",
53
- "green",
54
- "red"
55
- ]
56
-
57
- # Define initial game state
58
- board = np.zeros((BOARD_HEIGHT, BOARD_WIDTH))
59
- current_shape = None
60
- current_shape_pos = (0, 0)
61
- current_shape_color = None
62
- score = 0
63
- game_over = False
64
-
65
- def get_random_shape():
66
- shape_idx = np.random.randint(len(SHAPES))
67
- shape = SHAPES[shape_idx]
68
- color = COLORS[shape_idx]
69
- return shape, color
70
-
71
- def place_shape_on_board(board, shape, position, color):
72
- shape_height, shape_width = shape.shape
73
- board_height, board_width = board.shape
74
-
75
- # Check if shape can fit on the board at the given position
76
- if position[0] + shape_height > board_height or position[1] + shape_width > board_width:
77
- return False
78
-
79
- # Check if there are any overlapping blocks
80
- if np.any(board[position[0]:position[0]+shape_height, position[1]:position[1]+shape_width] * shape):
81
- return False
82
-
83
- # Place shape on the board
84
- board[position[0]:position[0]+shape_height, position[1]:position[1]+shape_width] += shape * color
85
-
86
- return True
87
-
88
- def check_if_row_is_complete(board, row):
89
- return np.all(board[row, :])
90
-
91
- def remove_row(board, row):
92
- board[row+1:,:] = board[row:-1,:]
93
- board[0,:] = 0
94
-
95
- def remove_completed_rows(board):
96
- for row in range(board.shape[0]):
97
- if check_if_row_is_complete(board, row):
98
- remove_row(board, row)
99
-
100
- def update_game_state():
101
- global board, current_shape, current_shape_pos, current_shape_color, score, game_over
102
-
103
- # Move shape down by one position
104
- new_shape_pos = (current_shape_pos[0] + 1, current_shape_pos[1])
105
-
106
- # Check if shape can be placed on the board
107
- if not place_shape_on_board(board, current_shape, new_shape_pos, current_shape_color):
108
- # Shape cannot be placed, lock it in place
109
- place_shape_on_board(board, current_shape, current_shape_pos, current_shape_color)
110
-
111
- # Check if any rows have been completed
112
- remove_completed_rows(board)
113
-
114
- # Check if game is over
115
- if np.any(board[0,:]):
116
- game_over = True
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import random
3
+ import csv
4
+ import os
5
+
6
+ # Define the player card attributes
7
+ player_cards = {
8
+ "Player 1": {
9
+ "sketch": "๐Ÿ‘ฉ",
10
+ "character": "Nurse",
11
+ "player_board": "๐Ÿฅ",
12
+ "action_dice": "๐ŸŽฒ",
13
+ "health_tokens": "โค๏ธ",
14
+ "coin": "๐Ÿ’ฐ",
15
+ "battle_tokens": "โš”๏ธ"
16
+ },
17
+ "Player 2": {
18
+ "sketch": "๐Ÿ‘จ",
19
+ "character": "Doctor",
20
+ "player_board": "๐Ÿฅ",
21
+ "action_dice": "๐ŸŽฒ",
22
+ "health_tokens": "โค๏ธ",
23
+ "coin": "๐Ÿ’ฐ",
24
+ "battle_tokens": "โš”๏ธ"
25
+ }
26
+ }
27
+
28
+ # Define the health problems
29
+ health_problems = ["Flu", "COVID-19", "Diabetes", "Heart Disease", "Cancer"]
30
+
31
+ # Define the game rules
32
+ attack_range = (1, 20)
33
+ defense_range = (1, 10)
34
+
35
+ # Create a function to play a single round of the game
36
+ def play_round(player_card, health_problem):
37
+ st.write(f"{player_card['sketch']} {player_card['character']} attacks {health_problem} with {player_card['action_dice']}...")
38
+ attack_score = random.randint(*attack_range)
39
+ defense_score = random.randint(*defense_range)
40
+ health_ferocity = random.randint(*attack_range)
41
+ health_resistance = random.randint(*defense_range)
42
+ if attack_score > health_resistance:
43
+ st.write(f"{player_card['sketch']} {player_card['character']} deals {attack_score - health_resistance} damage to {health_problem}!")
44
+ else:
45
+ st.write(f"{player_card['sketch']} {player_card['character']} misses the attack!")
46
+ if health_ferocity > defense_score:
47
+ st.write(f"{health_problem} deals {health_ferocity - defense_score} damage to {player_card['sketch']} {player_card['character']}!")
48
+ else:
49
+ st.write(f"{health_problem} fails to attack!")
50
+
51
+ # Create a function to play multiple rounds of the game
52
+ def play_game(num_games):
53
+ # Initialize the game state
54
+ player_scores = {player: 0 for player in player_cards}
55
+ health_problem_scores = {problem: 0 for problem in health_problems}
56
+ for i in range(num_games):
57
+ # Randomly select a player and health problem
58
+ player = random.choice(list(player_cards.keys()))
59
+ health_problem = random.choice(health_problems)
60
+ # Play the round
61
+ play_round(player_cards[player], health_problem)
62
+ # Update the scores
63
+ player_scores[player] += 1
64
+ health_problem_scores[health_problem] += 1
65
+ # Save the game state to a CSV file
66
+ with open("game_state.csv", "a", newline="") as f:
67
+ writer = csv.writer(f)
68
+ if os.stat("game_state.csv").st_size == 0:
69
+ writer.writerow(["Player", "Sketch", "Character", "Player Board", "Action Dice", "Health Tokens", "Coin", "Battle Tokens", "Score"])
70
+ for player, attributes in player_cards.items():
71
+ row = [player, attributes["sketch"], attributes["character"], attributes["player_board"], attributes["action_dice"], attributes["health_tokens"], attributes["coin"], attributes["battle_tokens"], player_scores[player]]
72
+ writer.writerow(row)
73
+ for problem in health_problems:
74
+ row = [problem, health_problem_scores[problem]]
75
+ writer.writerow(row)
76
+ # Display the game results
77
+ st.write("# Game Results")
78
+ for player, score in player_scores.items():
79
+ st.write(f"{player}: {score} wins")
80
+ for problem, score in health_problem_scores.items():
81
+ st.write(f"{problem}: {score} defeats")
82
+ # Display a button to download the game state CSV file
83
+ if os.path.exists("game_state.csv"):
84
+ st.write("# Download Game State")
85
+ files = [f for f in os.listdir(".") if os.path.isfile(f) and f.endswith(".csv")]
86
+ if "game_state.csv" in files:
87
+ files.remove("game_state.csv")
88
+ if len(files) > 0:
89
+ file_to_delete = st.selectbox("Select a file to delete", files)
90
+ if st.button("Delete File"):
91
+ os.remove(file_to_delete)
92
+ if st.button("Download Game State"):
93
+ with open("game_state.csv", "r") as f:
94
+ csv_data = f.read()
95
+ st.download_button("game_state.csv", csv_data, file_name="game_state.csv", mime="text/csv")
96
+ st.write("*Note: Downloaded files are saved in your browser's default download location*")
97
+
98
+ # Define the Streamlit app
99
+ def app():
100
+ st.set_page_config(page_title="Health Care Game", page_icon="๐Ÿฅ", layout="wide")
101
+ st.title("Health Care Game")
102
+ st.sidebar.write("# Game Settings")
103
+ num_games = st.sidebar.slider("Number of games to play", 1, 100, 10)
104
+ st.sidebar.write("# Player Cards")
105
+ for player, attributes in player_cards.items():
106
+ st.sidebar.write(f"## {player}")
107
+ st.sidebar.write(f"Sketch: {attributes['sketch']}")
108
+ st.sidebar.write(f"Character: {attributes['character']}")
109
+ st.sidebar.write(f"Player Board: {attributes['player_board']}")
110
+ st.sidebar.write(f"Action Dice: {attributes['action_dice']}")
111
+ st.sidebar.write(f"Health Tokens: {attributes['health_tokens']}")
112
+ st.sidebar.write(f"Coin: {attributes['coin']}")
113
+ st.sidebar.write(f"Battle Tokens: {attributes['battle_tokens']}")
114
+ st.sidebar.write("# Health Problems")
115
+ for problem in health_problems:
116
+ st.sidebar.write(f"- {problem}")
117
+ # Start the game when the user clicks the "Play Game" button
118
+ if st.button("Play Game"):
119
+ play_game(num_games)
120
+
121
+ if __name__ == "__main__":
122
+ app()