2012-12-14 3 views
-3
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() 
+1

어떻게'board'가 정의 :

난 당신이 원하는 생각하십니까? (추신 : 글로벌 변수를 사용하는 이유는 무엇입니까?) – abarnert

+0

또한 "작동하지 않는다"는 것은 무엇을 의미합니까? 나는 글로벌'board = [ '범위 내 (9)]'를 만들고, 붙여 넣기하고'getCSpot' 함수를 실행했다. 결과는'[' ',' ',' ',' ','O ',' ',' ',' ',' ']'와 같은 문자를 사용할 수 있습니다. 그게 효과가있는 것처럼 보입니다. 사실 9 번 연속으로 호출하면 빈 공간을 찾아 'O'로 바꿔 궁극적으로 9 'O의 목록을 얻습니다. (물론 10 번째 전화라고 부르면 영원히 멈 춥니 다.) 그리고 나서 didWin ('O') '는 다음 번에 더 나은 운을 보여 주며 잃어버린 것입니다.'. – abarnert

+2

'getCSpot()'을 정의했지만'getspot()'를 호출하고 있습니다. 스크립트 파일이 아닌 인터랙티브 인터프리터에서 이것을하는 경우,'getspot'는 이전에 세션에서 정의한 이전 함수이며, 작동하지 않습니다. – abarnert

답변

2

나는 문제가 무엇이든간에 당신이 우리에게 보여주지 않은 코드에 있다고 확신합니다.

일을 간단하게하기 위해 endGame 함수를 테스트하지 않았습니다. 그러나 나는 다른 코드에 붙여, 다음과 같은 추가 :

board = [0, 1, 2, 3, 4, 5, 6, 7, 8] 
gameOver = False 
def endGame(): 
    print 'endGame called' 
for i in range(9): 
    getCSpot() 
    print(board) 
    didwin('O') 
    if gameOver: 
     print 'gameOver' 
     break 

결과였다 : 정확히마다 동일하지 그래서 순서는 무작위 물론

[0, 'O', 2, 3, 4, 5, 6, 7, 8] 
[0, 'O', 2, 3, 'O', 5, 6, 7, 8] 
[0, 'O', 2, 3, 'O', 5, 'O', 7, 8] 
[0, 'O', 2, 3, 'O', 5, 'O', 'O', 8] 
Better luck next time, you lost! 
endGame called 
gameOver 

하지만 항상 한 번에 하나의 난수를 'O'로 대체하여 3-in-a-row가 나오면 잃어 버렸다고 알려줍니다.

따라서 정확하게 의도 한대로 작동합니다. 코드가 작동하지 않는다면, 당신은 뭔가 잘못하고있는 것입니다. 어쩌면 당신은 board 또는 초기 설정에서 다른 무엇인가를 설정하고 있습니까? 당신은 '이 Y'하는 그것이 NameError을 제기, 존재하지 않는 game1game라는 이름의 어떤 함수를 호출 포인트, 또 다른 게임을 말할 때까지

displayBoardendGame 코드를 추가, 그것을 작동합니다.

getCSpot()을 호출하는 대신 게임 루프에서 getspot()이라는 함수를 호출 했으므로 시작 기능 (삭제 한 메모에서부터 메모리에 있음)을 보았습니다. getspot이 어딘가에 정의되어 있다면 문제는 올바른 기능과 올바르지 않은 기능 중 하나 일 가능성이 있습니다. 또는 어딘지 정의되지 않았거나 "작동하지 않습니다"및 "전혀 문자열에 O를 넣지 않습니다"라는 말은 "기회가 오기 전에 게임이 예외로 중단되기 때문에 호출되지 않습니다" ?

이제 모든 것을 게시 했으므로 몇 가지 명백한 문제가 있습니다.

먼저 어디서도 getCSpot으로 전화하지 마십시오. 그것이 아무 것도하지 않는 이유입니다. 아마도 당신이 원하는 : getspot에서 또한

def mainGame(): 
    gameOver = False 
    while gameOver == False: 
     displayBoard() 
     getspot() 
     didwin('X') 
     didwin('O') 
     getCSpot() 
     didwin('X') 
     didwin('O') 

를, 당신의 if 문이 모두 잘못이다. board[playerSpot] != 'X' 일 경우 이동을 허용하고 board[playerSpot] != 'O' 인 경우 이동을 허용합니다. 즉, 항상 입력해야합니다. playerSpot == 'O'이 거짓 일 때까지 이동 한 플레이어를 계산합니다. 숫자가되어야하기 때문에 항상 그렇습니다.

if board[playerSpot] == 'X': 
     playerSpot = raw_input('You have already chosen that spot. Please choose another. ') 
    elif board[playerSpot] == 'O': 
     playerSpot = raw_input('The computer chose that spot already. Please choose another. ') 
    else: 
     board[playerSpot] = 'X' 
     spotchosen = True 
+0

내 코드를 주석에 게시하는 방법을 알고 있다면 (단지 hehe라고 생각할 수 없습니다) 전체 코드를 게시합니다. – user1861771

+1

@ user1861771 : 주석에 코드를 유용하게 게시 할 수 없습니다. 그건 네 잘못이 아니야. 예를 들어 pastebin.com에 게시 한 다음 여기에 링크를 게시 할 수 있습니다. 또는 질문을 편집하십시오. – abarnert

+0

코드 전체를 하단에 포함하도록 질문을 편집했습니다. – user1861771

관련 문제