예제 #1
0
    def start_computer_computer_game(self):
        printBoard = True
        tie = True

        # game loop
        while(True):
            if printBoard:
                self.print_game_board()

            count = 0
            for y in range(self.WIDTH):
                if self.board[0][y] == "X" or self.board[0][y] == "O":
                    count += 1
            isFull = (count==self.WIDTH)
            if isFull:
                break

            tempBoard = copy.deepcopy(self.board)
            if self.activePlayer == 0:
                if self.FIRST:
                    colChoice = "3"
                    self.FIRST = False
                else:
                    st = time.time()
                    colChoice = startPar(tempBoard, self.activePlayer)
                    et = time.time()
            else:
                if self.FIRST:
                    colChoice = "3"
                    self.FIRST = False
                else:
                    st = time.time()
                    colChoice = startSeq(tempBoard, self.activePlayer)
                    et = time.time()

            if colChoice == "k":
                sys.exit(0)
            elif colChoice not in [str(x) for x in range(self.WIDTH)]:
                print "[ERROR] AI player #" + str(self.activePlayer) + " did not choose a valid column"
                printBoard = False
                continue
            else:
                moveSuccess = self.make_move(int(colChoice))
                if moveSuccess:
                    if gameOver(self.board, self.activePlayer):
                        self.print_winner(self.activePlayer)
                        tie = False
                        break
                    self.activePlayer = (self.activePlayer + 1) % 2
                    printBoard = True
                else:
                    print "[ERROR] AI player #" + str(self.activePlayer) + " did not make a valid move, column full"
                    printBoard = False
                    continue
        #final game board
        self.print_game_board()
        if tie:
            print "GAME WAS A TIE\n"
예제 #2
0
    def start_computer_computer_game(self):
        printBoard = True
        tie = True

        # game loop
        while (True):
            if printBoard:
                self.print_game_board()

            count = 0
            for y in range(self.WIDTH):
                if self.board[0][y] == "X" or self.board[0][y] == "O":
                    count += 1
            isFull = (count == self.WIDTH)
            if isFull:
                break

            tempBoard = copy.deepcopy(self.board)
            if self.activePlayer == 0:
                if self.FIRST:
                    colChoice = "3"
                    self.FIRST = False
                else:
                    st = time.time()
                    colChoice = startPar(tempBoard, self.activePlayer)
                    et = time.time()
            else:
                if self.FIRST:
                    colChoice = "3"
                    self.FIRST = False
                else:
                    st = time.time()
                    colChoice = startSeq(tempBoard, self.activePlayer)
                    et = time.time()

            if colChoice == "k":
                sys.exit(0)
            elif colChoice not in [str(x) for x in range(self.WIDTH)]:
                print "[ERROR] AI player #" + str(
                    self.activePlayer) + " did not choose a valid column"
                printBoard = False
                continue
            else:
                moveSuccess = self.make_move(int(colChoice))
                if moveSuccess:
                    if gameOver(self.board, self.activePlayer):
                        self.print_winner(self.activePlayer)
                        tie = False
                        break
                    self.activePlayer = (self.activePlayer + 1) % 2
                    printBoard = True
                else:
                    print "[ERROR] AI player #" + str(
                        self.activePlayer
                    ) + " did not make a valid move, column full"
                    printBoard = False
                    continue
        #final game board
        self.print_game_board()
        if tie:
            print "GAME WAS A TIE\n"
예제 #3
0
    def start_human_computer_game(self, is_parallel):
        printBoard = True
        tie = True

        # game loop
        while (True):
            if printBoard:
                self.print_game_board()

            count = 0
            for y in range(self.WIDTH):
                if self.board[0][y] == "X" or self.board[0][y] == "O":
                    count += 1

            isFull = (count == self.WIDTH)
            if isFull:
                break

            if self.activePlayer == 0:
                print "Enter column number to drop piece"
                print "Player 1 (X) >> "
                colChoice = getch()
                self.FIRST = False
            else:
                print "Player 2 (O) >> "
                if self.FIRST:
                    colChoice = "3"
                    self.FIRST = False
                else:
                    tempBoard = copy.deepcopy(self.board)
                    st = time.time()

                    if is_parallel:
                        colChoice = startPar(tempBoard, self.activePlayer)
                    else:
                        colChoice = startSeq(tempBoard, self.activePlayer)

                    et = time.time()
                    print "[INFO] Time taken: " + str(et - st)
                print "[INFO] AI is playing column " + colChoice

            if colChoice == "k":
                sys.exit(0)
            elif colChoice not in [str(x) for x in range(self.WIDTH)]:
                print "You must enter a valid column"
                printBoard = False
                continue
            else:
                moveSuccess = self.make_move(int(colChoice))
                if moveSuccess:
                    if gameOver(self.board, self.activePlayer):
                        self.print_winner(self.activePlayer)
                        tie = False
                        break
                    self.activePlayer = (self.activePlayer + 1) % 2
                    printBoard = True
                else:
                    print "Invalid move, column full"
                    printBoard = False
                    continue
        self.print_game_board()
        if tie:
            print "GAME WAS A TIE\n"
예제 #4
0
    def start_human_computer_game(self, is_parallel):
        printBoard = True
        tie = True

        # game loop
        while(True):
            if printBoard:
                self.print_game_board()

            count = 0
            for y in range(self.WIDTH):
                if self.board[0][y] == "X" or self.board[0][y] == "O":
                    count += 1

            isFull = (count==self.WIDTH)
            if isFull:
                break

            if self.activePlayer == 0:
                print "Enter column number to drop piece"
                print "Player 1 (X) >> "
                colChoice = getch()
                self.FIRST = False
            else:
                print "Player 2 (O) >> "
                if self.FIRST:
                    colChoice = "3"
                    self.FIRST = False
                else:
                    tempBoard = copy.deepcopy(self.board)
                    st = time.time()

                    if is_parallel:
                        colChoice = startPar(tempBoard, self.activePlayer)
                    else:
                        colChoice = startSeq(tempBoard, self.activePlayer)

                    et = time.time()
                    print "[INFO] Time taken: " + str(et - st)
                print "[INFO] AI is playing column " + colChoice

            if colChoice == "k":
                sys.exit(0)
            elif colChoice not in [str(x) for x in range(self.WIDTH)]:
                print "You must enter a valid column"
                printBoard = False
                continue
            else:
                moveSuccess = self.make_move(int(colChoice))
                if moveSuccess:
                    if gameOver(self.board, self.activePlayer):
                        self.print_winner(self.activePlayer)
                        tie = False
                        break
                    self.activePlayer = (self.activePlayer + 1) % 2
                    printBoard = True
                else:
                    print "Invalid move, column full"
                    printBoard = False
                    continue
        self.print_game_board()
        if tie:
            print "GAME WAS A TIE\n"