코딩어려웡

코딩어려웡

python자바
코딩어려웡코딩어려웡· 3년

질문파이썬 틱택토 돌 중복 질문입니다

밑에 코드를 실행하면 컴퓨터가 먼저 돌을 두고 제가 두는데 컴퓨터가 돌을 둔자리에 두면 제 돌로 덮어지면서 중복이 가능합니다 코드 내에 get_user_move( ) 함수만을 수정해서 사용자가 돌이 있는 곳에 돌을 두려고 하면 비어 있는 곳에 돌을 놓을 때 까지 계속해서 입력을 받도록 어떻게 할수 있을까요 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     if is_free(board, loc):             return; def get_computer_move(board):     for i in range(1, 10):         if is_free(board, i):             board[i] = 'o'             if check_for_win(board, 'o'):                 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 = 'o'  #72 문제 1.  while playing:     draw_board(board)     if turn == 'x':         loc = get_user_move(board)         board[loc] = 'x'     else :         loc = get_computer_move(board)         board[loc] = 'o'     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 = 'o'             else : turn = 'x'
932
1
0