Пишу игру крестики-нолики, а пока только функцию, вычисляющую по коду(строка с координатами ходов, например, a1b2c1a3) игры, встали ли крестики или нолики в линию.
def result(code): number_of_lettercode = 0 number_of_symbol = 0 player1 = [None]*5 player2 = [None]*5 result = '' while number_of_lettercode<len(code): player1[number_of_symbol] = code[number_of_lettercode:number_of_lettercode+2] number_of_symbol=number_of_symbol+1 number_of_lettercode=number_of_lettercode+4 number_of_lettercode = 2 number_of_symbol = 0 while number_of_lettercode<len(code): player2[number_of_symbol] = code[number_of_lettercode:number_of_lettercode+2] number_of_symbol=number_of_symbol+1 number_of_lettercode=number_of_lettercode+4 if 'a1' and 'b1' and 'c1' in player1: result = 'player1' if 'a2' and 'b2' and 'c2' in player1: result = 'player1' if 'a3' and 'b3' and 'c3' in player1: result = 'player1' if 'a1' and 'a2' and 'a3' in player1: result = 'player1' if 'b1' and 'b2' and 'b3' in player1: result = 'player1' if 'c1' and 'c2' and 'c3' in player1: result = 'player1' if 'a1' and 'b2' and 'c3' in player1: result = 'player1' if 'a3' and 'b2' and 'c1' in player1: result = 'player1' print(player1) print(player2) print(result) coord = input('input code, please ') result(coord)
Проблем в заполнении списков с координатами крестиков и ноликов игроков нет, print(player1) и print(player2) выдает их верно. Проблема в некорректном поведении and, т.е., and реагирует на вхождение только первого и третьего значения в списке. Например, если ввести код игры a1c3c1, где в списке координат player1 будут a1 и с1, то сработает строка if ‘a1’ and ‘b1’ and ‘c1’ in player1:, не обращая внимания на то, что не хватает b1. Данная проблема есть на python 3.4.2 и 3.4.3 , не помогают разные способы написания типа if ‘a1’ in player1 and ‘b1’ in player1 and ‘c1’ in player1: или попытки группирования по if, т.е.,
if 'a1' and 'b1' in player1: if 'c1' in player1: result = 'player1'