嘿,我被困在最后一部分,我不得不写位置标记(place_marker(the_Board,player2_marker,position)),它无法识别标记,它说这是一个未定义的变量,有什么想法吗?
这是我的脚本:(由于该问题尚未完成)
def display_board(board):
print('\n'*3)
print(board[7]+'|'+board[8]+'|'+board[9])
print(board[4]+'|'+board[5]+'|'+board[6])
print(board[1]+'|'+board[2]+'|'+board[3])
#test_board = ['#','X','O','X','O','X','O','X','O','X'] #test
#display_board(test_board) #test
def player_input():
print('\n'*3)
Player1_name=input('What is your name player1 ? : ')
Player2_name=input('What is your name player2 ? : ')
marker = ''
while marker != 'X' and marker != '0' :
Player1=marker = input('{}, please chose X or 0 : '.format(Player1_name))
if Player1 == 'X':
Player2 = '0'
else:
Player2= 'X'
return ('OK, {} is : {}\nAnd {} is : {} ' .format(Player1_name,Player1,Player2_name,Player2))
#print(player_input()) #test
def place_marker(board, marker, position):
board[position] = marker
#print(place_marker(test_board,'2',8))
#print(display_board(test_board))
def win_check(board, mark):
return ((board[7] == mark and board[8] == mark and board[9] == mark) or (board[4] == mark and board[5] == mark and board[6] == mark) or (board[1] == mark and board[2] == mark and board[3] == mark) or (board[7] == mark and board[4] == mark and board[1] == mark) or (board[8] == mark and board[5] == mark and board[2] == mark) or (board[9] == mark and board[6] == mark and board[3] == mark) or (board[7] == mark and board[5] == mark and board[3] == mark) or (board[9] == mark and board[5] == mark and board[1] == mark))
#print(win_check(test_board,'X')) #test
import random
def choose_first():
flip = random.randint(0,1)
if flip == 0 :
return 'Player1'
else:
return 'Player2'
def space_check(board, position):
return board[position] == ''
def full_board_check(board):
for i in range(1, 10) :
if space_check(board, i):
return False
return True
def player_choice(board):
print('\n'*3)
position = 0
while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board,position):
position = int(input('Please chose a position from 1 to 9 : '))
return position
def replay():
choise = input('Do you wnat to play again, yes or no ? : ')
return choise == 'yes'
print('Welcome to Tic Tac Toe!')
while True:
the_board=[' '] *10
player_input()
turn=choose_first()
print(turn+'will go first')
play_game=input('Ready to play ? y or n ? : ')
if play_game == 'y' :
game_on = True
else:
game_on = False
while game_on :
if turn == 'Player1' :
display_board(the_board)
position=player_choice(the_board)
place_marker(the_Board, player2_marker, position) ### HERE
if win_check (the_board ,Player1) :
display_board(the_board)
print('{} has Won !!')
game_on = False
if not replay() :
break
运行代码后,这是控制台,由于最终脚本未完成,它无法识别我的数字,
Welcome to Tic Tac Toe!
What is your name player1 ? : SALLY
What is your name player2 ? : Sammy
Player1will go first
Ready to play ? y or n ? : y
| |
| |
| |
Please chose a position from 1 to 9 : 1
Please chose a position from 1 to 9 : 2
Please chose a position from 1 to 9 : 3
它仍然要求我提供电话号码
我试图更改place_marker代码的某些参数,但是没有用
def place_marker(board, marker, position):
board[position] = marker
place_marker(the_Board, player2_marker, position)
答案 0 :(得分:0)
您的问题正好在您标记的地方:
place_marker(the_Board, player2_marker, position)
这是对player2_marker
的首次引用。您需要在引用之前对其进行初始化!我认为这很简单,只要您将其设置为“ O”或“ X”即可。
答案 1 :(得分:0)
您好,谢谢您的回答,我意识到,我将自己的脚本与课程解决方案脚本进行了比较,并且看到我在播放器输入处进行了修改,只是返回了格式化的句子,所以我用那个修改了我的代码返回分配给玩家1的'x','0'或'0'或'x',玩家2 = player_input(),玩家1 = x且玩家2 = o或reverse,因此该变量我可以在place_marker函数中引入X(分配给播放器1)或O(分配给播放器2),也没有什么错误,现在终于可以使用了!!