Example #1
0
def parse_input():
    start_state = None
    end_state = None
    if INPUT_FROM_FILE:
        with open('input.txt') as f:
            start_state = f.readline().strip()
            end_state = f.readline().strip()
    else:
        start_state = input('Start state: ')
        end_state = input('End state: ')
    start_board = models.Board(start_state)
    end_board = models.Board(end_state, board_id=-1)
    return start_board, end_board
Example #2
0
def add_comment():
    user = utils.session_check(request)
    board = models.Board(request.args.get('board'))
    post = models.Post(id=request.args.get('post'), board=board)
    if 'subject' in request.form:
        subject = request.form['subject'][:int(cfg['max_subject_length'])]
    else:
        subject = ''
    comment = request.form['comment'][:int(cfg['max_comment_length'])]
    if not user or user.fng:
        return utils.response(
            403,
            'You have been banned or have never visited before. If its the latter visit home first.',
            user)
    if not board.name:
        return utils.response(404, 'That board does not exist.', user)
    if not utils.restrict_check(request, board):
        return utils.response(
            403, 'You have been banned from posting in this board.', user)
    post = models.Post(id=request.args.get('post'), board=board)
    if not post.board:
        return utils.response(404, 'That post does not exist.', user)
    if post.thread == 0:
        thread = post.id
    else:
        thread = post.thread
    if 'file' in request.files:
        file = request.files['file']
        if not utils.allowed_file(file):
            return utils.response(
                400, 'The image is of an unacceptable format or too large.',
                user)
        img = models.Image(file=file)
        if not img:
            return utils.response(500, 'Something went wrong. Try again later',
                                  user)
    else:
        img = models.Image(dummy=True)
        img.id = 0
    comment = ">>{}\n{}".format(post.id, comment)
    post = models.Post(board=board,
                       author=user,
                       subject=subject,
                       comment=comment,
                       image=img.id,
                       thread=thread)
    if post:
        return utils.response(201, post.dictate(), user)
    else:
        return utils.response(500, 'Something went wrong. Try again later.',
                              user)
Example #3
0
def get_post():
    user = utils.session_check(request)
    if not user or user.fng:
        return utils.response(
            403,
            'You have been banned or have never visited before. If its the latter visit home first.',
            user)
    board = models.Board(request.args.get('board'))
    if not board.name:
        return utils.response(400, 'That board does not exist.', user)
    res = models.Post(board=board, id=request.args.get('id'))
    if res.board:
        return utils.response(200, res.dictate(), user)
    else:
        return utils.response(404, 'That post does not exit', user)
Example #4
0
def get_posts():
    user = utils.session_check(request)
    if not user or user.fng:
        return utils.response(
            403,
            'You have been banned or have never visited before. If its the latter visit home first.',
            user)
    board = models.Board(request.args.get('board'))
    if not board.name:
        return utils.response(404, 'That board does not exist.', user)
    limit = int(request.args.get('limit'))
    if limit > int(cfg['post_pull_limit']):
        return utils.response(
            400, 'You can only pull {} posts per request'.format(
                cfg['post_pull_limit']), user)
    posts = models.get_posts(board, limit, dicc=True)
    return utils.response(200, posts, user)
def getBoards():
    if request.method == 'GET':  # get all boards
        if 'logged_in' in session:
            return getUserBoards(session['id'])
        if ON_LOCAL:
            return jsonify(query_select("SELECT * FROM Board"))
        else:
            return jsonify(
                json_list=[i.serialize for i in models.Board.query.all()])

    elif request.method == 'POST':  # create new board
        board_json = json.loads(request.data)
        board = models.Board(
            user_id=session['id'] if 'id' in session else board_json['id'],
            name=board_json['name'],
            public=board_json['public'],
            last_modified='NOW()')
        db_session.add(board)
        db_session.commit()
        return jsonify(board.serialize)
Example #6
0
def delete_post():
    user = utils.session_check(request)
    if not user or user.fng:
        return utils.response(
            403,
            'You have been banned or have never visited before. If its the latter visit home first.',
            user)
    board = models.Board(request.args.get('board'))
    if not board.name:
        return utils.response(404, 'That board does not exist.', user)
    post = models.Post(id=request.args.get('post'), board=board)
    if not post.board:
        return utils.response(404, 'That post does not exist.', user)
    if post.author.id == user.id:
        if post.delete():
            return utils.response(200, 'Post deleted successfully', user)
        else:
            return utils.response(500,
                                  'Something went wrong. Try again later.',
                                  user)
    else:
        return utils.response(403, 'You are not the author of this post.',
                              user)
Example #7
0
class ConnectFourGame:
    players = [models.Player() for _ in range(2)]
    board = models.Board()
    gameType = None
    exitState = False
    playerWin = False

    def run(self):
        print("\n------\nWelcome to Connect Four!\n------\n")
        self.userChoice()

    def runGame(self):
        self.nameInputs()
        self.board.__init__()
        self.runXvX()
        self.resetAllVariables()

    def userChoice(self):
        prompt = "What would you like to do?\nPlease only enter 1-4\n1.)Player vs Player\n2.)Player vs Computer\n3.)Computer vs Computer\n4.)Exit\n"
        userChoice = self.numberPrompt(prompt, 4, 1)
        if userChoice == 1:
            self.gameType = models.GameType.PvP
            self.runGame()
        elif userChoice == 2:
            self.gameType = models.GameType.PvC
            self.runGame()
        elif userChoice == 3:
            self.gameType = models.GameType.CvC
            self.runGame()
        elif userChoice == 4:
            print("\n---\nExiting\n---\n")
            self.setExit(True)

    def nameInputs(self):
        nameOne = input("\nWhat is the name of Player One? \n")
        isInvalid = len(nameOne) == 0
        if isInvalid == True:
            print("Ok, we will just use the default: Player1")
            nameOne = "Player1"
        nameTwo = input("\nWhat is the name of Player Two? \n")
        isInvalid = len(nameTwo) == 0
        if isInvalid == True:
            print("Ok, we will just use the default: Player2")
            nameTwo = "Player2"
        self.createPlayers(nameOne, nameTwo)

    def runXvX(self):
        rand = self.randomTurn()
        count = 0
        randSecond = 0
        if rand == 2:
            randSecond = 1
        else:
            randSecond = 2
        while self.playerWin == False:
            check = self.playerTurn(rand)
            count = count + 1
            if check == 1 or check == 2:
                self.board.printBoard()
                if check == 1:
                    print("\n------\n" + self.getPlayerName(1) +
                          " WINS\n------")
                    self.playerWin = True
                    break
                elif check == 2:
                    print("\n------\n" + self.getPlayerName(2) +
                          " WINS\n------")
                    self.playerWin = True
                    break
            checkTwo = self.playerTurn(randSecond)
            count = count + 1
            if checkTwo == 1 or checkTwo == 2:
                self.board.printBoard()
                if checkTwo == 1:
                    print("\n------\n" + self.getPlayerName(1) +
                          " WINS\n------")
                    self.playerWin = True
                    break
                elif checkTwo == 2:
                    print("\n------\n" + self.getPlayerName(2) +
                          " WINS\n------")
                    self.playerWin = True
                    break
            if count == 42:
                print("\n------\nDRAW\n------")
                self.playerWin = True
                break

    def playerTurn(self, player):
        if self.players[player - 1].getHumanState() == True:
            self.board.printBoard()
            self.humanMove(player)
        else:
            self.computerMove(player)
            self.board.printBoard()
        playerWhoWon = self.board.checkForFour()
        return playerWhoWon

    def randomTurn(self):
        p1, p2 = (1, 2)
        return random.choice([p1, p2])

    def humanMove(self, player):
        prompt = "\nWhich column would you like to player your piece in? (1-7) \n"
        isFull = True
        while isFull == True:
            playerMove = self.numberPrompt(prompt, 6, 1)
            isFull = self.board.checkColumn(playerMove)
            if isFull == True:
                print("\nColumn is full...try another...\n")
            else:
                self.board.setPiece(playerMove,
                                    self.players[player - 1].getColor())

    def computerMove(self, player):
        isFull = True
        isInvalid = True
        while isFull == True:
            while isInvalid == True:
                playerMove = random.randint(1, 6)
                isInvalid = playerMove > 6 or playerMove < 1
            isFull = self.board.checkColumn(playerMove)
        self.board.setPiece(playerMove, self.players[player - 1].getColor())

    def createPlayers(self, nameOne, nameTwo):
        if self.gameType == models.GameType.PvC:
            player1, player2 = (models.Player(), models.Player())
            player1.constructor(nameOne, True, 'R')
            player2.constructor(("Computer: " + nameTwo), False, 'Y')
            self.players.insert(0, player1)
            self.players.insert(0, player2)
        elif self.gameType == models.GameType.PvP:
            player1, player2 = (models.Player(), models.Player())
            player1.constructor(nameOne, True, 'R')
            player2.constructor(nameTwo, True, 'Y')
            self.players.insert(0, player1)
            self.players.insert(0, player2)
        elif self.gameType == models.GameType.CvC:
            player1, player2 = (models.Player(), models.Player())
            player1.constructor(("Computer: " + nameOne), False, 'R')
            player2.constructor(("Computer: " + nameTwo), False, 'Y')
            self.players.insert(0, player1)
            self.players.insert(0, player2)

    def getPlayerName(self, playerNumber):
        return self.players[playerNumber - 1].getName()

    def numberPrompt(self, inputPrompt, higherBound, lowerBound):
        isInvalid = True
        while isInvalid == True:
            userInput = input(inputPrompt)
            userChoice = int(userInput)
            isInvalid = userChoice < lowerBound or userChoice > higherBound
        return userChoice

    def resetAllVariables(self):
        self.board.newBoard()
        self.players.clear()
        self.playerWin = False
        self.gameType = None

    def setExit(self, exitBool):
        self.exitState = exitBool

    def getExit(self):
        return self.exitState