2016-05-30 6 views
0

私はpython(pycharm)で "tic tac toe"ゲームを書いた。ゲームの最後に、私は彼が再びプレイしたいかどうかをユーザに尋ねた。彼が「y」を押すとゲームが再び始まり、「n」を押すとゲーム/コードが停止した。あなたはどうやって彼にエラーマッシジを見せることができるか知っていますか?「間違った答えを入力しました!もう一度やり直してください。たとえば:? FNJ 『(彼の答えを)「?あなたはもう一度プレーしたい』と、彼は正しい答えを入力するまで、このマッサージを表示TIC TAC TOE GAME - python

class color: 
    PURPLE = '\033[95m' 
    CYAN = '\033[96m' 
    DARKCYAN = '\033[36m' 
    BLUE = '\033[94m' 
    GREEN = '\033[92m' 
    YELLOW = '\033[93m' 
    RED = '\033[91m' 
    BOLD = '\033[1m' 
    UNDERLINE = '\033[4m' 
    END = '\033[0m' 

def playTicTacToe(): 
    rows,cols = 3,3 
    winLength = 3 
    board = makeNewBoard(rows, cols) 
    moves = 0 
    player = 1 

    while moves < rows*cols: 
     row,col = getMove(board, player) 
     board = setPiece(board, row, col, player) 
     if (didWin(board, player, winLength)): 
      printBoard(board) 
      print "" 
      #print '%s The Player %s color.GREEN + color.Bold + "-->" + color.END, """ IS THE WINNER OF THE GAME!!!""" + color.END % (color.GREEN,getPieceLabel(player), 
      print color.RED + color.BOLD + "The Player" + color.END, getPieceLabel(player), color.RED + color.BOLD + "IS THE WINNER OF THE GAME!!! " + color.END 
      return 
     player = otherPlayer(player) 
     moves += 1 
    print color.RED + color.BOLD + "TIE GAME!" + color.END 


def makeNewBoard(rows, cols): 
    return [([0]*cols) for row in xrange(rows)] 


def getRows(board): 
    return len(board) 

def getCols(board): 
    return len(board[0]) 

def getPiece(board, row, col): 
    return board[row][col] 

def setPiece(board, row, col, value): 
    board[row][col] = value 
    return board 

def isEmpty(board, row, col): 
    return (getPiece(board, row, col) == 0) 

def isOnBoard(board, row, col): 
    rows = getRows(board) 
    cols = getCols(board) 
    return ((row >= 0) and (row < rows) and 
      (col >= 0) and (col < cols)) 

def getPieceLabel(piece): 
    if (piece == 1): 
     return "|" + color.PURPLE + color.BOLD + "X" + color.END + "|" 
    elif (piece == 2): 
     return "|" + color.BLUE + color.BOLD + "O" + color.END + "|" 
    else: 
     return color.BOLD + "|_|" + color.END 

def printBoard(board): 
    print "\n-----------------------------------------------------------------------------------------------------------" 
    rows = getRows(board) 
    cols = getCols(board) 
    for row in xrange(rows): 
     for col in xrange(cols): 
      piece = getPiece(board, row, col) 
      label = getPieceLabel(piece) 
      print label, 
     print 


def didWin(board, player, winLength): 
    rows = getRows(board) 
    cols = getCols(board) 
    for startRow in xrange(rows): 
     for startCol in xrange(cols): 
      if (didWin1(board, player, winLength, startRow, startCol)): 
       return True 
    return False 

def didWin1(board, player, winLength, startRow, startCol): 
    for drow in xrange(-1,+2): 
     for dcol in xrange(-1,+2): 
      if ((drow != 0) or (dcol != 0)): 
       if (didWin2(board, player, winLength, 
          startRow, startCol, drow, dcol)): 
        return True 
    return False 

def didWin2(board, player, winLength, 
      startRow, startCol, drow, dcol): 
    rows = getRows(board) 
    cols = getCols(board) 
    for step in xrange(winLength): 
     row = startRow + step*drow 
     col = startCol + step*dcol 
     if (not isOnBoard(board, row, col)): 
      return False 
     elif (getPiece(board, row, col) != player): 
      return False 
    return True 


def otherPlayer(player): 
    return 1 if (player == 2) else 2 


def oops(msg): 
    print " ", msg, color.RED + color.BOLD + "Try again." + color.END 


def readInt(prompt): 
    while True: 
     try: 
      return int(raw_input(prompt)) 
     except: 
      oops(color.RED + color.BOLD + "Input must be an integer." + color.END) 

def getMove(board, player): 
    while True: 
     printBoard(board) 
     print "Enter move for player:" + getPieceLabel(player) 
     row = readInt(" Row --> ") - 1 
     col = readInt(" Col --> ") - 1 
     if (not isOnBoard(board, row, col)): 
      oops(color.RED + color.BOLD + """\n \n Out of range (not on the board)!""" + color.END) 
     elif (not isEmpty(board, row, col)): 
      oops(color.RED + color.BOLD +"""\n Already occupied!""" + color.END) 
     else: 
      return (row, col) 


def finish(): 
    answer = raw_input(color.PURPLE + color.BOLD + """Would you like to play again?! (press \'y\' for yes and \'n\' for no):""" + color.END) 
    if answer.lower() == "n": 
     print "" 
     print color.BLUE + color.BOLD + "Bye Bye! See you next time! :)" + color.END 
     return True 
    if answer.lower() == "y": 
     print (color.BLUE + color.BOLD + """\nGreat choice! The game will start again""" + color.END) 
     return False 












def main(): 
    while True: 
     playTicTacToe() 
     if finish(): 
      break 








if __name__ == '__main__': 
    main() 
+0

whileループでfinish関数内のifsを両方とも折り返すと、answer.lower()は「n」に等しくなく、「y」と等しくなりません。両方のifで戻るので、無限ループを行うこともできます。 – duncan

+0

だから、あなたは「チックタックつま先を書いたが、あなたは何回ループがあるのか​​分からない? – polku

答えて

0
if answer.lower() == "n": 
     print color.BLUE + color.BOLD + "Bye Bye! See you next time! :)" + color.END 
     return True 
elif answer.lower() == "y": 
     print (color.BLUE + color.BOLD + """\nGreat choice! The game will start again""" + color.END) 
     return False 
else: 
    print (color.BLUE + color.BOLD + """\nyou entered a wrong answer! please try again:""" + color.END) 
     return False 
1

次のように、仕上げ機能を変更することができます。このところで

def finish(): 

    while True: 

     answer = raw_input(color.PURPLE + color.BOLD + """Would you like to play again?! (press \'y\' for yes and \'n\' for no):""" + color.END) 
     if answer.lower() == "n": 
      print "" 
      print color.BLUE + color.BOLD + "Bye Bye! See you next time! :)" + color.END 
      return True 

     elif answer.lower() == "y": 
      print (color.BLUE + color.BOLD + """\nGreat choice! The game will start again""" + color.END) 
      return False 

     else: 
      print 'Not valid input...' 

は、プログラムが不正な入力を無視し、ユーザーに尋ねる再び。

PS。私は、GAが完了する前に終了するためのメカニズムを挿入することをお勧めユーザーがゲームを終了したい場合は私に連絡してください。

+0

非常にmucchhhありがとう:)) – emanuel

+0

あなたの提案:どのように私はそれを行う?申し訳ありませんが、私は新しいユーザーです:) – emanuel