• 使用 Pygame 构建和可视化数独游戏


    使用 Pygame 构建和可视化数独游戏

    原文地址

    数独是一个基于逻辑的组合数字放置谜题。目标是用数字填充 9×9 网格,以便每一列、每一行和组成网格的九个 3×3 子网格中的每一个都包含从 1 到 9 的所有数字。

    我们将使用pygame库在 python 中构建数独游戏,并使用回溯算法自动化游戏。

    实现的功能:

    • 游戏界面玩
    • 自动解决
    • 自动求解的可视化,即回溯算法可视化
    • 选项:重置,清除游戏

    先决条件:

    实施步骤:

    **1.**用 Sudoku Board 填充 pygame 窗口,即构建一个 9×9 的网格。
    **2.**用默认数字填充板。
    **3.**为每个操作分配一个特定的键并听它。
    **4.**将回溯算法融入其中。
    **5.**使用一组颜色来可视化自动求解。

    操作说明:

    • 按“Enter”自动求解和可视化。
    • 要手动玩游戏,
      请将光标放在您想要的任何单元格中并输入数字。
    • 在任何时候,按回车键自动求解。

    以下是实施:

    • Python3
    # import pygame library
    import pygame
    
    # initialise the pygame font
    pygame.font.init()
    
    # Total window
    screen = pygame.display.set_mode((500, 600))
    
    # Title and Icon
    pygame.display.set_caption("SUDOKU SOLVER USING BACKTRACKING")
    img = pygame.image.load('icon.png')
    pygame.display.set_icon(img)
    
    x = 0
    y = 0
    dif = 500 / 9
    val = 0
    # Default Sudoku Board.
    grid =[
    		[7, 8, 0, 4, 0, 0, 1, 2, 0],
    		[6, 0, 0, 0, 7, 5, 0, 0, 9],
    		[0, 0, 0, 6, 0, 1, 0, 7, 8],
    		[0, 0, 7, 0, 4, 0, 2, 6, 0],
    		[0, 0, 1, 0, 5, 0, 9, 3, 0],
    		[9, 0, 4, 0, 6, 0, 0, 0, 5],
    		[0, 7, 0, 3, 0, 0, 0, 1, 2],
    		[1, 2, 0, 0, 0, 7, 4, 0, 0],
    		[0, 4, 9, 2, 0, 6, 0, 0, 7]
    	]
    
    # Load test fonts for future use
    font1 = pygame.font.SysFont("comicsans", 40)
    font2 = pygame.font.SysFont("comicsans", 20)
    def get_cord(pos):
    	global x
    	x = pos[0]//dif
    	global y
    	y = pos[1]//dif
    
    # Highlight the cell selected
    def draw_box():
    	for i in range(2):
    		pygame.draw.line(screen, (255, 0, 0), (x * dif-3, (y + i)*dif), (x * dif + dif + 3, (y + i)*dif), 7)
    		pygame.draw.line(screen, (255, 0, 0), ( (x + i)* dif, y * dif ), ((x + i) * dif, y * dif + dif), 7)
    
    # Function to draw required lines for making Sudoku grid		
    def draw():
    	# Draw the lines
    		
    	for i in range (9):
    		for j in range (9):
    			if grid[i][j]!= 0:
    
    				# Fill blue color in already numbered grid
    				pygame.draw.rect(screen, (0, 153, 153), (i * dif, j * dif, dif + 1, dif + 1))
    
    				# Fill grid with default numbers specified
    				text1 = font1.render(str(grid[i][j]), 1, (0, 0, 0))
    				screen.blit(text1, (i * dif + 15, j * dif + 15))
    	# Draw lines horizontally and verticallyto form grid		
    	for i in range(10):
    		if i % 3 == 0 :
    			thick = 7
    		else:
    			thick = 1
    		pygame.draw.line(screen, (0, 0, 0), (0, i * dif), (500, i * dif), thick)
    		pygame.draw.line(screen, (0, 0, 0), (i * dif, 0), (i * dif, 500), thick)	
    
    # Fill value entered in cell	
    def draw_val(val):
    	text1 = font1.render(str(val), 1, (0, 0, 0))
    	screen.blit(text1, (x * dif + 15, y * dif + 15))
    
    # Raise error when wrong value entered
    def raise_error1():
    	text1 = font1.render("WRONG !!!", 1, (0, 0, 0))
    	screen.blit(text1, (20, 570))
    def raise_error2():
    	text1 = font1.render("Wrong !!! Not a valid Key", 1, (0, 0, 0))
    	screen.blit(text1, (20, 570))
    
    # Check if the value entered in board is valid
    def valid(m, i, j, val):
    	for it in range(9):
    		if m[i][it]== val:
    			return False
    		if m[it][j]== val:
    			return False
    	it = i//3
    	jt = j//3
    	for i in range(it * 3, it * 3 + 3):
    		for j in range (jt * 3, jt * 3 + 3):
    			if m[i][j]== val:
    				return False
    	return True
    
    # Solves the sudoku board using Backtracking Algorithm
    def solve(grid, i, j):
    	
    	while grid[i][j]!= 0:
    		if i<8:
    			i+= 1
    		elif i == 8 and j<8:
    			i = 0
    			j+= 1
    		elif i == 8 and j == 8:
    			return True
    	pygame.event.pump()
    	for it in range(1, 10):
    		if valid(grid, i, j, it)== True:
    			grid[i][j]= it
    			global x, y
    			x = i
    			y = j
    			# white color background\
    			screen.fill((255, 255, 255))
    			draw()
    			draw_box()
    			pygame.display.update()
    			pygame.time.delay(20)
    			if solve(grid, i, j)== 1:
    				return True
    			else:
    				grid[i][j]= 0
    			# white color background\
    			screen.fill((255, 255, 255))
    		
    			draw()
    			draw_box()
    			pygame.display.update()
    			pygame.time.delay(50)
    	return False
    
    # Display instruction for the game
    def instruction():
    	text1 = font2.render("PRESS D TO RESET TO DEFAULT / R TO EMPTY", 1, (0, 0, 0))
    	text2 = font2.render("ENTER VALUES AND PRESS ENTER TO VISUALIZE", 1, (0, 0, 0))
    	screen.blit(text1, (20, 520))	
    	screen.blit(text2, (20, 540))
    
    # Display options when solved
    def result():
    	text1 = font1.render("FINISHED PRESS R or D", 1, (0, 0, 0))
    	screen.blit(text1, (20, 570))
    run = True
    flag1 = 0
    flag2 = 0
    rs = 0
    error = 0
    # The loop thats keep the window running
    while run:
    	
    	# White color background
    	screen.fill((255, 255, 255))
    	# Loop through the events stored in event.get()
    	for event in pygame.event.get():
    		# Quit the game window
    		if event.type == pygame.QUIT:
    			run = False
    		# Get the mouse position to insert number
    		if event.type == pygame.MOUSEBUTTONDOWN:
    			flag1 = 1
    			pos = pygame.mouse.get_pos()
    			get_cord(pos)
    		# Get the number to be inserted if key pressed
    		if event.type == pygame.KEYDOWN:
    			if event.key == pygame.K_LEFT:
    				x-= 1
    				flag1 = 1
    			if event.key == pygame.K_RIGHT:
    				x+= 1
    				flag1 = 1
    			if event.key == pygame.K_UP:
    				y-= 1
    				flag1 = 1
    			if event.key == pygame.K_DOWN:
    				y+= 1
    				flag1 = 1
    			if event.key == pygame.K_1:
    				val = 1
    			if event.key == pygame.K_2:
    				val = 2
    			if event.key == pygame.K_3:
    				val = 3
    			if event.key == pygame.K_4:
    				val = 4
    			if event.key == pygame.K_5:
    				val = 5
    			if event.key == pygame.K_6:
    				val = 6
    			if event.key == pygame.K_7:
    				val = 7
    			if event.key == pygame.K_8:
    				val = 8
    			if event.key == pygame.K_9:
    				val = 9
    			if event.key == pygame.K_RETURN:
    				flag2 = 1
    			# If R pressed clear the sudoku board
    			if event.key == pygame.K_r:
    				rs = 0
    				error = 0
    				flag2 = 0
    				grid =[
    				[0, 0, 0, 0, 0, 0, 0, 0, 0],
    				[0, 0, 0, 0, 0, 0, 0, 0, 0],
    				[0, 0, 0, 0, 0, 0, 0, 0, 0],
    				[0, 0, 0, 0, 0, 0, 0, 0, 0],
    				[0, 0, 0, 0, 0, 0, 0, 0, 0],
    				[0, 0, 0, 0, 0, 0, 0, 0, 0],
    				[0, 0, 0, 0, 0, 0, 0, 0, 0],
    				[0, 0, 0, 0, 0, 0, 0, 0, 0],
    				[0, 0, 0, 0, 0, 0, 0, 0, 0]
    				]
    			# If D is pressed reset the board to default
    			if event.key == pygame.K_d:
    				rs = 0
    				error = 0
    				flag2 = 0
    				grid =[
    					[7, 8, 0, 4, 0, 0, 1, 2, 0],
    					[6, 0, 0, 0, 7, 5, 0, 0, 9],
    					[0, 0, 0, 6, 0, 1, 0, 7, 8],
    					[0, 0, 7, 0, 4, 0, 2, 6, 0],
    					[0, 0, 1, 0, 5, 0, 9, 3, 0],
    					[9, 0, 4, 0, 6, 0, 0, 0, 5],
    					[0, 7, 0, 3, 0, 0, 0, 1, 2],
    					[1, 2, 0, 0, 0, 7, 4, 0, 0],
    					[0, 4, 9, 2, 0, 6, 0, 0, 7]
    				]
    	if flag2 == 1:
    		if solve(grid, 0, 0)== False:
    			error = 1
    		else:
    			rs = 1
    		flag2 = 0
    	if val != 0:		
    		draw_val(val)
    		# print(x)
    		# print(y)
    		if valid(grid, int(x), int(y), val)== True:
    			grid[int(x)][int(y)]= val
    			flag1 = 0
    		else:
    			grid[int(x)][int(y)]= 0
    			raise_error2()
    		val = 0
    	
    	if error == 1:
    		raise_error1()
    	if rs == 1:
    		result()	
    	draw()
    	if flag1 == 1:
    		draw_box()	
    	instruction()
    
    	# Update window
    	pygame.display.update()
    
    # Quit pygame window
    pygame.quit()	
    	
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265

    输出:

    在这里插入图片描述

  • 相关阅读:
    JAVAEE初阶 操作系统
    Springboot知识点必知必会(一)
    Vite2.0+Typescript+React+Antd+Less+Eslint+Prettier+Precommit构建标准化react应用
    修改 MySQL 最大连接数
    电脑重装系统后usbcleaner怎么格式化u盘
    东北大学acm暑期夏令营指针与引用初步
    GGTalk 开源即时通讯系统源码剖析之:虚拟数据库
    进程的概念
    【图像去噪】基于matlab偏微分方程PDE图像去噪【含Matlab源码 1890期】
    互联网刚需岗位 前景一片大好?
  • 原文地址:https://blog.csdn.net/acktomas/article/details/126228612