首页 > 编程语言 >【Python】数独游戏

【Python】数独游戏

时间:2023-10-08 18:15:54浏览次数:40  
标签:游戏 Python self num board SIZE col 数独 row

Start

import random
class SudokuGenerator:
    BOARD_SIZE=9
    SUBGRID_SIZE=3
    def __init__(self) -> None:
        self.board = [[0 for _ in range(self.BOARD_SIZE)] for _ in range(self.BOARD_SIZE)]
    
    def generate(self):
        self.fill_values()
        return self.board

    def fill_values(self):
        self.fill_diagonal_subgrids()
        self.fill_remaining(0,self.SUBGRID_SIZE)

    def fill_diagonal_subgrids(self):
        for i in range(0,self.BOARD_SIZE, self.SUBGRID_SIZE):
            self.fill_subgrid(i,i)

    def fill_remaining(self, row, col):
        if row == self.BOARD_SIZE-1 and col == self.BOARD_SIZE:
            return True
        
        if col == self.BOARD_SIZE:
            row += 1
            col = 0

        if self.board[row][col] !=0:
            return self.fill_remaining(row,col+1)
        
        for num in range(1,self.BOARD_SIZE+1):
            if self.is_safe_to_place_num(row,col,num):
                self.board[row][col] = num
                if self.fill_remaining(row,col+1):
                    return True
        self.board[row][col] = 0
        return False

    def fill_subgrid(self, row, col):
        for i in range(self.SUBGRID_SIZE):
            for j in range(self.SUBGRID_SIZE):
                num = 0
                while not self.is_safe_to_place_num(row+i,col+j,num):
                    num = self.get_random_number()
                self.board[row+i][col+j] = num

    def is_safe_to_place_num(self, row, col, num):
        return not self.is_in_row(row,num) and not self.is_in_col(col,num) and not self.is_in_subgrid(row-row % self.SUBGRID_SIZE,col-col % self.SUBGRID_SIZE,num)
                

    def is_in_row(self, row, num):
        return num in self.board[row]
    
    def is_in_col(self, col, num):
        return num in [self.board[i][col] for i in range(self.BOARD_SIZE)]

    def is_in_subgrid(self, row, col, num):
        for i in range(self.SUBGRID_SIZE):
            for j in range(self.SUBGRID_SIZE):
                if self.board[row+i][col+j] == num:
                    return True
        return False

    def get_random_number(self):
        return random.randint(1,self.BOARD_SIZE)

class Sudoku:
    def __init__(self, board) -> None:
        self.board = board
    
    def printf(self):
        for row in self.board:
            print(" ".join(str(num) for num in row))

class SudokuGame:
    def main(self):
        generator = SudokuGenerator()
        board = generator.generate()
        sudoku = Sudoku(board)
        sudoku.printf();

if __name__ == "__main__":
    game = SudokuGame()
    game.main()

 

End

标签:游戏,Python,self,num,board,SIZE,col,数独,row
From: https://www.cnblogs.com/lnsylt/p/17749792.html

相关文章

  • Python入门示例系列10 字符串(初级)
     字符串(string)Python中的字符串用单引号'或双引号"括起来,同时使用反斜杠\转义特殊字符。'ABC' 与"ABC"完全等价。示例:s1="hello"#双引号"括起来s2='hello'#单引号'括起来s3=''#空字符串s4=""#空字符串s5='''......
  • Python入门示例系列07 Python注释
     Python中的注释有单行注释(linecomment)和多行注释(paragraphcomment,blockcomment):Python中单行注释以#(hash,pound)开头,例如:#这是一个单行注释acommentlineprint("Hello!")#这是一个单行注释acommentline示例:#thisisthefirstcommentspam=1#andthisist......
  • Python入门示例系列06 使用PyCharm单步调试
    摘要: 使用PyCharm单步调试视频演示(0分52秒)启动PyCharm#新建Project#输入项目位置#注意:选定解释器新建py文件输入文件名,按回车输入代码a=1b=2c=3print("helloworld")左侧点击设置断点调试(代码区域,右键,调试)查看Variable结果 系列目录Python入门示例系列0......
  • Python入门示例系列05 使用PyCharm
     摘要: 使用PyCharm视频演示(1分35秒) 启动PyCharm新建Project【File菜单--NewProject】输入项目位置【即保存项目文件,代码文件的位置】注意:选定解释器【如果安装了多个Python,要选择合适的编译器,Python.exe文件】新建py文件【File菜单--New--Pythonfile】输......
  • Python入门示例系列04 使用 IDLE Shell
    启动IDLEShell安装Python之后,点击开始菜单,找到P开头的菜单项,点开某个版本的Python,如下图:  点击IDLE    输入代码之后按回车Enter可以运行代码【上图,输入x=1+2,然后按回车,即开始执行】输入变量(对象)名可以查看值【上图,输入x,可以查看x的值】Alt+P上一条代码(st......
  • Python入门示例系列03 安装Python开发工具
    视频:https://www.bilibili.com/video/BV1yZ4y1f7so?spm_id_from=333.999.0.0 Python常用的IDE有:IDLE(python自带,推荐安装Python),PyCharm(推荐安装),WingIDE,JupyterNotebook(推荐安装Anaconda3),其中 IDLE,PyCharm,WingIDE是专为Python语言使用的IDE。 下载Python Python3.6......
  • Python入门示例系列02 Python 语言的特点
    Python语言的特点视频:https://www.bilibili.com/video/BV1JF411p7k8?spm_id_from=333.999.0.0优点:简单易学--阅读一个良好的Python程序就感觉像是在读英语一样。它使你能够专注于解决问题而不是去搞明白语言本身。Python容易上手。Python有极其简单的语法。注:面向对象的语法与......
  • Python 电子版PDF图书教材下载
    Python基础教程(第3版)ISBN:9787115474889《Python基础教程第三版》原版高清中文PDF,483页,带书签目录,文字可复制;《Python基础教程第三版》原版高清英文PDF,544页,带书签目录,文字可复制;配有源代码。链接:https://pan.baidu.com/s/1RosPwnmen8LfxM3QHG420g提取码:1234  Python编程从入......
  • python 使用 PyAV 进行 rtmp 直播推流
    importcv2importtimeimportnumpyasnpimportavimportav.datasetsimportlibrosafromfractionsimportFraction#创建一个FLV容器container=av.open('rtmp://test-push.xxxxx.com/livetest/video1',mode='w',format='flv')#......
  • Python程序调用图(Call Graph)
      vitsalis/PyCG:StaticPythoncallgraphgenerator(github.com)2103.00587.pdf(arxiv.org) PyCG-PracticalPythonCallGraphs PyCGgeneratescallgraphsforPythoncodeusingstaticanalysis.ItefficientlysupportsHigherorderfunctionsTwisted......