Minesweeper Clone V2

I made it so spaces with no mines adjacent are empty instead of 0 and that clicking those spaces autofills the surrounding spaces.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 24 02:41:21 2022

@author: jtjumper
"""
import random
import numpy
import tkinter as tk
import tkinter.font as tkFont


def boardmaker(dimension_x, dimension_y,mine_count):
	mines = {(random.randint(0, dimension_x-1), 
			random.randint(0, dimension_y-1)) }
	while len(mines)<mine_count:
		mines.add((random.randint(0, dimension_x-1), 
		random.randint(0, dimension_y-1)))
		
	return [[(1 if (x,y) in mines else 0)  for x in range(dimension_x)] 
	for y in range(dimension_y)]

def minesweeper_numbers(board):
	if board==[]:
		return []
	else: 
		return [[mine_spaces(x,y,board) for x in range(len(board[0]))]
		for y in range(len(board))]
def mine_spaces(x,y,board):
	return 9 if board[y][x]==1 else	numpy.sum(
	numpy.array(board)[max(0,y-1):min(len(board),
	y+2),max(0,x-1):min(len(board[0]),x+2)])

class App:
	dx , dy , mine_count = 10, 10, 10
	space_count=dx*dy
	free_spaces= space_count-mine_count
	spaces_checked = 0
	board = boardmaker(dx, dy, mine_count)
	mboard = minesweeper_numbers(board)
	GLabel_830=0
	GButton_array=0
	playing=True
	for x in board:
		print(x)
	print(sum(sum(x for x in y) for y in board))
	print ("Nums:")
	for x in mboard:
		print(["@" if y==9 else str(y) for y in x])
	def __init__(self, root):
		#setting title
		root.title("Tippin Mine Search")
		#setting window size
		width=500
		height=350
		screenwidth = root.winfo_screenwidth()
		screenheight = root.winfo_screenheight()
		alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
		root.geometry(alignstr)
		root.resizable(width=False, height=False)
		dimension_x, dimension_y =App.dx ,App.dy 
		
		App.GButton_array=[[self.make_button(self,30*x+20,30*y+20,y,x) for x in range(0,dimension_x)] for y in range(0,dimension_y)]
		
		GLabel_830=tk.Label(root)
		ft = tkFont.Font(family='Times',size=10)
		GLabel_830["font"] = ft
		GLabel_830["fg"] = "#333333"
		GLabel_830["justify"] = "center"
		GLabel_830["text"] = "Spaces checked: 0"
		GLabel_830.place(x=360,y=70,width=120,height=25)
		App.GLabel_830=GLabel_830

		GButton_Reset=tk.Button(root)
		GButton_Reset["bg"] = "#f0f0f0"
		ft = tkFont.Font(family='Times',size=10)
		GButton_Reset["font"] = ft
		GButton_Reset["fg"] = "#000000"
		GButton_Reset["justify"] = "center"
		GButton_Reset["text"] = "Reset"
		GButton_Reset.place(x=360,y=120,width=120,height=30)
		GButton_Reset["command"] = self.reset

	
	def make_button(self,text,x,y,xidx,yidx):
		GButton_Place=tk.Button(root)
		GButton_Place["bg"] = "#f0f0f0"
		ft = tkFont.Font(family='Times',size=10)
		GButton_Place["font"] = ft
		GButton_Place["fg"] = "#000000"
		GButton_Place["justify"] = "center"
		GButton_Place["text"] = "?"
		GButton_Place.place(x=x,y=y,width=30,height=30)
		GButton_Place["command"] = lambda : self.button_command( xidx, yidx, 	GButton_Place)
		return GButton_Place
	def button_command(self,x,y,button):
		if button["text"]=="?" and App.playing:
			val =App.mboard[x][y]
			button["text"] = str("@" if val==9 else  " "  if val==0 else str(val))
			print("Space " + str(x) + ", " + str(y) +" is " + button["text"]) 
			App.spaces_checked+=1
			if App.mboard[x][y]!=9:
				App.GLabel_830["text"] = "Spaces checked: " + str(App.spaces_checked)
				#auto click for spaces with zero surrounding mines
				if App.mboard[x][y]==0:
					print(range(max(0,x-1),min(App.dx,x+2)))
					print(range(max(0,y-1),min(App.dy,y+2)))
					for i in range(max(0,x-1),min(App.dx,x+2)):
						for j in range(max(0,y-1),min(App.dy,y+2)):
							App.button_command(self, i, j, App.GButton_array[i][j])
			else:
				App.playing = False
				App.GLabel_830["text"] = "You Lose"
				pass
			if App.spaces_checked==App.free_spaces:
				App.win(self)
	def win(self):
		App.playing = False
		App.GLabel_830["text"] = "You Win!"
	def reset(self):
		App.spaces_checked = 0
		App.GLabel_830["text"] = "Spaces checked: 0"
		App.board = boardmaker(App.dx, App.dy, App.mine_count)
		App.mboard = minesweeper_numbers(App.board)
		App.playing = True 
		for y in App.GButton_array:
			for x in y:
				x["text"]="?"

	
	
if __name__ == "__main__":
	root = tk.Tk()
	app = App(root)
	root.mainloop()

This entry was posted in Uncategorized. Bookmark the permalink.