Renamed
This commit is contained in:
parent
65ff0bd56f
commit
b951a51d82
2 changed files with 0 additions and 129 deletions
55
game.py
55
game.py
|
|
@ -1,55 +0,0 @@
|
||||||
from player import get_player_move
|
|
||||||
from random import shuffle
|
|
||||||
|
|
||||||
|
|
||||||
drug = [3, 5, 7]
|
|
||||||
|
|
||||||
playerType = {
|
|
||||||
"human": get_player_move
|
|
||||||
}
|
|
||||||
|
|
||||||
players = [
|
|
||||||
{
|
|
||||||
"name": "Joueur 1",
|
|
||||||
"type": "human"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "Joueur 2",
|
|
||||||
"type": "human"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def show_current_game():
|
|
||||||
print("=============")
|
|
||||||
for index, val in enumerate(drug):
|
|
||||||
print(f"L{index + 1}: {'o' * val}")
|
|
||||||
print("=============")
|
|
||||||
|
|
||||||
|
|
||||||
def execute_move(row, value, name):
|
|
||||||
drug[row] -= value
|
|
||||||
print(f"{name} a enlevé {value} allumettes(s) sur la ligne L{row + 1}")
|
|
||||||
|
|
||||||
|
|
||||||
def play(name, type_):
|
|
||||||
show_current_game()
|
|
||||||
execute_move(*playerType[type_](name, drug), name)
|
|
||||||
if sum(drug) == 0:
|
|
||||||
show_current_game()
|
|
||||||
print(f"{name} à gangné !!")
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
# Choisi au hasard le premier joueur
|
|
||||||
shuffle(players)
|
|
||||||
|
|
||||||
# Affiche l'ordre de jeu
|
|
||||||
print(f"Voici l'ordre des joueurs: {' => '.join([player['name'] for player in players])}")
|
|
||||||
|
|
||||||
while True:
|
|
||||||
for player in players:
|
|
||||||
if play(player["name"], player["type"]):
|
|
||||||
exit(0)
|
|
||||||
|
|
@ -1,74 +0,0 @@
|
||||||
def toBin(integer):
|
|
||||||
"""
|
|
||||||
Convert a decimal integer into a binary"
|
|
||||||
:param integer: the integer to be converted
|
|
||||||
:type integer int
|
|
||||||
:return: int
|
|
||||||
"""
|
|
||||||
return int(str(bin(integer))[2:])
|
|
||||||
|
|
||||||
|
|
||||||
def toDec(binVal):
|
|
||||||
"""
|
|
||||||
Convert a binary value to a decimal value
|
|
||||||
:param binVal: The bin value to be converted
|
|
||||||
:type binVal: int, str
|
|
||||||
:return: int
|
|
||||||
"""
|
|
||||||
return int(str(binVal), 2)
|
|
||||||
|
|
||||||
|
|
||||||
def toBinArray(array):
|
|
||||||
"""
|
|
||||||
Turn an array of decimal into an array of binary
|
|
||||||
:param array:
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
return [toBin(el) for el in array]
|
|
||||||
|
|
||||||
|
|
||||||
def computeS(array):
|
|
||||||
"""
|
|
||||||
Sum all values in an array
|
|
||||||
:param array: the array to compute on
|
|
||||||
:type array: list
|
|
||||||
:return: int
|
|
||||||
"""
|
|
||||||
return sum(array)
|
|
||||||
|
|
||||||
|
|
||||||
def nimSomme(integer):
|
|
||||||
"""Return an array with 0 if element is even, 1 otherwise"""
|
|
||||||
return [int(el) % 2 for el in str(integer)]
|
|
||||||
|
|
||||||
|
|
||||||
def isSafe(array):
|
|
||||||
"""
|
|
||||||
Return True if value computed from the array is zero, False otherwise
|
|
||||||
:param array: the array to compute
|
|
||||||
:type array: list
|
|
||||||
:return: bool
|
|
||||||
"""
|
|
||||||
def list2str(array):
|
|
||||||
a = ""
|
|
||||||
for el in array:
|
|
||||||
a += str(el)
|
|
||||||
return a
|
|
||||||
|
|
||||||
binString = list2str([int(el) % 2 for el in list2str(array)])
|
|
||||||
return toDec(binString) == 0
|
|
||||||
|
|
||||||
|
|
||||||
array = [3, 5, 7]
|
|
||||||
|
|
||||||
binArr = toBinArray(array)
|
|
||||||
print("Bin Array: ", binArr)
|
|
||||||
|
|
||||||
S = computeS(binArr)
|
|
||||||
print("S: ", S)
|
|
||||||
|
|
||||||
nimS = nimSomme(S)
|
|
||||||
print("Somme de NIM: ", nimS)
|
|
||||||
|
|
||||||
safe = isSafe(nimS)
|
|
||||||
print("Safe: ", safe)
|
|
||||||
Loading…
Reference in a new issue