λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
βœ’οΈ 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