用python實現小時候的石子遊戲

來源:互聯網
上載者:User

標籤:blog   else   str   move   elf   []   python   sse   import   

# 倆子抵一個# 遊戲規則# 1 黑白雙方各四枚棋,棋盤 4X4# 2 黑棋先# 3 橫向或豎向 三枚棋子連續 黑棋2枚白棋一枚則該枚白棋出局,反之亦然。# 4 一方剩餘棋子小於2枚,則另一方勝import pprintppclass Chess(object):    def __init__(self, uid, flag, location=None):        self.uid = uid        self.flag = flag        self.location = location    def __str__(self):        return self.flagclass Board(object):    dir = {‘u‘: (-1, 0), ‘r‘: (0, 1), ‘d‘: (1, 0), ‘l‘: (0, -1)}    error = ‘ERROR‘    def __init__(self, flag1, flag2):        self.layout = [[0, 0, 0, 0],                       [0, 0, 0, 0],                       [0, 0, 0, 0],                       [0, 0, 0, 0]]        self.flag1 = flag1        self.flag2 = flag2        str1 = self.flag1+self.flag1+self.flag2        str2 = self.flag2+self.flag2+self.flag1        self.label =[str1, str2]        self.chesses = {flag1: [], flag2: []}        for x in range(4):            self.layout[0][x] = self.flag1+str(x+1)            self.layout[3][x] = self.flag2+str(x+1)            a = Chess(x, self.flag1+str(x+1), (0, x))            self.chesses[flag1].append(a)            b = Chess(x, self.flag2+str(x+1), (3, x))            self.chesses[flag2].append(b)    def __rule(self):        lay = self.layout        label = self.label        result = None        for x in range(4):            for y in range(4):                a = lay[x][y]                if a == 0:                    continue                else:                    a = a[0]                if x < 2:                    b = lay[x - 3][y]                    b = b[0] if b != 0 else b                    c = lay[x - 2][y]                    c = c[0] if c != 0 else c                    abc = str(a) + str(b) + str(c)                    if abc in label or abc[::-1] in label:                        if a == b:                            lay[x - 2][y] = 0                            result = x+2, y                            break                        else:                            lay[x][y] = 0                            result = x, y                            break                if y < 2:                    b = lay[x][y - 3]                    b = b[0] if b != 0 else b                    c = lay[x][y - 2]                    c = c[0] if c != 0 else c                    abc = str(a) + str(b) + str(c)                    if abc in label or abc[::-1] in label:                        if a == b:                            lay[x][y - 2] = 0                            result = x, y + 2                            break                        else:                            lay[x][y] = 0                            result = x, y                            break        return result    def __update(self):        a = self.__rule()        info =‘‘        if a:            ch1 = len(self.chesses[self.flag1])            self.chesses[self.flag1] = [ch for ch in self.chesses[self.flag1] if ch.location != a]            self.chesses[self.flag2] = [ch for ch in self.chesses[self.flag2] if ch.location != a]            if len(self.chesses[self.flag1]) < ch1:                info = ‘【‘+self.flag1+‘】失去一枚棋子‘            else:                info = ‘【‘ + self.flag2 + ‘】失去一枚棋子‘        return info    def chess_move(self, flag, num, d):        chess = [x for x in self.chesses[flag] if x.uid == (num-1)][0]        a, b = chess.location        x = a + Board.dir[d][0]        y = b + Board.dir[d][1]        info =‘‘        if x < 0 or x > 3 or y < 0 or y > 3 or self.layout[x][y] != 0:            return Board.error        else:            self.layout[a][b] = 0            chess.location = x, y            self.layout[x][y] = chess.flag            print(‘【‘+flag+‘】移動了棋子{}-->{}‘.format((a, b), (x, y)))            rst = self.__update()            print(rst)            print(self.layout)            if len(self.chesses[self.flag1]) < 2:                info = ‘【‘+self.flag2+‘】獲勝,GAME OVER‘            if len(self.chesses[self.flag2]) < 2:                info = ‘【‘ + self.flag1 + ‘】獲勝,GAME OVER ‘            return infoclass Gamer(object):    def __init__(self, flag):        self.flag = flag    def move_chess(self, num, d, bd):        return bd.chess_move(self.flag, num, d)if __name__ == ‘__main__‘:    person1 = Gamer(‘b‘)    person2 = Gamer(‘w‘)    board = Board(person1.flag, person2.flag)    while True:        print(board.layout)        tmp = ‘【{}】擁有棋子:{};【{}】擁有棋子:{}‘        info1, info2 = ‘‘, ‘‘        for x in board.chesses[person1.flag]:            info1 += x.flag+‘ ‘        for x in board.chesses[person2.flag]:            info2 += x.flag+‘ ‘        print(tmp.format(person1.flag, info1, person2.flag, info2))        print(‘【‘ + person1.flag + ‘】請選擇棋子,輸入棋子數字‘)        c = int(input())        print(‘請選擇移動方向 u:上,r:右 d:下 l:左‘)        direction = input()        result = person1.move_chess(c, direction, board)        if "GAME OVER" in result:            print(result)            break        elif result == "ERROR":            print("重新開始")            continue        else:            print(result)        print(‘【‘ + person2.flag + ‘】請選擇棋子,輸入棋子數字‘)        c = int(input())        print(‘請選擇移動方向 u:上,r:右 d:下 l:左‘)        direction = input()        result = person2.move_chess(c, direction, board)        if "GAME OVER" in result:            print(result)            break        elif result == "ERROR":            print("重新開始")            continue        else:            print(result)

  自己寫的小遊戲,原創原創原創

玩家類 Gamer , 棋子類 Chess,棋盤類 Board,

用python實現小時候的石子遊戲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.