# Tic-Tac-Toe 코드 짜기
# input, 2 numbers
이것이 보드 좌표
# '''
#
# 0,0 0,1 0,2
#
# 1,0 1,1 1,2
#
# 2,0 2,1 2,2
#
# '''
class ttt:
def __init__(self):
print('Tic-Tac-Toe Start')
self.map = []
for i in range(3):
v = [0] * 3
self.map.append(v)
self.drawMap()
self.controller()
def drawMap(self):
for i in range(3):
for j in range(3):
if self.map[i][j] == 0:
print('_ ', end='')
elif self.map[i][j] == 1:
print('O ', end='')
elif self.map[i][j] == 2:
print('X ', end='')
print()
a = 0
def controller(self):
for i in range(9999999999999999999):
n, m = map(int, input().split())
if (i % 2 == 0):
a = 1
elif (i % 2 == 1):
a = 2
if (a == 1):
print('O turn')
self.map[n][m] = 1
self.drawMap()
elif (a == 2):
print('X turn')
self.map[n][m] = 2
self.drawMap()
if self.winlose():
print('Game End')
break
def winlose(self):
if self.map[0][0] != 0 and self.map[0][0] == self.map[1][1] and self.map[1][1] == self.map[2][2]:
print('O is the winner' if self.map[0][0] == 1 else 'X is the winner')
return True
elif (self.map[0][0] != 0 and self.map[0][0] == self.map[0][1] and self.map[0][1] == self.map[0][2]):
print('O is the winner' if self.map[0][1] == 1 else 'X is the winner')
return True
elif (self.map[1][0] != 0 and self.map[1][0] == self.map[1][1] and self.map[1][1] == self.map[1][2]):
print('O is the winner' if self.map[1][0] == 1 else 'X is the winner')
return True
elif (self.map[2][0] != 0 and self.map[2][0] == self.map[2][1] and self.map[2][1] == self.map[2][2]):
print('O is the winner' if self.map[2][1] == 1 else 'X is the winner')
return True
elif (self.map[0][2] != 0 and self.map[0][2] == self.map[1][1] and self.map[1][1] == self.map[2][0]):
print('O is the winner' if self.map[0][2] == 1 else 'X is the winner')
return True
elif (self.map[0][0] != 0 and self.map[0][0] == self.map[1][0] and self.map[1][0] == self.map[2][0]):
print('O is the winner' if self.map[2][0] == 1 else 'X is the winner')
return True
elif (self.map[0][1] != 0 and self.map[0][1] == self.map[1][1] and self.map[1][1] == self.map[2][1]):
print('O is the winner' if self.map[1][1] == 1 else 'X is the winner')
return True
elif (self.map[0][2] != 0 and self.map[0][2] == self.map[1][2] and self.map[1][2] == self.map[2][2]):
print('O is the winner' if self.map[1][2] == 1 else 'X is the winner')
return True
elif (self.map[0][2] != 0 and self.map[0][2] == self.map[1][1] and self.map[1][1] == self.map[2][0]):
print ('O is the winner' if self.map[0][2] ==1 else 'X is the winner')
return True
elif (self.map[0][0] and self.map[2][0] and self.map[1][2] and self.map[0][1] != 0):
print ('Draw')
return True
else:
return False
while True:
x = ttt()
print('Retry?Y or N')
n = input() // 이걸 받고 들어 간 문자가 n이면 끝내고 y면 처음부터 다시 실행하게 만드는 것.
if n == 'N':
break
다른 버전 (조금 더 간단한 클래스를 이용하지 않은 코드로 만든 틱택토)
# data = []
#
# for i in range(3):
# v = [0] * 3
# data.append(v)
#
# print(data)
#
# turn = False
#
# while True:
# x, y = map(int, input().split())
# x -= 1
# y -= 1
#
# if turn:
# data[x][y] = 1
# else:
# data[x][y] = 2
#
# if data[0][0] == data[0][1] and data[0][1] == data[0][2]:
# print('winner', 'first player' if data[0][0]==1 else 'second player')
여기 조건문 8개 그리고 위에 심볼을 x o 로 바꾸면 위에 있는 틱택토 와 같은 것이 실행이 된다. 이게 조금 더 간단한 방법.
#
#
# for i in range(3):
# for j in range(3):
# print(data[i][j], end=' ')
# print()
# turn = not(turn)
# import os
# #
# # os.makedirs('C:/jelly/')
# print(os.listdir('C:/'))
# PyQt5 Tutorial 들어가서 코드 배워오기
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('My First TTT')
self.move(800, 300)
self.resize(450, 450)
self.turn = True
# QPushButton 을 넣었을때 빨간줄이 뜨면 우클릭 후 IMPORT 누르면 위에 추가 됨.
self.btn1 = QPushButton('', self)
self.btn1.move(0, 0)
self.btn1.clicked.connect(self.onClick1)
self.btn1.resize(150, 150)
self.btn2 = QPushButton('', self)
self.btn2.move(150, 0)
self.btn2.clicked.connect(self.onClick2)
self.btn2.resize(150, 150)
self.btn3 = QPushButton('', self)
self.btn3.move(300, 0)
self.btn3.clicked.connect(self.onClick3)
self.btn3.resize(150, 150)
self.btn4 = QPushButton('', self)
self.btn4.move(0, 150)
self.btn4.clicked.connect(self.onClick4)
self.btn4.resize(150, 150)
self.btn5 = QPushButton('', self)
self.btn5.move(150, 150)
self.btn5.clicked.connect(self.onClick5)
self.btn5.resize(150, 150)
self.btn6 = QPushButton('', self)
self.btn6.move(300, 150)
self.btn6.clicked.connect(self.onClick6)
self.btn6.resize(150, 150)
self.btn7 = QPushButton('', self)
self.btn7.move(0, 300)
self.btn7.clicked.connect(self.onClick7)
self.btn7.resize(150, 150)
self.btn8 = QPushButton('', self)
self.btn8.move(150, 300)
self.btn8.clicked.connect(self.onClick8)
self.btn8.resize(150, 150)
self.btn9 = QPushButton('', self)
self.btn9.move(300, 300)
self.btn9.clicked.connect(self.onClick9)
self.btn9.resize(150, 150)
# SELF.SHOW 전에 코드를 짜서 넣어야지 보임.
self.show()
def evaluation(self):
if self.btn1.text() != '' and self.btn1.text() == self.btn2.text() and self.btn2.text() == self.btn3.text():
self.closeEvent()
# elif self.btn4.text() != '' and self.btn4.text() == self.btn5.text() and self.btn5.text() == self.btn6.text():
#
# elif self.btn7.text() != '' and self.btn7.text() == self.btn8.text() and self.btn8.text() == self.btn9.text():
#
# elif self.btn1.text() != '' and self.btn1.text() == self.btn4.text() and self.btn4.text() == self.btn7.text():
#
# elif self.btn2.text() != '' and self.btn2.text() == self.btn5.text() and self.btn5.text() == self.btn8.text():
#
# elif self.btn3.text() != '' and self.btn3.text() == self.btn6.text() and self.btn6.text() == self.btn9.text():
#
# elif self.btn1.text() != '' and self.btn1.text() == self.btn5.text() and self.btn5.text() == self.btn9.text():
#
# elif self.btn3.text() != '' and self.btn3.text() == self.btn5.text() and self.btn5.text() == self.btn7.text():
def onClick1(self):
if self.turn:
self.btn1.setText('X')
else:
self.btn1.setText('O')
self.turn = not(self.turn)
self.evaluation()
#이 뜻은 거짓이 아니면 진실, 진실이 아니면 거짓이라는 뜻이다.
def onClick2(self):
if self.turn:
self.btn2.setText('X')
else:
self.btn2.setText('O')
self.turn = not (self.turn)
self.evaluation()
def onClick3(self):
if self.turn:
self.btn3.setText('X')
else:
self.btn3.setText('O')
self.turn = not (self.turn)
self.evaluation()
def onClick4(self):
if self.turn:
self.btn4.setText('X')
else:
self.btn4.setText('O')
self.turn = not (self.turn)
self.evaluation()
def onClick5(self):
if self.turn:
self.btn5.setText('X')
else:
self.btn5.setText('O')
self.turn = not (self.turn)
self.evaluation()
def onClick6(self):
if self.turn:
self.btn6.setText('X')
else:
self.btn6.setText('O')
self.turn = not (self.turn)
self.evaluation()
def onClick7(self):
if self.turn:
self.btn7.setText('X')
else:
self.btn7.setText('O')
self.turn = not (self.turn)
self.evaluation()
def onClick8(self):
if self.turn:
self.btn8.setText('X')
else:
self.btn8.setText('O')
self.turn = not (self.turn)
self.evaluation()
def onClick9(self):
if self.turn:
self.btn9.setText('X')
else:
self.btn9.setText('O')
self.turn = not (self.turn)
self.evaluation()
def closeEvent(self, event):
reply = QMessageBox.question (self,'Message','Retry?',QMessageBox.Yes | QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
https://wikidocs.net/21935
메세지창 띄우는거 연구해오기
top of page
실제 작동 상태를 확인하려면 라이브 사이트로 이동하세요.
20230516
20230516
댓글 0개
좋아요
댓글(0)
bottom of page