편집 기록

편집 기록
  • 프로필 초보자님의 편집
    날짜2022.11.21

    파이썬 코드 질문입니다


    import random
    
    def draw_board(board):
        print('------------')
        print(' '+ board[1] + ' | ' + board[2] + ' | ' + board[3])
        print('------------')
        print(' '+ board[4] + ' | ' + board[5] + ' | ' + board[6])
        print('------------')
        print(' '+ board[7] + ' | ' + board[8] + ' | ' + board[9])
        print('------------')
    
    
    def check_for_win(b, p) :
        if ((b[1] == p and b[2] == p and b[3] == p) or
            (b[4] == p and b[5] == p and b[6] == p) or
            (b[7] == p and b[8] == p and b[9] == p) or
            (b[1] == p and b[4] == p and b[7] == p) or
            (b[2] == p and b[5] == p and b[8] == p) or
            (b[3] == p and b[6] == p and b[9] == p) or
            (b[3] == p and b[5] == p and b[7] == p) or
            (b[1] == p and b[5] == p and b[9] == p)) :
                return True
        else:
            return False
    
    
    def is_free(board, loc):
        return board[loc] == ' '
    
    def get_user_move(board):
        loc = int(input('1부터 9 사이의 정수: '))
        return loc
    
    def get_computer_move(board):
        for i in range(1,10):
            if is_free(board, i):
                board[i] = '0'
                if check_for_win(board, '0'):
                    board[i] = ' '
                    return i
                board[i] = ' '
    
        for i in range(1,10):
            if is_free(board, i):
                board[i] = 'x'
                if check_for_win(board, 'x'):
                    board[i] = ' '
                    return i
                board[i] = ' '
    
        for i in [ 1, 3, 7, 9]:
            if is_free(board, i):
                return i
    
        if is_free(board, 5):
            return 5
    
        for i in [ 2, 4, 6, 8]:
            if is_free(board, i):
                return i
    
    def check_for_tie(board):
             for i in range(1, 10):
            if is_free(board, i):
                return False
            return True
    
    print('tic tac toe 게임에 오신 것을 환영합니다. ')
    board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' ]
    playing = True
    turn = '0'
    while playing:
        if turn == 'x':
            draw_board(board)
            loc = get_user_move(board)
            board[loc] = 'x'
        else :
            loc = get_computer_move(board)
            board[loc] = '0'
    
        if check_for_win(board, turn):
            draw_board(board)
            print(turn + '승리')
            playing = False
        else:
            if check_for_tie(board):
                draw_board(board)
                print('무승부')
                playing = False
            else:
                if turn == 'x': turn = '0'
                else : turn = 'x'
    

    이 코드는 실행하면 가로 3칸 세로 3칸으로 돌을 컴퓨터와 놓는 게임입니다

    근데 제가 아무리 봐도 틀린점이 없는거 같은데 이상하게 컴퓨터가 처음에 시작할 때 돌을 하나 두고 무승부로 바로 끝나버립니다. 어디가 잘못된지를 못찾겠어요 도와주세요

    그리고 질문 3가지가 있습니다

    1. 처음에 시작할 때 컴퓨터가 먼저 두는 게 아니라 사람이 먼저 두게 하고 싶은데 어떻게 해야 할까요?

    2. 컴퓨터 대신에 사람 대 사람으로 게임을 하려면 어떻게 고쳐야 할까요?

    3. 마지막으로 이 코드를 실행했을 때 돌을 놓은 곳에 계속해서 덮어서 돌을 놓을 수 있는 버그가 있는데 get_user_move() 함수 만을 수정해서 이미 돌이 있는 곳에 두려고 해도 비어있는 곳에 돌을 놓을 때까지 입력을 받도록 어떻게 바로 잡나요?

    사실 이게 대학교 과젠데 아무리 고민해도 모르겠어서 가입해서 여쭤봅니다. 코드는 최대한 유지하도록 해서 고칠 수 있도록 도와주세요..

  • 프로필 자하님의 편집
    날짜2022.11.19

    파이썬 코드 질문입니다


    import random

    def draw_board(board):

    print('------------')
    print(' '+ board[1] + ' | ' + board[2] + ' | ' + board[3])
    print('------------')
    print(' '+ board[4] + ' | ' + board[5] + ' | ' + board[6])
    print('------------')
    print(' '+ board[7] + ' | ' + board[8] + ' | ' + board[9])
    print('------------')
    

    def check_for_win(b, p) :

    if ((b[1] == p and b[2] == p and b[3] == p) or
        (b[4] == p and b[5] == p and b[6] == p) or
        (b[7] == p and b[8] == p and b[9] == p) or
        (b[1] == p and b[4] == p and b[7] == p) or
        (b[2] == p and b[5] == p and b[8] == p) or
        (b[3] == p and b[6] == p and b[9] == p) or
        (b[3] == p and b[5] == p and b[7] == p) or
        (b[1] == p and b[5] == p and b[9] == p)) :
            return True
    else:
        return False
    

    def is_free(board, loc):

    return board[loc] == ' '
    

    def get_user_move(board):

    loc = int(input('1부터 9 사이의 정수: '))
    return loc
    

    def get_computer_move(board):

    for i in range(1,10):
        if is_free(board, i):
            board[i] = '0'
            if check_for_win(board, '0'):
                board[i] = ' '
                return i
            board[i] = ' '
    
    
    for i in range(1,10):
        if is_free(board, i):
            board[i] = 'x'
            if check_for_win(board, 'x'):
                board[i] = ' '
                return i
            board[i] = ' '
    
    for i in [ 1, 3, 7, 9]:
        if is_free(board, i):
            return i
    
    if is_free(board, 5):
        return 5
    
    for i in [ 2, 4, 6, 8]:
        if is_free(board, i):
            return i
    

    def check_for_tie(board):

         for i in range(1, 10):
        if is_free(board, i):
            return False
        return True
    

    print('tic tac toe 게임에 오신 것을 환영합니다. ')

    board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' ]

    playing = True

    turn = '0'

    while playing:

    if turn == 'x':
        draw_board(board)
        loc = get_user_move(board)
        board[loc] = 'x'
    else :
        loc = get_computer_move(board)
        board[loc] = '0'
    
    if check_for_win(board, turn):
        draw_board(board)
        print(turn + '승리')
        playing = False
    else:
        if check_for_tie(board):
            draw_board(board)
            print('무승부')
            playing = False
        else:
            if turn == 'x': turn = '0'
            else : turn = 'x'
    

    이 코드는 실행하면 가로 3칸 세로 3칸으로 돌을 컴퓨터와 놓는 게임입니다

    근데 제가 아무리 봐도 틀린점이 없는거 같은데 이상하게 컴퓨터가 처음에 시작할 때 돌을 하나 두고 무승부로 바로 끝나버립니다. 어디가 잘못된지를 못찾겠어요 도와주세요