Exemple #1
0
 def test_board_create(self):
     board = Board()
     self.assertEqual(board.win(), None)
     self.assertEqual(board.tie(), False)
     for row in range(15):
         for col in range(15):
             self.assertEqual(board.get_square(row, col), 0)
Exemple #2
0
 def test_full_board(self):
     board = Board()
     board_service = BoardService(board)
     for x in range(7):
         for y in range(6):
             board.set_position(x, y, 1)
     self.assertEqual(board_service.game_state(), 0)
Exemple #3
0
 def test_board_win(self):
     board = Board()
     board.move(1, 1, 'b')
     board.move(1, 5, 'b')
     board.move(1, 4, 'b')
     board.move(1, 3, 'b')
     board.move(1, 2, 'b')
     self.assertEqual(board.win(), 5)
Exemple #4
0
 def test_menu_exception(self):
     board = Board()
     board_service = BoardService(board)
     ai = AI(board)
     ui = UI(board_service, ai)
     with patch('builtins.input', side_effect='0'):
         self.assertRaises(MenuException, lambda: ui.menu())
Exemple #5
0
 def test_board_service_add_piece_and_exceptions(self):
     board = Board()
     board_service = BoardService(board)
     self.assertEqual(
         board_service.show,
         '0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 \n',
         "This shouldn't not work anymore")
     board_service.add_piece(1, 1)
     self.assertEqual(board.get_position(1, 0), 1, "add_piece not working")
     # Testing column full exception
     board_service.add_piece(1, 1)
     board_service.add_piece(1, 1)
     board_service.add_piece(1, 1)
     board_service.add_piece(1, 1)
     board_service.add_piece(1, 1)
     self.assertRaises(ServiceException,
                       lambda: board_service.add_piece(1, 1))
Exemple #6
0
 def test_ai(self):
     board = Board()
     ai = AI(board)
     board.set_position(0, 5, 1)
     board.set_position(1, 5, 1)
     board.set_position(2, 5, 1)
     board.set_position(3, 5, 1)
     board.set_position(4, 5, 1)
     board.set_position(5, 5, 1)
     self.assertIsInstance(ai.generator(), int)
Exemple #7
0
 def test_algorithm(self):
     board = Board()
     algortihm = AlgorithmSimple()
     self.assertEqual(algortihm.next_move(board), (0, 0))
     board.move(0, 0, 'b')
     self.assertEqual(algortihm.next_move(board), (0, 1))
     board.move(0, 1, 'b')
     self.assertEqual(algortihm.next_move(board), (0, 2))
Exemple #8
0
 def test_board_domain(self):
     board = Board()
     for x in range(7):
         for y in range(6):
             self.assertEqual(
                 board.get_position(x, y), 0,
                 "Board is not initialised with 0, error is located at: %s, %s"
                 % (x, y))
     board.set_position(1, 0, 1)
     self.assertEqual(board.get_position(1, 0), 1,
                      "set_position not working for Board")
 def setUp(self):
     self.repo = Board()
Exemple #10
0
 def __init__(self):
     self.board = Board()
Exemple #11
0
 def test_board_move(self):
     board = Board()
     board.move(1, 1, 'b')
     self.assertEqual(board.get_square(1, 1), 1)
Exemple #12
0
    def start(self):
        inp = 0
        moves = 0
        winner_found = False
        #if inp % 2 == 0 user have to add a new value, if inp % 2 != 0 the computer will play
        while winner_found == False and moves != 42:
            try:
                self.display_board()
                if inp % 2 == 0:
                    q = self.user_move()
                    while q == 0:
                        q = self.user_move()
                    moves = moves + 1
                else:
                    q = self.computer_move()
                    while q == 0:
                        q = self.computer_move()
                    moves = moves + 1
                if self.check_for_winner(winner_found) == 1:
                    print(self.service.table_value())
                    return
            except IndexError as m:
                print(m)
            except ValueError as m:
                print(m)
            inp = inp + 1
b = Board()
s = Service(b)
u = UI(s)
u.start()
Exemple #13
0
class Game:

    def __init__(self, height, width, bombs):
        self.__mainBoard = Board(height, width, bombs)
        self.__visited = [[0 for i in range(width)] for j in range(height)]
        self.__flags = [[0 for i in range(width)] for j in range(height)]
        self.__mainBoard.fillBoardWithBombs()
        self.__mainBoard.fillBoardNumbers()

    def bfsOnBoard(self, row, col):
        '''
        :param row:
        :param col:
        :return: list of squares that must be uncovered
        '''
        copy_board = self.__mainBoard.getCopyBoard()
        current = copy_board[row][col]
        if current == BOMB:
            return BOMB
        if current != 0:
            return [Square(row, col)]
        q = Queue()
        q.put(Square(row, col))
        self.__visited[row][col] = 1
        ret_lst = []
        while not q.empty():
            now = q.get()
            r = now.get_row
            c = now.get_col
            if copy_board[r][c] == 0:
                neighbours = self.__mainBoard.getListOfNeighbours(r, c)
                for neigh in neighbours:
                    value = copy_board[neigh.get_row][neigh.get_col]
                    is_visited = self.__visited[neigh.get_row][neigh.get_col]
                    if not is_visited and value != BOMB:
                        q.put(neigh)
                        self.__visited[neigh.get_row][neigh.get_col] = 1
            if copy_board[r][c] != BOMB:
                ret_lst.append(now)
        return ret_lst

    def getNeighboursNotFlagedNotVisited(self, row, col):
        neighbours = self.__mainBoard.getListOfNeighbours(row, col)
        l = filter(lambda square: not self.__flags[square.get_row][square.get_col]
                                  and not self.__visited[square.get_row][square.get_col], neighbours)
        return deepcopy(l)

    def setFlag(self, row, col):
        self.__flags[row][col] = FLAG

    def unsetFlag(self, row, col):
        self.__flags[row][col] = 0

    def isFlag(self, row, col):
        return self.__flags[row][col] == FLAG

    def getFlagedBoard(self):
        return deepcopy(self.__flags)

    def getMainBoard(self):
        return self.__mainBoard.getCopyBoard()

    def getNumberOfRows(self):
        return self.__mainBoard.getNumberOfRows()

    def getNumberOfCols(self):
        return self.__mainBoard.getNumberOfCols()

    def getNumberOfBombs(self):
        return self.__mainBoard.getNumberOfBombs()
Exemple #14
0
from ui import UI
from gui import GUI
from domain import Board
from settings import Settings
try:
    Settings().valid()
    for _ in range(500):
        Board.random_ships(
            good_random=False
        )  # Checking for impossibilities regarding ship placements
    if Settings().ui() == "ui":
        UI().start()
    else:
        GUI().start()
except Exception as e:
    print(e)
Exemple #15
0
 def test_y_axis(self):
     board = Board()
     board_service = BoardService(board)
     for y in range(1, 5):
         board_service.add_piece(1, 1)
     self.assertEqual(board_service.game_state(), 1)
Exemple #16
0
 def test_diagonal(self):
     board = Board()
     board_service = BoardService(board)
     board.set_position(0, 0, 1)
     board.set_position(1, 1, 1)
     board.set_position(2, 2, 1)
     board.set_position(3, 3, 1)
     self.assertEqual(board_service.game_state(), 1)
     board.set_position(2, 2, 0)
     board.set_position(6, 0, 1)
     board.set_position(5, 1, 1)
     board.set_position(4, 2, 1)
     board.set_position(3, 3, 1)
     self.assertEqual(board_service.game_state(), 1)
Exemple #17
0
 def __init__(self, height, width, bombs):
     self.__mainBoard = Board(height, width, bombs)
     self.__visited = [[0 for i in range(width)] for j in range(height)]
     self.__flags = [[0 for i in range(width)] for j in range(height)]
     self.__mainBoard.fillBoardWithBombs()
     self.__mainBoard.fillBoardNumbers()
Exemple #18
0
from Ui import UserInterface


class Game:
    '''
    This comunicates with the domain layer
    '''
    def __init__(self, board):
        self.__board = board

    def makeMoveAi(self):
        return self.__board.makeMoveAi()

    def makeMovePlayer(self, point):
        return self.__board.makeMovePlayer(point)

    def __str__(self):
        return str(self.__board)

    def verifyWinPlayer(self):
        return self.__board.verifyWinPlayer()

    def checkWin(self):
        return self.__board.checkWin()


GameBoard = Board()
Gm = Game(GameBoard)
ui = UserInterface(Gm)
ui._start()
class TestBoard(TestCase):
    def setUp(self):
        self.repo = Board()

    def test_checkWin1(self):
        """
        We verify if the check works well when we have a diagonal
        """
        self.repo.makeMovePlayer(Point(0, 0, "O"))
        self.repo.makeMovePlayer(Point(1, 1, "O"))
        self.repo.makeMovePlayer(Point(2, 2, "O"))
        self.repo.makeMovePlayer(Point(3, 3, "O"))
        self.repo.makeMovePlayer(Point(4, 4, "O"))
        self.assertEqual(True, self.repo.checkWin())

    def test_checkWin2(self):
        """
        We verify if the check works well when we have 5 consecutive in a row
        """
        self.repo.Empty()
        self.repo.makeMovePlayer(Point(0, 0, "O"))
        self.repo.makeMovePlayer(Point(0, 1, "O"))
        self.repo.makeMovePlayer(Point(0, 2, "O"))
        self.repo.makeMovePlayer(Point(0, 3, "O"))
        self.repo.makeMovePlayer(Point(0, 4, "O"))
        self.assertEqual(True, self.repo.checkWin())

    def test_checkWin3(self):
        """
        We verify if the check works well when we have a diagonal
        """
        self.repo.Empty()
        self.repo.makeMovePlayer(Point(0, 5, "X"))
        self.repo.makeMovePlayer(Point(1, 4, "X"))
        self.repo.makeMovePlayer(Point(2, 3, "X"))
        self.repo.makeMovePlayer(Point(3, 2, "X"))
        self.repo.makeMovePlayer(Point(4, 1, "X"))
        self.assertEqual(True, self.repo.checkWin())

    def test_checkWin4(self):
        """
        We verify if the check works well when we have a 5 consecutive in a column
        """
        self.repo.Empty()
        self.repo.makeMovePlayer(Point(0, 0, "O"))
        self.repo.makeMovePlayer(Point(1, 0, "O"))
        self.repo.makeMovePlayer(Point(2, 0, "O"))
        self.repo.makeMovePlayer(Point(3, 0, "O"))
        self.repo.makeMovePlayer(Point(4, 0, "O"))
        self.assertEqual(True, self.repo.checkWin())

    def test_checkWin5(self):
        """
        We verify if the check works well when we don't have a win
        """
        self.repo.Empty()
        self.repo.makeMovePlayer(Point(0, 0, "O"))
        self.repo.makeMovePlayer(Point(0, 1, "O"))
        self.repo.makeMovePlayer(Point(0, 2, "O"))
        self.repo.makeMovePlayer(Point(0, 3, "O"))
        self.assertEqual(False, self.repo.checkWin())

    def test_checkWin6(self):
        """
        We verify if the check works well when we don't have a win
        """
        self.repo.Empty()
        self.repo.makeMovePlayer(Point(2, 0, "X"))
        self.repo.makeMovePlayer(Point(2, 1, "X"))
        self.repo.makeMovePlayer(Point(2, 2, "X"))
        self.repo.makeMovePlayer(Point(2, 3, "X"))
        self.repo.makeMovePlayer(Point(2, 4, "X"))
        self.assertEqual(True, self.repo.checkWin())

    def test_valide_move(self):
        self.assertEqual(self.repo.valide_move(1, 3), True)
        self.assertEqual(self.repo.valide_move(0, -1), False)
        self.assertEqual(self.repo.valide_move(5, 5), True)
        self.assertEqual(self.repo.valide_move(1, 0), True)
        self.repo.makeMovePlayer(Point(0, 3, "O"))
        self.assertEqual(self.repo.valide_move(0, 3), False)

    def test_makeMovePlayer(self):
        self.repo.makeMovePlayer(Point(0, 3, "O"))
        self.assertEqual(self.repo.valide_move(0, 3), False)

    def test_makeMoveAi(self):
        self.repo.makeMoveAi()
        self.repo.makeMovePlayer(Point(2, 1, "X"))
        self.repo.makeMovePlayer(Point(2, 2, "X"))
        self.repo.makeMovePlayer(Point(2, 3, "X"))
        self.repo.makeMoveAi()
        self.assertEqual(self.repo.checkWin(), False)
        self.repo.makeMovePlayer(Point(2, 4, "X"))
        self.repo.makeMoveAi()
        self.assertEqual(self.repo.checkWin(), True)
Exemple #20
0
from ai import AI
from domain import Board
from menu import UI
from service import BoardService

board = Board()
board_service = BoardService(board)
ai = AI(board)
ui = UI(board_service, ai)
ui.menu()
Exemple #21
0
 def __init__(self):
     self.__board = Board(6, 6).getBoard()
     # the next list represents the directions: up, down, right, left and every diagonal
     self.__directions = [(0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1)]
     self.__usedCells = []  # the list that keeps the occupied cells