class StartQt4(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self,parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.view = BoardView(self.ui.graphicsView) self.opponent = Ai() #event bindings QtCore.QObject.connect(self.ui.newGameButton,QtCore.SIGNAL("clicked()"),self.newGame) def newGame(self): self.view.clearBoard() self.board = None self.ui.winnerLabel.setText("Player goes first") def mousePressEvent(self, event): """ Event executed after every mouse press; All internal game loop logic contained here """ if self.view.winCheck(): self.playerWin() #AI move self.opponent.updateBoard(self.view.boardMatrix) self.opponent.move() #check win def playerWin(self): self.ui.winnerLabel.setText("Congratulations! You won!")
class Connect4(): def __init__(self): self.game_board = board.Board() self.moves = self.game_board.moves self.player1 = Ai(self.moves) self.player2 = Ai(self.moves) def test(self, input_values): def mock_input(s): print(s) return input_values.pop(0) human.input = mock_input def start(self): self.players() player = 2 not_over = True turn = 1 while not_over and turn <= 42: if player == 1: player = 2 self.game_board.make_move(2, self.player2.move()) else: player = 1 self.game_board.make_move(1, self.player1.move()) self.show_board() not_over = not self.gameover() turn += 1 if not_over == True: print("It's a Draw!") else: self.show_board() print(f'Player {player} wins') def show_board(self): string = "" i = len(self.game_board.game_board) - 1 while i > -1: row = self.game_board.game_board[i] for token in row: string += f'|{token}|' string += "\n" i -= 1 print(string) def gameover(self): return self.game_board.moves.check_win() def players(self): if input("Player 1 is a Human or AI?") == "Human": self.player1 = human.Human(self.moves) if input("Player 2 is a Human or AI?") == "Human": self.player2 = human.Human(self.moves) return
def test_can_not_place_in_a_full_collum(): class Moves(): def __init__(self): self.legal_moves = [ "Full", [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0] ] move_array = [0, 1] def mock_move(): return move_array.pop(0) computer = Ai(Moves()) computer.select_move = mock_move i = 0 assert computer.move() == 1
def test_Can_make_a_move(): computer = Ai(Moves()) number = computer.move() assert number >= 0 and number <= 6