def check(board, game):
    #check only double ships in every row
    for row in board:
        row = "".join(str(x) for x in row)
        if row.count(str(misc.SHIP)*3) > 0:
            misc.fail("3 long ships (or two ships touching horizontally) "+str(board))
        if len(row) != game.board_size:
            misc.fail("Incorrect row length")
    #check no touching in any column
    for x in range(game.board_size):
        column = "".join(str(row[x]) for row in board)
        if column.count(str(misc.SHIP)*2) > 0:
            misc.fail("Ships touching vertically")
    #check number of ships correct
    board_string = "".join("".join(str(x) for x in row) for row in board)
    if board_string.count(str(misc.SHIP)*2) != game.num_ships:
        misc.fail("Incorrect number of ships")
        return False
    return True
Exemple #2
0
import cgitb
import cgi

import misc

cgitb.enable()

fields = cgi.FieldStorage()

name, hash, gameID, x, y = (fields.getvalue("name"), fields.getvalue("hash"),
                            fields.getvalue("gameID"), fields.getvalue("x"),
                            fields.getvalue("y"))

if None in {name, hash, gameID, x, y}:
    misc.fail("Missing parameters, one of (name, hash, x, y}")

try:
    x = int(x)
    y = int(y)
except ValueError:
    misc.fail("coordinates must be integers")

games = misc.load_games()
game = misc.find_game(games, gameID)
player = misc.validate_player(game, name, hash)
opponent = 2 if player == 1 else 1
board = misc.get_boards(gameID)[opponent - 1]

if player is None:
    misc.fail("Invalid name/hash/gameID")
Exemple #3
0
import misc
from misc.Game import Game
cgitb.enable()

fields = cgi.FieldStorage()

name = fields.getvalue("name")
hash = misc.make_hash()
num_ships = fields.getvalue("num_ships")
board_size = fields.getvalue("board_size")

try:
    num_ships = int(num_ships)
    board_size = int(board_size)
except ValueError:
    misc.fail("Required parameters board_size and num_ships must be integers")

if None in {name, hash, num_ships, board_size}:
    misc.fail("Missing parameters, one of (name, hash, num_ships, board_size")

if not 3 <= board_size <= 10 or not 1 <= num_ships < board_size:
    misc.fail("Invalid parameters, board_size must be between 3 and 10, "
              "num_ships must be at least 1 and less than board_size")

games = misc.load_games()

id = games[-1].gameID + 1 if len(games) > 0 else 1
new_game = Game(
    dict(gameID=id,
         player_1=name,
         player_1_hash=hash,
    for i, v in enumerate(ships):
        y = i // game.board_size
        x = i % game.board_size
        if v == "2":
            board[y][x] *= misc.SHIP
    return board

fields = cgi.FieldStorage()

name, hash, gameID, ships = (fields.getvalue("name",None),
                             fields.getvalue("hash",None),
                             fields.getvalue("gameID",None),
                             fields.getvalue("ships",None))

if None in {name, hash, gameID, ships}:
    misc.fail("Missing parameters, one of (name, hash, gameID, ships}")

games = misc.load_games()
game = misc.find_game(games, gameID)
player = misc.validate_player(game, name, hash)

if player is None:
    misc.fail("Invalid name/hash/gameID")

board = make_board(game.board_size, ships)


board_checker.check(board, game)
past_layout_phase = game.game_state != "L" and game.game_state != "W"
waiting_for_opponent_only = player == 1 and game.waiting_for == 2 or game.waiting_for == 1 and player == 2
if waiting_for_opponent_only or past_layout_phase:
import cgitb
import cgi

import misc
cgitb.enable()


fields = cgi.FieldStorage()

name = fields.getvalue("name")
hash = fields.getvalue("hash")
gameID = fields.getvalue("gameID")

if None in {name, hash, gameID}:
    misc.fail("Missing parameters, one of (name, hash, gameID")

games = misc.load_games()
found = misc.find_game(games, gameID)

if found is None:
    misc.fail("No game by that ID found")
if found.player_1 == name and found.player_1_hash == hash:
    found.game_state = "X-1"
elif found.player_2 == name and found.player_2_hash == hash:
    found.game_state = "X-2"
else:
    misc.fail("name or hash incorrect for given game")

misc.save_games(games)
misc.succeed("Left game")
def obscure(board2):
    return [
        [obscureCell(cell) for cell in line]
        for line in board2
        ]

cgitb.enable()

fields = cgi.FieldStorage()

name, hash, gameID = (fields.getvalue("name"),
                      fields.getvalue("hash"),
                      fields.getvalue("gameID"))
if None in {name, hash, gameID}:
    misc.fail("Missing fields, one of (name, hash, gameID)")

game = misc.find_game(misc.load_games(), gameID)
if game is None:
    misc.fail("No game found")
validated_player_id = misc.validate_player(game, name, hash)
try:
    board1, board2 = misc.get_boards(gameID)
    your_board = board1 if validated_player_id == 1 else board2
    opponent_board = obscure(board2 if validated_player_id == 1 else board1)
except:
    your_board, opponent_board = [[]], [[]]

misc.succeed("Game state loaded", game=game.to_personalized_dict(validated_player_id), your_board=your_board, opponent_board=opponent_board, validated_player_id=validated_player_id)

Exemple #7
0
import cgitb
import cgi
import misc
from misc.Game import Game

cgitb.enable()


fields = cgi.FieldStorage()
gameID = fields.getvalue("gameID")
hash = misc.make_hash()
name = fields.getvalue("name")

games = misc.load_games()
found = misc.find_game(games, gameID)

if None in {gameID, hash, name}:
    misc.fail("Missing parameter, one of gameID, hash, name")
if found is None:
    misc.fail("No game with that ID found")
if found.game_state != "W":
    misc.fail("That game is no longer open")

found.player_2 = name
found.player_2_hash = hash
found.game_state = "L"

misc.save_games(games)

misc.succeed("Joined game", **found.to_personalized_dict(2))