def test_oversaturation_7x7_board_builds(): # The wall structure prevents more than two keys from being added # This behavior will likely change in the future to try lower saturation automatically board = build_board(side_length=7, num_seed_walls=10) assert get_side_length(board) == 7 assert ensure_walls_exist(board) assert count_keys(board) == 2
def write_html(): # see https://docs.python.org/3.4/library/cgi.html for the basic usage # here. form = cgi.FieldStorage() # connect to the database conn = MySQLdb.connect(host=pnsdp.SQL_HOST, user=pnsdp.SQL_USER, passwd=pnsdp.SQL_PASSWD, db=pnsdp.SQL_DB) if "game" not in form or "user" not in form: report_error("Invalid parameters.") return game = int(form["game"].value) (players, size, state) = get_game_info(conn, game) user = form["user"].value if user not in players: report_error("Sorry, the player '%s' is not part of this game." % user) return curUser = players.index(user) (board, nextToPlay, letter) = build_board(conn, game, size) # TODO: read this from the DB, later last = "1 Jan 1970" print("""<html> <head><title>346 - Russ Lewis - Tic-Tac-Toe</title></head> <body> <font size="+1"><b>Game %d:</b> %s (X) vs. %s (O)</font> <p><b>Size:</b> %dx%x <p><b>State:</b> %s <p><b>Next to Play:</b> %s <br><b>You Are:</b> %s """ % (game, players[0], players[1], size, size, state, players[nextToPlay], players[curUser]), end="") print("""<p> <form action="move.py" method="get"> <input type=hidden name="user" value="%s"> <input type=hidden name="game" value="%s"> <table border=2> """ % (players[nextToPlay], game), end="") for y in range(size): print(" <tr height=50 valign=center>") for x in range(size): if board[x][y] != "": content = board[x][y] elif curUser != nextToPlay or state != "Active": content = "" else: content = """<button type=submit name="pos" value="%d,%d" style="height:50px;width:50px"></button>""" % ( x, y) print(" <td width=50 align=center>" + content + "</td>") print(" </tr>") print("""</table> """, end="") if curUser == nextToPlay and state == "Active": print("""<input type=submit value="Resign" name="resign">\n\n""") print("</form>\n\n") if state == "Active": print("<p>Last activity: %s\n\n" % last, end="") print("""<p>HTTP Variables: <pre> """, end="") for k in form: print(" %s=%s" % (k, repr(form.getlist(k)[0]))) print(""" </pre> </body> </html> """, end="") conn.close()
def process_form(): # see https://docs.python.org/3.4/library/cgi.html for the basic usage # here. form = cgi.FieldStorage() # connect to the database conn = MySQLdb.connect(host = pnsdp.SQL_HOST, user = pnsdp.SQL_USER, passwd = pnsdp.SQL_PASSWD, db = pnsdp.SQL_DB) if "user" not in form or "game" not in form: raise FormError("Invalid parameters.") if "pos" not in form and "resign" not in form: raise FormError("Invalid parameters.") game = int(form["game"].value) (players,size,state) = get_game_info(conn, game) user = form["user"].value if user not in players: raise FormError("Invalid player ID - player is not part of this game") if "resign" in form: resign = True else: resign = False pos = form["pos"].value.split(",") assert len(pos) == 2 x = int(pos[0]) y = int(pos[1]) (board,nextPlayer,letter) = build_board(conn, game,size) if user != players[nextPlayer]: raise FormError("Internal error, incorrect player is attempting to move.") if resign: # this user is choosing to resign. Update the game state to reflect that. other_player_name = players[1-nextPlayer] cursor = conn.cursor() cursor.execute("""UPDATE games SET state=%s WHERE id=%s;""", (other_player_name+":resignation",game)) cursor.close() else: assert x >= 0 and x < size assert y >= 0 and y < size assert board[x][y] == "" board[x][y] = "XO"[nextPlayer] # we've done all of our sanity checks. We now know enough to say that # it's safe to add a new move. cursor = conn.cursor() cursor.execute("""INSERT INTO moves(gameID,x,y,letter,time) VALUES(%s,%s,%s,%s,NOW());""", (game,x,y,letter)) if cursor.rowcount != 1: raise FormError("Could not make move, reason unknown.") cursor.close() result = analyze_board(board) if result != "": if result == "win": result = players[nextPlayer]+":win" cursor = conn.cursor() cursor.execute("""UPDATE games SET state=%s WHERE id=%s;""", (result,game)) cursor.close() # we've made changes, make sure to commit them! conn.commit() conn.close() # return the parms to the caller, so that they can build a good redirect return (user,game)
def test_blank_15x15_board_has_correct_key_count(): board = build_board(side_length=15, num_seed_walls=0) assert get_side_length(board) == 15 assert count_keys(board) == 4
def test_create_15x15_board(): board = build_board(side_length=15, num_seed_walls=3) assert ensure_walls_exist(board) assert get_side_length(board) == 15
def test_create_blank_15x15_board(): board = build_board(side_length=15, num_seed_walls=0) assert ensure_no_walls(board) assert get_side_length(board) == 15
def test_saturated_7x7_board_all_keys_reachable(): board = build_board(side_length=7, num_seed_walls=3) assert get_side_length(board) == 7 assert count_keys(board) == 4 assert len(get_reachable_key_locations(board)) == 4
def test_saturated_7x7_board_has_clear_edges(): # Edges should always be clear in the game design to allow movement on perimeter of board board = build_board(side_length=7, num_seed_walls=3) assert get_side_length(board) == 7 assert count_keys(board) == 4 assert edges_clear(board)
def test_saturated_7x7_board_has_correct_key_count(): board = build_board(side_length=7, num_seed_walls=3) assert get_side_length(board) == 7 assert count_keys(board) == 4
def test_saturated_30x30_board_has_clear_edges(): board = build_board(side_length=30, num_seed_walls=5) assert get_side_length(board) == 30 assert count_keys(board) == 4 assert edges_clear(board)
def test_saturated_30x30_board_has_correct_key_count(): board = build_board(side_length=30, num_seed_walls=5) assert get_side_length(board) == 30 assert count_keys(board) == 4
def test_create_30x30_board(): board = build_board(side_length=30, num_seed_walls=3) assert ensure_walls_exist(board) assert get_side_length(board) == 30