Using Python to achieve a childhood pebble game

Source: Internet
Author: User

# two children against a # game # 1 Black and white both sides four chess, chessboard 4x4# 2 black Chess first # 3 horizontal or vertical three pieces continuous black Chess 2 white chess one of the white chess, and vice versa.  # 41 remaining pieces less than 2 pieces, then the other party wins 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 (obje CT): 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].app End (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 (sel            F.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.ch ESSES[SELF.FLAG1]) <Ch1:info = ' ' +self.flag1+ ' "loses a pawn ' else:info = '" ' + Self.flag2 + ' "loses a piece" 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 (' "' +f Lag+ ' "moved the pawn {}-->{} '. Format ((A, B), (x, y))) rst = Self.__update () print (RST) print (SEL            F.layout) If Len (Self.chesses[self.flag1]) < 2:info = ' ' +self.flag2+ ' "win, GAME over ' If Len (Self.chesses[self.flag2]) < 2:info = ' ' + self.flag1 + ' "Win, GAME over ' return I Nfoclass 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, PE        Rson2.flag) while True:print (board.layout) tmp = ' {} ' has a pawn: {}; ' {} ' has a pawn: {} ' 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 (' "' + person        1.flag + ' "Please select a pawn, enter a pawn number ') c = int (input ()) Print (' Please select Move direction u: up, R: Right D: Lower L: Left ') direction = input () result = Person1.move_chess (c, Direction, board) if "GAME over" in result:print (result) BR        Eak elif result = = "ERROR": Print ("restart") continue Else:print (Result) Print (' "' + person2.fLag + ' "Please select a pawn, enter a pawn number ') c = int (input ()) Print (' Please select Move direction u: up, R: Right D: Lower L: Left ') direction = input () r         Esult = Person2.move_chess (c, Direction, board) if ' GAME over ' in result:print (result) break elif result = = "ERROR": Print ("restart") continue Else:print (Result)

Self-written mini-game, original original and original

Player Class Gamer, chess class Chess, Chessboard class Board,

Using Python to achieve a childhood pebble game

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.