public Cell(State initial_state, Board_Style style, Rectangle screen_rectangle) { state = initial_state; Screen_Rectangle = screen_rectangle; Style = style; int off_left = Screen_Rectangle.X + style.Cell_Size / 2 - style.Cell_Off_Size; int off_right = Screen_Rectangle.X + style.Cell_Size / 2 + style.Cell_Off_Size; int off_top = Screen_Rectangle.Y + style.Cell_Size / 2 - style.Cell_Off_Size; int off_bottom = Screen_Rectangle.Y + style.Cell_Size / 2 + style.Cell_Off_Size; Off_Lines = new Line_Segment[] { new Line_Segment { First = new Point(off_left, off_top), Second = new Point(off_right, off_bottom), }, new Line_Segment { First = new Point(off_right, off_top), Second = new Point(off_left, off_bottom), }, }; }
// Initialize from clues public Clue(int[] group_sizes, Board_Style style, Point start_point, bool draw_groups_vertically) { Group_Sizes = new int[group_sizes.Length]; for (int i = 0; i < group_sizes.Length; i++) { Group_Sizes[i] = group_sizes[i]; } // Record data for Draw Style = style; Group_Positions = new Point[group_sizes.Length]; Initialize_Visual(start_point, draw_groups_vertically); }
private static Board_Style Get_Style(Puzzle puzzle) { int largest_side = Math.Max(puzzle.Row_Hints.Length, puzzle.Column_Hints.Length); if (largest_side <= 5) { return(Board_Style.Get_Style(Board_Style.Type.Small)); } else if (largest_side <= 10) { return(Board_Style.Get_Style(Board_Style.Type.Medium)); } else if (largest_side <= 15) { return(Board_Style.Get_Style(Board_Style.Type.Large)); } else { return(Board_Style.Get_Style(Board_Style.Type.Huge)); } }
// Initialize from a known solution public Clue(Cell.State[] states, Board_Style style, Point start_point, bool draw_groups_vertically) { // Initialize our temp group list to 0s int[] temp_group_sizes = new int[states.Length / 2 + 1]; for (int i = 0; i < temp_group_sizes.Length; i++) { temp_group_sizes[i] = 0; } // Find each group size int total_group_count = 0; int current_group_length = 0; for (int i = 0; i < states.Length; i++) { if (states[i] == Cell.State.on) { // If we find an 'on' cell, we're in a group. current_group_length++; } else if (current_group_length > 0) { // if we find a non-on cell, record the previous group if there was one. temp_group_sizes[total_group_count] = current_group_length; total_group_count++; current_group_length = 0; } } // Record the last group if we ended in an 'on' cell. if (current_group_length > 0) { temp_group_sizes[total_group_count] = current_group_length; total_group_count++; current_group_length = 0; } // Copy the temp group list to the final one, now that we know how big the array is. Group_Sizes = new int[total_group_count]; for (int i = 0; i < total_group_count; i++) { Group_Sizes[i] = temp_group_sizes[i]; } // Record data for Draw Style = style; Group_Positions = new Point[total_group_count]; Initialize_Visual(start_point, draw_groups_vertically); }
// Public methods public Board(Puzzle puzzle, Point start_point) { m_rows = puzzle.Row_Hints.Length; m_columns = puzzle.Column_Hints.Length; m_style = Get_Style(puzzle); // Initialize the Board's background m_exterior_rectangle = new Rectangle( new Point( start_point.X + m_style.Clue_Spacing * 4, start_point.Y + m_style.Clue_Spacing * 4), new Size( m_columns * m_style.Cell_Size, m_rows * m_style.Cell_Size)); m_interior_lines = new Line_Segment[m_rows - 1 + m_columns - 1]; int line_segment_index = 0; for (int column = 1; column < m_columns; column++, line_segment_index++) { int x_position = m_exterior_rectangle.Left + m_style.Cell_Size * column; m_interior_lines[line_segment_index] = new Line_Segment { First = new Point(x_position, m_exterior_rectangle.Top), Second = new Point(x_position, m_exterior_rectangle.Bottom), }; } for (int row = 1; row < m_rows; row++, line_segment_index++) { int y_position = m_exterior_rectangle.Top + m_style.Cell_Size * row; m_interior_lines[line_segment_index] = new Line_Segment { First = new Point(m_exterior_rectangle.Left, y_position), Second = new Point(m_exterior_rectangle.Right, y_position), }; } // Initialize the grid of cells m_cells = new Cell[m_columns, m_rows]; for (int row = 0; row < m_rows; row++) { for (int column = 0; column < m_columns; column++) { m_cells[column, row] = new Cell( Cell.State.maybe, m_style, new Rectangle( m_exterior_rectangle.Left + m_style.Cell_Size * column, m_exterior_rectangle.Top + m_style.Cell_Size * row, m_style.Cell_Size, m_style.Cell_Size)); } } // Initialize the clues to match the puzzle m_column_clues = new Clue[m_columns]; for (int column = 0; column < m_columns; column++) { Point clue_position = m_cells[column, 0].Screen_Rectangle.Location; clue_position.X += m_style.Cell_Size / 2; clue_position.Y += m_style.Cell_Size / 2 - m_style.Clue_Board_Offset; m_column_clues[column] = new Clue(puzzle.Column_Hints[column], m_style, clue_position, true); } m_row_clues = new Clue[m_rows]; for (int row = 0; row < m_rows; row++) { Point clue_position = m_cells[0, row].Screen_Rectangle.Location; clue_position.X += m_style.Cell_Size / 2 - m_style.Clue_Board_Offset; clue_position.Y += m_style.Cell_Size / 2; m_row_clues[row] = new Clue(puzzle.Row_Hints[row], m_style, clue_position, false); } // Collect the cells into groups m_groups = new Group[m_rows + m_columns]; int group_count = 0; for (int column = 0; column < m_columns; column++, group_count++) { Group group = new Group(m_rows, m_column_clues[column]); for (int row = 0; row < m_rows; row++) { group.Cells[row] = m_cells[column, row]; } m_groups[group_count] = group; } for (int row = 0; row < m_rows; row++, group_count++) { Group group = new Group(m_columns, m_row_clues[row]); for (int column = 0; column < m_columns; column++) { group.Cells[column] = m_cells[column, row]; } m_groups[group_count] = group; } }