github.com/joshprzybyszewski/masyu@v0.0.0-20230508015604-f31a025f6e7e/solve/rule_white_branches.go (about)

     1  package solve
     2  
     3  import "github.com/joshprzybyszewski/masyu/model"
     4  
     5  func newWhiteHorizontalBranchRule(
     6  	nodeRow, nodeCol model.Dimension,
     7  ) rule {
     8  	r := rule{
     9  		affects: 4,
    10  		row:     nodeRow,
    11  		col:     nodeCol,
    12  	}
    13  	r.check = r.checkWhiteHorizontalBranch
    14  	return r
    15  }
    16  
    17  func (r *rule) checkWhiteHorizontalBranch(
    18  	s *state,
    19  ) {
    20  	cannotBranchLeft := (r.col > 1 && s.horLineAt(r.row, r.col-2)) ||
    21  		(s.verAvoidAt(r.row, r.col-1) &&
    22  			s.verAvoidAt(r.row-1, r.col-1))
    23  	cannotBranchRight := s.horLineAt(r.row, r.col+1) ||
    24  		(s.verAvoidAt(r.row, r.col+1) &&
    25  			s.verAvoidAt(r.row-1, r.col+1))
    26  
    27  	if cannotBranchLeft && cannotBranchRight {
    28  		// all four possible branches for a horizontal white node are impossible.
    29  		// Therefore, this must be a vertical node
    30  		s.lineVer(r.row-1, r.col)
    31  		s.lineVer(r.row, r.col)
    32  		s.avoidHor(r.row, r.col-1)
    33  		s.avoidHor(r.row, r.col)
    34  	}
    35  }
    36  
    37  func newWhiteVerticalBranchRule(
    38  	nodeRow, nodeCol model.Dimension,
    39  ) rule {
    40  	r := rule{
    41  		affects: 4,
    42  		row:     nodeRow,
    43  		col:     nodeCol,
    44  	}
    45  	r.check = r.checkWhiteVerticalBranch
    46  	return r
    47  }
    48  
    49  func (r *rule) checkWhiteVerticalBranch(
    50  	s *state,
    51  ) {
    52  	cannotBranchUp := (r.row > 1 && s.verLineAt(r.row-2, r.col)) ||
    53  		(s.horAvoidAt(r.row-1, r.col) &&
    54  			s.horAvoidAt(r.row-1, r.col-1))
    55  	cannotBranchDown := s.verLineAt(r.row+1, r.col) ||
    56  		(s.horAvoidAt(r.row+1, r.col) &&
    57  			s.horAvoidAt(r.row+1, r.col-1))
    58  
    59  	if cannotBranchUp && cannotBranchDown {
    60  		// all four possible branches for a vertical white node are impossible.
    61  		// Therefore, this must be a horizontal node
    62  		s.lineHor(r.row, r.col-1)
    63  		s.lineHor(r.row, r.col)
    64  		s.avoidVer(r.row-1, r.col)
    65  		s.avoidVer(r.row, r.col)
    66  	}
    67  }