# TIC TAC TOE
import random
TICTACTOEBOARD = ['', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
USER = 'X'
COMPUTER = 'O'
def draw_tictactoe_board(board):
print("-------------------------------------------------")
print("l l l l")
print("l " + TICTACTOEBOARD[7] + " l " + TICTACTOEBOARD[8] + " l " + TICTACTOEBOARD[9] + " l")
print("l l l l")
print("-------------------------------------------------")
print("l l l l")
print("l " + TICTACTOEBOARD[4] + " l " + TICTACTOEBOARD[5] + " l " + TICTACTOEBOARD[6] + " l")
print("l l l l")
print("-------------------------------------------------")
print("l l l l")
print("l " + TICTACTOEBOARD[1] + " l " + TICTACTOEBOARD[2] + " l " + TICTACTOEBOARD[3] + " l")
print("l l l l")
print("-------------------------------------------------")
def tictactoe_input(whatInput, whoInput):
if type(whatInput) == list:
choice = random.choice(whatInput)
TICTACTOEBOARD[choice] = whoInput
else:
TICTACTOEBOARD[whatInput] = whoInput
def user_input(board):
copy = board_copy(board)
while True:
user_input = input()
if user_input not in '123456789':
print("Please in 10 number")
if len(user_input) != 1:
print("input a just one number")
continue
user_input = int(user_input)
if this_position_empty(copy, user_input):
tictactoe_input(user_input, USER)
break
else:
print("Please input empty")
def com_input(board):
copy = board_copy(board)
for i in range(3, 8, 2):
if this_position_empty(copy, i):
copy[i] = USER
if isWinner(copy, USER):
tictactoe_input(i, COMPUTER)
return
copy = board_copy(board)
for i in range(1, 10, 4):
if this_position_empty(copy, i):
copy[i] = USER
if isWinner(copy, USER):
tictactoe_input(i, COMPUTER)
return
copy = board_copy(board)
whatInput = random.choice([1, 3, 7, 9])
for i in range(4):
if this_position_empty(copy, whatInput):
tictactoe_input(whatInput, COMPUTER)
return
for i in range(4):
whatInput = random.choice([4, 8, 6, 2])
if this_position_empty(copy, whatInput):
tictactoe_input(whatInput, COMPUTER)
return
tictactoe_input(5, COMPUTER)
def isWinner(board, who):
if ((TICTACTOEBOARD[7] == who and TICTACTOEBOARD[5] == who and TICTACTOEBOARD[3] == who) or
(TICTACTOEBOARD[9] == who and TICTACTOEBOARD[5] == who and TICTACTOEBOARD[1] == who) or
(TICTACTOEBOARD[7] == who and TICTACTOEBOARD[8] == who and TICTACTOEBOARD[9] == who) or
(TICTACTOEBOARD[4] == who and TICTACTOEBOARD[5] == who and TICTACTOEBOARD[6] == who) or
(TICTACTOEBOARD[1] == who and TICTACTOEBOARD[2] == who and TICTACTOEBOARD[3] == who) or
(TICTACTOEBOARD[1] == who and TICTACTOEBOARD[4] == who and TICTACTOEBOARD[7] == who) or
(TICTACTOEBOARD[2] == who and TICTACTOEBOARD[5] == who and TICTACTOEBOARD[8] == who) or
(TICTACTOEBOARD[3] == who and TICTACTOEBOARD[6] == who and TICTACTOEBOARD[9] == who)):
return True
return False
def board_copy(board):
copy = []
for i in board:
copy.append(i)
return copy
def this_position_empty(board, user_input):
return board[user_input] == ' '
print("TIC TAC TOE!")
while True:
draw_tictactoe_board(TICTACTOEBOARD)
user_input(TICTACTOEBOARD)
if isWinner(TICTACTOEBOARD, USER):
print()
print("Player is Winner!")
draw_tictactoe_board(TICTACTOEBOARD)
break
com_input(TICTACTOEBOARD)
if isWinner(TICTACTOEBOARD, COMPUTER):
print()
print("Computer is Winner!")
draw_tictactoe_board(TICTACTOEBOARD)
break
프린이,, 나름 인공지능도 넣어보고 했습니다...
넵... 코드 복사 됩니다,,,
잘햇다
코드 뭘로올린거냐
그냥 vs code 복붙 하니까 저렇게 나오더라구요 - dc App
굿 - dc App
이거 그거내 makegameswithpytgon인가 그거
그거 참고해서 만들었심다 - dc App