Backgammon (console version)

File Structure

 

  . 1 Finish = False # game is ended 
  2 flagNum. 1 =   # current chess by numerals 
  . 3 flagch = ' * '   # current by chess pieces 
  . 4 X = 0     # current piece of abscissa 
  . 5 Y = 0     # current ordinate stones 
  . 6  Print ( ' \ 033 [. 1; 37 [; 41M --------- simple backgammon game (console Version) --------- \ 033 [0m ' )
   . 7  # board initialization 
  . 8 checkerboard = []
   . 9  for I in Range (10 ):
 10      checkerboard.append ([])
 . 11     for J in Range (10 ):
 12 is          Checkerboard [I] .append ( ' - ' )
 13 is  DEF MSG ():
 14      # outputs final victory board 
15      Print ( " \ 033 [. 1; 37 [; 44M ----- --------------------------- " )
 16      Print ( "    . 1. 5. 4. 3 2. 6. 7. 9 10. 8 " )
 . 17      for I in Range (len (Checkerboard)):
 18 is          Print (CHR (I + the ord ( ' A ' )) + "  ", End = '  ' )
 . 19          for J in Range (len (Checkerboard [I])):
 20 is              Print (Checkerboard [I] [J] + "  " , End = '  ' )
 21 is          Print ()
 22 is      Print ( " - ------------------------------ \ 033 [0m " )
 23      # output winner 
24-      IF (== flagNum 1 ):
 25          Print ( ' ! \ 033 [32M * chess victories *** \ 033 [0m ' )
 26      the else :
 27          Print( ' ! \ 033 [32mo chess victories *** \ 033 [0m ' )
 28  
29  the while  not Finish:
 30      # print board 
31      Print ( " \ 033 [1; 30; 46M ---------- ---------------------- " )
 32      Print ( "    . 1. 5. 4. 3 2. 6. 7. 9 10. 8 " )
 33 is      for I in Range (len (Checkerboard) ):
 34 is          Print (CHR (I + the ord ( ' A ' )) + "  " , End = '  ' );
 35          forJ in Range (len (Checkerboard [I])):
 36              Print (Checkerboard [I] [J] + "  " , End = '  ' )
 37 [          Print ()
 38 is      Print ( " ---------- ---------------------- \ 033 [0m " )
 39      # is determined by the current chess 
40      IF flagNum ==. 1 :
 41 is          flagch = ' * ' 
42 is          Print ( ' \ 033 [. 1; 37 [; 40m * Please enter coordinates pawn (e.g. A1): \ 033 [0m ' , End = '  ' ) # white black
43 is      the else :
 44 is          flagch = ' o ' 
45          Print ( ' \ 033 [. 1; 30; 42m o Please enter coordinates pawn (e.g. J5): \ 033 [0m ' , End = '  ' ) # black green substrate 
46      # input coordinates pawn 
47      STR = INPUT ()
 48      CH = STR [0] # Get the first character capitalized 
49      X the ord = (CH) - 65
 50      Y = int (STR [. 1]) -. 1
 51 is      # determines whether the coordinates in the board 
52 is      IF (X <0 or X>. 9 or Y <0 orthe y-> 9 ):
 53          Print ( ' ! \ 033 [*** 31M coordinates you entered is incorrect, please re-enter *** \ 033 [0m ' )
 54          the Continue 
55      # Is there a pawn on the judgment coordinate 
56      IF (the Checkerboard [ X] [Y] == ' - ' ):
 57 is          IF (flagNum ==. 1 ):
 58              Checkerboard [X] [Y] = ' * ' 
59          the else :
 60              Checkerboard [X] [Y] = ' O ' 
61 is      the else :
 62 is          Print ( '\ 033 [31m ****** enter your location already has other pieces, please re-enter! \ 033 [0m ' )
 63 is          Continue 
64      # determines left piece 
65      IF (Y -. 4> = 0):
 66          IF (Checkerboard [X] [Y -. 1] == flagch
 67                  and Checkerboard [X] [Y - 2 ] == flagch
 68                  and Checkerboard [X] [Y -. 3] == flagch
 69                  and Checkerboard [X] [Y -. 4] == flagch):
 70              Finish = True
 71 is              MSG ()
 72  
73 is      # Analyzing the right piece 
74      IF (. 4 + Y <=. 9):
 75         if (checkerboard[x][y + 1] == flagch
 76                 and checkerboard[x][y + 2] == flagch
 77                 and checkerboard[x][y + 3] == flagch
 78                 and checkerboard[x][y + 4] == flagch):
 79             finish = True
 80             msg()
 81 
 82     # 判断棋子上方
 83     if (x - 4 >= 0):
 84         if (checkerboard[x - 1][y] == flagch
 85                 and checkerboard[x - 2][y] == flagch
 86                 and checkerboard[x - 3][y] == flagch
 87                 and checkerboard[x - 4][y] == flagch):
 88             finish = True
 89             msg()
 90 
 91     # 判断棋子下方
 92     if (x + 4 <= 9):
 93         if (checkerboard[x + 1][y] == flagch
 94                 and checkerboard[x + 2][y] == flagch
 95                 and checkerboard[x + 3][y] == flagch
 96                 and checkerboard[x + 4][y] == flagch):
 97             finish = True
98              MSG ()
 99  
100      # determines the upper right pieces 
101      IF (X -. 4> = 0 and Y -. 4> = 0):
 102          IF (Checkerboard [X -. 1] [Y -. 1] == flagch
 103                  and Checkerboard [ X - 2] [Y - 2] == flagch
 104                  and Checkerboard [X -. 3] [Y -. 3] == flagch
 105                  and Checkerboard [X -. 4] [Y -. 4] == flagch):
 106              Finish = True
 107              MSG ()
 108  
109      # Analyzing the lower right pieces 
110      IF (. 4 + X <=. 9and y - 4 >= 0):
111         if (checkerboard[x + 1][y - 1] == flagch
112                 and checkerboard[x + 2][y - 2] == flagch
113                 and checkerboard[x + 3][y - 3] == flagch
114                 and checkerboard[x + 4][y - 4] == flagch):
115             finish = True
116             msg()
117 
118     # 判断棋子左上方向
119     if (x - 4 >= 0 and y + 4 <= 9):
120         if (checkerboard[x - 1][y + 1] == flagch
121                 and checkerboard[x - 2][y + 2] == flagch
122                 and checkerboard[x - 3][y + 3] == flagch
123                 and checkerboard[x - 4][y + 4] == flagch):
124             finish = True
125             msg()
126 
127     # 判断棋子左下方向
128     if (x + 4 <= 9 and y + 4 <= 9):
129         if (checkerboard[x + 1][y + 1] == flagch
130                 and checkerboard[x + 2][y + 2] == flagch
131                 and checkerboard[x + 3][y + 3] == flagch
132                  and Checkerboard [X +. 4] [Y +. 4] == flagch):
 133              Finish = True
 134              MSG ()
 135      flagNum * = -1; # replacement by numerals chess

 

Guess you like

Origin www.cnblogs.com/lws865/p/11454543.html
Recommended