2016-05-30 8 views
0

Я написал игру «tic tac toe» в питоне (pycharm). В конце игры я спросил пользователя, хочет ли он снова играть: если он нажал «y», игра снова началась, и если он нажал «n», игра/код остановилась. знаете ли вы, как я могу показать ему ошибку massege, например: «вы ввели неправильный ответ! Повторите попытку:« Когда он не нажимает ни одно из этих писем, а что-то еще? например: «Вы хотите, чтобы играть снова?„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

Оберните оба элемента if в функции финиша в цикле while, чтобы продолжить, пока answer.lower() не равен «n» и не равен «y». Вы также можете сделать бесконечный цикл, так как вы возвращаетесь в обоих случаях. – duncan

+0

Итак, вы писали 'tic-tac-носок, но вы не знаете, что такое цикл while? – 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. Я хотел бы предложить, чтобы вставить механизм для выхода до завершения га мне в случае, если пользователь хочет завершить игру.

+0

Спасибо очень mucchhh :)) – emanuel

+0

и за ваше предложение: как я могу это сделать? извините, но im новый пользователь :) – emanuel