Ejemplo n.º 1
0
    def setUp(self) -> None:
        self.game = battleship.Game(player_1_name='John', player_2_name='Jack')

        self.player_1 = self.game.player_1
        self.player_2 = self.game.player_2

        self.board_1 = self.player_1.board
        self.board_2 = self.player_2.board

        self.ship_1 = self.board_1.ship
        self.ship_2 = self.board_2.ship
Ejemplo n.º 2
0
 def get(self, game_id):
     print(game_id)
     game = games.get(game_id, False)
     if not game:
         game = bs.Game()
         games[game_id] = game
     x = request.args.get('x',False)
     y = request.args.get('y',False)
     if x and y:
         game.board[int(x)%game.n][int(y)%game.m].hit = True
     print(game)
     return {'board': game.to_json()}
Ejemplo n.º 3
0
def run_game(p1sock, p2sock):
    g = battleship.Game()
    while True:
        send_messages_to_player1()
        p1_move = p1sock.recv(BUFFER_SIZE)
        print("received move %s from player 1" % p1_move)
        p1_move = parse_move(p1_move)
        g.p1Input(p1_move)
        send_messages_to_player1()

        send_messages_to_player2()
        p2_move = p2sock.recv(BUFFER_SIZE)
        print("received move %s from player 1" % p2_move)
        p2_move = parse_move(p2_move)
        g.p2Input(p2_move)
        send_messages_to_player2()
Ejemplo n.º 4
0
def new_game():
    return battleship.Game()
Ejemplo n.º 5
0
def start_game():
    game = battleship.Game()
    # TODO: make arbitrary placements
    return game
Ejemplo n.º 6
0
import  battleship

game = battleship.Game()

END = False
game.NUM_SHIPS = 1
game.reset()

while not END:
    # player plays from command line:
    move = input('Player 1 please enter move x y orientation:')
    move = move.split()

    #send move to server as an array/string 
    #send(move)

    # the player process its move locally, and the server updates another copy remotely 
    game.p1Input([(int(move[0]), int(move[1])), move[2]])

    # or player 1 can process the game state and send it to server (which I don't recommend)
    # because it takes way lot more internet traffic
    print("this is the updated game board. \n")
    gameboard1 = game.p1Board 
    print(gameboard1)   
    print("or I can send this board over to the server. \n")

    # receives move from player2 => should become to wait for an array containing the result of 
    # recv(result_of_my_move) 
    # recv(move_player2)
    # update player2's hidden board on player1's end
   
Ejemplo n.º 7
0
import test_battleship
import battleship
import unittest
import time

if __name__ == "__main__":
  a = battleship.Game("Max")
  a.game()
Ejemplo n.º 8
0
def main():
    p1 = battleship.Player(GAME_HEIGHT, GAME_WIDTH)
    p2 = battleship.Player(GAME_HEIGHT, GAME_WIDTH)
    p3 = battleship.Player(GAME_HEIGHT, GAME_WIDTH)
    players = [p1, p2, p3]

    # p2 and p3 placed their ships on the same tiles, but p2's ship is bigger
    p1.add_ship(battleship.Cruiser(0, 0, is_vertical=False))
    p2.add_ship(battleship.Submarine(1, 0, is_vertical=True))
    p3.add_ship(battleship.Destroyer(1, 0, is_vertical=True))

    g = battleship.Game(players, GAME_HEIGHT, GAME_WIDTH)

    s1 = battleship.Shot(p1, 1, 0)  # Hit
    s2 = battleship.Shot(p2, 0, 0)  # Hit
    s3 = battleship.Shot(p3, 3, 3)  # Miss

    print()
    print('Round 1:')
    print()
    print_results(*g.process_shots([s1, s2, s3]))

    s1 = battleship.Shot(p1, 2, 2)  # Miss
    s2 = battleship.Shot(p2, 0, 1)  # Hit
    s3 = battleship.Shot(p3, 0, 2)  # Hit

    print()
    print('Round 2:')
    print()
    print_results(*g.process_shots([s1, s2, s3]))

    s2 = battleship.Shot(p2, 2, 0)  # Hit
    s3 = battleship.Shot(p3, 4, 4)  # Miss

    print()
    print('Round 3:')
    print()
    print_results(*g.process_shots([s2, s3]))

    print()
    print('Final public grid:')
    print()

    print_grid(g.get_public_grid())

    print()
    print('Final private grid:')
    print()

    print_grid(g.get_private_grid())

    print()
    print('Final player grids:')
    print()

    print_grid(p1.get_grid())
    print()
    print_grid(p2.get_grid())
    print()
    print_grid(p3.get_grid())
    print()