示例#1
0
def example_two_players_users():
    # Creating the ships MANUALLY for the 2 players Alice and Bob

    list_ships_player_alice = [
        Ship(coord_start=(3, 1), coord_end=(3, 5)),  # length = 5
        Ship(coord_start=(9, 7), coord_end=(9, 10)),  # length = 4
        Ship(coord_start=(1, 9), coord_end=(3, 9)),  # length = 3
        Ship(coord_start=(5, 2), coord_end=(6, 2)),  # length = 2
        Ship(coord_start=(8, 3), coord_end=(8, 3)),  # length = 1
    ]

    list_ships_player_bob = [
        Ship(coord_start=(5, 8), coord_end=(9, 8)),  # length = 5
        Ship(coord_start=(5, 4), coord_end=(8, 4)),  # length = 4
        Ship(coord_start=(3, 1), coord_end=(5, 1)),  # length = 3
        Ship.get_ship_from_str_coordinates(
            coord_str_start='F10',
            coord_str_end='G10'),  # Another way of creating a Ship
        Ship.get_ship_from_str_coordinates(
            coord_str_start='A4',
            coord_str_end='A4'),  # Another way of creating a Ship
    ]

    # Creating their boards
    board_player_alice = Board(list_ships_player_alice)
    board_player_bob = Board(list_ships_player_bob)

    # Creating the players
    player_alice = PlayerUser(board_player_alice, name_player="Alice")
    player_bob = PlayerUser(board_player_bob, name_player="Bob")

    # Creating and launching the game
    game = Game(player_1=player_alice, player_2=player_bob)

    game.play()
示例#2
0
def PlayerAutomatic_vs_PlayerAutomatic():
    player_a = PlayerAutomatic(name_player="MachineA")
    player_b = PlayerAutomatic(name_player="MachineB")

    game = Game(player_1=player_a, player_2=player_b)

    game.play()
示例#3
0
def example_automatic_user_vs_automatic_user():
    # board_player_jack = BoardAutomatic()
    # board_player_jill = BoardAutomatic()
    player_jack = PlayerAutomatic(name_player="Jack")
    player_jill = PlayerAutomatic(name_player="Jill")

    game = Game(player_1=player_jack, player_2=player_jill)

    game.play()
示例#4
0
def PlayerAutomatic_vs_PlayerRandom():
    player_alice = PlayerRandom(name_player="Alice")

    # Creating a Random Player Bob, its board is automatically created randomly
    player_bob = PlayerAutomatic(name_player="Cho'gath the destroyer")

    # Creating and launching the game
    game = Game(player_1=player_alice, player_2=player_bob)

    game.play()
示例#5
0
def main():
    running = True
    clock = pygame.time.Clock()

    initial1 = Initialize(WIN, True, 0)
    initial2 = Initialize(WIN, False, initial1.shipCount)

    p0ships = initial1.returnShip()
    p1ships = initial2.returnShip()

    game = Game(WIN, p0ships, p1ships)
    game.switch_players()
    while running:
        clock.tick(FPS)

        if game.game_over():
            winner(game)
            running = False

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                game.select(pos)

        game.update()

    pygame.quit()
示例#6
0
def test_no_winner():
    dims = (5, 5)
    s1 = {Ship(1, 1, 3, "v")}
    p1 = Player(Board(dims, s1))
    s2 = {Ship(1, 1, 3, "v")}
    p2 = Player(Board(dims, s2))
    game = Game(p1, p2, dims)

    result = game.get_winner()

    assert result is None
示例#7
0
def test_game_text():
    dims = (3, 3)
    s1 = {Ship(0, 0, 3, "v")}
    p1 = Player(Board(dims, s1))
    s2 = {Ship(0, 0, 3, "v")}
    p2 = Player(Board(dims, s2))
    game = Game(p1, p2, dims)

    p1_lines = game.p1_lines()
    p2_lines = game.p2_lines()

    assert list(p1_lines) == [["s", ".", "."], ["s", ".", "."], ["s", ".", "."]]
    assert list(p2_lines) == [["s", ".", "."], ["s", ".", "."], ["s", ".", "."]]
示例#8
0
def example_user_automatic_board_vs_full_automatic():
    # Creating the Board Automatically for the User (Alice)
    board_player_alice = BoardAutomatic()

    # Creating her player
    player_alice = PlayerUser(board_player_alice, name_player="Alice")

    # Creating a Random Player Bob, its board is automatically created randomly
    player_bob = PlayerRandom(name_player="Bob")

    # Creating and launching the game
    game = Game(player_1=player_alice, player_2=player_bob)

    game.play()
示例#9
0
def test_setup():
    game = Game('test_nick', 3)

    p1_board = [0, 0, 0, 1, 1, 1, 0, 0, 0]
    game.setup(p1_board)
    assert game.board == [0, 0, 0, 1, 1, 1, 0, 0, 0]

    p2_board = [0, 2, 0, 0, 2, 0, 0, 2, 0]
    game.connect_player2('test_nick2')
    game.setup(p2_board)
    assert game.board == [0, 2, 0, 1, 6, 1, 0, 2, 0]
    assert game.player1 == 'test_nick'
    assert game.player2 == 'test_nick2'
    assert game.round == 1 or game.round == 2
示例#10
0
def example_user_manual_board_vs_full_automatic():
    # Creating the ships MANUALLY for the User (Alice)

    list_ships_player_alice = [
        Ship(coord_start=(3, 1), coord_end=(3, 5)),  # length = 5
        Ship(coord_start=(9, 7), coord_end=(9, 10)),  # length = 4
        Ship(coord_start=(1, 9), coord_end=(3, 9)),  # length = 3
        Ship(coord_start=(5, 2), coord_end=(6, 2)),  # length = 2
        Ship(coord_start=(8, 3), coord_end=(8, 3)),  # length = 1
    ]

    # Creating her boards
    board_player_alice = Board(list_ships_player_alice)

    # Creating her player
    player_alice = PlayerUser(board_player_alice, name_player="Alice")

    # Creating a Random Player Bob, its board is automatically created randomly
    player_bob = PlayerRandom(name_player="Bob")

    # Creating and launching the game
    game = Game(player_1=player_alice, player_2=player_bob)

    game.play()
示例#11
0
def test_p2_winner():
    dims = (5, 5)
    s1 = {Ship(1, 1, 3, "v")}
    p1 = Player(Board(dims, s1))
    s2 = {Ship(1, 1, 3, "v")}
    p2 = Player(Board(dims, s2))
    game = Game(p1, p2, dims)

    game.p2_move(1, 1)
    game.p2_move(1, 2)
    game.p2_move(1, 3)
    result = game.get_winner()

    assert result == 2
示例#12
0
from battleship.game import Game

if __name__ == "__main__":
    print("Hi")
    new_game = Game()

    new_game.play_game()

    
示例#13
0
文件: main.py 项目: mhadam/battleship
def print_game(p1_lines, p2_lines):
    print("Player 1:")
    for line in p1_lines:
        print("".join(line))
    print("Player 2:")
    for line in p2_lines:
        print("".join(line))


if __name__ == "__main__":
    dimensions = (10, 10)
    p1_board = Board(dimensions, {Ship(1, 1, 9, "h"), Ship(3, 5, 4, "v")})
    p1 = Player(p1_board)

    p2_board = Board(dimensions, {Ship(1, 2, 2, "v"), Ship(5, 6, 2, "h")})
    p2 = Player(p2_board)

    game = Game(p1, p2, dimensions)
    game.p1_move(3, 1)
    game.p1_move(3, 7)
    #
    game.p2_move(1, 1)
    # win
    game.p1_move(1, 2)
    game.p1_move(1, 3)
    game.p1_move(5, 6)
    game.p1_move(6, 6)
    print(f"winner: {game.get_winner()}")
    print_game(game.p1_lines(), game.p2_lines())
示例#14
0
from battleship import constants
from battleship.game import Game
from battleship.player import Player
from battleship.position import Position

alphabets = [chr(x) for x in range(ord('a'), ord('k'))]

player1 = Player('Player1', constants.GAME_PLAYER_TYPE_HUMAN)
player2 = Player('Player2', constants.GAME_PLAYER_TYPE_HUMAN)
player3 = Player('Computer', constants.GAME_PLAYER_TYPE_COMPUTER)

games = [Game('game1', player1, player2), Game('game2', player1, player3)]

current_game = games[0]
current_game.play()

print('Playing {}'.format(current_game.name))

while True:

    if current_game.state == constants.GAME_STATE_FINISHED:
        print('GAME FINISHED')
        break

    print('\n' + current_game.attacking_player.name + ':')

    command = input().lower()
    if command == 'change game':
        print('Enter name of game:', end=' ')
        game_name = input().lower()
        current_game = current_game.attacking_player.get_game(game_name)
示例#15
0
def test_shot():
    game = Game('test_nick', 3)
    game.board = [0, 2, 0, 1, 6, 1, 0, 2, 0]
    game.round = 1

    game.shot(0, 0)
    assert game.board == [5, 2, 0, 1, 6, 1, 0, 2, 0]
    assert game.round == 2

    game.shot(0, 1)
    assert game.board == [5, 2, 0, 3, 6, 1, 0, 2, 0]
    assert game.round == 2

    game.shot(2, 2)
    assert game.round == 1

    game.shot(1, 0)
    assert game.board == [5, 4, 0, 3, 6, 1, 0, 2, 5]
    assert game.round == 1
    assert game.won == None

    game.shot(2, 1)
    assert game.board == [5, 4, 0, 3, 6, 3, 0, 2, 5]
    assert game.won == 2
示例#16
0
def test_sanity_check():
    game = Game('test_nick', 12)
    assert type(game) is Game
示例#17
0
from battleship.game import Game

game = Game()
game.start()
示例#18
0
import sys

from battleship.game import Game
from battleship.grid import Grid
from battleship.ships import Ship

if __name__ == "__main__":
    is_debug = "--debug" in sys.argv
    try:
        # Initialize grid and the game
        if is_debug:
            print("Initializing game...")
        ships = [
            Ship(length=2, name="Patrol"),
            Ship(length=3, name="Submarine"),
            Ship(length=4, name="Battleship"),
            Ship(length=5, name="Carrier")
        ]
        grid = Grid(10, 10)
        game = Game(grid=grid, ships=ships)
        if is_debug:
            print("Game initialized...")
            game.print_grid()
        game.play()
    except KeyboardInterrupt:
        print("")
        exit()
示例#19
0
文件: run.py 项目: theherk/battleship
from battleship.game import Game


if __name__ == '__main__':
    game = Game()

    ship1 = ['j6', 'j7']
    ship2 = ['a1', 'a2', 'a3']
    ship3 = ['b1', 'b2', 'b3']
    ship4 = ['f4', 'e4', 'c4', 'd4']
    ship5 = ['d1', 'e1', 'f1', 'g1', 'h1']

    game.add_ship(ship1)
    game.add_ship(ship2)
    game.add_ship(ship3)
    game.add_ship(ship4)
    game.add_ship(ship5)

    for move in [l for ship in [ship1, ship2, ship3, ship4, ship5] for l in ship]:
        print(game.move(move))