이번엔 제가 예전에 만들어본 틱택토 게임을 올리는데요. 이건 제가 비교적 초보일 때 만든 작품이라 코드도 쓸데없이 길고 효율적이지도 않죠 ㅋㅋ.
버튼도 눌렸을때 함수를 각각 만들어 줘서 만들 때 코드를 쓴 것보다 복붙 한 게 더 많았죠. 만드는 동안에는 코드가 그리 길어 보이지 않았는데 이제 보니 엄청 길기만 하고 비효율적이네요 ㅠㅠ.
from tkinter import *
root = Tk()
root.title("TicTacToe")
root.geometry('240x170')
O_or_X = True
x = 0
y = 0
coordinate1 = 0
coordinate2 = 0
coordinate3 = 0
coordinate4 = 0
coordinate5 = 0
coordinate6 = 0
coordinate7 = 0
coordinate8 = 0
coordinate9 = 0
def check1win():
global x
if coordinate1 * coordinate2 * coordinate3 == 1:
print("Player 1 wins!")
if coordinate4 * coordinate5 * coordinate6 == 1:
print("Player 1 wins!")
if coordinate7 * coordinate8 * coordinate9 == 1:
print("Player 1 wins!")
if coordinate1 * coordinate4 * coordinate7 == 1:
print("Player 1 wins!")
if coordinate2 * coordinate5 * coordinate8 == 1:
print("Player 1 wins!")
if coordinate3 * coordinate6 * coordinate9 == 1:
print("Player 1 wins!")
if coordinate1 * coordinate5 * coordinate9 == 1:
print("Player 1 wins!")
if coordinate3 * coordinate5 * coordinate7 == 1:
print("Player 1 wins!")
x += 1
if x == 9:
print("Draw!")
def check2win():
global y
if coordinate1 * coordinate2 * coordinate3 == 8:
print("Player 2 wins!")
if coordinate4 * coordinate5 * coordinate6 == 8:
print("Player 2 wins!")
if coordinate7 * coordinate8 * coordinate9 == 8:
print("Player 2 wins!")
if coordinate1 * coordinate4 * coordinate7 == 8:
print("Player 2 wins!")
if coordinate2 * coordinate5 * coordinate8 == 8:
print("Player 2 wins!")
if coordinate3 * coordinate6 * coordinate9 == 8:
print("Player 2 wins!")
if coordinate1 * coordinate5 * coordinate9 == 8:
print("Player 2 wins!")
if coordinate3 * coordinate5 * coordinate7 == 8:
print("Player 2 wins!")
y += 1
if y == 9:
print("Draw!")
def whenClicked1():
global O_or_X
global Btn1
global coordinate1
Btn1.grid_forget()
if O_or_X == True:
Btn1 = Button(root, height=2, width=10, text="O")
Btn1.grid(row=0, column=0)
coordinate1 = 1
print(coordinate1)
if O_or_X == False:
Btn1 = Button(root, height=2, width=10, text="X")
Btn1.grid(row=0, column=0)
coordinate1 = 2
print(coordinate1)
check2win()
check1win()
if O_or_X == True:
O_or_X = False
else:
O_or_X = True
def whenClicked2():
global O_or_X
global Btn2
global coordinate2
Btn2.grid_forget()
if O_or_X == True:
Btn2 = Button(root, height=2, width=10, text="O")
Btn2.grid(row=0, column=1)
coordinate2 = 1
print(coordinate2)
if O_or_X == False:
Btn2 = Button(root, height=2, width=10, text="X")
Btn2.grid(row=0, column=1)
coordinate2 = 2
print(coordinate2)
check2win()
check1win()
if O_or_X == True:
O_or_X = False
else:
O_or_X = True
def whenClicked3():
global O_or_X
global Btn3
global coordinate3
Btn3.grid_forget()
if O_or_X == True:
Btn3 = Button(root, height=2, width=10, text="O")
Btn3.grid(row=0, column=2)
coordinate3 = 1
print(coordinate3)
if O_or_X == False:
Btn3 = Button(root, height=2, width=10, text="X")
Btn3.grid(row=0, column=2)
coordinate3 = 2
print(coordinate3)
check2win()
check1win()
if O_or_X == True:
O_or_X = False
else:
O_or_X = True
def whenClicked4():
global O_or_X
global Btn4
global coordinate4
Btn4.grid_forget()
if O_or_X == True:
Btn4 = Button(root, height=2, width=10, text="O")
Btn4.grid(row=1, column=0)
coordinate4 = 1
print(coordinate4)
if O_or_X == False:
Btn4 = Button(root, height=2, width=10, text="X")
Btn4.grid(row=1, column=0)
coordinate4 = 2
print(coordinate4)
check2win()
check1win()
if O_or_X == True:
O_or_X = False
else:
O_or_X = True
def whenClicked5():
global O_or_X
global Btn5
global coordinate5
Btn5.grid_forget()
if O_or_X == True:
Btn5 = Button(root, height=2, width=10, text="O")
Btn5.grid(row=1, column=1)
coordinate5 = 1
print(coordinate5)
if O_or_X == False:
Btn5 = Button(root, height=2, width=10, text="X")
Btn5.grid(row=1, column=1)
coordinate5 = 2
print(coordinate5)
check2win()
check1win()
if O_or_X == True:
O_or_X = False
else:
O_or_X = True
def whenClicked6():
global O_or_X
global Btn6
global coordinate6
Btn6.grid_forget()
if O_or_X == True:
Btn6 = Button(root, height=2, width=10, text="O")
Btn6.grid(row=1, column=2)
coordinate6 = 1
print(coordinate6)
if O_or_X == False:
Btn6 = Button(root, height=2, width=10, text="X")
Btn6.grid(row=1, column=2)
coordinate6 = 2
print(coordinate6)
check2win()
check1win()
if O_or_X == True:
O_or_X = False
else:
O_or_X = True
def whenClicked7():
global O_or_X
global Btn7
global coordinate7
Btn7.grid_forget()
if O_or_X == True:
Btn7 = Button(root, height=2, width=10, text="O")
Btn7.grid(row=2, column=0)
coordinate7 = 1
print(coordinate7)
if O_or_X == False:
Btn7 = Button(root, height=2, width=10, text="X")
Btn7.grid(row=2, column=0)
coordinate7 = 2
print(coordinate7)
check2win()
check1win()
if O_or_X == True:
O_or_X = False
else:
O_or_X = True
def whenClicked8():
global O_or_X
global Btn8
global coordinate8
Btn8.grid_forget()
if O_or_X == True:
Btn8 = Button(root, height=2, width=10, text="O")
Btn8.grid(row=2, column=1)
coordinate8 = 1
print(coordinate8)
if O_or_X == False:
Btn8 = Button(root, height=2, width=10, text="X")
Btn8.grid(row=2, column=1)
coordinate8 = 2
print(coordinate8)
check2win()
check1win()
if O_or_X == True:
O_or_X = False
else:
O_or_X = True
def whenClicked9():
global O_or_X
global Btn9
global coordinate9
Btn9.grid_forget()
if O_or_X == True:
Btn9 = Button(root, height=2, width=10, text="O")
Btn9.grid(row=2, column=2)
coordinate9 = 1
print(coordinate9)
if O_or_X == False:
Btn9 = Button(root, height=2, width=10, text="X")
Btn9.grid(row=2, column=2)
coordinate9 = 2
print(coordinate9)
check2win()
check1win()
if O_or_X == True:
O_or_X = False
else:
O_or_X = True
# Buttons
Btn1 = Button(root, height=2, width=10, text="click", command=whenClicked1)
Btn1.grid(row=0, column=0)
Btn2 = Button(root, height=2, width=10, text="click", command=whenClicked2)
Btn2.grid(row=0, column=1)
Btn3 = Button(root, height=2, width=10, text="click", command=whenClicked3)
Btn3.grid(row=0, column=2)
Btn4 = Button(root, height=2, width=10, text="click", command=whenClicked4)
Btn4.grid(row=1, column=0)
Btn5 = Button(root, height=2, width=10, text="click", command=whenClicked5)
Btn5.grid(row=1, column=1)
Btn6 = Button(root, height=2, width=10, text="click", command=whenClicked6)
Btn6.grid(row=1, column=2)
Btn7 = Button(root, height=2, width=10, text="click", command=whenClicked7)
Btn7.grid(row=2, column=0)
Btn8 = Button(root, height=2, width=10, text="click", command=whenClicked8)
Btn8.grid(row=2, column=1)
Btn9 = Button(root, height=2, width=10, text="click", command=whenClicked9)
Btn9.grid(row=2, column=2)
root.mainloop()
진짜 무진장하게 길죠? 이건 제가 봐도 좀 많이 못 만들긴 했네요. 만들때는 tkinter라는 GUI라이브러리를 사용해서 만들었답니다.
이 코드를 실행해보면 이렇게 조그만한 창이 생기면서 9개의 버튼들이 생겨납니다.
버튼들을 클릭하면 바뀌고요
하지만 게임이 끝나거나 비겼을 땐 터미널에만 출력하게 되어있어서 아직 100% 완성이라고 보기는 어렵죠. 제가 코드를 올려놓긴 했는데 이걸 쓰는 건 비추해요. 저보다 잘 만드신 분들이 넘쳐나시니까 다른 분들 걸 참고해서 만들어 보세요.
다음에 시간있으면 새로 한 번 더 만들어 보겠습니다.
'코딩' 카테고리의 다른 글
간단한 계산기 만들기 (0) | 2021.05.16 |
---|---|
파이썬으로 행맨 만들기 3 - 이겼는지 졌는지 확인하기 (0) | 2021.04.20 |
파이썬으로 행맨 만들기 2 - 알파벳 확인하고 출력하기 (0) | 2021.04.17 |
파이썬으로 행맨 만들기 1 - 입력 받고 저장하기 (0) | 2021.04.16 |
프로그래머스 1단계 문제 후기 (0) | 2021.04.16 |