Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. class Field():
  2.     def __init__(self):
  3.         self.data = [0,0,0,
  4.                      0,0,0,
  5.                      0,0,0]
  6.  
  7.         self.win = -1
  8.  
  9.     def step(self, who, pos):
  10.         if not self.check_step_possible(pos):
  11.             return False
  12.         self.data[pos] = who
  13.         return True
  14.    
  15.  
  16.     def step_x(self, pos):
  17.         if not self.check_step_possible(pos):
  18.             return False
  19.         self.data[pos] = 1
  20.         return True
  21.  
  22.     def step_o(self, pos):
  23.         if not self.check_step_possible(pos):
  24.             return False
  25.         self.data[pos] = 2
  26.         return True
  27.  
  28.     def check_step_possible(self, pos):
  29.         return self.data[pos] == 0
  30.  
  31.     def check_win(self):
  32.         win_combinations = [
  33.                 [1,2,3],
  34.                 [4,5,6],
  35.                 [7,8,9],
  36.                 [1,4,7],
  37.                 [2,5,8],
  38.                 [3,6,9],
  39.                 [1,5,9],
  40.                 [3,5,7]
  41.             ]
  42.         # combinations to indexes
  43.         for ar in range(0, len(win_combinations)):
  44.             for it in range(0, len(win_combinations[ar])):
  45.                 win_combinations[ar][it] = win_combinations[ar][it] - 1
  46.  
  47.         # print(win_combinations)
  48.  
  49.  
  50.         for win_indexes in win_combinations:
  51.             win = True
  52.             letter = self.data[win_indexes[0]]
  53.             # print(win_indexes)
  54.             if letter == 0:
  55.                 continue
  56.             for i in win_indexes:
  57.                 if self.data[i] != letter:
  58.                     win = False
  59.             if (win):
  60.                 self.win = letter
  61.  
  62.         if self.win > 0:
  63.             return self.win
  64.         else:
  65.             return False
  66.  
  67.     def print_state(self):
  68.         msg =  "----------------\n"
  69.         msg += "| %s | %s | %s |\n"
  70.         msg += "| %s | %s | %s |\n"
  71.         msg += "| %s | %s | %s |\n"
  72.         msg += "----------------\n"
  73.  
  74.         tpl = ()
  75.         for item in self.data:
  76.             tpl = tpl + (item,)
  77.  
  78.         print(msg % tpl)
  79.  
  80. if __name__ == "__main__":
  81.     f = Field()
  82.     cur_player = 1
  83.     f.print_state()
  84.     win = False
  85.  
  86.     while not win:
  87.         print("Player %s step: " % cur_player)
  88.         step = int(input())
  89.         while not f.step(cur_player, step - 1):
  90.             print("Cant go there! Input another position, Player %s:" % cur_player)
  91.             step = int(input())
  92.         f.print_state()
  93.         cur_player = 3 - cur_player
  94.         win = f.check_win()
  95.         # print(win)
  96.     print("Player %s win!" % f.win)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement