# 6μ₯ Lab λμ λ¬Έμ νμ΄
278p_Lab λμ λ¬Έμ )
def menu_choice(i):
if i == 1:
print(friends)
elif i == 2:
name = input('μ΄λ¦μ μ
λ ₯νμμ€: ')
friends.append(name)
elif i == 3:
del_name = input('μμ ν μ΄λ¦μ μ
λ ₯νμμ€: ')
if del_name in friends:
friends.remove(del_name)
else:
print('μ΄λ¦μ΄ μΉκ΅¬ λͺ©λ‘μ μ‘΄μ¬νμ§ μμ΅λλ€.')
elif i == 4:
old_name = input('λ³κ²½ν μ΄λ¦μ μ
λ ₯νμμ€: ')
if old_name in friends:
index = friends.index(old_name)
new_name = input('μ μ΄λ¦μ μ
λ ₯νμμ€: ')
friends[index] = new_name
else:
print('μ΄λ¦μ΄ μΉκ΅¬ λͺ©λ‘μ μ‘΄μ¬νμ§ μμ΅λλ€.')
menu = 0
friends = []
while menu != 5:
print('-------------------')
print('1. μΉκ΅¬ 리μ€νΈ μΆλ ₯')
print('2. μΉκ΅¬ μΆκ°')
print('3. μΉκ΅¬ μμ ')
print('4. μ΄λ¦ λ³κ²½')
print('5. μ’
λ£')
menu = int(input('λ©λ΄λ₯Ό μ ννμμ€: '))
menu_choice(menu)
287p_Lab λμ λ¬Έμ )
board = [[' ' for x in range (3)] for y in range(3)]
def print_board():
for r in range(3):
print(" " + board[r][0] + "| " + board[r][1] + "| " + board[r][2])
if (r != 2):
print("---|---|---")
def victory_check():
if board[0][0] != ' ' and board[1][1] == board[0][0] == board[2][2]:
return 10
elif board[0][2] != ' ' and board[0][2] == board[1][1] == board[2][0]:
return 10
elif board[0][0] != ' ' and board[0][0] == board[1][0] == board[2][0]:
return 10
elif board[0][1] != ' ' and board[0][1] == board[1][1] == board[2][1]:
return 10
elif board[0][2] != ' ' and board[0][2] == board[1][2] == board[2][2]:
return 10
elif board[0][0] != ' ' and board[0][0] == board[0][1] == board[0][2]:
return 10
elif board[1][0] != ' ' and board[1][0] == board[1][1] == board[1][2]:
return 10
elif board[2][0] != ' ' and board[2][0] == board[2][1] == board[2][2]:
return 10
else:
return 20
while True:
print_board()
# μ¬μ©μλ‘λΆν° μ’ν μ
λ ₯λ°κΈ°
x = int(input("λ€μ μμ xμ’νλ₯Ό μ
λ ₯νμμ€: "))
y = int(input("λ€μ μμ yμ’νλ₯Ό μ
λ ₯νμμ€: "))
if board[x][y] != ' ':
print('μλͺ»λ μμΉμ
λλ€. ')
continue
else:
board[x][y] = 'X'
if victory_check() != 20:
print_board()
print('μ¬μ©μ§ μΉλ¦¬')
break
# μ»΄ν¨ν°κ° λκ°μ λ°©ν₯μ λ¨Όμ μ μ νλλ‘ μ½λ μΆκ°
done = False
for a in range(3):
if board[a][a] == ' ' and not done:
board[a][a] = 'O'
done = True
break
# λ§μ½ λκ°μ 곡κ°μ΄ λ€ μ°¨κ³ μμΌλ©΄ 첫 λ²μ§Έλ‘ λ°κ²¬νλ 곡λ μ ν
if done == False:
for i in range(3):
for j in range(3):
if board[i][j] == ' ' and not done:
board[i][j] = 'O'
done = True
break
if victory_check() != 20:
print_board()
print('μ»΄ν¨ν° μΉλ¦¬')
break
289p_Lab λμ λ¬Έμ )
import random
Computer_Board = []
User_Board = []
for computer in range(10):
Computer_Board.append(['.'] * 10)
for user in range(11):
User_Board.append(['.'] * 10)
def main():
global User_Board
global Computer_Board
print_Board(User_Board)
user_input = input('νκ³Ό μ΄μ μ
λ ₯νμμ€: ')
x, y = user_input.split(",")
x, y = int(x), int(y)
generate_Board(x, y)
open_tile(x, y)
while True:
print_Board(User_Board)
user_input = input('νκ³Ό μ΄μ μ
λ ₯νμμ€: ')
x, y = user_input.split(",")
x, y = int(x), int(y)
open_tile(x, y)
if check_victory() == 1:
exit()
def generate_Board(x, y):
global Computer_Board
global User_Board
for row in range(10):
for col in range(10):
if random.random() < 0.3:
Computer_Board[row][col] = '#'
for row in range(y - 2, y + 1):
for col in range(x - 2, x + 1):
Computer_Board[row][col] = '.'
for row in range(10):
for col in range(10):
Mine_Num = 0
if Computer_Board[row][col] == '.':
if 0 <= (row - 1) and (row - 1) <= 9 and 0 <= (col - 1) and (col - 1) <= 9:
if Computer_Board[row - 1][col - 1] == '#':
Mine_Num += 1
if 0 <= (row - 1) and (row - 1) <= 9 and 0 <= (col) and (col) <= 9:
if Computer_Board[row - 1][col] == '#':
Mine_Num += 1
if 0 <= (row - 1) and (row - 1) <= 9 and 0 <= (col + 1) and (col + 1) <= 9:
if Computer_Board[row - 1][col + 1] == '#':
Mine_Num += 1
if 0 <= (row) and (row) <= 9 and 0 <= (col - 1) and (col - 1) <= 9:
if Computer_Board[row][col - 1] == '#':
Mine_Num += 1
if 0 <= (row) and (row) <= 9 and 0 <= (col + 1) and (col + 1) <= 9:
if Computer_Board[row][col + 1] == '#':
Mine_Num += 1
if 0 <= (row + 1) and (row + 1) <= 9 and 0 <= (col - 1) and (col - 1) <= 9:
if Computer_Board[row + 1][col - 1] == '#':
Mine_Num += 1
if 0 <= (row + 1) and (row + 1) <= 9 and 0 <= (col) and (col) <= 9:
if Computer_Board[row + 1][col] == '#':
Mine_Num += 1
if 0 <= (row + 1) and (row + 1) <= 9 and 0 <= (col + 1) and (col + 1) <= 9:
if Computer_Board[row + 1][col + 1] == '#':
Mine_Num += 1
Computer_Board[row][col] = str(Mine_Num)
def print_Board(board):
for row in range(10):
for col in range(10):
print(board[row][col], end=' ')
print('')
def open_tile(x, y):
global User_Board
global Computer_Board
if Computer_Board[y - 1][x - 1] == '#':
User_Board[y - 1][x - 1] = Computer_Board[y - 1][x - 1]
print_Board(User_Board)
print('Game over!')
exit()
else:
Continue_Open(x, y)
def Continue_Open(x, y):
global User_Board
global Computer_Board
User_Board[y - 1][x - 1] = Computer_Board[y - 1][x - 1]
row = x - 1
col = y - 1
if Computer_Board[col][row] == '0':
if 0 <= (col - 1) and (col - 1) <= 9 and 0 <= (row - 1) and (row - 1) <= 9:
if Computer_Board[col - 1][row - 1] == '0' and User_Board[col - 1][row - 1] == '.':
Continue_Open(x - 1, y - 1)
if 0 <= (col - 1) and (col - 1) <= 9 and 0 <= (row) and (row) <= 9:
if Computer_Board[col - 1][row] == '0' and User_Board[col - 1][row] == '.':
Continue_Open(x, y - 1)
if 0 <= (col - 1) and (col - 1) <= 9 and 0 <= (row + 1) and (row + 1) <= 9:
if Computer_Board[col - 1][row + 1] == '0' and User_Board[col - 1][row + 1] == '.':
Continue_Open(x + 1, y - 1)
if 0 <= (col) and (col) <= 9 and 0 <= (row - 1) and (row - 1) <= 9:
if Computer_Board[col][row - 1] == '0' and User_Board[col][row - 1] == '.':
Continue_Open(x - 1, y)
if 0 <= (col) and (col) <= 9 and 0 <= (row + 1) and (row + 1) <= 9:
if Computer_Board[col][row + 1] == '0' and User_Board[col][row + 1] == '.':
Continue_Open(x + 1, y)
if 0 <= (col + 1) and (col + 1) <= 9 and 0 <= (row - 1) and (row - 1) <= 9:
if Computer_Board[col + 1][row - 1] == '0' and User_Board[col + 1][row - 1] == '.':
Continue_Open(x - 1, y + 1)
if 0 <= (col + 1) and (col + 1) <= 9 and 0 <= (row) and (row) <= 9:
if Computer_Board[col + 1][row] == '0' and User_Board[col + 1][row] == '.':
Continue_Open(x, y + 1)
if 0 <= (col + 1) and (col + 1) <= 9 and 0 <= (row + 1) and (row + 1) <= 9:
if Computer_Board[col + 1][row + 1] == '0' and User_Board[col + 1][row + 1] == '.':
Continue_Open(x + 1, y + 1)
if Computer_Board[col][row] == '0':
if 0 <= (col - 1) and (col - 1) <= 9 and 0 <= (row - 1) and (row - 1) <= 9:
User_Board[col - 1][row - 1] = Computer_Board[col - 1][row - 1]
if 0 <= (col - 1) and (col - 1) <= 9 and 0 <= (row) and (row) <= 9:
User_Board[col - 1][row] = Computer_Board[col - 1][row]
if 0 <= (col - 1) and (col - 1) <= 9 and 0 <= (row + 1) and (row + 1) <= 9:
User_Board[col - 1][row + 1] = Computer_Board[col - 1][row + 1]
if 0 <= (col) and (col) <= 9 and 0 <= (row - 1) and (row - 1) <= 9:
User_Board[col][row - 1] = Computer_Board[col][row - 1]
if 0 <= (col) and (col) <= 9 and 0 <= (row + 1) and (row + 1) <= 9:
User_Board[col][row + 1] = Computer_Board[col][row + 1]
if 0 <= (col + 1) and (col + 1) <= 9 and 0 <= (row - 1) and (row - 1) <= 9:
User_Board[col + 1][row - 1] = Computer_Board[col + 1][row - 1]
if 0 <= (col + 1) and (col + 1) <= 9 and 0 <= (row) and (row) <= 9:
User_Board[col + 1][row] = Computer_Board[col + 1][row]
if 0 <= (col + 1) and (col + 1) <= 9 and 0 <= (row + 1) and (row + 1) <= 9:
User_Board[col + 1][row + 1] = Computer_Board[col + 1][row + 1]
def check_victory():
global User_Board
exit_game = 0
for row in range(10):
for col in range(10):
if Computer_Board[row][col] != '#':
if User_Board[row][col] == Computer_Board[row][col]:
exit_game = 1
else:
exit_game = 0
return 0
if exit_game == 1:
print('Victory!')
return 1
main()