Example #1
0
func check_field_error(f bool, y1 uint, x1 uint, y2 uint, x2 uint) bool {
	if f {
		entries[y1][x1].GrabFocus()
	}
	modify_base(unsafe.Pointer(entries[y2][x2].Widget), gdk.Color("red"))
	return false
}
Example #2
0
func Init(size uint) {
	var (
		files          *gtk.GtkHBox
		examples_cnt   int
		newfile_flag   bool
		cs_desc_normal *_Ctype_char
		cs_desc_bold   *_Ctype_char
	)

	s = new(solver.Solver)
	s.Size = size

	if s.Size == 9 {
		cs_desc_normal = C.CString("Sans 14")
		cs_desc_bold = C.CString("Sans Bold 14")
	} else {
		cs_desc_normal = C.CString("Sans 16")
		cs_desc_bold = C.CString("Sans Bold 16")
	}
	desc_normal = C.pango_font_description_from_string(cs_desc_normal)
	desc_bold = C.pango_font_description_from_string(cs_desc_bold)
	C.free_string(cs_desc_normal)
	C.free_string(cs_desc_bold)

	gtk.Init(&os.Args)

	window := gtk.Window(gtk.GTK_WINDOW_TOPLEVEL)
	window.SetResizable(false)
	window.SetTitle("Sudoku solver")
	window.Connect("destroy", func() {
		gtk.MainQuit()
	})
	window.Connect("key-press-event", func(ctx *glib.CallbackContext) {
		arg := ctx.Args(0)
		kev := *(**gdk.EventKey)(unsafe.Pointer(&arg))
		r, st := rune(kev.Keyval), gdk.GdkModifierType(kev.State)
		if st&gdk.GDK_CONTROL_MASK != 0 {
			if r == 122 || r == 90 { // Ctrl-Z
				m := cs_pop()
				clear()
				for i := uint(0); i < s.Size; i++ {
					for j := uint(0); j < s.Size; j++ {
						v := int(m[i][j])
						if v != 0 {
							entries[i][j].SetText(strconv.Itoa(v))
						}
					}
				}
			}
		}
	})

	vbox := gtk.VBox(false, 10)

	table := gtk.Table(3, s.Size/3, false)
	bg := [2]*gdk.GdkColor{gdk.Color("white"), gdk.Color("#e9f2ea")}
	for y := uint(0); y < 3; y++ {
		for x := uint(0); x < s.Size/3; x++ {
			subtable := gtk.Table(s.Size/3, 3, false)
			for sy := uint(0); sy < s.Size/3; sy++ {
				for sx := uint(0); sx < 3; sx++ {
					w := gtk.Entry()
					w.SetWidthChars(1)
					w.SetMaxLength(1)
					if s.Size == 9 {
						w.SetSizeRequest(23, 25)
					} else {
						w.SetSizeRequest(25, 27)
					}
					w.Connect("key-press-event", func(ctx *glib.CallbackContext) bool {
						data := ctx.Data().([]uint)
						y, x := data[0], data[1]
						arg := ctx.Args(0)
						kev := *(**gdk.EventKey)(unsafe.Pointer(&arg))
						r := rune(kev.Keyval)
						switch r & 0xFF {
						case 81:
							if x != 0 || y != 0 {
								if x == 0 {
									x = s.Size - 1
									y--
								} else {
									x--
								}
							}
						case 82:
							if y != 0 {
								y--
							}
						case 83:
							if x != s.Size-1 || y != s.Size-1 {
								if x == s.Size-1 {
									x = 0
									y++
								} else {
									x++
								}
							}
						case 84:
							if y != s.Size-1 {
								y++
							}
						}
						if y != data[0] || x != data[1] {
							entries[y][x].GrabFocus()
						}
						if unicode.IsOneOf([]*unicode.RangeTable{unicode.L, unicode.Z}, r) {
							return true
						}
						return false
					}, []uint{(s.Size/3)*y + sy, 3*x + sx})
					w.Connect("grab-focus", func(ctx *glib.CallbackContext) {
						data := ctx.Data().([]uint)
						y, x := data[0], data[1]
						for k := 0; k < 2; k++ {
							for i := uint(0); i < s.Size; i++ {
								modify_base(unsafe.Pointer(entries[i][prev_x].Widget), bg[k])
							}
							for j := uint(0); j < s.Size; j++ {
								modify_base(unsafe.Pointer(entries[prev_y][j].Widget), bg[k])
							}
							prev_y, prev_x = y, x
						}
					}, []uint{(s.Size/3)*y + sy, 3*x + sx})
					subtable.Attach(w, sx, sx+1, sy, sy+1, gtk.GTK_FILL, gtk.GTK_FILL, 0, 0)
					entries[(s.Size/3)*y+sy][3*x+sx] = w
					modify_font((s.Size/3)*y+sy, 3*x+sx, desc_bold)
				}
			}
			table.Attach(subtable, x, x+1, y, y+1, gtk.GTK_FILL, gtk.GTK_FILL, 3, 3)
		}
	}

	solve_btn := gtk.ButtonWithLabel("Solve")
	solve_btn.Clicked(func() {
		var m1, m2 [9][9]uint

		for i := uint(0); i < s.Size; i++ {
			for j := uint(0); j < s.Size; j++ {
				v, _ := strconv.Atoi(entries[i][j].GetText())
				m1[i][j] = uint(v)
			}
		}
		if !check_field(&m1) {
			return
		}
		cs_push(m1)
		s.Load(m1)
		s.Solve()
		if s.Finals != s.Size*s.Size {
			// let's try some tough algorithms :)
			s.ToughSolve()
		}
		for i := uint(0); i < s.Size; i++ {
			for j := uint(0); j < s.Size; j++ {
				v := int(s.Get(i, j))
				m2[i][j] = uint(v)
				if v != 0 {
					entries[i][j].SetText(strconv.Itoa(v))
				} else {
					var c_string [9]string
					// get list of candidates
					c_uint, l := s.GetCandidates(i, j)
					for k := uint(0); k < l; k++ {
						c_string[k] = strconv.Itoa(int(c_uint[k]))
					}
					// make a tooltip with them
					entries[i][j].SetTooltipText(strings.Join(c_string[:l], " "))
				}
			}
		}
		// check for differences
		for i := uint(0); i < s.Size; i++ {
			for j := uint(0); j < s.Size; j++ {
				if m1[i][j] == m2[i][j] {
					modify_font(i, j, desc_bold)
				} else {
					modify_font(i, j, desc_normal)
				}
			}
		}
	})
	clear_btn := gtk.ButtonWithLabel("Clear")
	clear_btn.Clicked(func() {
		m := [9][9]uint{}
		for i := uint(0); i < s.Size; i++ {
			for j := uint(0); j < s.Size; j++ {
				m[i][j] = s.Get(i, j)
			}
		}
		cs_push(m)
		clear()
	})

	examples = gtk.ComboBoxText()
	// scan `examples` folder
	sz := strconv.Itoa(int(s.Size))
	dir, err := os.Open("examples/" + sz + "x" + sz)
	if err == nil {
		names, err := dir.Readdirnames(0)
		if err == nil {
			for _, v := range names {
				examples.AppendText(v)
				examples_cnt++
			}
		}
		dir.Close()
	}
	examples.Connect("changed", func() {
		sz := strconv.Itoa(int(s.Size))
		load_sudoku("examples/" + sz + "x" + sz + "/" + examples.GetActiveText())
	})

	newfile := gtk.Entry()
	newfile.Connect("activate", func() {
		filename := newfile.GetText()
		if filename != "" {
			sz := strconv.Itoa(int(s.Size))
			f, err := os.Create("examples/" + sz + "x" + sz + "/" + filename)
			if err == nil {
				for i := uint(0); i < s.Size; i++ {
					for j := uint(0); j < s.Size; j++ {
						v := []byte(entries[i][j].GetText())
						if len(v) == 0 || v[0] < 49 || v[0] > byte(48+s.Size) {
							v = []byte{' '}
						}
						f.Write(v)
						if (j+1)%3 == 0 && j+1 != s.Size {
							f.WriteString("*")
						}
					}
					f.WriteString("\n")
					if (i+1)%(s.Size/3) == 0 && i+1 != s.Size {
						if s.Size == 9 {
							f.WriteString("***********\n")
						} else {
							f.WriteString("*******\n")
						}
					}
				}
				f.Close()
			}
			examples.AppendText(filename)
			examples.SetActive(examples_cnt)
			examples_cnt++
		}
		files.ShowAll()
		newfile.SetText("")
		newfile.Hide()
	})

	icon := gtk.Image()
	icon.SetFromStock(gtk.GTK_STOCK_SAVE_AS, gtk.GTK_ICON_SIZE_BUTTON)
	export := gtk.Button()
	export.SetImage(icon)
	export.Clicked(func() {
		if !newfile_flag {
			files.Add(newfile)
			newfile_flag = true
		}
		files.ShowAll()
		examples.Hide()
		export.Hide()
		newfile.GrabFocus()
	})

	files = gtk.HBox(false, 0)
	files.Add(export)
	files.Add(examples)

	buttons := gtk.HBox(true, 5)
	buttons.Add(solve_btn)
	buttons.Add(clear_btn)

	vbox.Add(table)
	vbox.Add(files)
	vbox.Add(buttons)

	window.Add(vbox)
	window.ShowAll()
}
Example #3
0
func main() {
	gtk.Init(&os.Args)
	window := gtk.Window(gtk.GTK_WINDOW_TOPLEVEL)
	window.SetTitle("GTK DrawingArea")
	window.Connect("destroy", gtk.MainQuit)

	vbox := gtk.VBox(true, 0)
	vbox.SetBorderWidth(5)
	drawingarea := gtk.DrawingArea()

	var p1, p2 point
	var gdkwin *gdk.GdkWindow
	var pixmap *gdk.GdkPixmap
	var gc *gdk.GdkGC
	p1.x = -1
	p1.y = -1

	drawingarea.Connect("configure-event", func() {
		if pixmap != nil {
			pixmap.Unref()
		}
		var allocation gtk.GtkAllocation
		drawingarea.GetAllocation(&allocation)
		pixmap = gdk.Pixmap(drawingarea.GetWindow().GetDrawable(), allocation.Width, allocation.Height, 24)
		gc = gdk.GC(pixmap.GetDrawable())
		gc.SetRgbFgColor(gdk.Color("white"))
		pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1)
		gc.SetRgbFgColor(gdk.Color("black"))
		gc.SetRgbBgColor(gdk.Color("white"))
	})

	drawingarea.Connect("motion-notify-event", func(ctx *glib.CallbackContext) {
		if gdkwin == nil {
			gdkwin = drawingarea.GetWindow()
		}
		arg := ctx.Args(0)
		mev := *(**gdk.EventMotion)(unsafe.Pointer(&arg))
		var mt gdk.GdkModifierType
		if mev.IsHint != 0 {
			gdkwin.GetPointer(&p2.x, &p2.y, &mt)
		} else {
			p2.x, p2.y = int(mev.X), int(mev.Y)
		}
		if p1.x != -1 && p2.x != -1 && (gdk.GdkEventMask(mt)&gdk.GDK_BUTTON_PRESS_MASK) != 0 {
			pixmap.GetDrawable().DrawLine(gc, p1.x, p1.y, p2.x, p2.y)
			drawingarea.GetWindow().Invalidate(nil, false)
		}
		p1 = p2
	})

	drawingarea.Connect("expose-event", func() {
		if pixmap != nil {
			drawingarea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1)
		}
	})

	drawingarea.SetEvents(int(gdk.GDK_POINTER_MOTION_MASK | gdk.GDK_POINTER_MOTION_HINT_MASK | gdk.GDK_BUTTON_PRESS_MASK))
	vbox.Add(drawingarea)

	window.Add(vbox)
	window.SetSizeRequest(400, 400)
	window.ShowAll()

	gtk.Main()
}
Example #4
0
func main() {
	var prueba int

	var Actual Juego

	Actual.Init()

	var heuristica int

	heuristica = 1

	var r1 [4]int
	var r2 [8]int

	Actual.fila1 = Actual.fila1Objetivo
	Actual.fila2 = Actual.fila2Objetivo

	r1 = Actual.fila1Objetivo
	r2 = Actual.fila2Objetivo

	//fmt.Println(r1,r2)

	prueba = 100
	//var menuitem *gtk.GtkMenuItem
	gtk.Init(nil)
	window := gtk.Window(gtk.GTK_WINDOW_TOPLEVEL)
	window.SetPosition(gtk.GTK_WIN_POS_CENTER)
	window.SetTitle(" A star")
	window.Connect("destroy", func(ctx *glib.CallbackContext) {
		println("got destory!\n", ctx.Data().(string))
		gtk.MainQuit()
	}, "foo")

	vbox := gtk.HBox(false, 14)

	//menubar:=gtk.MenuBar()
	//vbox.PackStart(menubar,false,false,0)

	vpaned := gtk.VPaned()
	vbox.Add(vpaned)

	frame := gtk.Frame("Profundiad")
	framebox := gtk.HBox(false, 14)

	framebox1 := gtk.VBox(true, 0)
	frame1 := gtk.Frame("Rompecabezas ")

	frame.Add(framebox)
	frame1.Add(framebox1)

	drawingarea := gtk.DrawingArea()

	vpaned.Add(frame)
	vpaned.Add(frame1)

	button := gtk.ButtonWithLabel("Resolver")
	button2 := gtk.ButtonWithLabel("Desordenar")
	button3 := gtk.ButtonWithLabel(" Nivel de Desorden")

	//TxtPasos:=gtk.Entry()

	frameHu := gtk.VBox(true, 2)

	lheuristica := gtk.Label("Heuristica")
	comboBox := gtk.ComboBoxEntryNewText()
	comboBox.AppendText("Piezas Desordenadas")
	comboBox.AppendText("Distancia entre Piezas")

	comboBox.Connect("changed", func() {
		if strings.EqualFold(comboBox.GetActiveText(), "Piezas Desordenadas") {
			heuristica = 2
		} else {
			heuristica = 1
		}
	})

	frameHu.Add(lheuristica)
	frameHu.Add(comboBox)

	entry := gtk.Entry()

	framebox.Add(entry)

	framebox.Add(button)
	framebox.Add(frameHu)

	framebox.Add(button2)
	framebox.Add(button3)

	//framebox1.Add(TxtPasos)
	//var gdkwin *gdk.GdkWindow
	var pixmap *gdk.GdkPixmap
	var gc *gdk.GdkGC

	drawingarea.Connect("configure-event", func() {
		if pixmap != nil {
			pixmap.Unref()
		}
		var allocation gtk.GtkAllocation
		drawingarea.GetAllocation(&allocation)
		pixmap = gdk.Pixmap(drawingarea.GetWindow().GetDrawable(), allocation.Width, allocation.Height, 24)
		gc = gdk.GC(pixmap.GetDrawable())
		gc.SetRgbFgColor(gdk.Color("white"))
		pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1)
		gc.SetRgbFgColor(gdk.Color("blue"))
		gc.SetRgbBgColor(gdk.Color("white"))
		pixmap.GetDrawable().DrawArc(gc, false, 100, 100, 200, 200, 0, 30000)
		pixmap.GetDrawable().DrawArc(gc, false, 150, 150, 100, 100, 0, 30000)

		pixmap.GetDrawable().DrawLine(gc, 200, 100, 200, 300)
		pixmap.GetDrawable().DrawLine(gc, 100, 200, 300, 200)

		pixmap.GetDrawable().DrawLine(gc, 130, 130, 165, 165)
		pixmap.GetDrawable().DrawLine(gc, 235, 235, 270, 270)

		pixmap.GetDrawable().DrawLine(gc, 271, 129, 235, 165)
		pixmap.GetDrawable().DrawLine(gc, 165, 235, 129, 271)

		pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 200, 200, strconv.Itoa(prueba))

		pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 100, 200, strconv.Itoa(prueba))

		pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 100, 300, strconv.Itoa(prueba))
		pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 200, 300, strconv.Itoa(prueba))

	})

	drawingarea.Connect("expose-event", func() {
		if pixmap != nil {
			drawingarea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1)
			gc.SetRgbFgColor(gdk.Color("white"))
			pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1)
			gc.SetRgbFgColor(gdk.Color("blue"))
			gc.SetRgbBgColor(gdk.Color("white"))
			pixmap.GetDrawable().DrawArc(gc, false, 100, 100, 200, 200, 0, 30000)
			pixmap.GetDrawable().DrawArc(gc, false, 150, 150, 100, 100, 0, 30000)

			pixmap.GetDrawable().DrawLine(gc, 200, 100, 200, 300)
			pixmap.GetDrawable().DrawLine(gc, 100, 200, 300, 200)

			pixmap.GetDrawable().DrawLine(gc, 130, 130, 165, 165)
			pixmap.GetDrawable().DrawLine(gc, 235, 235, 270, 270)

			pixmap.GetDrawable().DrawLine(gc, 271, 129, 235, 165)
			pixmap.GetDrawable().DrawLine(gc, 165, 235, 129, 271)

			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 210, 190, strconv.Itoa(r1[0]))
			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 210, 140, strconv.Itoa(r2[0]))
			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 260, 190, strconv.Itoa(r2[1]))

			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 210, 230, strconv.Itoa(r1[1]))
			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 260, 230, strconv.Itoa(r2[2]))
			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 210, 280, strconv.Itoa(r2[3]))

			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 170, 230, strconv.Itoa(r1[2]))
			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 170, 280, strconv.Itoa(r2[4]))
			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 120, 230, strconv.Itoa(r2[5]))

			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 170, 190, strconv.Itoa(r1[3]))
			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 120, 190, strconv.Itoa(r2[6]))
			pixmap.GetDrawable().DrawString(gdk.FontsetLoad("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1"), gc, 170, 140, strconv.Itoa(r2[7]))

			drawingarea.GetWindow().Invalidate(nil, false)
		}
	})

	button.Clicked(func() {
		//prueba=prueba+1
		vdeep := entry.GetText()

		fo, err := os.Create("Pasos")
		if err != nil {
			panic(err)
		}
		defer fo.Close()

		Actual.tipoHeuristica = heuristica
		Actual.HeuristicaJuego()

		deep, err := strconv.Atoi(vdeep)
		if err == nil {
			fmt.Println("", deep)
			b, camino := AStar(Actual, deep)
			if b {

				for i := 0; i < len(camino); i++ {

					r1 = camino[i].fila1
					r2 = camino[i].fila2

					var Valores string
					Valores = "["

					for j := 0; j < len(r1); j++ {
						Valores = Valores + " " + strconv.Itoa(r1[j])
					}
					Valores = Valores + "]["

					for j := 0; j < len(r2); j++ {
						Valores = Valores + " " + strconv.Itoa(r2[j])
					}
					Valores = Valores + "] " + strconv.Itoa(camino[i].heuristica) + "\n"

					if _, err := fo.WriteString(Valores); err != nil {
						panic(err)
					}

				}

			} else {
				fmt.Println("No encontro la soluciĆ³n")
			}
		}
	})

	button2.Clicked(func() {

		Actual.Init()
		Actual.tipoHeuristica = heuristica
		Actual.HeuristicaJuego()

		r1 = Actual.fila1
		r2 = Actual.fila2

		//prueba=prueba+1
		//fmt.Println("->",prueba)
	})

	button3.Clicked(func() {

		Actual.fila1 = r1
		Actual.fila2 = r2

		Actual.Init2()
		Actual.tipoHeuristica = heuristica
		Actual.HeuristicaJuego()

		r1 = Actual.fila1
		r2 = Actual.fila2
	})

	drawingarea.SetEvents(int(gdk.GDK_POINTER_MOTION_MASK | gdk.GDK_POINTER_MOTION_HINT_MASK | gdk.GDK_BUTTON_PRESS_MASK))
	framebox1.Add(drawingarea)

	window.Add(vbox)
	window.SetSizeRequest(600, 600)
	window.ShowAll()
	gtk.Main()
}