Пример #1
0
    def run(self):
        #prints current board
        board(self.playboard.fields)

        while True:

            #Ask Player for move
            if self.playboard.current == 1:
                print(self.player1, ', please make your move')
            else:
                print(self.player2, ', please make your move')

            #ask for coordinates from player
            x = int(input('Enter row: '))
            y = int(input('Enter column: '))

            #makes the move
            self.playboard.makeMove(x,y)

            #prints board after new move
            board(self.playboard.fields)

            #check if the game is over
            if self.playboard.isOver() == True:
                #return result of the game
                return self.playboard.getResult()
Пример #2
0
def smartCompMatch(list):
    '''BONUS WORK: start a match of tictactoe, player 1 is a smart computer, player 2 is a computer that makes random moves'''

    #analogous to comments in compMatch

    boardlist = list
    counter = 0

    while True:
        if counter % 2 == 0:
            player = 1
            gametuple = smartMove(boardlist, player)
        else:
            player = 2
            gametuple = randomMove(boardlist, player)
        boardlist = modifyBoard(boardlist, gametuple, player)
        board(boardlist)

        counter += 1

        if haswon(boardlist, player):
            print('Congratulations, Player', player, ", you won!")
            break

        if checkgame(boardlist) == 0:
            break
Пример #3
0
def compMatch(list):
    '''Start a computer match of tictactoe in which player 1 and player 2 are played by a computer that makes random moves'''

    boardlist = list
    counter = 0

    while True:

        #determine whether player 1 or player 2 is playing
        if counter % 2 == 0:
            player = 1
        else:
            player = 2

        #generate coordinates and new list, all stored in a tuple
        gametuple = randomMove(boardlist, player)

        #extract list from tuple and display board according on the computers move
        boardlist = modifyBoard(boardlist, gametuple, player)
        board(boardlist)

        counter += 1

        #break out of loop if a winning position is on the board
        if haswon(boardlist, player):
            print('Congratulations, Player', player, ", you won!")
            break

        #break if all tiles are occupied
        if checkgame(boardlist) == 0:
            break
Пример #4
0
def vsComp():
    '''Play tictactoe against a Computer as Player 2'''

    #ask player 1 for name, player 2 is named Computer
    name1 = input('Player 1, please enter your name: ')
    name2 = 'Computer'

    #counter to keep track whether Player 1 or computer is playing
    counter = 0

    #set starting board
    boardlist = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

    #Announces whose turn it is and makes sure the game is not over yet
    while not checkgame(boardlist) == 0:

        #if it is Player 1's turn
        if counter % 2 == 0:

            #ask Player 1 for coordinates
            print(name1, end=", ")
            x = int(input("Enter x: "))
            y = int(input("Enter y: "))

            #change list (map of game) according to player 1's moves
            boardlist = makemove(x, y, boardlist, 1)

            #display board after move
            board(boardlist)

            #break out of loop if last move won the game
            if haswon(boardlist, name1):
                print('Congratulations,', name1, ", you won!")
                break

            #increase counter and print an empty line
            counter += 1
            print('\n')

        #if it is the computer's turn
        elif counter % 2 != 0:

            #generate a random move from the computer
            gametuple = randomMove(boardlist, 2)

            #extract list from the tuple
            boardlist = modifyBoard(boardlist, gametuple, 2)

            #display board after computer's move
            board(boardlist)

            #break out of the loop if the last move won
            if haswon(boardlist, name2):
                print("The Computer won. Nobody's perfect.")
                break

            #increase counter and print an empty line
            counter += 1
            print('\n')
Пример #5
0
def tictactoe():
    '''Plays an interactive game of tictactoe. Asks players to input coordinates that describe where the symbol is to be placed. x gives information about the row (1-3), y gives information about the column (1-3).'''

    # counter to keep track of whether it is player 1's or player 2's turn
    counter = 0

    #starting board, all tiles are empty
    boardlist = [[0,0,0],[0,0,0],[0,0,0]]

    #ask players for their names
    name1 = input('Player 1, please enter your name: ')
    name2 = input('Player 2, please enter your name: ')

    #repeat as long as there are still free tiles
    while not checkgame(boardlist) == 0:

        #player 1's turn
        if counter%2 == 0:

            #ask for coordinates to place the symbol on the board
            print(name1, end=", ")
            x = int(input("Enter x: "))
            y = int(input("Enter y: "))

            #update boardlist based on user input of coordinates
            boardlist = makemove(x, y, boardlist, 1)

            #display board
            board(boardlist)

            #check whether player 1's move just won the game, if so, break out of the while loop and end the game
            if haswon(boardlist, 1):
                print('Congratulations', name1, ", you won!")
                break

            #update counter
            counter += 1

        #player 2's turn
        else:

            #analogous to comments for player 1
            print(name2, end=", ")
            x = int(input("Enter x: "))
            y = int(input("Enter y: "))
            boardlist = makemove(x, y, boardlist, 2)
            board(boardlist)
            if haswon(boardlist, 2):
                print('Congratulations', name2, ", you won!")
                break


            counter += 1
Пример #6
0
    def run(self):
        '''Runs a game that makes different moves depending on the chosen player classes'''

        #print board
        board(self.playboard.fields)

        while True:

            #if it is player 1's turn
            if self.playboard.current == 1:
                #ask player 1 to make a move
                print(self.player1.getName() + ', please make your move.')
                #get coordinates for the position of the next symbol to be placed. based on respective way of playing (depending on chosen subclass and according getMove function). Coordinates will be the first 2 positions of the tuple that results from calling the getMove method.
                coordinates = self.player1.getMove(self.playboard)

                #change the boardlist that is an attribute of the instance self.playboard of the class Board2 according to coordinates and the current player number.
                try:
                    self.playboard.makeMove(coordinates[0] + 1,
                                            coordinates[1] + 1)
                except OccupiedMove as e:
                    print(e)
                    print(2)
                    return 2
                except InvalidMove as e:
                    print(e)
                    print(2)
                    return 2

                #draws board after new move
                board(self.playboard.getBoard())

            #if it is player 2's turn
            else:

                #for comments, see above
                print(self.player2.getName() + ', please make your move.')
                coordinates = self.player2.getMove(self.playboard)
                try:
                    self.playboard.makeMove(coordinates[0] + 1,
                                            coordinates[1] + 1)
                except OccupiedMove as e:
                    print(e)
                    print(1)
                    return 1
                except InvalidMove as e:
                    print(e)
                    print(1)
                    return 1

                board(self.playboard.fields)

            #if the game is over
            if self.playboard.isOver() == True:

                #return the result of the game
                winner = self.playboard.getResult()
                if winner == 0:
                    print('Draw')
                else:
                    print(winner)
                return winner