// Write out the maze to the writer channel
Write out the maze to the writer channel
// Write out the maze to the writer channel
func (maze *Maze) Write(writer chan string, format *Format) {
	startLeft := format.StartLeft
	if maze.Solved {
		startLeft = format.SolutionStartLeft
	} else if maze.Started {
		startLeft = format.VisitedStartLeft
	}
	startRight := format.StartRight
	if maze.Solved {
		startRight = format.SolutionStartRight
	} else if maze.Started {
		startRight = format.VisitedStartRight
	}
	goalLeft := format.GoalLeft
	if maze.Solved {
		goalLeft = format.SolutionGoalLeft
	} else if maze.Finished {
		goalLeft = format.VisitedGoalLeft
	}
	goalRight := format.GoalRight
	if maze.Solved {
		goalRight = format.SolutionGoalRight
	} else if maze.Finished {
		goalRight = format.VisitedGoalRight
	}
	solved := (Up | Down | Left | Right) << SolutionOffset
	visited := (Up | Down | Left | Right) << VisitedOffset
	writer <- "\n"
	for x, row := range maze.Directions {
		for _, direction := range []int{Up, Right} {
			writer <- format.Path 
			if maze.Start.X == x && maze.Start.Y == 0 && direction == Right {
				writer <- startLeft
			} else if maze.Goal.X == x && maze.Goal.Y == 0 && maze.Width > 1 && direction == Right {
				writer <- goalLeft
			} else {
				writer <- format.Wall
			}
			for y, directions := range row {
				if direction == Right {
					if directions&solved != 0 {
						writer <- format.Solution
					} else if directions&visited != 0 {
						if maze.Cursor.X == x && maze.Cursor.Y == y {
							writer <- format.Cursor
						} else {
							writer <- format.Visited
						}
					} else {
						writer <- format.Path
					}
				}
				if maze.Start.X == x && maze.Start.Y == y && y == maze.Width-1 && 0 < y && direction == Right {
					writer <- startRight
				} else if maze.Goal.X == x && maze.Goal.Y == y && y == maze.Width-1 && direction == Right {
					writer <- goalRight
				} else
				if maze.Start.X == x && maze.Start.Y == y && x == 0 && maze.Height > 1 && 0 < y && y < maze.Width-1 && direction == Up {
					writer <- startLeft
				} else if maze.Goal.X == x && maze.Goal.Y == y && x == 0 && maze.Height > 1 && 0 < y && y < maze.Width-1 && direction == Up {
					writer <- goalLeft
				} else
				if directions&direction != 0 {
					if (directions>>SolutionOffset)&direction != 0 {
						writer <- format.Solution
					} else if (directions>>VisitedOffset)&direction != 0 {
						writer <- format.Visited
					} else {
						writer <- format.Path
					}
				} else {
					writer <- format.Wall
				}
				if direction == Up {
					writer <- format.Wall
				}
			}
			writer <- "\n"
		}
	}
	writer <- format.Path
	writer <- format.Wall
	for y := 0; y < maze.Width; y++ {
		if maze.Start.X == maze.Height-1 && maze.Start.Y == y && maze.Height > 1 && 0 < y && y < maze.Width-1 {
			writer <- startLeft
		} else if maze.Goal.X == maze.Height-1 && maze.Goal.Y == y && 0 < y && y < maze.Width-1 {
			writer <- goalRight
		} else {
			writer <- format.Wall
		}
		writer <- format.Wall
	}
	writer <- "\n\n"
	writer <- "\u0000"
}
func ( maze * Maze ) Write ( writer chan string , format * Format ) { startLeft := format . StartLeft if maze . Solved { startLeft = format . SolutionStartLeft } else if maze . Started { startLeft = format . VisitedStartLeft } startRight := format . StartRight if maze . Solved { startRight = format . SolutionStartRight } else if maze . Started { startRight = format . VisitedStartRight } goalLeft := format . GoalLeft if maze . Solved { goalLeft = format . SolutionGoalLeft } else if maze . Finished { goalLeft = format . VisitedGoalLeft } goalRight := format . GoalRight if maze . Solved { goalRight = format . SolutionGoalRight } else if maze . Finished { goalRight = format . VisitedGoalRight } solved := ( Up | Down | Left | Right ) << SolutionOffset visited := ( Up | Down | Left | Right ) << VisitedOffset writer <- " \n " for x , row := range maze . Directions { for _ , direction := range [ ] int { Up , Right } { writer <- format . Path if maze . Start . X == x && maze . Start . Y == 0 && direction == Right { writer <- startLeft } else if maze . Goal . X == x && maze . Goal . Y == 0 && maze . Width > 1 && direction == Right { writer <- goalLeft } else { writer <- format . Wall } for y , directions := range row { if direction == Right { if directions & solved != 0 { writer <- format . Solution } else if directions & visited != 0 { if maze . Cursor . X == x && maze . Cursor . Y == y { writer <- format . Cursor } else { writer <- format . Visited } } else { writer <- format . Path } } if maze . Start . X == x && maze . Start . Y == y && y == maze . Width - 1 && 0 < y && direction == Right { writer <- startRight } else if maze . Goal . X == x && maze . Goal . Y == y && y == maze . Width - 1 && direction == Right { writer <- goalRight } else if maze . Start . X == x && maze . Start . Y == y && x == 0 && maze . Height > 1 && 0 < y && y < maze . Width - 1 && direction == Up { writer <- startLeft } else if maze . Goal . X == x && maze . Goal . Y == y && x == 0 && maze . Height > 1 && 0 < y && y < maze . Width - 1 && direction == Up { writer <- goalLeft } else if directions & direction != 0 { if ( directions >> SolutionOffset ) & direction != 0 { writer <- format . Solution } else if ( directions >> VisitedOffset ) & direction != 0 { writer <- format . Visited } else { writer <- format . Path } } else { writer <- format . Wall } if direction == Up { writer <- format . Wall } } writer <- " \n " } } writer <- format . Path writer <- format . Wall for y := 0 ; y < maze . Width ; y ++ { if maze . Start . X == maze . Height - 1 && maze . Start . Y == y && maze . Height > 1 && 0 < y && y < maze . Width - 1 { writer <- startLeft } else if maze . Goal . X == maze . Height - 1 && maze . Goal . Y == y && 0 < y && y < maze . Width - 1 { writer <- goalRight } else { writer <- format . Wall } writer <- format . Wall } writer <- " \n \n " writer <- " \u0000 " }
Write out the maze to the writer channel
Write out the maze to the writer channel
// If solved or started, it changes the appearance of the start and the goal // We can use & to check if the direction is the solution path or the path user has visited // Print out the maze // There are two lines printed for each maze lines // The left margin // The left wall // In the `direction == Right` line, we print the path cell // Print the start or goal point on the right hand side // Print the start or goal point on the top wall of the maze // If there is a path in the direction (Up or Right) on the maze // Print the path cell, or the solution cell if solved or the visited cells if the user visited // Print the wall cell // In the `direction == Up` line, we print the wall cell // Print the bottom wall of the maze // Inform that we finished printing the maze