import numpy as np
import random as rd
from tabulate import tabulate
import sys
player = ['Jorgen', 'Henrik']
pos = np.zeros((16, 2)).astype(int)
totalsumma = np.zeros(2).astype(int)
delsumma = np.zeros(2).astype(int)
bonus = np.zeros(2).astype(int)
for i in range(0, 16):
for j in range(0, 2):
pos[i, j] = -1
def kast1():
dice1 = [rd.randint(1, 6) for i in range(5)]
print(f'Tärningsutslag efter första kastet = {dice1}')
def kast2():
k = 0
pick1 = []
while True:
save = int(input(f'Spara undan tärning {k+1} (0=sluta spara): '))
k = k + 1
if (save != 0) and (k < 5):
pick1.append(save)
else:
k = k - 1
break
dice2 = [rd.randint(1, 6) for i in range(5 - k)]
print(f'Kast 2 gav: {dice2}')
print(f'Tärningsutslag efter andra kastet = {dice2 + pick1}')
def kast3():
k = 0
pick2 = []
while True:
save = int(input(f'Spara undan tärning {k+1} (0=sluta spara): '))
k = k + 1
if (save != 0) and (k < 5):
pick2.append(save)
else:
k = k - 1
break
dice2 = [rd.randint(1, 6) for i in range(5 - k)]
print(f'Kast 3 gav: {dice2}')
print(f'Tärningsutslag efter tredje kastet = {dice2 + pick2}')
def summationer():
delsumma[0] = 0
delsumma[1] = 0
for j in range( 0, 2 ):
for i in range( 1, 7 ):
q = pos[i, j]
if q == -1:
q = 0
delsumma[j] = delsumma[j] + q
for j in range( 0, 2 ):
if delsumma[j] >= 63:
bonus[j] = 50
totalsumma[0] = delsumma[0] + bonus[0]
totalsumma[1] = delsumma[1] + bonus[1]
for j in range( 0, 2 ):
for i in range( 7, 16 ):
q = pos[i, j]
if q == -1:
q = 0
totalsumma[j] = totalsumma[j] + q
def get(x_):
if x_ == -1:
return ' '
else:
return x_
def print_table():
col_names = [' ', ' ', 'Spelare 1', 'Spelare 2']
data = [['Pos', 'Beskrivning', player[0], player[1]],
[' 1', 'Ettor', get(pos[1, 0]), get(pos[1, 1])],
[' 2', 'Tvåor', get(pos[2, 0]), get(pos[2, 1])],
[' 3', 'Treor', get( pos[3, 0] ), get( pos[3, 1] )],
[' 4', 'Fyror', get( pos[4, 0] ), get( pos[4, 1] )],
[' 5', 'Femmor', get( pos[5, 0] ), get( pos[5, 1] )],
[' 6', 'Sexor', get( pos[6, 0] ), get( pos[6, 1] )],
[' ', 'Delsumma:', delsumma[0], delsumma[1]],
[' ', 'Bonus:', bonus[0], bonus[1]],
[' 7', 'Ett par', get( pos[7, 0] ), get( pos[7, 1] )],
[' 8', 'Två par', get( pos[8, 0] ), get( pos[8, 1] )],
[' 9', 'Tretal', get( pos[9, 0] ), get( pos[9, 1] )],
['10', 'Fyrtal', get( pos[10, 0] ), get( pos[10, 1] )],
['11', 'Liten straight', get( pos[11, 0] ), get( pos[11, 1] )],
['12', 'Stor straight', get( pos[12, 0] ), get( pos[12, 1] )],
['13', 'Kåk', get( pos[13, 0] ), get( pos[13, 1] )],
['14', 'Chans', get( pos[14, 0] ), get( pos[14, 1] )],
['15', 'Yatzy', get( pos[15, 0] ), get( pos[15, 1] )],
[' ', 'Totalsumma:', totalsumma[0], totalsumma[1]]]
print(tabulate(data, headers=col_names, tablefmt='fancy_grid'))
################################################
##################YATZY#########################
################################################
m = 1
player_ = 0
print_table()
while m < 31:
print('#' * 52)
print(f'############## Spelare: {player[player_]} #####################')
print('#' * 52)
kast1()
val1 = input('Vill du kasta om (j/n)?: ')
if val1.lower() == 'j':
kast2()
val2 = input('Vill du kasta om (j/n)?: ')
if val2.lower() == 'j':
kast3()
position = int(input(f'Var vänlig välj en position att sätta poäng på:'))
poang = int(input(f'Ange antal poäng: '))
pos[position, player_] = poang
m = m + 1
if player_ == 0:
player_ = 1
else:
player_ = 0
summationer()
print_table()
utskrift = input('Vill du skriva ut resultatet till textfil (j/n)?: ')
if utskrift.lower() == 'j':
orig_stdout = sys.stdout
f = open('Yatzy_resultat.txt', 'w')
sys.stdout = f
print_table()
sys.stdout = orig_stdout
f.close()