-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
82 lines (73 loc) · 3.45 KB
/
Main.hs
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
module Main where
import System.IO
import Generator
import AuxiliaryFunctions
import AlgoritmsOfVerification
import BackTracking
main :: IO()
main = do
hSetBuffering stdin NoBuffering
hSetBuffering stdout NoBuffering
mostraAbertura
printaMenu
mostraAbertura :: IO()
mostraAbertura = do
putStrLn "***************************************"
putStrLn "************** SUDOKU *****************"
putStrLn "***************************************"
printaMenu :: IO()
printaMenu = do
putStrLn "1. Gerar Sudoku"
putStrLn "2. Resolvedor de sudoku"
putStrLn "3. Sair"
opcao <- getLine
escolherOpcaoMenu opcao
escolherOpcaoMenu :: String -> IO()
escolherOpcaoMenu "1" = do {
gerarSudoku
}
escolherOpcaoMenu "2" = resolverSudoku
escolherOpcaoMenu "3" = return ()
escolherOpcaoMenu _ = do {putStrLn "";
putStrLn "Opçao invalida, tente novamente!";
printaMenu
}
resolverSudoku :: IO()
sudokuInserido = [[' ',' ','2',' ',' ','6','8',' ',' '],['3',' ',' ',' ','5',' ',' ',' ','1'],[' ','4',' ','9',' ',' ',' ','3',' '],[' ',' ',' ',' ',' ','4','5',' ','6'],[' ','5',' ',' ','6',' ',' ','1',' '],['1',' ','8','5',' ',' ',' ',' ',' '],[' ','3',' ',' ',' ','7',' ','6',' '],['8',' ',' ',' ','2',' ',' ',' ','3'],[' ',' ','7','4',' ',' ','9',' ',' ']]
(a,sudokuResolvido) = backTrackingResolution sudokuInserido (0,0) 1
resolverSudoku = do
mostrarSudoku sudokuResolvido
gerarSudoku :: IO()
sudoku = [[' ',' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' ',' ']]
gerado = gerador sudoku 0 0 25
gerarSudoku = do {
escolheCoordenadas gerado
}
escolheCoordenadas :: [[Char]] -> IO()
escolheCoordenadas matriz = do
mostrarSudoku matriz
putStr "Escolha a linha (0 para desistir):"
linha <- readLn :: IO Int
putStr "Escolha a coluna (0 para desistir):"
coluna <- readLn :: IO Int
putStr "Insira o numero: "
numero <- readLn :: IO Int
if (linha == 0 && coluna == 0)
then do
let (inteiro,matrizNova) = verificaDesistencia matriz (-1) (-1) (retornaNumerosInChar numero)
mostrarSudoku matrizNova
else do
if ((verificaTotalModific matriz (linha - 1) (coluna - 1) (retornaNumerosInChar numero)) == 1) && ((retornaElemento matriz (0,0) ((linha - 1),(coluna - 1))) == ' ') && (numero >= 1 && numero <= 9)
then do
let (inteiro,matrizNova) = verificaDesistencia matriz (linha - 1) (coluna - 1) (retornaNumerosInChar numero)
if (inteiro == 1)
then do
mostrarSudoku matrizNova
putStrLn "Sudoku concluido!"
else escolheCoordenadas matrizNova
else do
putStrLn "Não foi possível adicionar esse número!"
escolheCoordenadas matriz
verificaDesistencia :: [[Char]] -> Int -> Int -> Char -> (Int,[[Char]])
verificaDesistencia matriz (-1) (-1) z = backTrackingResolution matriz (0,0) 1
verificaDesistencia matriz x y z = (0,adicionaElementoPosicao matriz (0,0) (x,y) z)