Facebook
From MEHOW, 3 Years ago, written in Python.
This paste is a reply to TTT from MY - view diff
Embed
Download Paste or View Raw
Hits: 147
  1.  
  2. def init_board():
  3.     board = [['.', '.', '.'],
  4.                 ['.', '.', '.'],
  5.                 ['.', '.', '.']]
  6.     print (board)            
  7.     return board
  8.  
  9.    
  10.    
  11. def get_move():
  12.     board = init_board()
  13.     round = 5 #wstępnie tutaj umieściłam round
  14.     expected_result = [[0, 0], [0, 1], [0, 2], [
  15.         1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
  16.     move = input("Enter coordinates of choosen blank field(e.g. A1): ")
  17.     round = round-1 #tutaj round - 1, ale nie wiem czy to będzie działać.
  18.     move = move.upper()
  19.     coordinates = []
  20.     x = []
  21.     y = []
  22.     result = []
  23.    
  24.     def spliting():
  25.         for letter in move:
  26.             coordinates.append(letter)
  27.    
  28.     def checking():
  29.         for it in coordinates:
  30.             if it.isalpha() == True:
  31.                 if it == "A" or "B" or "C":
  32.                     x.append(it)
  33.             elif it.isnumeric() == True:
  34.                 if it == '1' or '2' or '3':
  35.                     y.append(it)
  36.    
  37.     def convert():
  38.         coordinates = []
  39.         for item in x:
  40.             if item == "A":
  41.                 result.append(0)
  42.             elif item == "B":
  43.                 result.append(1)
  44.             elif item == "C":
  45.                 result.append(2)
  46.             elif item != 'A' or 'B' or 'C':
  47.                 result.append("err")
  48.         for item in y:
  49.             if item == "1":
  50.                 result.append(0)
  51.             elif item == "2":
  52.                 result.append(1)
  53.             elif item == "3":
  54.                 result.append(2)
  55.             elif item != '1' or '2' or '3':
  56.                 result.append("err")
  57.         if x == [] or y == []:
  58.             result.append("err")
  59.    
  60.     def error():
  61.         print("\n")
  62.         coordinates.clear()
  63.         x.clear()
  64.         y.clear()
  65.         result.clear()
  66.         move = input("Enter coordinates of choosen blank field(e.g. A1): ")
  67.         move = move.upper()
  68.         for letter in move:
  69.             coordinates.append(letter)
  70.         checking()
  71.         convert()
  72.         return result
  73.    
  74.     spliting()
  75.     checking()
  76.     convert()
  77.     while True:
  78.         while result in expected_result != True:
  79.             if board[result[0]][result[1]] != '.':
  80.                 print("\n")
  81.                 print("This place is taken. Choose another empty field :) ")
  82.                 print("\n")
  83.                 error()
  84.             elif board[result[0]][result[1]] == '.':
  85.                 return result
  86.         while result[0] == "err" or result[1] == "err":
  87.             print("\n")
  88.             print("Invalid input. Please enter your choose again :) ")
  89.             print("\n")
  90.             error()
  91.         else:
  92.             while result in expected_result != True:
  93.                 if board[result[0]][result[1]] != '.':
  94.                     print("\n")
  95.                     print("This place is taken. Choose another empty field :) ")
  96.                     print("\n")
  97.                     error()
  98.                 elif board[result[0]][result[1]] == '.':
  99.                     return result
  100.    
  101.    
  102. # init_board()
  103. # print(get_move())
  104.  
  105. def mark (round,board) #w odpowiednim miejscu trzeba wstawić round = 5 i round = round - 1, żeby po każdym wyborze miejsca kółka lub krzyżyka odpadała jedna runda
  106.     if round == 5:
  107.         board[result[0]][result[1]] = "0"
  108.  
  109.     if round == 4:
  110.         board[result[0]][result[1]] = "X"
  111.  
  112.     if round == 3:
  113.         board[result[0]][result[1]] = "0"
  114.  
  115.     if round == 2:
  116.     board[result[0]][result[1]] = "X"
  117.  
  118.     if round == 1:
  119.     board[result[0]][result[1]] = "0"
  120.  
  121.     if round == 0:
  122.         print ("KONIEC")
  123.  
  124.    
  125. def has_won():
  126.     board = init_board()
  127.     win = ((board[0][0],board[0][1],board[0][2]),
  128.         (board[1][0],board[1][1],board[1][2]),
  129.         (board[2][0],board[2][1],board[2][2]),
  130.         (board[0][0],board[1][0],board[2][0]),
  131.         (board[0][1],board[1][1],board[2][1]),
  132.         (board[0][2],board[1][2],board[2][2]),
  133.         (board[0][0],board[1][1],board[2][2]),
  134.         (board[2][0],board[1][1],board[0][2]))
  135.     result = False
  136.     for line in win:
  137.         if line[0] == line[1] == line[2] != ".":
  138.             result = True
  139.     return result
  140.  
  141.  
  142. # TEST HAS_WON
  143. # if has_won() == True:
  144. #     print("WYGRANA")
  145.  
  146.  
  147. def is_full():
  148.     board = init_board()
  149.     result = True
  150.     if board[0][0] == '.':
  151.         result = False
  152.     elif board[0][1] == '.':
  153.         result = False
  154.     elif board[0][2] == '.':
  155.         result = False
  156.     elif board[1][1] == '.':
  157.         result = False
  158.     elif board[2][1] == '.':
  159.         result = False
  160.     elif board[1][0] == '.':
  161.         result = False
  162.     elif board[1][2] == '.':
  163.         result = False
  164.     elif board[2][0] == '.':
  165.         result = False
  166.     elif board[2][2] == '.':
  167.         result = False
  168.     return result
  169.  
  170. # TEST IS FULL  
  171. # board = init_board()
  172. # if is_full() == True:
  173. #     print("pełna")
  174.  
  175. get_move()