Facebook
From MY, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 133
  1.  
  2.  
  3. def init_board():
  4.     board = [['.', '.', '.'],
  5.                 ['.', '.', '.'],
  6.                 ['.', '.', '.']]
  7.     print (board)            
  8.     return board
  9.  
  10.    
  11.    
  12. def get_move():
  13.     board = init_board()
  14.     round = 5 #wstępnie tutaj umieściłam round
  15.     expected_result = [[0, 0], [0, 1], [0, 2], [
  16.         1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
  17.     move = input("Enter coordinates of choosen blank field(e.g. A1): ")
  18.     round = round-1 #tutaj round - 1, ale nie wiem czy to będzie działać.
  19.     move = move.upper()
  20.     coordinates = []
  21.     x = []
  22.     y = []
  23.     result = []
  24.    
  25.     def spliting():
  26.         for letter in move:
  27.             coordinates.append(letter)
  28.    
  29.     def checking():
  30.         for it in coordinates:
  31.             if it.isalpha() == True:
  32.                 if it == "A" or "B" or "C":
  33.                     x.append(it)
  34.             elif it.isnumeric() == True:
  35.                 if it == '1' or '2' or '3':
  36.                     y.append(it)
  37.    
  38.     def convert():
  39.         coordinates = []
  40.         for item in x:
  41.             if item == "A":
  42.                 result.append(0)
  43.             elif item == "B":
  44.                 result.append(1)
  45.             elif item == "C":
  46.                 result.append(2)
  47.             elif item != 'A' or 'B' or 'C':
  48.                 result.append("err")
  49.         for item in y:
  50.             if item == "1":
  51.                 result.append(0)
  52.             elif item == "2":
  53.                 result.append(1)
  54.             elif item == "3":
  55.                 result.append(2)
  56.             elif item != '1' or '2' or '3':
  57.                 result.append("err")
  58.         if x == [] or y == []:
  59.             result.append("err")
  60.    
  61.     def error():
  62.         print("\n")
  63.         coordinates.clear()
  64.         x.clear()
  65.         y.clear()
  66.         result.clear()
  67.         move = input("Enter coordinates of choosen blank field(e.g. A1): ")
  68.         move = move.upper()
  69.         for letter in move:
  70.             coordinates.append(letter)
  71.         checking()
  72.         convert()
  73.         return result
  74.    
  75.     spliting()
  76.     checking()
  77.     convert()
  78.     while True:
  79.         while result in expected_result != True:
  80.             if board[result[0]][result[1]] != '.':
  81.                 print("\n")
  82.                 print("This place is taken. Choose another empty field :) ")
  83.                 print("\n")
  84.                 error()
  85.             elif board[result[0]][result[1]] == '.':
  86.                 return result
  87.         while result[0] == "err" or result[1] == "err":
  88.             print("\n")
  89.             print("Invalid input. Please enter your choose again :) ")
  90.             print("\n")
  91.             error()
  92.         else:
  93.             while result in expected_result != True:
  94.                 if board[result[0]][result[1]] != '.':
  95.                     print("\n")
  96.                     print("This place is taken. Choose another empty field :) ")
  97.                     print("\n")
  98.                     error()
  99.                 elif board[result[0]][result[1]] == '.':
  100.                     return result
  101.    
  102.    
  103. # init_board()
  104. # print(get_move())
  105.  
  106. 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
  107.     if round == 5:
  108.         board[result[0]][result[1]] = "0"
  109.  
  110.     if round == 4:
  111.         board[result[0]][result[1]] = "X"
  112.  
  113.     if round == 3:
  114.         board[result[0]][result[1]] = "0"
  115.  
  116.     if round == 2:
  117.     board[result[0]][result[1]] = "X"
  118.  
  119.     if round == 1:
  120.     board[result[0]][result[1]] = "0"
  121.  
  122.     if round == 0:
  123.         print ("KONIEC")
  124.  
  125.    
  126. def has_won():
  127.     board = init_board()
  128.     win = ((board[0][0],board[0][1],board[0][2]),
  129.         (board[1][0],board[1][1],board[1][2]),
  130.         (board[2][0],board[2][1],board[2][2]),
  131.         (board[0][0],board[1][0],board[2][0]),
  132.         (board[0][1],board[1][1],board[2][1]),
  133.         (board[0][2],board[1][2],board[2][2]),
  134.         (board[0][0],board[1][1],board[2][2]),
  135.         (board[2][0],board[1][1],board[0][2]))
  136.     result = False
  137.     for line in win:
  138.         if line[0] == line[1] == line[2] != ".":
  139.             result = True
  140.     return result
  141. # TEST HAS_WON
  142. # if has_won() == True:
  143. #     print("WYGRANA")
  144.  
  145. get_move()

Replies to TTT rss

Title Name Language When
Re: TTT MEHOW python 3 Years ago.