Skip to content
Extraits de code Groupes Projets
Valider 72e35a08 rédigé par Adrien Payen's avatar Adrien Payen
Parcourir les fichiers

clean base

parent 600a7b54
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
import random
from pprint import pprint
import numpy as np
# Initialisation du board
g = [[0 for j in range(15)]for i in range(15)]
# Liens entre les cases
for i in range(9):
g[i][i+1]=1
for i in range(10,14):
g[i][i+1]=1
g[2][10]=1
g[9][14]=1
pprint(g)
# Initialisation du safetydice
gsd = [[0 for j in range(15)]for i in range(15)]
for i in range(9) :
gsd[i][i+1]= 0.5
gsd[i][i]= 0.5
for i in range(10,14):
gsd[i][i+1]= 0.5
gsd[i][i]= 0.5
gsd[2][3] *= 0.5
gsd[2][10] = gsd[2][3]
gsd[9][9]= 0.5 # le fait de faire 0 et rester en case 10
gsd[9][14] = 0.5
pprint(gsd)
\ No newline at end of file
Fichier ajouté
import numpy as np
from tmc import *
from tmc import TransitionMatrixCalculator
def markovDecision(layout, circle) :
k_states = 15
layout = np.array([0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 1, 0])
print(markovDecision(layout, False))
print(markovDecision(layout, True))
import random
from pprint import pprint
import numpy as np
# Initialisation du board
g = [[0 for j in range(15)]for i in range(15)]
# pprint(g)
# Liens entre les cases
for i in range(9):
g[i][i+1]=1
for i in range(10,14):
g[i][i+1]=1
g[2][10]=1
g[9][14]=1
pprint(g)
# 3 types de dés
def safe():
choix = [0,1]
return random.choices(choix,[0.5, 0.5])[0]
def normal():
choix = [0,1,2]
return random.choices(choix,[0.33, 0.33, 0.33])[0]
def risky():
choix = [0,1,2,3]
return random.choices(choix,[0.25, 0.25, 0.25, 0.25])[0]
def restart() :
if i in layout == 1 :
g[i] = g[0]
return
def Penalty() :
if i in layout == 2 :
g[i] = g[i-3]
return
def Prison() :
if i in layout == 3 :
# passer son tour
# markovDecision(layout,circle)
def markovDecision(layout, circle : bool) :
return Expec, Dice
# Fonction de modification du circle pour faire une boucle si True
def circle(bool) :
if bool == True :
g[14][0] = 1
# pprint(g) # si le dés dépasse la case 15 alors il recommence au début
return g
pprint(circle(False))
np.random.seed(0)
layout = np.ndarray([0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 1, 0])
choices = [0,1,2,3]
print(layout)
print(np.linspace(1,15,15, dtype=np.float16))
markovDecision(layout, circle(True))
players = dict()
player_values = list()
def Create_players(players, player_values) :
while True :
total_players = input("Choose total number of player (1): ").strip()
if total_players not in ['1'] :
print("Invalid input !")
else :
print("Total players in the game {}".format{total_players})
total_players = int(total_players)
for player_index in range(1, total_players + 1 ) :
while True :
set_marker = input("Now set your sign player {} : (alphabet only)".format(player_index)).strip().upper()
if len(set_marker) != 1 or not(set_marker >= "A" and set_marker <= "Z") :
print("Invalid Sign Choosen !")
else :
player_values.append(set_marker) # market set
player_values.append(0) # position initiale en 0
import numpy as np
import random
from pprint import pprint
class Board:
def __init__(self, layout):
self.size = len(layout)
self.structure = [[0 for _ in range(self.size)] for _ in range(self.size)]
for i in range(len(layout) - 1):
self.structure[i][layout[i]] = 1
def is_valid_position(self, position):
return 0 <= position < self.size
def get_next_position(self, current_position):
if self.is_valid_position(current_position):
for i in range(current_position + 1, self.size):
if self.structure[current_position][i] == 1:
return i
return current_position
def circle(self, bool_value):
if bool_value:
self.structure[self.size - 1][0] = 1
return self.structure
def print_board(self, player_position):
for i in range(self.size):
if i == player_position:
print("P", end=' ')
elif self.structure[i][player_position] == 1:
print("T", end=' ')
else:
print("-", end=' ')
print()
class Player:
def __init__(self, name):
self.name = name
self.position = 0
self.skip_turns = 0
def move(self, steps):
self.position += steps
def apply_trap(self, trap_type, dice_type):
# Si le dé est "safe", aucun piège ne s'applique
if dice_type == "safe":
return
# Si le dé est "normal", la probabilité qu'un piège s'applique est de 1/2
elif dice_type == "normal":
if random.random() < 0.5:
self.apply_trap_effect(trap_type)
# Si le dé est "risky", un piège s'applique toujours
elif dice_type == "risky":
self.apply_trap_effect(trap_type)
def apply_trap_effect(self, trap_type):
if trap_type == 1:
self.position = 0
elif trap_type == 2:
self.position -= 3
if self.position < 0:
self.position = 0
elif trap_type == 3:
self.skip_turns += 1
class Dice:
def __init__(self, dice_type):
self.dice_type = dice_type
def roll(self):
if self.dice_type == "safe":
return random.randint(0, 1)
elif self.dice_type == "normal":
return random.randint(0, 2)
elif self.dice_type == "risky":
return random.randint(0, 3)
def main():
layout = np.array([0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 1, 0])
board = Board(layout)
print("\nCurrent board structure:")
board.print_board(0)
player_name = input("Enter your name: ")
player = Player(player_name)
restart_at_beginning = input("Do you want to restart at the beginning when passing the position 15? (yes/no): ").lower() == "yes"
board.circle(restart_at_beginning)
while True:
dice_choice = input(f"\n{player.name}, choose your dice (safe/normal/risky): ").lower()
while dice_choice not in ["safe", "normal", "risky"]:
print("Invalid choice. Please choose again.")
dice_choice = input(f"{player.name}, choose your dice (safe/normal/risky): ").lower()
input(f"\n{player.name}, it's your turn. Press Enter to roll the dice.")
steps = Dice(dice_choice).roll()
print(f"{player.name} rolled a {steps}.")
player.move(steps)
player.position = board.get_next_position(player.position)
print(f"{player.name} is now at position {player.position}.")
board.print_board(player.position)
if layout[player.position] == 1:
print(f"{player.name} encountered a 'restart' trap and goes back to square 1.")
player.position = 0
elif layout[player.position] == 2:
print(f"{player.name} encountered a 'penalty' trap and goes back 3 steps.")
player.apply_trap(2, dice_choice)
elif layout[player.position] == 3:
print(f"{player.name} encountered a 'prison' trap and will skip the next turn.")
player.apply_trap(3, dice_choice)
if player.position >= len(layout) - 1:
print(f"\nCongratulations! {player.name} won the game!")
return
if player.skip_turns > 0:
print(f"{player.name} skips the turn due to 'prison' trap.")
player.skip_turns -= 1
continue
if __name__ == "__main__":
main()
......@@ -218,7 +218,7 @@ class TransitionMatrixCalculator:
# create a function that test the transition matrix for different layout each time with one trap of each sort
def tst_transition_matrix(self):
# create a list of 100 different layouts
layouts = self.generate_arrays(100)
layouts = self.generate_arrays(1000)
for array in layouts:
print(array)
self.compute_transition_matrix(array, False)
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter