LoginSignup
0
0

More than 5 years have passed since last update.

すごろく作ってみた(CUI)

Posted at

$GREEDに引き続きすごろくを作ってみました。
Classを活用したいと思っていたのですが、なかなか難しいですね。

from random import randint
import unicodedata


def get_length_unicode(line):
    lng = 0
    for i in line:
        if unicodedata.east_asian_width(i) in "WFA":
            lng += 2
        else:
            lng += 1
    return lng


def dice_roll():
    return randint(1, 6)


class Mas:
    def __init__(self, step=0, jump=None, wait=0, evnt=""):
        self.evnt = evnt
        self.step = step
        self.jump = jump
        self.wait = wait
    def exe(self,now,name):
        if self.evnt:
            print(self.evnt)
        if self.jump != None:
            now = self.jump
            print("{} > jump:{}".format(name,self.jump))
        if self.step:
            print("{} > step:{}".format(name,self.step))
        now += self.step
        return now,self.wait

class Player:
    def __init__(self, name):
        self.name = name
        self.mas = 0
        self.wait = 0


def get_player_date():
    players = []
    name_max_len = 0
    pon = ""
    while True:
        pon = input("number of player:")
        try:
            pon = int(pon)
            break
        except ValueError:
            print("TypeError: write int")
    for i in range(1,pon+1):
        name = input("PL{}'s name:".format(i))
        name_max_len = max((get_length_unicode(name),name_max_len))
        players.append(
            Player(
                name if name else "PL{}".format(i)
            )
        )
    for i in players:
        i.name += " " * (name_max_len - get_length_unicode(i.name) + 1)
    return tuple(players)


def map_(*mas):
    return {i+1:v for i,v in enumerate(mas)}


def print_map_view(pl,map_):
            m = list("■"*len(map_))       


            m[pl] = "□"
            print("".join(m))

def play(players,map_):
    clear = []
    while True:
        for i in players:

            if i in clear:
                continue
            if i.wait > 0:
                print(str(i.name)+" > wait: "+str(i.wait))
                print("====================================")
                i.wait -= 1
                continue

            roll = dice_roll()
            i.mas += roll

            if i.mas >= len(map_):
                print_map_view(len(map_)-1,map_)
                print(i.name+" > roll: "+str(roll))
                input(str(i.name)+" > total: "+str(i.mas)+"/{}".format(len(map_)-1))
                print("clear!:{}".format(i.name))
                print("====================================")
                clear.append(i)
                continue
            if i.mas:
                i.mas,i.wait = map_[i.mas].exe(i.mas,i.name)

            print_map_view(i.mas,map_)
            print(i.name+" > roll: "+str(roll))
            input(str(i.name)+" > total: "+str(i.mas)+"/{}".format(len(map_)-1))
            print("====================================")

def main():
    m = map_(
        Mas(step = 5),
        Mas(step = 5),
        Mas(step = 5),
        Mas(step = 5),
        Mas(step = 5),
        Mas(step = 5),
        Mas(),
        Mas(),
        Mas(),
        Mas(),
        Mas(),
        Mas(evnt = "ものまねしてくれ"),
        Mas(),
        Mas(),
        Mas(wait = 1),
        Mas(wait = 1),
        Mas(wait = 1),
        Mas(),
        Mas(),
        Mas(),
        Mas(),
        Mas(),
        Mas(jump = 5),
        Mas(jump = 5),
        Mas(),
    )
    p = get_player_date()
    print("===========")
    play(p,m)


main()
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0