Exemplo n.º 1
0
 def setUp(self):
     self.__board = Board()
     self.__repoHuman = RepoBoard()
     self.__repoComputer = RepoBoard()
     self.__validHuman = SquareValidator(self.__repoHuman)
     self.__validComputer = SquareValidator(self.__repoComputer)
     self.__controller = BoardController(self.__repoHuman,
                                         self.__repoComputer,
                                         self.__validHuman,
                                         self.__validComputer)
Exemplo n.º 2
0
class TestBoard(TestCase):
    def setUp(self):
        self.board = Board()

    def test_add(self):
        p = Point("A", 1, "#")
        self.board.add([p])
        self.assertEqual(self.board[1][1], "#")

    def test_setitem(self):
        self.board[1][1] = "!"
        self.assertEqual(self.board[1][1], "!")

    def test_all(self):
        self.assertEqual(self.board.all(),
                         [[" " for c in range(0, 9)] for l in range(0, 9)])

    def test_str(self):
        string = "0|1|2|3|4|5|6|7|8|\nA| | | | | | | | |\nB| | | | | | | | |\nC| | | | | | | | |\nD| | | | | | | | |\nE| | | | | | | | |\nF| | | | | | | | |\nG| | | | | | | | |\nH| | | | | | | | |\n"
        self.assertEqual(str(self.board), string)
Exemplo n.º 3
0
 def setUp(self):
     self.player_planes = Board()
     self.player_hits = Board()
     self.player = PlayerController(self.player_planes, self.player_hits)
Exemplo n.º 4
0
 def setUp(self):
     self.board = Board()
Exemplo n.º 5
0
class Tests(unittest.TestCase):
    def setUp(self):
        self.__board = Board()
        self.__repoHuman = RepoBoard()
        self.__repoComputer = RepoBoard()
        self.__validHuman = SquareValidator(self.__repoHuman)
        self.__validComputer = SquareValidator(self.__repoComputer)
        self.__controller = BoardController(self.__repoHuman,
                                            self.__repoComputer,
                                            self.__validHuman,
                                            self.__validComputer)

    def testBoard(self):
        self.__board.setSquare(3, "F", "15")
        self.assertEqual(self.__board.getElement(3, "F"), "15")
        self.__board.setSquare(7, "A", "test")
        self.assertEqual(self.__board.getElement(7, "A"), "test")
        self.__board.setSquare(4, "C", " 2, 3")
        self.assertEqual(self.__board.getElement(4, "C"), " 2, 3")

    def testPlaceElement(self):
        self.__repoHuman.placeElement(2, "B", 4, 3, "test")
        self.assertEqual(self.__repoHuman.findElement(2, "B"), "test")
        self.assertEqual(self.__repoHuman.findElement(2, "C"), "test")
        self.assertEqual(self.__repoHuman.findElement(2, "D"), "test")
        self.assertNotEqual(self.__repoHuman.findElement(2, "A"), "test")
        self.assertNotEqual(self.__repoHuman.findElement(2, "F"), "test")
        self.__repoComputer.replaceElement(7, "H", 1)
        self.assertEqual(self.__repoComputer.findElement(7, "H"), 1)
        self.__repoComputer.replaceElement(4, "E", 42)
        self.assertEqual(self.__repoComputer.findElement(4, "E"), 42)
        self.__repoComputer.replaceElement(1, "G", "python")
        self.assertEqual(self.__repoComputer.findElement(1, "G"), "python")

    def testScore(self):
        self.__repoHuman.placeElement(6, "B", 4, 0, "B")
        self.assertEqual(self.__repoHuman.findElement(5, "B"), "B")
        self.assertEqual(self.__repoHuman.getScore(5, "B"), 15)
        self.assertEqual(self.__repoHuman.findElement(7, "B"), 0)
        self.assertEqual(self.__repoHuman.getScore(3, "B"), 15)
        self.__repoHuman.placeElement(6, "B", 4, 0, "H")
        self.assertEqual(self.__repoHuman.evaluateBoard("human"), 4)
        self.__repoHuman.placeElement(8, "F", 3, 1, "H")
        self.__repoHuman.placeElement(3, "G", 2, 3, "H")
        self.assertEqual(self.__repoHuman.evaluateBoard("human"), -10)
        self.assertEqual(self.__repoHuman.evaluateBoard("computer"), 10)

    def testRepo(self):
        self.assertTrue(self.__repoHuman.gameOver())
        self.__repoHuman.placeElement(6, "B", 3, 3, "battleship")
        self.assertTrue(self.__repoHuman.gameOver())
        self.__repoHuman.placeElement(2, "F", 3, 2, "B")
        self.assertFalse(self.__repoHuman.gameOver())
        self.assertTrue(self.__repoHuman.sunkenShip("game")[0])
        self.assertFalse(self.__repoHuman.sunkenShip("B")[0])
        self.assertEqual(self.__repoHuman.sunkenShip("B")[1], "B")
        self.assertFalse(self.__repoHuman.isDraw())
        columns = ["A", "B", "C", "D", "E", "F", "G", "H"]
        for i in range(1, 9):
            for j in columns:
                self.__repoHuman.replaceElement(i, j, 3)

        self.assertTrue(self.__repoHuman.isDraw())
        self.assertEqual(self.__repoHuman.getTotalScore(), 0)
        self.__repoHuman.replaceElement(3, "A", "B")
        self.assertEqual(self.__repoHuman.getTotalScore(), 15)
        self.__repoHuman.replaceElement(5, "C", "D")
        self.assertEqual(self.__repoHuman.getTotalScore(), 30)

    def testValidations(self):
        try:
            self.__validHuman.validateOccupiedSquare(2, "B")
        except ValidError as ve:
            self.assertEqual("You cannot place your ship here\n", str(ve))
        self.__repoHuman.replaceElement(7, "G", "elem")
        try:
            self.__validHuman.validateOccupiedSquare(7, "G")
        except ValidError as re:
            self.assertEqual("You cannot place your ship here\n", str(re))

        self.assertFalse(self.__validHuman.validMove(12, "C"))
        self.assertFalse(self.__validHuman.validMove(-3, 12))
        self.assertFalse(self.__validHuman.validMove(3, 15))
        self.assertFalse(self.__validHuman.validMove(2, "column"))
        self.assertFalse(self.__validHuman.validMove(1, "S"))
        self.assertFalse(self.__validHuman.validMove("test", "D"))
        self.assertFalse(self.__validHuman.validMove(0, "F"))

        self.assertTrue(self.__validHuman.validCoordinates(2, "C"))
        self.assertFalse(self.__validHuman.validCoordinates("-17", "ships"))
        self.assertFalse(self.__validHuman.validCoordinates(0, 29))

        try:
            self.__validHuman.validPlace(0, 6, "F")
        except ValidError as v:
            self.assertEqual("...", str(v))
        try:
            self.__validHuman.validPlace("pam", 7, "C")
            self.assertEqual(False)
        except ValidError as v:
            self.assertEqual(str(v), "invalid operand type !\n")

        try:
            self.__validHuman.validPlace(0, "test", "D")
            self.assertEqual(False)
        except ValidError as v:
            self.assertEqual(str(v), "invalid operand type !\n")

        try:
            self.__validHuman.validPlace(15, 7, "D")
            self.assertEqual(False)
        except ValidError as v:
            self.assertEqual(str(v), "invalid direction\n")

        try:
            self.__validHuman.validPlace(3, -7, "A")
            self.assertEqual(False)
        except ValidError as v:
            self.assertEqual(str(v), "The required line is not on the board\n")

        try:
            self.__validHuman.validPlace(3, 4, "J")
            self.assertEqual(False)
        except ValidError as v:
            self.assertEqual(str(v),
                             "The required column is not on the board\n")

        self.__repoHuman.replaceElement(3, "H", "H")
        try:
            self.__validHuman.validHit(2, "G")
        except ValidError as v:
            self.assertEqual(str(v), "...")

        try:
            self.__validHuman.validHit(3, "H")
            self.assertEqual(False)
        except ValidError as v:
            self.assertEqual(str(v), "You have hit this square before")

        try:
            self.__validHuman.validDirection("pam")
            self.assertEqual(False)
        except ValidError as v:
            self.assertEqual(str(v), "invalid operand type !\n")

        try:
            self.__validHuman.validDirection(-31)
            self.assertEqual(False)
        except ValidError as v:
            self.assertEqual(str(v), "invalid direction\n")

        try:
            self.__validHuman.validDirection(4)
            self.assertEqual(False)
        except ValidError as v:
            self.assertEqual(str(v), "invalid direction\n")

        try:
            self.__validHuman.validDirection(3)
        except ValidError as v:
            self.assertEqual(str(v), "...")

    def testController(self):
        self.__controller.placeShip(6, "B", 4, "0", "B", "human")
        self.assertEqual(self.__controller.searchElement(5, "B"), "B")
        self.assertNotEqual(self.__controller.searchElement(7, "B"), "B")
        self.__controller.placeShip(8, "F", 3, 1, "C", "human")
        self.assertEqual(self.__controller.searchElement(8, "E"), "C")
        self.assertEqual(self.__controller.searchElement(7, "F"), 0)
        self.assertFalse(self.__controller.final("human"))
        self.assertTrue(self.__controller.final("computer"))
        self.__controller.hitHuman(7, "D")
        self.assertEqual(self.__controller.searchHit(7, "D"), "M")
        self.__repoComputer.replaceElement(8, "A", "B")
        self.__controller.hitHuman(8, "A")
        self.assertEqual(self.__controller.searchHit(8, "A"), "H")
Exemplo n.º 6
0
from controller.bigboss import Boss
from controller.controller import PlayerController
from domain.entities import Board
from ui.UI import UI

player_planes = Board()
player_hits = Board()
player = PlayerController(player_planes, player_hits)
computer_planes = Board()
computer_hits = Board()
computer = PlayerController(computer_planes, computer_hits)
controller = Boss(player, computer)
ui = UI(controller)
ui.run()
# player.new_plane(Point("E", 4, "W"))
# player.new_plane(Point("A", 3, "N"))
Exemplo n.º 7
0
 def __init__(self):
     self.__board = Board()
     self.__validate = SquareValidator(self.__board)
Exemplo n.º 8
0
class RepoBoard(object):
    def __init__(self):
        self.__board = Board()
        self.__validate = SquareValidator(self.__board)

    def placeElement(self, x, y, dimension, direction, value):
        '''
        Function that places a ship on a given position
        Input: x, y - the coordinates
               dimension - the dimension of the ship
               direction - the direction of the ship
               value - the type of the ship
        Precondition: x, dimension - intigers
                      y, direction, value - strings
        Output: -
        Postcondition: -
        '''
        x = int(x)
        if str(direction) == "0":
            for i in range(dimension):
                self.__board.setSquare(x - i, y, value)
        elif str(direction) == "1":
            for i in range(dimension):
                elem = ord(y) - i
                self.__board.setSquare(x, chr(elem), value)
        else:
            if str(direction) == "2":
                for i in range(dimension):
                    self.__board.setSquare(x + i, y, value)
            elif str(direction) == "3":
                for i in range(dimension):
                    elem = ord(y) + i
                    self.__board.setSquare(x, chr(elem), value)

    def replaceElement(self, x, y, value):
        '''
        Function that replaces an element 
        Input: x, y - the coordinates
               value - the new value of the element
        Precondition: x - intiger
                      y, value - strings
        Output: -
        Postcondition: -
        '''
        self.__board.setSquare(x, y, value)

    def findElement(self, x, y):
        '''
        Function that returns the value of an element given by its coordinates
        Input: x, y - the coordinates
        Precondition: x - intiger
                      y - string
        Output: -
        Postcondition: -
        '''
        return self.__board.getElement(x, y)

    def printElements(self, command):
        if command == "human":
            print(str(self.__board))
        else:
            print(self.__board.computerBoard())

    def getScore(self, x, y):
        '''
        Function that returns the score of a given square
        Input: x, y - the coordinates
        Precondition: x - intiger
                      y - string
        Output: re - intiger
        Postcondition: re = 15 if the square is a hit
                          = 0  otherwise
        '''
        list = ["B", "C", "D", "H"]
        cnt = 0
        x = int(x)
        if self.__validate.validCoordinates(
                x, y) == True and self.__board.getElement(x, y) in list:
            return 15
        return 5
        '''
        for i in range(-3, 4):
            if self.__validate.validCoordinates(x + i, y) == True and self.__board.getElement(x + i, y) in list:
                cnt += 1

        for i in range(-3, 4):
            if self.__validate.validCoordinates(x, chr(ord(y) + i)) == True and self.__board.getElement(x, chr(ord(y) + i)) in list:
                cnt += 1
        return cnt
        '''

    def evaluateBoard(self, player):
        '''
        Function that evaluates the value of the board(the number of hits)
        Input: player - the player
        Precondition: player - string
        Output: re - intiger
        Postcondition: re = -10 if the human has won
                          = 10 if the computer has won
                          = score otherwise
        '''
        score = 0
        columns = ["A", "B", "C", "D", "E", "F", "G", "H"]
        for i in range(1, 9):
            for j in columns:
                if self.__board.getElement(i, j) == "H":
                    score += 1
        if score == 9:
            if player == "human":
                return -10
            return 10
        return score

    def gameOver(self):
        '''
        Function that checks if the game is over or not
        Input: -
        Precondition: -
        Output: re - boolean
        Postcondition: re = True if the game is over
                          = False otherwise
        '''
        columns = ["A", "B", "C", "D", "E", "F", "G", "H"]
        cnt = 0
        for i in range(1, 9):
            for j in columns:
                if (self.__board.getElement(i, j) == "B"
                        or self.__board.getElement(i, j)
                        == "C") or self.__board.getElement(i, j) == "D":
                    cnt += 1
        if cnt > 0:
            return False
        return True

    def sunkenShip(self, lastEliminated):
        '''
        Function that checks if the given player has sunk one of his oponent's ships
        Input: lastEliminated - the last eliminated element
        Precondition: lastEliminated - string
        Output: (re, el) - touple
        Postcondition: re - true if the ship was sunken and false otherwise
                       el - the last eliminated element
        '''
        columns = ["A", "B", "C", "D", "E", "F", "G", "H"]
        cnt = 0
        for i in range(1, 9):
            for j in columns:
                if self.__board.getElement(i, j) == lastEliminated:
                    cnt += 1

        if cnt > 0:
            return (False, lastEliminated)
        return (True, lastEliminated)

    def isDraw(self):
        columns = ["A", "B", "C", "D", "E", "F", "G", "H"]
        for i in range(1, 8):
            for j in columns:
                if str(self.__board.getElement(i, j)) == "0":
                    return False
        return True

    def getTotalScore(self):
        columns = ["A", "B", "C", "D", "E", "F", "G", "H"]
        score = 0
        for i in range(1, 9):
            for j in columns:
                score += int(self.getScore(i, j))
        return score

    def getAllMoves(self):
        moves = []
        columns = ["A", "B", "C", "D", "E", "F", "G", "H"]
        for i in range(1, 9):
            for j in columns:
                if self.__board.getElement(
                        i, j) != "H" and self.__board.getElement(i, j) != "M":
                    moves.append([i, j])
        return moves