๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ Python Programming/์–ด์„œ์™€ ํŒŒ์ด์ฌ์€ ์ฒ˜์Œ์ด์ง€!_2019 Ver

[์–ด์„œ์™€ ํŒŒ์ด์ฌ์€ ์ฒ˜์Œ์ด์ง€!_2019 Ver] 6์žฅ Lab ๋„์ „๋ฌธ์ œ ํ’€์ด

by A Lim Han 2023. 2. 20.

# 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()

์ฐธ๊ณ : https://yunseong.tistory.com/entry/Python%EC%9C%BC%EB%A1%9C-%EC%A7%80%EB%A2%B0%EC%B0%BE%EA%B8%B0-%EB%A7%8C%EB%93%A4%EA%B8%B0