Guest User

Untitled

a guest
Aug 14th, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.92 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. from random import randrange
  4. import tkinter as tk
  5.  
  6.  
  7. class Field:
  8.     def __init__(self, width, height, pawns_count):
  9.         self.width, self.height = width, height
  10.         self.knigth_alive = True
  11.         self.pawns_count = pawns_count
  12.         self.bias = 3
  13.         self.EMPTY = '.'
  14.         self.PAWN = 'p'
  15.         self.KNIGHT = 'K'
  16.         self.field = ([[self.EMPTY] * (self.width + self.bias * 2)
  17.                       for _ in range(self.height + self.bias * 2)])
  18.         self.pawnsonfield = 0
  19.         self.score = 0
  20.         self.knight_xy = (randrange(self.width), randrange(self.height))
  21.         self.put_knight(*self.knight_xy)
  22.         self.random_put_pawns()
  23.         self.moves_count = 0
  24.         self.buttonList = []
  25.  
  26.     def get(self, x, y):
  27.         return self.field[y + self.bias - 1][x + self.bias-1]
  28.  
  29.     def _set(self, value, x, y):
  30.         self.get(x, y)
  31.         self.field[y + self.bias - 1][x + self.bias-1] = value
  32.  
  33.     def refresh(self):
  34.         gap = '   '
  35.         for x in range(self.width):
  36.             print('  '+str(x + 1), end=' ')
  37.         print()
  38.         for y in range(self.height):
  39.             linefield = str(y + 1) + ' '
  40.             for x in range(self.width):
  41.                 linefield += (self.get(x, y) + gap)
  42.             print(linefield)
  43.             print()
  44.  
  45.     def random_put_pawns(self):
  46.         while self.pawnsonfield < self.pawns_count:
  47.             self.pawn_xy = (randrange(self.width), randrange(self.height))
  48.             if (self.get(*self.pawn_xy) and self.get(self.pawn_xy[0]-1, self.pawn_xy[1]+1) and self.get(self.pawn_xy[0]-1, self.pawn_xy[1]-1)) == self.EMPTY:
  49.                 self._set(self.PAWN, *self.pawn_xy)
  50.                 self.pawnsonfield += 1
  51.  
  52.     def put_knight(self, x, y):
  53.         self._set(self.EMPTY, *self.knight_xy)
  54.         if self.get(x, y) == self.PAWN:
  55.             self.score += 1
  56.             self.pawns_count -= 1
  57.             self.pawnsonfield -=1
  58.         self._set(self.KNIGHT, x, y)
  59.         self.knight_xy = (x, y)
  60.  
  61.     def pawns_attack(self):
  62.         for y in range(len(self.field)):
  63.             if self.get(self.knight_xy[0]+1, self.knight_xy[1]+1) == self.PAWN:
  64.                 self._set(self.PAWN, *self.knight_xy)
  65.                 self._set(self.EMPTY, self.knight_xy[0]+1, self.knight_xy[1]+1)
  66.                 self.knigth_alive = False
  67.             if self.get(self.knight_xy[0]+1, self.knight_xy[1]-1) == self.PAWN:
  68.                 self._set(self.PAWN, *self.knight_xy)
  69.                 self._set(self.EMPTY, self.knight_xy[0]+1, self.knight_xy[1]-1)
  70.                 self.knigth_alive = False
  71.  
  72.     def move_horse(self, x, y):
  73.         mlst = [[-1, -2], [-2, -1], [-2, 1], [-1, 2], [1, -2], [2, -1], [2, 1], [1, 2]]
  74.         dist = [self.knight_xy[0] - x, self.knight_xy[1] - y]
  75.         if dist in mlst:
  76.             self.put_knight(x, y)
  77.             self.moves_count += 1
  78.             self.pawns_attack()
  79.             self.refresh()
  80.             for y in range(self.height):
  81.                 self.buttonList.append([])
  82.                 for x in range(self.width):
  83.                     self.buttonList[y][x].config(text=self.get(x, y))
  84.  
  85.  
  86. class Game:
  87.     def __init__(self, field):
  88.         self.game = 1
  89.         self.field = field
  90.         self.win = False
  91.         self.defeat = False
  92.  
  93.     def win(self):
  94.         print(".    .")
  95.         print(r"|\/\/|")
  96.         print("|____|")
  97.         print('Score: ' + str(self.field.score))
  98.  
  99.     def defeat(self):
  100.         print("Game over!")
  101.         print('Score: ' + str(self.field.score))
  102.  
  103.     def check_score(self):
  104.         print('Score: '+str(self.field.score), 'Moves: '+str(self.field.moves_count))
  105.         if not self.field.pawns_count:
  106.             self.win = True
  107.         if not self.field.knigth_alive:
  108.             self.defeat = True
  109.  
  110. class Frame:
  111.     def __init__(self, field, play):
  112.         self.play = play
  113.         self.field = field
  114.         self.clicks = 0
  115.         self.window = tk.Tk()
  116.         self.score_label = tk.Label(self.window , text='Score \n'+str(self.field.score))
  117.         self.moves_label = tk.Label(self.window , text='Moves \n'+str(self.field.moves_count))
  118.         self.pawns_left_label = tk.Label(self.window , text='Pawns left \n'+str(self.field.pawnsonfield))
  119.  
  120.     def create_window(self):
  121.         for y in range(self.field.height):
  122.             self.field.buttonList.append([])
  123.             for x in range(self.field.width):
  124.                 color = 'orange' if (y + x) % 2 else 'yellow'
  125.                 self.field.buttonList[y].append(tk.Button(self.window,
  126.                                                         text=self.field.get(x,y),
  127.                                                         bg=color, height=2, width=2, command=lambda y=y,
  128.                                                         x=x, self=self: self.commands_in_one_button_handler(x, y)))
  129.                 self.field.buttonList[y][x].grid(row=y, column=x)
  130.             self.score_label.grid(row=1, column=self.field.width+1)
  131.             self.moves_label.grid(row=2, column=self.field.width+1)
  132.             self.pawns_left_label.grid(row=3, column=self.field.width+1)
  133.         self.window.mainloop()
  134.  
  135.     def commands_in_one_button_handler(self, x, y):
  136.         self.field.move_horse(x, y)
  137.         self.count_stat()
  138.         self.refresh_window(x, y)
  139.         self.game_final()
  140.  
  141.     def refresh_window(self, y, x):
  142.         for y in range(self.field.height):
  143.             for x in range(self.field.width):
  144.                     self.field.buttonList[y][x].config(text=self.field.get(x, y))
  145.  
  146.     def count_stat(self):
  147.         self.clicks += 1
  148.         self.score_label.config(text='Score \n'+str(self.field.score))
  149.         self.moves_label.config(text='Moves \n'+str(self.field.moves_count))
  150.         self.pawns_left_label.config(text='Pawns left \n'+str(self.field.pawnsonfield))
  151.  
  152.     def game_final(self):
  153.         self.play.check_score()
  154.         if self.play.defeat or self.play.win:
  155.             for y in range(self.field.height):
  156.                 for x in range(self.field.width):
  157.                     self.field.buttonList[y][x].config(state='disabled')
  158.             self.dialog = tk.Tk()
  159.             final = ' win' if self.play.win else ' lose'
  160.             message = tk.Label(self.dialog, text='You' + final +'!')
  161.             exit = tk.Button(self.dialog, text='Exit', command = self.quit_handler)
  162.             restart = tk.Button(self.dialog, text='Restart')
  163.             message.grid(row=1, column=2)
  164.             exit.grid(row=3, column=4)
  165.             restart.grid(row=3, column=6)
  166.             self.dialog.mainloop()
  167.  
  168.     def quit_handler(self):
  169.         self.dialog.destroy()
  170.         self.window.destroy()
  171.  
  172.     def restart_handler(self):
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181. def main(width=8, height=8, knigth_alive=True, pawns=3):
  182.     field = Field(width, height, pawns)
  183.     field.refresh()
  184.     play = Game(field)
  185.     frame = Frame(field, play)
  186.     frame.create_window()
  187.  
  188.  
  189. if __name__ == '__main__':
  190.     main()
Advertisement
Add Comment
Please, Sign In to add comment