public void InitializeSquareCluster(int x, int y, int z) { for (int s = 0; s < Length; ++s) { nodes_ids[s] = UTL.CoordToIdx(x * RootLength + s % RootLength, y * RootLength + s / RootLength, z); } }
// Propagate the last node in _nodes_to_propagate. This means applying all the constraints in its clusters. Returns false if we get a conflict from this propagation private bool Propagate() { int node_idx_to_propagate = UTL.ExtractBack(ref _nodes_to_propagate); if (_nodes[node_idx_to_propagate].is_propagated) { return(true); // the node has already been propagated. Shouldn't happen } bool legal_propagation = true; if (_nodes[node_idx_to_propagate].is_true) { foreach (int neighbour_idx in _nodes[node_idx_to_propagate].neighbour_ids) { if (!GiveValueToNode(neighbour_idx, false)) { legal_propagation = false; } } } foreach (int cluster_idx in _nodes[node_idx_to_propagate].cluster_ids) { if (!GiveNodeValueToCluster(ref _clusters[cluster_idx], _nodes[node_idx_to_propagate].is_true)) { legal_propagation = false; } } _nodes[node_idx_to_propagate].is_propagated = true; ++_num_fixed_nodes_propagated; return(legal_propagation); }
public void InitializeYCluster(int x, int z) { for (int y = 0; y < Length; ++y) { nodes_ids[y] = UTL.CoordToIdx(x, y, z); } }
public void InitializeZCluster(int x, int y) { for (int z = 0; z < Length; ++z) { nodes_ids[z] = UTL.CoordToIdx(x, y, z); } }
public void InitializeXCluster(int y, int z) { for (int x = 0; x < Length; ++x) { nodes_ids[x] = UTL.CoordToIdx(x, y, z); } }
private TileButton _tile_button; // the button that need to be modified public Node(int x, int y, int z, TileButton tile_button_) { _tile_button = tile_button_; is_fixed = false; is_true = false; is_propagated = false; time = InvalidInt; coord = new UTL.Coord3(x, y, z); neighbour_ids = new int[NumNeighbours]; cluster_ids = new int[4]; int idx = 0; for (int xi = 0; xi < Length; ++xi) { if (xi == x) { continue; } neighbour_ids[idx] = UTL.CoordToIdx(xi, y, z); ++idx; } for (int yi = 0; yi < Length; ++yi) { if (yi == y) { continue; } neighbour_ids[idx] = UTL.CoordToIdx(x, yi, z); ++idx; } for (int zi = 0; zi < Length; ++zi) { if (zi == z) { continue; } neighbour_ids[idx] = UTL.CoordToIdx(x, y, zi); ++idx; } for (int yi = (y / RootLength) * RootLength; yi < (y / RootLength + 1) * RootLength; ++yi) { if (yi == y) { continue; } for (int xi = (x / RootLength) * RootLength; xi < (x / RootLength + 1) * RootLength; ++xi) { if (xi == x) { continue; } neighbour_ids[idx] = UTL.CoordToIdx(xi, yi, z); ++idx; } } cluster_ids[0] = UTL.CoordToIdx(y, z); cluster_ids[1] = UTL.CoordToIdx(x, z) + Area; cluster_ids[2] = UTL.CoordToIdx(x, y) + Area * 2; cluster_ids[3] = UTL.CoordToIdx((x / RootLength) + RootLength * (y / RootLength), z) + Area * 3; }
// create and initialize _tile_buttons private void CreateTileButtons() { _tile_buttons = new Button[_GridArea]; for (int tile_idx = 0; tile_idx < _GridArea; tile_idx++) { _tile_buttons[tile_idx] = Instantiate(tileButtonPrefab, new Vector3(0f, 0f, 0f), Quaternion.identity) as Button; _tile_buttons[tile_idx].transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false); _tile_buttons[tile_idx].GetComponent <TileButton>().Initialize(UTL.GetX(tile_idx), UTL.GetY(tile_idx)); } }
// initialize _nodes and _clusters public void InitializeGraph() { _nodes = new Node[Volume]; _clusters = new Cluster[Area * 4]; for (int node_idx = 0; node_idx < Volume; ++node_idx) { _nodes[node_idx] = new Node(UTL.GetX(node_idx), UTL.GetY(node_idx), UTL.GetZ(node_idx), GameManager.Instance.GetTileButtons()[node_idx % Area].GetComponent <TileButton>()); } for (int cl_idx = 0; cl_idx < Area * 4; ++cl_idx) { _clusters[cl_idx] = new Cluster(); } for (int cl_idx = 0; cl_idx < Area; ++cl_idx) { _clusters[cl_idx].InitializeXCluster(cl_idx % Length, cl_idx / Length); _clusters[cl_idx + Area].InitializeYCluster(cl_idx % Length, cl_idx / Length); _clusters[cl_idx + Area * 2].InitializeZCluster(cl_idx % Length, cl_idx / Length); _clusters[cl_idx + Area * 3].InitializeSquareCluster(cl_idx % RootLength, (cl_idx % Length) / RootLength, cl_idx / Length); } }
// removes the last Guess, since it proved to be wrong private void RemoveLastGuess() { if (_guess_list.Count == 0) { return; // this should never happen } for (int node_idx = 0; node_idx < Volume; ++node_idx) { if (_nodes[node_idx].time == NumGuesses()) { FreeNode(node_idx); } ; } int last_guessed_node = UTL.ExtractBack(ref _guess_list); GiveValueToNode(last_guessed_node, false); _nodes_to_propagate = new List <int> { last_guessed_node }; }