private void _newGame(String game) { String open_file_name = Directory.GetCurrentDirectory() + "/games/" + game + ".gagh"; if (!File.Exists(open_file_name)) { return; } game_over = false; selected_element_row = -1; selected_element_col = -1; current_game = game; this.Text = "GAtomic - " + current_game; // Read file bool error = false; try { // Load formula GLDoorsFileOperations file_operations = new GLDoorsFileOperations(open_file_name); file_operations.OpenFileForRead(); String s = ""; bool b = false; // Name OK, read line anyway file_operations.ReadStringLineFromFile(out s, out b); // Read sizes file_operations.ReadStringLineFromFile(out s, out b); String sizes_string = s; s = s.Substring(5); int rows = Convert.ToInt32(s.Substring(0, s.IndexOf("x"))); int cols = Convert.ToInt32(s.Substring(s.IndexOf("x") + 1)); //---------------------------Board-size-------------------------------- elements = Array.CreateInstance(typeof(GAtomicObject), rows, cols); file_operations.ReadStringLineFromFile(out s, out b); sizes_string = s; s = s.Substring(10); rows = Convert.ToInt32(s.Substring(0, s.IndexOf("x"))); cols = Convert.ToInt32(s.Substring(s.IndexOf("x") + 1)); miniature_elements = Array.CreateInstance(typeof(GAtomicObject), rows, cols); //---------------------------Board-size-------------------------------- //---------------------------Placement-and-sizes-------------------------------- _setSizesAndPlacement(elements.GetLength(0), elements.GetLength(1), miniature_elements.GetLength(0), miniature_elements.GetLength(1)); Point miniature_top_left = new Point(miniature_area_rect.Left, miniature_area_rect.Top); //---------------------------Placement-and-sizes-------------------------------- //--------------------------------Miniatures----------------------------------------------- number_of_elements = 0; int row = 0; int col = 0; for (row = 0; row < miniature_elements.GetLength(0); row++) { for (col = 0; col < miniature_elements.GetLength(1); col++) { file_operations.ReadStringLineFromFile(out s, out b); if (!b) { // erreur error = true; break; } if (s == "EMPTY") { miniature_elements.SetValue(new GAtomicObject(new Point(miniature_area_rect.Left, miniature_area_rect.Top), row, col, GAtomicConfiguration._getMiniatureAreaAtomicObjectSize()), row, col); } else { // Read and add object o = GAtomicElementObject._getAtomicElementFromString(s, new Point(miniature_area_rect.Left, miniature_area_rect.Top), row, col, GAtomicConfiguration._getMiniatureAreaAtomicObjectSize(), 0); if (o != null) { miniature_elements.SetValue((GAtomicElementObject)o, row, col); number_of_elements++; } } } // end of for (col = 0; col < miniature_elements.GetLength (1); cols++) if (error) { break; } } //--------------------------------Miniatures----------------------------------------------- //--------------------------------Checking-elements-------------------------------------- _selectCheckingConnectionsAndElements(); //--------------------------------Checking-elements-------------------------------------- int index = 0; Random rnd = new Random(); int sel_index = rnd.Next(); sel_index %= number_of_elements; int start_element_row = 0; int start_element_col = 0; //-----------------------------------------Game-Area-------------------------------------------- for (row = 0; row < elements.GetLength(0); row++) { for (col = 0; col < elements.GetLength(1); col++) { file_operations.ReadStringLineFromFile(out s, out b); if (!b) { // erreur error = true; break; } if (s == "EMPTY") { elements.SetValue(new GAtomicObject(GAtomicConfiguration._getPlayingAreaTopLeft(), row, col, GAtomicConfiguration._getPlayingAreaAtomicObjectSize()), row, col); } else if (s == "BRICK") { elements.SetValue(new GAtomicBrick(GAtomicConfiguration._getPlayingAreaTopLeft(), row, col, GAtomicConfiguration._getPlayingAreaAtomicObjectSize()), row, col); } else { // Read and add object o = GAtomicElementObject._getAtomicElementFromString(s.Substring(0, 19), GAtomicConfiguration._getPlayingAreaTopLeft(), row, col, GAtomicConfiguration._getPlayingAreaAtomicObjectSize(), index++); if (o != null) { elements.SetValue((GAtomicElementObject)o, row, col); if (sel_index == index - 1) { start_element_row = row; start_element_col = col; } } else { error = true; break; } } } if (error) { break; } } // Select start element _selectElement(start_element_row, start_element_col); this.Invalidate(); file_operations.CloseFile(); } catch (GAtomicElementObjectInvalidException) { MessageBox.Show("Invalid atomic element object.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); error = true; } catch { error = true; } }
private void _selectCheckingConnectionsAndElements() { checking_elements = Array.CreateInstance(typeof(GAtomicObject), number_of_elements); // read miniature elements and read connections from it in an array (connection direction and element) // forget first and last row, first and last column, only borders int row = 0, col = 0; int neighbour_row = row; int neighbour_col = col; object array_object = null; ArrayList neighbours = new ArrayList(); ArrayList connections = new ArrayList(); GAtomicElementObject base_element = null; int index = 0; for (row = 1; row < miniature_elements.GetLength(0) - 1; row++) { for (col = 1; col < miniature_elements.GetLength(1) - 1; col++) { array_object = miniature_elements.GetValue(row, col); if (array_object is GAtomicElementObject) { base_element = (GAtomicElementObject)array_object; neighbours = new ArrayList(); connections = new ArrayList(); // check if it has connecting elements assigned to it for (GAtomicConnectionDirection direction = GAtomicConnectionDirection.TOP; direction <= GAtomicConnectionDirection.TRIPLE_LEFT; direction++) { if (((GAtomicElementObject)array_object)._hasConnection(direction)) { // Check connections neighbour_row = row; neighbour_col = col; if ((direction == GAtomicConnectionDirection.TOP) || (direction == GAtomicConnectionDirection.DOUBLE_TOP) || (direction == GAtomicConnectionDirection.TRIPLE_TOP)) { neighbour_row--; } else if (direction == GAtomicConnectionDirection.TOP_RIGHT) { neighbour_row--; neighbour_col++; } else if ((direction == GAtomicConnectionDirection.RIGHT) || (direction == GAtomicConnectionDirection.DOUBLE_RIGHT) || (direction == GAtomicConnectionDirection.TRIPLE_RIGHT)) { neighbour_col++; } else if (direction == GAtomicConnectionDirection.BOTTOM_RIGHT) { neighbour_row++; neighbour_col++; } else if ((direction == GAtomicConnectionDirection.BOTTOM) || (direction == GAtomicConnectionDirection.DOUBLE_BOTTOM) || (direction == GAtomicConnectionDirection.TRIPLE_BOTTOM)) { neighbour_row++; } else if (direction == GAtomicConnectionDirection.BOTTOM_LEFT) { neighbour_row++; neighbour_col--; } else if ((direction == GAtomicConnectionDirection.LEFT) || (direction == GAtomicConnectionDirection.DOUBLE_LEFT) || (direction == GAtomicConnectionDirection.TRIPLE_LEFT)) { neighbour_col--; } else if (direction == GAtomicConnectionDirection.TOP_LEFT) { neighbour_row--; neighbour_col--; } if (!_validRowAndColumn(neighbour_row, neighbour_col)) { continue; } base_element._setConnectionElement(direction, (GAtomicElementObject)miniature_elements.GetValue(neighbour_row, neighbour_col)); } } // end of for (GAtomicConnectionDirection direction = GAt // you need to add en element with its directions********** // add element, direction and element at that direction // we want take the array object and put the elements in the direction instead of the null // so we need to make a new copy of this element // and then put the elements instead of null with each connection checking_elements.SetValue(base_element, index); index++; } // end of if (array_object is GAtomicElementObject) } // end of for col } // end of for row }
private bool _gameSuccess() { //checking_elements // check the array for all elements with the characteristics needed object element_object = null; GAtomicElementObject atomic_element = null; GAtomicElementObject element_to_check_against = null; GAtomicElementObject element_to_check_against_connection_element = null; int neighbour_row = 0, neighbour_col = 0; int checking_elements_index = 0; bool element_ok = true; for (int row = 0; row < elements.GetLength(0); row++) { if (!element_ok) { // an element is in error, it's all we need to know return(false); } for (int col = 0; col < elements.GetLength(1); col++) { if (!element_ok) { // an element is in error, it's all we need to know return(false); } element_object = elements.GetValue(row, col); if ((element_object != null) && (element_object is GAtomicElementObject)) { atomic_element = (GAtomicElementObject)element_object; checking_elements_index = 0; element_ok = false; // check for same(s) in checking elements_array, can have many for (checking_elements_index = 0; checking_elements_index < checking_elements.GetLength(0) && !element_ok; checking_elements_index++) { // only directions are checked if (atomic_element == (GAtomicElementObject)checking_elements.GetValue(checking_elements_index)) { // now directions + elements element_to_check_against = (GAtomicElementObject)checking_elements.GetValue(checking_elements_index); // check if atomic_element has same connecting elements as element_to_check_against // check all the directions of element_to_check_against for (GAtomicConnectionDirection direction = GAtomicConnectionDirection.TOP; direction <= GAtomicConnectionDirection.TRIPLE_LEFT; direction++) { element_object = element_to_check_against._getConnectionElement(direction); if (element_object != null) { element_to_check_against_connection_element = (GAtomicElementObject)element_object; // get next row and col neighbour_row = row; neighbour_col = col; if ((direction == GAtomicConnectionDirection.TOP) || (direction == GAtomicConnectionDirection.DOUBLE_TOP) || (direction == GAtomicConnectionDirection.TRIPLE_TOP)) { neighbour_row--; } else if (direction == GAtomicConnectionDirection.TOP_RIGHT) { neighbour_row--; neighbour_col++; } else if ((direction == GAtomicConnectionDirection.RIGHT) || (direction == GAtomicConnectionDirection.DOUBLE_RIGHT) || (direction == GAtomicConnectionDirection.TRIPLE_RIGHT)) { neighbour_col++; } else if (direction == GAtomicConnectionDirection.BOTTOM_RIGHT) { neighbour_row++; neighbour_col++; } else if ((direction == GAtomicConnectionDirection.BOTTOM) || (direction == GAtomicConnectionDirection.DOUBLE_BOTTOM) || (direction == GAtomicConnectionDirection.TRIPLE_BOTTOM)) { neighbour_row++; } else if (direction == GAtomicConnectionDirection.BOTTOM_LEFT) { neighbour_row++; neighbour_col--; } else if ((direction == GAtomicConnectionDirection.LEFT) || (direction == GAtomicConnectionDirection.DOUBLE_LEFT) || (direction == GAtomicConnectionDirection.TRIPLE_LEFT)) { neighbour_col--; } else if (direction == GAtomicConnectionDirection.TOP_LEFT) { neighbour_row--; neighbour_col--; } if (!_validRowAndColumn(neighbour_row, neighbour_col)) { return(false); } element_object = elements.GetValue(neighbour_row, neighbour_col); if (!(element_object is GAtomicElementObject)) { return(false); } // check for same original element with same connections if ((GAtomicElementObject)element_object == element_to_check_against_connection_element) { element_ok = true; // continue for other directions continue; // break : for (checking_elements_index = 0; checking_elements_index < checking_elements.GetLength (0) && !element_ok; checking_elements_index++) } else { element_ok = false; break; } } // end of if (element_object != null) } // end of for (GAtomicConnectionDirection direction = GAtomicConnectionDirection.TOP; direction <= GAtomicConnectionDirection.TRIPLE_LEFT; direction++) } // end of if (atomic_element == (GAtomicElementObject)checking_elements.GetValue (checking_elements_index)) } // end of for (checking_elements_index = 0; checking_elements_index < checking_elements.GetLength (0) && !element_ok; checking_elements_index++) } // end of if ((element_object != null) && (element_object is GAtomicElementObject)) } // end of for (int col = 0; col < elements.GetLength (1); col++) } // end of for (int row = 0; row < elements.GetLength (0); row++) return(true); }