python 단기 오목 실현

15570 단어 python오목
간단 한 소개
이것 은 실험실 이 2018 년 말 에 새로운 시험 문 제 를 모집 하여 Python 을 이용 하여 기본 적 인 대전 을 완성 할 수 있 는 오목 게임 을 만 드 는 것 이다.초보 자 를 향 하 다.
프로그램 은 주로 두 부분 을 포함 하고 도형 생 성과 논리 작성 두 부분 을 포함한다.
프로그램의 실행 결과:

스타일 생 성
낡은 규칙,먼저 사용 하 는 가방 을 가 져 와라.

'''
@Auther : gaoxin
@Date : 2019.01.01
@Version : 1.0
'''

from tkinter import *
import math
그리고 스타일 의 클래스,클래스 이름 chessBoard 를 만 듭 니 다.여기에 많은 주석 을 넣 어서 초보 자 들 이 함수 의 작용 을 이해 하지 못 하 게 하 였 는데,솔직히 나 는 매우 어색 하 다 고 생각한다.

#     
class chessBoard() :
 def __init__(self) :
  #    tk  ,   
  self.window = Tk()
  #    
  self.window.title("     ")
  #      
  self.window.geometry("660x470")
  #        
  self.window.resizable(0,0)
  #        
  self.canvas=Canvas(self.window , bg="#EEE8AC" , width=470, height=470)
  #      
  self.paint_board()
  #         
  self.canvas.grid(row = 0 , column = 0)

 def paint_board(self) :
  #   
  for row in range(0,15) :
   if row == 0 or row == 14 :
    self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 2)
   else :
    self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 1)
  
  #   
  for column in range(0,15) :
   if column == 0 or column == 14 :
    self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 ,width = 2)
   else :
    self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 , width = 1)
  
  #  
  self.canvas.create_oval(112, 112, 118, 118, fill="black")
  self.canvas.create_oval(352, 112, 358, 118, fill="black")
  self.canvas.create_oval(112, 352, 118, 358, fill="black")
  self.canvas.create_oval(232, 232, 238, 238, fill="black")
  self.canvas.create_oval(352, 352, 358, 358, fill="black")
논리 적 작성
이곳 의 주요 기능 은 각 함수 의 기능 을 보면 된다.

#        
#0    , 1    , 2   
class Gobang() :
 #   
 def __init__(self) :
  self.board = chessBoard()
  self.game_print = StringVar()
  self.game_print.set("")
  #16*16     ,    out of index
  self.db = [([2] * 16) for i in range(16)]
  #        
  self.order = []
  #    
  self.color_count = 0 
  self.color = 'black'
  #        ,   1,    1
  self.flag_win = 1
  self.flag_empty = 1
  self.options()
  
  
 #    
 def change_color(self) :
  self.color_count = (self.color_count + 1 ) % 2
  if self.color_count == 0 :
   self.color = "black"
  elif self.color_count ==1 :
   self.color = "white"
 
 
 #  
 def chess_moving(self ,event) :
  #   “  ” “  ”        
  if self.flag_win ==1 or self.flag_empty ==0 :
   return
  #       
  x,y = event.x-25 , event.y-25
  x = round(x/30)
  y = round(y/30)
  #        ,        ,    
  while self.db[y][x] == 2 and self.limit_boarder(y,x):
   self.db[y][x] = self.color_count
   self.order.append(x+15*y) 
   self.board.canvas.create_oval(25+30*x-12 , 25+30*y-12 , 25+30*x+12 , 25+30*y+12 , fill = self.color,tags = "chessman")
   if self.game_win(y,x,self.color_count) :
    print(self.color,"  ")
    self.game_print.set(self.color+"  ")
   else :
    self.change_color()
    self.game_print.set(" "+self.color+"  ")
 

 #         
 def limit_boarder(self , y , x) :
  if x<0 or x>14 or y<0 or y>14 :
   return False
  else :
   return True


 #       ,         
 def chessman_count(self , y , x , color_count ) :
  count1,count2,count3,count4 = 1,1,1,1
  #   
  for i in range(-1 , -5 , -1) :
   if self.db[y][x+i] == color_count :
    count1 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y][x+i] == color_count :
    count1 += 1
   else:
    break 
  #   
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x] == color_count :
    count2 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x] == color_count :
    count2 += 1
   else:
    break 
  #/  
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x+i] == color_count :
    count3 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x+i] == color_count :
    count3 += 1
   else:
    break 
  #\  
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x-i] == color_count :
    count4 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x-i] == color_count :
    count4 += 1
   else:
    break 
   
  return max(count1 , count2 , count3 , count4)


 #    
 def game_win(self , y , x , color_count ) :
  if self.chessman_count(y,x,color_count) >= 5 :
   self.flag_win = 1
   self.flag_empty = 0
   return True
  else :
   return False
  

 #  ,    ,     n-1   
 def withdraw(self ) :
  if len(self.order)==0 or self.flag_win == 1:
   return
  self.board.canvas.delete("chessman")
  z = self.order.pop()
  x = z%15
  y = z//15
  self.db[y][x] = 2
  self.color_count = 1
  for i in self.order :
   ix = i%15
   iy = i//15
   self.change_color()
   self.board.canvas.create_oval(25+30*ix-12 , 25+30*iy-12 , 25+30*ix+12 , 25+30*iy+12 , fill = self.color,tags = "chessman")
  self.change_color()
  self.game_print.set(" "+self.color+"  ")
 

 #  
 def empty_all(self) :
  self.board.canvas.delete("chessman")
  #     
  self.db = [([2] * 16) for i in range(16)]
  self.order = []
  self.color_count = 0 
  self.color = 'black'
  self.flag_win = 1
  self.flag_empty = 1
  self.game_print.set("")


 # self.flag_win 0        
 def game_start(self) :
  #         0  
  if self.flag_empty == 0:
   return
  self.flag_win = 0
  self.game_print.set(" "+self.color+"  ")


 def options(self) :
  self.board.canvas.bind("<Button-1>",self.chess_moving)
  Label(self.board.window , textvariable = self.game_print , font = ("Arial", 20) ).place(relx = 0, rely = 0 ,x = 495 , y = 200)
  Button(self.board.window , text= "    " ,command = self.game_start,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=15)
  Button(self.board.window , text= "    " ,command = self.withdraw,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=60)
  Button(self.board.window , text= "    " ,command = self.empty_all,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=105)
  Button(self.board.window , text= "    " ,command = self.board.window.destroy,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)
  self.board.window.mainloop()
마지막,main 함수

if __name__ == "__main__":
 game = Gobang()
이상 의 모든 프로그램 을 복사 하여 붙 여 넣 으 면 완전한 프로그램 으로 실행 할 수 있 습 니 다.
마지막 으로 전체 프로그램 을 복사 하고 붙 여 넣 는 것 은 너무 번 거 롭 지 않 습 니 다.

'''
@Auther : gaoxin
@Date : 2019.01.01
@Version : 1.0

'''

from tkinter import *
import math

#     
class chessBoard() :
 def __init__(self) :
  self.window = Tk()
  self.window.title("     ")
  self.window.geometry("660x470")
  self.window.resizable(0,0)
  self.canvas=Canvas(self.window , bg="#EEE8AC" , width=470, height=470)
  self.paint_board()
  self.canvas.grid(row = 0 , column = 0)

 def paint_board(self) :
  for row in range(0,15) :
   if row == 0 or row == 14 :
    self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 2)
   else :
    self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 1)
  for column in range(0,15) :
   if column == 0 or column == 14 :
    self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 ,width = 2)
   else :
    self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 , width = 1)
   
  self.canvas.create_oval(112, 112, 118, 118, fill="black")
  self.canvas.create_oval(352, 112, 358, 118, fill="black")
  self.canvas.create_oval(112, 352, 118, 358, fill="black")
  self.canvas.create_oval(232, 232, 238, 238, fill="black")
  self.canvas.create_oval(352, 352, 358, 358, fill="black")


#        
#0    , 1    , 2   
class Gobang() :
 #   
 def __init__(self) :
  self.board = chessBoard()
  self.game_print = StringVar()
  self.game_print.set("")
  #16*16     ,    out of index
  self.db = [([2] * 16) for i in range(16)]
  #        
  self.order = []
  #    
  self.color_count = 0 
  self.color = 'black'
  #        ,   1,    1
  self.flag_win = 1
  self.flag_empty = 1
  self.options()
  
  
 #    
 def change_color(self) :
  self.color_count = (self.color_count + 1 ) % 2
  if self.color_count == 0 :
   self.color = "black"
  elif self.color_count ==1 :
   self.color = "white"
 
 
 #  
 def chess_moving(self ,event) :
  #   “  ” “  ”        
  if self.flag_win ==1 or self.flag_empty ==0 :
   return
  #       
  x,y = event.x-25 , event.y-25
  x = round(x/30)
  y = round(y/30)
  #        ,        ,    
  while self.db[y][x] == 2 and self.limit_boarder(y,x):
   self.db[y][x] = self.color_count
   self.order.append(x+15*y) 
   self.board.canvas.create_oval(25+30*x-12 , 25+30*y-12 , 25+30*x+12 , 25+30*y+12 , fill = self.color,tags = "chessman")
   if self.game_win(y,x,self.color_count) :
    print(self.color,"  ")
    self.game_print.set(self.color+"  ")
   else :
    self.change_color()
    self.game_print.set(" "+self.color+"  ")
 

 #         
 def limit_boarder(self , y , x) :
  if x<0 or x>14 or y<0 or y>14 :
   return False
  else :
   return True


 #       ,         
 def chessman_count(self , y , x , color_count ) :
  count1,count2,count3,count4 = 1,1,1,1
  #   
  for i in range(-1 , -5 , -1) :
   if self.db[y][x+i] == color_count :
    count1 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y][x+i] == color_count :
    count1 += 1
   else:
    break 
  #   
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x] == color_count :
    count2 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x] == color_count :
    count2 += 1
   else:
    break 
  #/  
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x+i] == color_count :
    count3 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x+i] == color_count :
    count3 += 1
   else:
    break 
  #\  
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x-i] == color_count :
    count4 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x-i] == color_count :
    count4 += 1
   else:
    break 
   
  return max(count1 , count2 , count3 , count4)


 #    
 def game_win(self , y , x , color_count ) :
  if self.chessman_count(y,x,color_count) >= 5 :
   self.flag_win = 1
   self.flag_empty = 0
   return True
  else :
   return False
  

 #  ,    ,     n-1   
 def withdraw(self ) :
  if len(self.order)==0 or self.flag_win == 1:
   return
  self.board.canvas.delete("chessman")
  z = self.order.pop()
  x = z%15
  y = z//15
  self.db[y][x] = 2
  self.color_count = 1
  for i in self.order :
   ix = i%15
   iy = i//15
   self.change_color()
   self.board.canvas.create_oval(25+30*ix-12 , 25+30*iy-12 , 25+30*ix+12 , 25+30*iy+12 , fill = self.color,tags = "chessman")
  self.change_color()
  self.game_print.set(" "+self.color+"  ")
 

 #  
 def empty_all(self) :
  self.board.canvas.delete("chessman")
  #     
  self.db = [([2] * 16) for i in range(16)]
  self.order = []
  self.color_count = 0 
  self.color = 'black'
  self.flag_win = 1
  self.flag_empty = 1
  self.game_print.set("")


 # self.flag_win 0        
 def game_start(self) :
  #         0  
  if self.flag_empty == 0:
   return
  self.flag_win = 0
  self.game_print.set(" "+self.color+"  ")


 def options(self) :
  self.board.canvas.bind("<Button-1>",self.chess_moving)
  Label(self.board.window , textvariable = self.game_print , font = ("Arial", 20) ).place(relx = 0, rely = 0 ,x = 495 , y = 200)
  Button(self.board.window , text= "    " ,command = self.game_start,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=15)
  Button(self.board.window , text= "    " ,command = self.withdraw,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=60)
  Button(self.board.window , text= "    " ,command = self.empty_all,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=105)
  Button(self.board.window , text= "    " ,command = self.board.window.destroy,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)
  self.board.window.mainloop()

 
if __name__ == "__main__":
 game = Gobang()
더 많은 재 미 있 는 클래식 게임 을 통 해 주 제 를 실현 하고 여러분 에 게 공유 합 니 다.
C++클래식 게임 모음
python 클래식 게임 모음
python 러시아 블록 게임 집합
JavaScript 클래식 게임 을 계속 합 니 다.
javascript 고전 게임 모음
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기