def getCSpot():
global board
global cspot
spotchosen = False
while spotchosen == False:
spotchosen = False
cspot = random.randint(0, 8)
if board[cspot] == 'X' or board[cspot] == 'O':
cspot = random.randint(0, 8)
else:
spotchosen = True
board[cspot] = 'O'
왜 이것이 작동하지 않는지 정말 이해하지 못합니다. 그것은 문자열에 O를 전혀 넣지 않습니다. 나는 3 개의 O 또는 X가 연속적으로 있고 그 중 하나가 작동하지 않을 때 승자가 있는지를 결정하는 코드의 또 다른 부분을 가지고있다. 여기에 그 코드가있다. 어떤 도움이라도 대단히 감사합니다.문자열의 임의의 부분을 대체 할 코드가 있습니다.
def didwin(player):
global gameOver
if (board[0] == player and board[1] == player and board[2] == player or
board[3] == player and board[4] == player and board[5] == player or
board[6] == player and board[7] == player and board[8] == player or
board[0] == player and board[3] == player and board[6] == player or
board[1] == player and board[4] == player and board[7] == player or
board[2] == player and board[5] == player and board[8] == player or
board[0] == player and board[4] == player and board[8] == player or
board[2] == player and board[4] == player and board[6] == player):
gameOver = True
if player == 'X':
print 'congratulations! You won!!'
endGame()
else:
print 'Better luck next time, you lost!'
endGame()
그리고 여기에 endGame 함수가 있습니다.
def endGame():
global board
displayBoard()
answer = ' '
while answer == ' ':
print 'Would you like to play another game?'
answer = raw_input('Y/N')
if answer == 'Y' or answer == 'y' or answer == 'Yes' or answer == 'yes':
board = [0, 1, 2, 3, 4, 5, 6, 7, 8]
game1()
game()
elif answer == 'N' or answer == 'n' or answer == 'No' or answer == 'no':
exit()
편집 : 다음은 처음부터 끝까지 편집하지 않은 전체 코드입니다.
board = [0, 1, 2,
3, 4, 5,
6, 7, 8]
import random
def displayBoard():
global board
print board[0], '|', board[1], '|', board[2]
print '----------'
print board[3], '|', board[4], '|', board[5]
print '----------'
print board[6], '|', board[7], '|', board[8]
def getspot():
global board
spotchosen = False
while spotchosen == False:
spotchosen = False
playerSpot = int(raw_input('Where would you like to go? '))
if board[playerSpot] != 'X':
board[playerSpot] = 'X'
if board[playerSpot] != 'O':
board[playerSpot] = 'X'
if playerSpot == 'X':
playerSpot = raw_input('You have already chosen that spot. Please choose another. ')
if playerSpot == 'O':
playerSpot = raw_input('The computer chose that spot already. Please choose another. ')
else:
spotchosen = True
def getCSpot():
global board
global cspot
spotchosen = False
while spotchosen == False:
spotchosen = False
cspot = random.randint(0, 8)
if board[cspot] == 'X' or board[cspot] == 'O':
cspot = random.randint(0, 8)
else:
spotchosen = True
board[cspot] = 'O'
def endGame():
global board
displayBoard()
answer = ' '
while answer == ' ':
print 'Would you like to play another game?'
answer = raw_input('Y/N')
if answer == 'Y' or answer == 'y' or answer == 'Yes' or answer == 'yes':
board = [0, 1, 2, 3, 4, 5, 6, 7, 8]
game1()
game()
elif answer == 'N' or answer == 'n' or answer == 'No' or answer == 'no':
exit()
def didwin(player):
global gameOver
if (board[0] == player and board[1] == player and board[2] == player or
board[3] == player and board[4] == player and board[5] == player or
board[6] == player and board[7] == player and board[8] == player or
board[0] == player and board[3] == player and board[6] == player or
board[1] == player and board[4] == player and board[7] == player or
board[2] == player and board[5] == player and board[8] == player or
board[0] == player and board[4] == player and board[8] == player or
board[2] == player and board[4] == player and board[6] == player):
gameOver = True
if player == 'X':
print 'congratulations! You won!!'
endGame()
else:
print 'Better luck next time, you lost!'
endGame()
else:
gameOver = False
def mainGame():
gameOver = False
while gameOver == False:
displayBoard()
getspot()
didwin('X')
didwin('O')
mainGame()
어떻게'board'가 정의 :
난 당신이 원하는 생각하십니까? (추신 : 글로벌 변수를 사용하는 이유는 무엇입니까?) – abarnert
또한 "작동하지 않는다"는 것은 무엇을 의미합니까? 나는 글로벌'board = [ '범위 내 (9)]'를 만들고, 붙여 넣기하고'getCSpot' 함수를 실행했다. 결과는'[' ',' ',' ',' ','O ',' ',' ',' ',' ']'와 같은 문자를 사용할 수 있습니다. 그게 효과가있는 것처럼 보입니다. 사실 9 번 연속으로 호출하면 빈 공간을 찾아 'O'로 바꿔 궁극적으로 9 'O의 목록을 얻습니다. (물론 10 번째 전화라고 부르면 영원히 멈 춥니 다.) 그리고 나서 didWin ('O') '는 다음 번에 더 나은 운을 보여 주며 잃어버린 것입니다.'. – abarnert
'getCSpot()'을 정의했지만'getspot()'를 호출하고 있습니다. 스크립트 파일이 아닌 인터랙티브 인터프리터에서 이것을하는 경우,'getspot'는 이전에 세션에서 정의한 이전 함수이며, 작동하지 않습니다. – abarnert