def checkMove(move, user1, channelID): """Precondtion: A game exists and is in some valid state. A user has made a move to be checked for validity Postcondition: Returns different states of move validity, (-1 to -5), denoted by the comments next to each return status""" user1_db = str(db.retrieveGameInfo(channelID)[0]) user2_db = str(db.retrieveGameInfo(channelID)[1]) board = db.retrieveMove(channelID)[0] turn = int(getMove(channelID)) if turn % 2 == 0: #user1 turn (even) if user1 == user2_db: return -5 #If the other who just made a turn tried to make another elif user1 != user1_db and user1 != user2_db: return -4 #if anyone else not in-game tried to make a move elif turn % 2 != 0: #user2 turn (odd) if user1 == user1_db: return -5 #If the other who just made a turn tried to make another elif user1 != user2_db and user1 != user1_db: return -4 #if anyone else not in-game tried to make a move try: move = int(move) if move >= 1 and move <= 9: if board[move - 1] != -1: return -1 # error a move attempted on an already taken cell else: return (move - 1) else: return -2 # attempted move on a cell > 9 or cell < 0 except: return -3 #non-integer input (e.g str)
def nextMove(position, channelID): """Precondition: A game exists and is in some valid state Postcondition: Makes next move, and returns updated board""" board = db.retrieveMove(channelID)[0] board[position - 1] = 1 if getMove(channelID) % 2 == 0 else 0 #1 if x, 0 if O db.saveMove(board, channelID) #save return board
def getBoard(channelID): """Precondition: A game exists and is in some valid state Postcondition: returns board status without making any changes to it""" return db.retrieveMove(channelID)[0]
def checkGame(channelID): """Postcondition: Returns bool whether or not a game is in place""" if db.retrieveMove(channelID): return True return False
def getMove(channelID): """Precondition: A game exists and is in some valid state Postcondition: An int value with the number of moves is returned """ move = db.retrieveMove(channelID)[1] return move