} /* search_field() */ /*********************************************************** * * Method: * flag_field * * Description: * Flags a given field for a mine. * ***********************************************************/ public void flag_field ( int x, int y ) { /*---------------------------------------------------------- * Get the flagged field * ----------------------------------------------------------*/ ms_field field = ms_model.mine_field[x, y]; if ((ms_game_status.WON == ms_model.status) || (ms_game_status.LOST == ms_model.status)) { return; } /*---------------------------------------------------------- * Rotate the status * ----------------------------------------------------------*/ switch (field.mine_status) { case ms_mine_status.FLAGGED: ms_model.mines_rem++; field.mine_status = ms_mine_status.QUESTION; break; case ms_mine_status.QUESTION: field.mine_status = ms_mine_status.UNCHECKED; break; case ms_mine_status.UNCHECKED: field.mine_status = ms_mine_status.FLAGGED; ms_model.mines_rem--; break; default: break; } } /* flag_field() */
} /* ms_game() */ /*********************************************************** * * Method: * ms_new_game * * Description: * Initialize new game board. * ***********************************************************/ public void ms_new_game ( int i_width, int i_height, int i_mine_count ) { /*---------------------------------------------------------- * Set all game values * ----------------------------------------------------------*/ field_width = i_width; field_height = i_height; mine_count = i_mine_count; mines_rem = mine_count; current_time = 0; fields_rem = field_height * field_width; status = ms_game_status.INACTIVE; /*---------------------------------------------------------- * Create new mine field * ----------------------------------------------------------*/ mine_field = new ms_field[field_width, field_height]; for (int i = 0; i < field_width; i++) { for (int j = 0; j < field_height; j++) { mine_field[i, j] = new ms_field(); } } /*---------------------------------------------------------- * Generate mines * ----------------------------------------------------------*/ gen_mines(); } /* ms_game() */
} /* UnloadContent() */ /*********************************************************** * * Method: * Update * * Description: * Passes input events to game controller. * ***********************************************************/ protected override void Update ( GameTime gameTime ) { /*---------------------------------------------------------- * Check if game should be exited * ----------------------------------------------------------*/ if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { Exit(); } /*---------------------------------------------------------- * Check if the toolbar handled the input * ----------------------------------------------------------*/ if (toolbar.input_handled()) { return; } /*---------------------------------------------------------- * Update mouse status * ----------------------------------------------------------*/ mouse.update(dims); /*---------------------------------------------------------- * Update time * ----------------------------------------------------------*/ if (ms_model.status == ms_game_status.ACTIVE) { ms_model.current_time = (int)((DateTime.Now.Ticks - ms_model.start_time) / 10000000); } else if (ms_model.status == ms_game_status.INACTIVE) { ms_model.current_time = 0; } /*---------------------------------------------------------- * Release double click * ----------------------------------------------------------*/ if ((button_state_type.UNHELD == mouse.left) && (button_state_type.UNHELD == mouse.right)) { double_click = false; } /*---------------------------------------------------------- * Check for double click * ----------------------------------------------------------*/ if ((ms_mouse_location.FIELD == mouse.capture_left) && (ms_mouse_location.FIELD == mouse.capture_right)) { int x = mouse.mine_loc.X; int y = mouse.mine_loc.Y; int flag_count = 0; ms_field field = ms_model.mine_field[x, y]; double_click = true; for (int i = Math.Max(0, x - 1); i <= Math.Min(ms_model.field_width - 1, x + 1); i++) { for (int j = Math.Max(0, y - 1); j <= Math.Min(ms_model.field_height - 1, y + 1); j++) { if (ms_model.mine_field[i, j].mine_status == ms_mine_status.FLAGGED) { flag_count++; } } } set_field_image(); /*---------------------------------------------------------- * Double click indenting * ----------------------------------------------------------*/ if ((button_state_type.HELD == mouse.left) && (button_state_type.HELD == mouse.right)) { for (int i = Math.Max(0, x - 1); i <= Math.Min(ms_model.field_width - 1, x + 1); i++) { for (int j = Math.Max(0, y - 1); j <= Math.Min(ms_model.field_height - 1, y + 1); j++) { if (ms_model.mine_field[i, j].mine_status == ms_mine_status.UNCHECKED) { field_status[i, j] = ms_mine_status.CHECKED_0; } } } } if ((ms_mine_status.CHECKED_0 < field.mine_status) && (field.mine_status <= ms_mine_status.CHECKED_8) && (field.mine_count == flag_count)) { /*---------------------------------------------------------- * Double click searching * ----------------------------------------------------------*/ if ((button_state_type.UNCLICKED == mouse.left) || (button_state_type.UNCLICKED == mouse.right)) { for (int i = Math.Max(0, x - 1); i <= Math.Min(ms_model.field_width - 1, x + 1); i++) { for (int j = Math.Max(0, y - 1); j <= Math.Min(ms_model.field_height - 1, y + 1); j++) { if (ms_model.mine_field[i, j].mine_status == ms_mine_status.UNCHECKED) { ms_ctlr.search_field(i, j); } } } set_field_image(); } } } /*---------------------------------------------------------- * Check for searching a field * ----------------------------------------------------------*/ else if ((ms_mouse_location.FIELD == mouse.capture_left) && (button_state_type.UNCLICKED == mouse.left) && (false == double_click)) { ms_ctlr.search_field(mouse.mine_loc.X, mouse.mine_loc.Y); set_field_image(); } /*---------------------------------------------------------- * Check for indenting a field * ----------------------------------------------------------*/ else if ((button_state_type.HELD == mouse.left) && ((ms_game_status.ACTIVE == ms_model.status) || (ms_game_status.INACTIVE == ms_model.status)) && (false == double_click)) { set_field_image(); if ((ms_mouse_location.FIELD == mouse.capture_left) && ((ms_mine_status.UNCHECKED == ms_model.mine_field[mouse.mine_loc.X, mouse.mine_loc.Y].mine_status) || (ms_mine_status.QUESTION == ms_model.mine_field[mouse.mine_loc.X, mouse.mine_loc.Y].mine_status))) { field_status[mouse.mine_loc.X, mouse.mine_loc.Y] = ms_mine_status.CHECKED_0; } } /*---------------------------------------------------------- * Check for flagging a field * ----------------------------------------------------------*/ else if ((ms_mouse_location.FIELD == mouse.capture_right) && (button_state_type.CLICKED == mouse.right) && (false == double_click)) { ms_ctlr.flag_field(mouse.mine_loc.X, mouse.mine_loc.Y); set_field_image(); } /*---------------------------------------------------------- * Check for face clicked * ----------------------------------------------------------*/ if ((ms_mouse_location.FACE == mouse.capture_left) && (ms_mouse_location.FACE == mouse.cursor_location) && (button_state_type.UNCLICKED == mouse.left)) { new_game_same(); } /*---------------------------------------------------------- * Set the face status * ----------------------------------------------------------*/ if ((ms_mouse_location.FACE == mouse.capture_left) && (ms_mouse_location.FACE == mouse.cursor_location) && (button_state_type.HELD == mouse.left)) { face_status = ms_face_status.PRESSED; } else if ((ms_mouse_location.FIELD == mouse.capture_left) && ((ms_game_status.ACTIVE == ms_model.status) || (ms_game_status.INACTIVE == ms_model.status)) && ((!double_click) || (double_click && ms_mouse_location.FIELD == mouse.capture_right))) { face_status = ms_face_status.MOUSE_DOWN; } else { switch (ms_model.status) { case ms_game_status.WON: face_status = ms_face_status.WON; break; case ms_game_status.LOST: face_status = ms_face_status.LOST; break; case ms_game_status.INACTIVE: case ms_game_status.ACTIVE: face_status = ms_face_status.ACTIVE; break; } } base.Update(gameTime); } /* Update() */
} /* new_game() */ /*********************************************************** * * Method: * search_field * * Description: * Searches a given field for a mine. * ***********************************************************/ public void search_field ( int x, int y ) { /*---------------------------------------------------------- * Get the searched field * ----------------------------------------------------------*/ ms_field field = ms_model.mine_field[x, y]; /*---------------------------------------------------------- * Verify the game is active * ----------------------------------------------------------*/ if ((ms_game_status.WON == ms_model.status) || (ms_game_status.LOST == ms_model.status)) { return; } /*---------------------------------------------------------- * If the game is just starting, save the start time * ----------------------------------------------------------*/ if (ms_game_status.INACTIVE == ms_model.status) { ms_model.start_time = DateTime.Now.Ticks; ms_model.status = ms_game_status.ACTIVE; if (field.is_mine) { ms_model.move_mine(x, y); } } /*---------------------------------------------------------- * Verify the field can be searched * ----------------------------------------------------------*/ if ((ms_mine_status.UNCHECKED == field.mine_status) || (ms_mine_status.QUESTION == field.mine_status)) { /*---------------------------------------------------------- * If a mine is hit, game over * ----------------------------------------------------------*/ if (field.is_mine) { field.mine_status = ms_mine_status.MINE_HIT; ms_model.status = ms_game_status.LOST; game_lost(); } /*---------------------------------------------------------- * If a mine is not hit, reveal the mine count. If the count * is zero, search surrounding fields. * ----------------------------------------------------------*/ else { field.mine_status = (ms_mine_status)field.mine_count; ms_model.fields_rem--; if (0 == field.mine_count) { for (int i = Math.Max(0, x - 1); i < Math.Min(ms_model.field_width, x + 2); i++) { for (int j = Math.Max(0, y - 1); j < Math.Min(ms_model.field_height, y + 2); j++) { search_field(i, j); } } } } } /*---------------------------------------------------------- * Game won * ----------------------------------------------------------*/ if (ms_model.fields_rem == ms_model.mine_count) { ms_model.win_time = DateTime.Now.Ticks - ms_model.start_time; ms_model.status = ms_game_status.WON; ms_model.mines_rem = 0; game_won(); } } /* search_field() */