Exemplo n.º 1
0
def real_nextturn():
    """Hidden method to really move on."""
    global phase, selected
    
    if selected is not None:
        riskgui.drawterritory(selected, 0)
        selected = None
    
    if currentplayer.conqueredTerritory == 1:
        if len(allcards) != 0: #too bad otherwise
            handcard = random.choice(allcards)
            currentplayer.cards.append(handcard)
            allcards.remove(handcard)
        currentplayer.conqueredTerritory = 0
        
    rotateplayers()
    riskgui.set_status("=== %s's Turn ===" % currentplayer.name)
    
    currentplayer.freeArmies += len(currentplayer.territories()) // 3
    for c in continents.values():
        existing = [t for t in territories.values() if t.continent == c]
        unowned = [t for t in existing if t.player != currentplayer]
        if len(existing) != 0 and len(unowned) == 0 :
            currentplayer.freeArmies += c.value
    
    riskgui.set_status("[%s] armies from territories: %d" % (currentplayer.name, currentplayer.freeArmies))
    riskgui.set_armies(currentplayer.freeArmies)
    
    setphase("Place")
Exemplo n.º 2
0
def load_game(filename):
    """Load a previously-saved game."""
    global currentplayer, phase, currentplayernum
    if phase != "Pregame":
        riskgui.set_status("You have already started a game")
        return
    
    savefile = open(filename)
    while savefile.readline() == "player\n":
        plname = savefile.readline().strip()
        plarmies = int(savefile.readline().strip())
        plconquered = int(savefile.readline().strip())
        cards = []
        currentline = savefile.readline()
        while currentline != "endplayer\n" and currentline != "ai\n":
            nline = savefile.readline().strip()
            cards.append(Card(currentline.strip(),nline))
            currentline = savefile.readline()
        ainame = None
        if currentline == "ai\n":
            ainame = savefile.readline().strip()
            aidatalen = int(savefile.readline().strip())
            aidata = savefile.read(aidatalen)
            savefile.readline()
        if not players.has_key(plname):
            if ainame and not os.path.exists(ainame):
                riskgui.set_status("AI " + ainame + " nonexistent")
                ainame = None
            riskgui.makeplayercolors(makeplayer(plname, ainame))
            if ainame:
                players[plname].ai.loaddata(aidata)
        players[plname].freeArmies = plarmies
        players[plname].conqueredTerritory = plconquered
        players[plname].cards = cards
        playerorder.append(players[plname])
    riskgui.relistplayers(playerorder)
    currentplayer = players[savefile.readline().strip()]
    currentplayernum = playerorder.index(currentplayer)
    riskgui.set_armies(currentplayer.freeArmies)
    riskgui.playersturn(currentplayer.name)
    setphase(savefile.readline().strip())
    
    while savefile.readline().strip() == "territory":
        tname = savefile.readline().strip()
        tplayer = savefile.readline().strip()
        if tplayer == "None":
            tplayer = None
        else:
            tplayer = players[tplayer]
        territories[tname].player = tplayer
        territories[tname].armies = int(savefile.readline().strip())
        riskgui.drawterritory(territories[tname], 0)
        savefile.readline()
    
    while 1:
        cardterr = savefile.readline().strip()
        if not cardterr:
            break
        allcards.append(Card(cardterr,savefile.readline().strip()))
    savefile.close()
Exemplo n.º 3
0
def startgame():
    """Start off the game."""
    global playerorder, currentplayer
    
    if phase != "Pregame":
        return False
    if len(players) < 2:
        riskgui.set_status("Not enough players")
        return False
    if len(players) > 6:
        riskgui.set_status("Too many players - 6 maximum")
        return False
    
    setphase("Preposition")
    
    createcards()
    for pl in players.values():
        pl.freeArmies = 50 - 5 * len(players)
    playerorder = players.values()[:]
    random.shuffle(playerorder)
    currentplayer = playerorder[0]
    riskgui.relistplayers(playerorder)
    riskgui.playersturn(currentplayer.name)
    riskgui.set_armies(currentplayer.freeArmies)
    
    while currentplayer.ai is not None and currentplayer.freeArmies > 0:
        currentplayer.ai.run_preplace(currentplayer)
        rotateplayers()
        riskgui.set_armies(currentplayer.freeArmies)
    
    if currentplayer.ai is not None:
        resetturn()
        nextturn()
    
    return True
Exemplo n.º 4
0
def real_nextturn():
    """Hidden method to really move on."""
    global phase, selected
    
    if selected is not None:
        riskgui.drawterritory(selected, 0)
        selected = None
    
    if currentplayer.conqueredTerritory == 1:
        if len(allcards) != 0: #too bad otherwise
            handcard = random.choice(allcards)
            currentplayer.cards.append(handcard)
            risknetwork.add_card(handcard, currentplayer)
            allcards.remove(handcard)
        currentplayer.conqueredTerritory = 0
        
    rotateplayers()
    riskgui.set_status("It is now " + currentplayer.name + "'s turn.")
    
    currentplayer.freeArmies += max(3, len(currentplayer.territories()) // 3)
    for c in continents:
        existing = [x for x in territories.values() if x.continent == c[0]]
        unowned = [x for x in existing if x.player != currentplayer]
        if len(existing) != 0 and len(unowned) == 0 :
            currentplayer.freeArmies += c[1]
    riskgui.set_armies(currentplayer.freeArmies)            
            
    setphase("Place")
Exemplo n.º 5
0
def turnincards(player, cards):
    """Turn in a set of cards."""
    if phase != "Place":
        return
    
    for c in cards:
        if not c in player.cards:
            return
    
    cards.sort(lambda a, b: cmp(a.picture, b.picture))
    
    card_set = CardSet(cards)
    value = card_set.value()
    if value is None: return
    
    for c in cards:
        if territories[c.territory].player == player:
            value += 2
    
    player.freeArmies += value
    
    for c in cards:
        allcards.append(c)
        for ca in player.cards:
            if c == ca:
                player.cards.remove(ca)
    
    riskgui.set_status("[%s] Card Set: %d" % (player.name, value))
    riskgui.set_armies(currentplayer.freeArmies)
Exemplo n.º 6
0
def real_nextturn():
    """Hidden method to really move on."""
    global phase, selected

    if selected is not None:
        riskgui.drawterritory(selected, 0)
        selected = None

    if currentplayer.conqueredTerritory == 1:
        if len(allcards) != 0:  #too bad otherwise
            handcard = random.choice(allcards)
            currentplayer.cards.append(handcard)
            risknetwork.add_card(handcard, currentplayer)
            allcards.remove(handcard)
        currentplayer.conqueredTerritory = 0

    rotateplayers()
    riskgui.set_status("It is now " + currentplayer.name + "'s turn.")

    currentplayer.freeArmies += max(3, len(currentplayer.territories()) // 3)
    for c in continents:
        existing = [x for x in territories.values() if x.continent == c[0]]
        unowned = [x for x in existing if x.player != currentplayer]
        if len(existing) != 0 and len(unowned) == 0:
            currentplayer.freeArmies += c[1]
    riskgui.set_armies(currentplayer.freeArmies)

    setphase("Place")
Exemplo n.º 7
0
def client_receive_message(mess, sock):
    """Run when the client receives a message."""
    global clplayer, am_client
    print >>riskengine.verbosefile, mess, "client received"
    parts = splitparms(mess)
    if parts[0] == "draw_army" or parts[0] == "draw_territory":
        terr = riskengine.territories[parts[1]]
        terr.armies = int(parts[2])
        terr.player = riskengine.players[parts[3]]
        if parts[0] == "draw_army":
            riskgui.drawarmy(terr)
        else:
            riskgui.drawterritory(terr, int(parts[4]))
    if parts[0] == "set_armies":
        riskgui.set_armies(int(parts[1]))
    if parts[0] == "won_game":
        riskgui.won_game(parts[1])
    if parts[0] == "playersturn":
        riskgui.playersturn(parts[1])
    if parts[0] == "set_status":
        riskgui.set_status(parts[1])
    if parts[0] == "newplayer":
        riskgui.net_newplayer(parts[1])
    if parts[0] == "removeplayer":
        riskgui.removeplayer(parts[1])   
    if parts[0] == "relistplayers":
        riskgui.relistplayers([riskengine.players[p] for p in parts[1:]])
    if parts[0] == "set_phase":
        riskengine.setphase(parts[1])
    if parts[0] == "add_card":
        riskengine.players[parts[3]].cards.append(riskengine.Card(parts[2], parts[1]))
    if parts[0] == "remove_card":
        foundcard = 0
        for n in riskengine.players[parts[3]].cards:
            if n.territory == parts[2]:
                riskengine.players[parts[3]].cards.remove(n)
                foundcard = 1
                break
        if foundcard == 0:
            print >>riskengine.verbosefile, "Couldn't find card"
            print >>riskengine.verbosefile, "players' cards:", riskengine.players[parts[3]].cards
        #riskengine.players[parts[3]].cards.remove(riskengine.card(parts[2], parts[1]))
    if parts[0] == "message":
        riskgui.set_status(parts[2] + " sent message: " + parts[1])
    if parts[0] == "leaving":
        riskgui.show_status_message("The server has left.")
        serv_sock.close()
        am_client = 0
Exemplo n.º 8
0
def client_receive_message(mess, sock):
    """Run when the client receives a message."""
    global clplayer, am_client
    print >>riskengine.verbosefile, mess, "client received"
    parts = splitparms(mess)
    if parts[0] == "draw_army" or parts[0] == "draw_territory":
        terr = riskengine.territories[parts[1]]
        terr.armies = int(parts[2])
        terr.player = riskengine.players[parts[3]]
        if parts[0] == "draw_army":
            riskgui.drawarmy(terr)
        else:
            riskgui.drawterritory(terr, int(parts[4]))
    if parts[0] == "set_armies":
        riskgui.set_armies(int(parts[1]))
    if parts[0] == "won_game":
        riskgui.won_game(parts[1])
    if parts[0] == "playersturn":
        riskgui.playersturn(parts[1])
    if parts[0] == "set_status":
        riskgui.set_status(parts[1])
    if parts[0] == "newplayer":
        riskgui.net_newplayer(parts[1])
    if parts[0] == "removeplayer":
        riskgui.removeplayer(parts[1])
    if parts[0] == "relistplayers":
        riskgui.relistplayers([riskengine.players[p] for p in parts[1:]])
    if parts[0] == "set_phase":
        riskengine.setphase(parts[1])
    if parts[0] == "add_card":
        riskengine.players[parts[3]].cards.append(riskengine.Card(parts[2], parts[1]))
    if parts[0] == "remove_card":
        foundcard = 0
        for n in riskengine.players[parts[3]].cards:
            if n.territory == parts[2]:
                riskengine.players[parts[3]].cards.remove(n)
                foundcard = 1
                break
        if foundcard == 0:
            print >>riskengine.verbosefile, "Couldn't find card"
            print >>riskengine.verbosefile, "players' cards:", riskengine.players[parts[3]].cards
        # riskengine.players[parts[3]].cards.remove(riskengine.card(parts[2], parts[1]))
    if parts[0] == "message":
        riskgui.set_status(parts[2] + " sent message: " + parts[1])
    if parts[0] == "leaving":
        riskgui.show_status_message("The server has left.")
        serv_sock.close()
        am_client = 0
Exemplo n.º 9
0
def handle_place(terr, button):
    """Handle the placing of the armies before a turn."""
    if terr.player != currentplayer:
        return 1
    if currentplayer.freeArmies > 0:
        if button == 3:
            currentplayer.place_army(terr, min(5, currentplayer.freeArmies))
        else:
            currentplayer.place_army(terr)
        riskgui.drawarmy(terr)
        riskgui.set_armies(currentplayer.freeArmies)
        return 1
    else:
        setphase("Attack")
        print >>verbosefile, "Attacking"
        return 0
Exemplo n.º 10
0
def handle_place(terr, button):
    """Handle the placing of the armies before a turn."""
    if terr.player != currentplayer:
        return 1
    if currentplayer.freeArmies > 0:
        if button == 3:
            currentplayer.place_army(terr, min(5, currentplayer.freeArmies))
        else:
            currentplayer.place_army(terr)
        riskgui.drawarmy(terr)
        riskgui.set_armies(currentplayer.freeArmies)
        return 1
    else:
        if len(currentplayer.cards) >= 5:
            riskgui.set_status("You must turn in cards to attack")
            return 1
        riskgui.set_status("Moving to Attack")
        setphase("Attack")
        print >> verbosefile, "Attacking"
        return 0
Exemplo n.º 11
0
def handle_place(terr, button):
    """Handle the placing of the armies before a turn."""
    if terr.player != currentplayer:
        return 1
    if currentplayer.freeArmies > 0:
        if button == 3:
            currentplayer.place_army(terr, min(5, currentplayer.freeArmies))
        else:
            currentplayer.place_army(terr)
        riskgui.drawarmy(terr)
        riskgui.set_armies(currentplayer.freeArmies)
        return 1
    else:
        if len(currentplayer.cards) >= 5:
            riskgui.set_status("You must turn in cards to attack")
            return 1
        riskgui.set_status("Moving to Attack")
        setphase("Attack")
        print >>verbosefile, "Attacking"
        return 0
Exemplo n.º 12
0
def startgame():
    """Start off the game."""
    global playerorder, currentplayer
    if risknetwork.am_client:
        riskgui.show_status_message("You aren't the server; you can't do that.")
        return False
        
    if phase != "Pregame":
        return False
    if len(players) < 2:
        riskgui.set_status("Not enough players")
        return False
    if len(players) > 7:
        riskgui.set_status("Too many players - 7 maximum")
        return False
    
    
    setphase("Preposition")
    
    createcards()
    for pl in players.values():
        pl.freeArmies = 45 - 5 * len(players)
    playerorder = players.values()[:]
    random.shuffle(playerorder)
    currentplayer = playerorder[0]
    riskgui.relistplayers(playerorder)
    riskgui.playersturn(currentplayer.name)
    riskgui.set_armies(currentplayer.freeArmies)
    
    
    while currentplayer.ai is not None and currentplayer.freeArmies > 0:
        currentplayer.ai.run_preplace(currentplayer)
        rotateplayers()  
        riskgui.set_armies(currentplayer.freeArmies)
    if currentplayer.ai is not None:
        resetturn()
        nextturn()
    return True
Exemplo n.º 13
0
def turnincards(player, cards):
    """Turn in a set of cards."""
    global currentcard
    if risknetwork.turnincards(cards):
        return
    
    if phase != "Place":
        return
        
    for c in cards:
        if not c in player.cards:
            return
            
    cards.sort(lambda a, b:cmp(a.picture, b.picture))
    
    if not cardset(cards):
        return
        
    for c in cards:
        if territories[c.territory].player == player:
            territories[c.territory].armies += 2
            riskgui.drawarmy(territories[c.territory])
        
    if currentcard < len(cardvals):
        player.freeArmies += cardvals[currentcard]
    else:
        player.freeArmies += cardvals[-1] + \
                             incrementval * (currentcard - len(cardvals) + 1)
    currentcard += 1
    
    
    for c in cards:
        allcards.append(c)
        for ca in player.cards:
            if c == ca:
                player.cards.remove(ca)
                risknetwork.remove_card(ca, currentplayer)
    riskgui.set_armies(currentplayer.freeArmies)  
Exemplo n.º 14
0
def startgame():
    """Start off the game."""
    global playerorder, currentplayer
    if risknetwork.am_client:
        riskgui.show_status_message(
            "You aren't the server; you can't do that.")
        return False

    if phase != "Pregame":
        return False
    if len(players) < 2:
        riskgui.set_status("Not enough players")
        return False
    if len(players) > 7:
        riskgui.set_status("Too many players - 7 maximum")
        return False

    setphase("Preposition")

    createcards()
    for pl in players.values():
        pl.freeArmies = 45 - 5 * len(players)
    playerorder = players.values()[:]
    random.shuffle(playerorder)
    currentplayer = playerorder[0]
    riskgui.relistplayers(playerorder)
    riskgui.playersturn(currentplayer.name)
    riskgui.set_armies(currentplayer.freeArmies)

    while currentplayer.ai is not None and currentplayer.freeArmies > 0:
        currentplayer.ai.run_preplace(currentplayer)
        rotateplayers()
        riskgui.set_armies(currentplayer.freeArmies)
    if currentplayer.ai is not None:
        resetturn()
        nextturn()
    return True
Exemplo n.º 15
0
def turnincards(player, cards):
    """Turn in a set of cards."""
    global currentcard
    if risknetwork.turnincards(cards):
        return

    if phase != "Place":
        return

    for c in cards:
        if not c in player.cards:
            return

    cards.sort(lambda a, b: cmp(a.picture, b.picture))

    if not cardset(cards):
        return

    for c in cards:
        if territories[c.territory].player == player:
            territories[c.territory].armies += 2
            riskgui.drawarmy(territories[c.territory])

    if currentcard < len(cardvals):
        player.freeArmies += cardvals[currentcard]
    else:
        player.freeArmies += cardvals[-1] + \
                             incrementval * (currentcard - len(cardvals) + 1)
    currentcard += 1

    for c in cards:
        allcards.append(c)
        for ca in player.cards:
            if c == ca:
                player.cards.remove(ca)
                risknetwork.remove_card(ca, currentplayer)
    riskgui.set_armies(currentplayer.freeArmies)
Exemplo n.º 16
0
def handle_preposition(terr, button):
    """Handle the preposition phase."""
    if currentplayer.freeArmies == 0:
        resetturn()
        return

    emptyTerritories = [x for x in territories.values() if x.armies == 0]
    if (emptyTerritories and terr.player is None) or \
            (not emptyTerritories and terr.player == currentplayer):
        terr.player = currentplayer
        currentplayer.place_army(terr)
        riskgui.drawterritory(terr, 0)
    else:
        return

    rotateplayers()
    riskgui.set_armies(currentplayer.freeArmies)
    while currentplayer.ai is not None:
        currentplayer.ai.run_preplace(currentplayer)
        rotateplayers()
        riskgui.set_armies(currentplayer.freeArmies)
        if currentplayer.freeArmies == 0:
            resetturn()
            return
Exemplo n.º 17
0
def handle_preposition(terr, button):
    """Handle the preposition phase."""
    if currentplayer.freeArmies == 0:
        resetturn()
        return
    
    emptyTerritories = [x for x in territories.values() if x.armies == 0] 
    if (emptyTerritories and terr.player is None) or \
            (not emptyTerritories and terr.player == currentplayer):
        terr.player = currentplayer
        currentplayer.place_army(terr)
        riskgui.drawterritory(terr, 0)
    else:
        return

    rotateplayers()
    riskgui.set_armies(currentplayer.freeArmies)
    while currentplayer.ai is not None:
        currentplayer.ai.run_preplace(currentplayer)
        rotateplayers()
        riskgui.set_armies(currentplayer.freeArmies)
        if currentplayer.freeArmies == 0:
            resetturn()
            return
Exemplo n.º 18
0
def load_game(filename):
    """Load a previously-saved game."""
    global currentplayer, phase, currentcard, currentplayernum
    if phase != "Pregame":
        riskgui.set_status("You have already started a game")
        return

    savefile = open(filename)
    while savefile.readline() == "player\n":
        plname = savefile.readline().strip()
        plarmies = int(savefile.readline().strip())
        plconquered = int(savefile.readline().strip())
        cards = []
        currentline = savefile.readline()
        while currentline != "endplayer\n" and currentline != "ai\n":
            nline = savefile.readline().strip()
            cards.append(Card(currentline.strip(), nline))
            currentline = savefile.readline()
        ainame = None
        if currentline == "ai\n":
            ainame = savefile.readline().strip()
            aidatalen = int(savefile.readline().strip())
            aidata = savefile.read(aidatalen)
            savefile.readline()
        if not players.has_key(plname):
            if ainame and not os.path.exists(ainame):
                riskgui.set_status("AI " + ainame + " nonexistent")
                ainame = None
            riskgui.makeplayercolors(makeplayer(plname, ainame))
            if ainame:
                players[plname].ai.loaddata(aidata)
        players[plname].freeArmies = plarmies
        players[plname].conqueredTerritory = plconquered
        players[plname].cards = cards
        for card in cards:
            risknetwork.add_card(card, players[plname])
        playerorder.append(players[plname])
    riskgui.relistplayers(playerorder)
    currentplayer = players[savefile.readline().strip()]
    currentplayernum = playerorder.index(currentplayer)
    riskgui.set_armies(currentplayer.freeArmies)
    riskgui.playersturn(currentplayer.name)
    setphase(savefile.readline().strip())
    currentcard = int(savefile.readline().strip())

    while savefile.readline().strip() == "territory":
        tname = savefile.readline().strip()
        tplayer = savefile.readline().strip()
        if tplayer == "None":
            tplayer = None
        else:
            tplayer = players[tplayer]
        territories[tname].player = tplayer
        territories[tname].armies = int(savefile.readline().strip())
        riskgui.drawterritory(territories[tname], 0)
        savefile.readline()

    while 1:
        cardterr = savefile.readline().strip()
        if not cardterr:
            break
        allcards.append(Card(cardterr, savefile.readline().strip()))
    savefile.close()