본문 바로가기
Python/☞

[36]-A. TIC TAC TOE

by TR. 2020. 7. 23.
map = [0,0,0,0,0,0,0,0,0]

# RUN
run = True
while run :
     # 초기화
     print("New Game!")
     for i in range(9):
          map[i] = i+1

     p1 = 0
     p2 = 0

     win = 0
     turn = 0

     while True :
          print("--TIC TAC TOE--")
          for i in range(9):
               if map[i] == 11:
                    print("[ ■ ]", end="")
               elif map[i] == 22:
                    print("[ □ ]", end="")
               else:
                    print("[ %d ]" % map[i], end="")
               if i % 3 == 2:
                    print()
          print("---------------")

          # result
          if win == 11:
               print("p1의 승리!\n")
               break               # 가장 가까운 반복문의 종료
          elif win == 22:
               print("p2의 승리!\n")
               break
          else:
               pass

          # input
          print("번호를 입력하세요.")
          if turn % 2 == 0 :
               p1 = int(input("p1 : "))
               if map[p1-1] < 10:
                    map[p1-1] = 11
               else:
                    continue
          else :
               p2 = int(input("p2 : "))
               if map[p2-1] < 10:
                    map[p2-1] = 22
               else:
                    continue

          # check
          # 가로
          # 012 345 678
          for i in range(0,7,3):
               if map[i] == map[i+1] == map[i+2]:
                    win = map[i]
                    
          # 세로
          # 036 147 258
          for i in range(3):
               if map[i] == map[i+3] == map[i+6]:
                    win = map[i]
          
          # 대각선 왼오
          # 048
          if map[0] == map[4] == map[8]:
               win = map[0]
          
          # 대각선 오왼
          # 246
          if map[2] == map[4] == map[6]:
               win = map[2]
          
          turn += 1

'Python > ' 카테고리의 다른 글

[37]-A. 정렬  (0) 2020.07.23
[37] 정렬  (0) 2020.07.23
[36] TIC TAC TOE  (0) 2020.07.23
[35] 연습문제 : 리스트  (0) 2020.07.23
[34] 튜플  (0) 2020.07.23

댓글