Я работаю над этой не очень "крутой" версией.Я буду делать это лучше, так как я продолжаю работать над этим.Вот код:
import random
board=[1,2,3,
4,5,6,
7,8,9]
def show_board():
print('' ,board[0],'|',board[1],'|',board[2],'\n',
'-----------','\n',
board[3], '|', board[4], '|', board[5], '\n',
'-----------','\n',
board[6], '|', board[7], '|', board[8]
)
show_board()
count=0
#winning by value and position - check if x in 3 spots?
def evaluate_line(value,pos1,pos2,pos3):
if(board[pos1]==value and board[pos2]==value and board[pos3]==value):
return True
#position combinations for winning value can be x or y
def get_result(value):
if (evaluate_line(value,0,1,2)):
return True
if (evaluate_line(value, 0, 3, 6)):
return True
if (evaluate_line(value, 0, 4, 8)):
return True
# While below is for human player
while True:
print("select a spot between 1-9 to put an 'x'")
human=int(input())-1# -1 so that it matches with board not index value
if(board[human]!='x' and human<=9 and board[human]!='y'):
board[human]='x'
count=count+1
# when count reaches 5 for human means no spot left,get ready to evaluate result
if (count == 5):
board[human] = 'x'# useless I thought it will forcefully insert x value
print('No empty spot left')
# will check result before break
if (get_result('x') == True):
print('Human wins')
break
# check result for X-human turn may be human already won
if (get_result('x') == True):
print('Human wins')
break
#Below is for computer turn AFTER human turn
while True:
computer=random.randint(0,len(board)-1)
if (board[computer] != 'x' and board[computer] != 'y' ):
board[computer] = 'y'
break
else:
print('That spot s taken')
show_board()
Проблема: для последнего значения x оно не меняется на x, поскольку не может выполнить очередь компьютера (я думаю).
x | x | x
-----------
x | y | y
-----------
y | y | 9
выберите точку от 1 до 9, чтобы поставить «х»исполняется.Я попытался переместить разрыв == 5, но затем компьютерная часть не выполнялась.