## Cvičení 5

### 05a

mat.txt:

1 2 3
4 5 6
7 8 9

read_mat1.py:

f=open("mat.txt")
pole=[]
for l in f:
pole.append(list(map(int,l.split())))

for i in pole:
print(*i, sep='\t')

pole2=[]
for x in pole:
r=[]
for i in x:
r.append(i)
pole2.append(r)

pole[0][0]=-10

print("pole2")
for r in pole2:
print(*r)
print("pole")
for r in pole:
print(*r)

read_mat2.py:

f=open("mat.txt")
pole=[]
for l in f:
pole.append(list(map(int,l.split())))

for i in pole:
print(*i, sep='\t')

pole2=[ [ i for i in x ] for x in pole ]

pole[0][0]=-10

print("pole2")
for r in pole2:
print(*r)
print("pole")
for r in pole:
print(*r)

copy_deepcopy.py:

f=open("mat.txt")
pole=[]
for l in f:
pole.append(list(map(int,l.split())))

for i in pole:
print(*i, sep='\t')

import copy
pole2=copy.deepcopy(pole)

pole[0][0]=-10
print("pole2")
for r in pole2:
print(*r)
print("pole")
for r in pole:
print(*r)

### 05b

Násobení matic:

f=open("mat.txt")
pole=[]
for l in f:
pole.append(list(map(int,l.split())))

f=open("mat2.txt")
pole2=[]
for l in f:
pole2.append(list(map(int,l.split())))

def print_mat(m):
for r in range(len(m)):
for s in range(len(m[r])):
print(m[r][s],end=' ')
print()

def mult_mat(a,b):
res=[]
for r in range(len(a)):
res_r=[]
for s in range(len(b[r])):
suma=0
for j in range(len(a[r])):
suma+=a[r][j]*b[j][s]
res_r.append(suma)
res.append(res_r)
return res

print("pole")
print_mat(pole)
print("pole2")
print_mat(pole2)
mult = mult_mat(pole,pole2)
print("Vysledek")
print_mat(mult)
mult2 = mult_mat(pole2,pole)
print("Vysledek2")
print_mat(mult2)
mat2.txt:
0 1 0
1 0 0
0 0 1

### 05c

piskvorky.py:

def print_pole(m):
for i in m:
for j in i:
c='.'
if j==1:
c='X'
elif j==2:
c='O'
print(c,end='')
print()

dir_r=[ 1, 1, 1, 0]
dir_s=[-1, 0, 1, 1]
def check_winner(m):
winner=0
fail=True
for r in range(len(m)):
for s in range(len(m[r])):
if m[r][s]!=0:
for dir in range(4):
if r+4*dir_r[dir]>=0 and r+4*dir_r[dir]<len(m):
if s+4*dir_s[dir]>=0 and s+4*dir_s[dir]<len(m[r]):
fail=False
for j in range(1,5):
if m[r][s]!=m[r+j*dir_r[dir]][s+j*dir_s[dir]]:
fail=True
break
if not fail:
winner=m[r][s]
break
if not fail:
break
if not fail:
break
return winner

size=20
pole=[ [0]*size for i in range(size)]

end=False
sign=1
while not end:
print_pole(pole)
good_move=False
while not good_move:
s=input()
move = list(map(int,s.split()))
if move[0]>=0 and move[0]<size and move[1]>=0 and move[1]<size:
if pole[move[0]][move[1]]==0:
good_move=True
else:
print("Obsazeno")
else:
print("Mimo hraci plochu")

pole[move[0]][move[1]]=sign

winner = check_winner(pole)
end = (winner!=0)

sign=3-sign

print_pole(pole)
if winner==1:
print("Vyhral krizek")
elif winner==2:
print("Vyhralo kolecko")

courses/b3b33alp/cviceni/kratka_videa/c05.txt · Last modified: 2021/10/10 22:22 by stepan