takuzu.go
changeset 2 d41c4f8fe066
parent 0 00371339bbcc
child 3 8415b6bd06e9
equal deleted inserted replaced
1:6a396d691a7d 2:d41c4f8fe066
    38 }
    38 }
    39 
    39 
    40 // NewFromString creates a new Takuzu board from a string definition
    40 // NewFromString creates a new Takuzu board from a string definition
    41 func NewFromString(s string) (*Takuzu, error) {
    41 func NewFromString(s string) (*Takuzu, error) {
    42 	l := len(s)
    42 	l := len(s)
    43 	// TODO: validate chars ([.01OI])
    43 	if l < 4 {
       
    44 		return nil, errors.New("bad string length")
       
    45 	}
       
    46 
    44 	size := int(math.Sqrt(float64(l)))
    47 	size := int(math.Sqrt(float64(l)))
    45 	if size*size != l {
    48 	if size*size != l {
    46 		return nil, errors.New("bad string length")
    49 		return nil, errors.New("bad string length")
    47 	}
    50 	}
       
    51 
       
    52 	// TODO: validate chars ([.01OI])
    48 
    53 
    49 	i := 0
    54 	i := 0
    50 	t := New(size)
    55 	t := New(size)
    51 
    56 
    52 	for line := 0; line < size; line++ {
    57 	for line := 0; line < size; line++ {
   115 // BoardsMatch compares a Takuzu board to another, optionally ignoring
   120 // BoardsMatch compares a Takuzu board to another, optionally ignoring
   116 // empty cells.  Returns true if the two boards match.
   121 // empty cells.  Returns true if the two boards match.
   117 func BoardsMatch(t1, t2 *Takuzu, ignoreUndefined bool) (match bool, line, col int) {
   122 func BoardsMatch(t1, t2 *Takuzu, ignoreUndefined bool) (match bool, line, col int) {
   118 	match = true
   123 	match = true
   119 
   124 
       
   125 	if t1 == nil || t2 == nil {
       
   126 		line, col = -1, -1
       
   127 		match = false
       
   128 		return
       
   129 	}
       
   130 
   120 	if t1.Size != t2.Size {
   131 	if t1.Size != t2.Size {
   121 		line, col = -1, -1
   132 		line, col = -1, -1
   122 		match = false
   133 		match = false
   123 		return
   134 		return
   124 	}
   135 	}
       
   136 
   125 	for line = range t1.Board {
   137 	for line = range t1.Board {
   126 		for col = range t1.Board[line] {
   138 		for col = range t1.Board[line] {
   127 			if !t1.Board[line][col].Defined || !t2.Board[line][col].Defined {
   139 			if !t1.Board[line][col].Defined || !t2.Board[line][col].Defined {
   128 				// At least one of the cells is empty
   140 				// At least one of the cells is empty
   129 				if ignoreUndefined ||
   141 				if ignoreUndefined ||