[gnome-sudoku] Fix the inconsistent code
- From: Sahil Sareen <ssareen src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-sudoku] Fix the inconsistent code
- Date: Wed, 22 Apr 2015 11:53:55 +0000 (UTC)
commit 51090da86dfb23463f8cdd051c711a4a67243dcd
Author: Sahil Sareen <sahil sareen hotmail com>
Date: Wed Apr 22 17:05:24 2015 +0530
Fix the inconsistent code
https://bugzilla.gnome.org/show_bug.cgi?id=748252
lib/sudoku-board.vala | 91 ++++++++++++++------------------------------
lib/sudoku-game.vala | 18 ++++++---
lib/sudoku-generator.vala | 8 ++--
lib/sudoku-saver.vala | 39 +++++++++++--------
src/gnome-sudoku.vala | 28 ++++++++-----
src/number-picker.vala | 3 +-
src/print-dialog.vala | 5 +--
src/sudoku-view.vala | 3 +-
8 files changed, 90 insertions(+), 105 deletions(-)
---
diff --git a/lib/sudoku-board.vala b/lib/sudoku-board.vala
index fbc206f..8bbb022 100644
--- a/lib/sudoku-board.vala
+++ b/lib/sudoku-board.vala
@@ -124,13 +124,9 @@ public class SudokuBoard : Object
}
}
for (var l1 = 0; l1 < block_rows; l1++)
- {
for (var l2 = 0; l2 < block_cols; l2++)
- {
for (var l3 = 0; l3 < max_val; l3++)
possible_in_block[l1, l2, l3] = true;
- }
- }
broken_coords = new HashSet<Coord?>((HashDataFunc<Coord>) Coord.hash, (EqualDataFunc<Coord>)
Coord.equal);
@@ -139,9 +135,8 @@ public class SudokuBoard : Object
{
coords_for_col.add (new ArrayList<Coord?> ((EqualDataFunc<Coord>) Coord.equal));
for (int row = 0; row < rows; row++)
- {
coords_for_col.get (col).add (Coord(row, col));
- }
+
coords_for_col[col] = coords_for_col[col].read_only_view;
}
coords_for_col = coords_for_col.read_only_view;
@@ -151,35 +146,25 @@ public class SudokuBoard : Object
{
coords_for_row.add (new ArrayList<Coord?> ((EqualDataFunc<Coord>) Coord.equal));
for (int col = 0; col < cols; col++)
- {
coords_for_row.get (row).add (Coord(row, col));
- }
+
coords_for_row[row] = coords_for_row[row].read_only_view;
}
coords_for_row = coords_for_row.read_only_view;
coords_for_block = new HashMap<Coord?, Gee.List<Coord?>> ((HashDataFunc<Coord>) Coord.hash,
(EqualDataFunc<Coord>) Coord.equal);
for (int col = 0; col < block_cols; col++)
- {
for (int row = 0; row < block_rows; row++)
- {
coords_for_block.set (Coord(row, col), new ArrayList<Coord?> ((EqualDataFunc<Coord>)
Coord.equal));
- }
- }
+
for (int col = 0; col < cols; col++)
- {
for (int row = 0; row < rows; row++)
- {
coords_for_block.get(Coord(row / block_rows, col / block_cols)).add(Coord(row, col));
- }
- }
+
for (int col = 0; col < block_cols; col++)
- {
for (int row = 0; row < block_rows; row++)
- {
coords_for_block[Coord(row, col)] = coords_for_block[Coord(row, col)].read_only_view;
- }
- }
+
coords_for_block = coords_for_block.read_only_view;
}
@@ -255,7 +240,8 @@ public class SudokuBoard : Object
for (var l = 1; l <= max_val; l++)
{
- if (is_possible (row, col, l)) {
+ if (is_possible (row, col, l))
+ {
possibilities[count] = l;
count++;
}
@@ -268,9 +254,7 @@ public class SudokuBoard : Object
var possibilities = new bool[max_val];
for (var l = 1; l <= max_val; l++)
- {
possibilities[l - 1] = is_possible (row, col, l);
- }
return possibilities;
}
@@ -301,19 +285,13 @@ public class SudokuBoard : Object
fixed++;
if (!possible_in_row[row, val - 1]) // If val was not possible in this row
- {
mark_breakages_for(coords_for_row[row], val); // Mark the breakages
- }
if (!possible_in_col[col, val - 1]) // If val was not possible in this col
- {
mark_breakages_for(coords_for_col[col], val); // Mark the breakages
- }
if (!possible_in_block[row / block_cols, col / block_rows, val - 1]) // If val was not possible in
this block
- {
mark_breakages_for(coords_for_block[Coord(row / block_cols, col / block_rows)], val); // Mark
the breakages
- }
// Then just mark it as not possible
val--;
@@ -328,17 +306,11 @@ public class SudokuBoard : Object
public new void set (int row, int col, int val)
{
if (val == 0)
- {
remove (row, col);
- }
else if (val > 0 && val <= max_val)
- {
insert (row, col, val);
- }
else
- {
assert_not_reached();
- }
}
public new int get (int row, int col)
@@ -401,11 +373,9 @@ public class SudokuBoard : Object
{
Set<Coord?> occurances = new HashSet<Coord?>((HashDataFunc<Coord>) Coord.hash,
(EqualDataFunc<Coord>) Coord.equal);
foreach (Coord coord in coords)
- {
- if (cells[coord.row, coord.col] == val) {
+ if (cells[coord.row, coord.col] == val)
occurances.add (coord);
- }
- }
+
return occurances;
}
@@ -427,11 +397,8 @@ public class SudokuBoard : Object
private void remove_breakages_for(Gee.List<Coord?> coords, int val)
{
foreach (Coord coord in coords)
- {
- if (cells[coord.row, coord.col] == val && broken_coords.contains(coord)) {
+ if (cells[coord.row, coord.col] == val && broken_coords.contains(coord))
broken_coords.remove(coord);
- }
- }
}
/* returns if val is possible in coords */
@@ -439,30 +406,23 @@ public class SudokuBoard : Object
{
Set<Coord?> occurances = get_occurances(coords, val);
if (occurances.size != 1)
- {
broken_coords.add_all(occurances);
- }
}
public void to_initial_state ()
{
for (var l1 = 0; l1 < rows; l1++)
- {
for (var l2 = 0; l2 < cols; l2++)
- {
if (!is_fixed[l1, l2])
remove (l1, l2);
- }
- }
}
- public void print (int indent = 0) {
+ public void print (int indent = 0)
+ {
for (var l1 = 0; l1 < 9; l1++)
{
for (int i = 0; i < indent; i++)
- {
stdout.printf(" ");
- }
for (var l2 = 0; l2 < 9; l2++)
{
if (cells[l1,l2] != 0)
@@ -475,7 +435,8 @@ public class SudokuBoard : Object
stdout.flush ();
}
- public void get_string () {
+ public void get_string ()
+ {
stdout.printf ("[ ");
for (var l1 = 0; l1 < 9; l1++)
{
@@ -514,7 +475,8 @@ public class SudokuBoard : Object
return cells;
}
- public HashMap<Coord?, Gee.List<int>> calculate_open_squares () {
+ public HashMap<Coord?, Gee.List<int>> calculate_open_squares ()
+ {
var possibilities = new HashMap<Coord?, Gee.List<int>> ((HashDataFunc<Coord>) Coord.hash,
(EqualDataFunc<Coord>) Coord.equal);
for (var l1 = 0; l1 < rows; l1++)
{
@@ -524,9 +486,8 @@ public class SudokuBoard : Object
{
Gee.List<int> possArrayList = new ArrayList<int> ();
int[] possArray = get_possibilities (l1, l2);
- foreach (int i in possArray) {
+ foreach (int i in possArray)
possArrayList.add (i);
- }
possibilities[Coord(l1, l2)] = possArrayList;
}
}
@@ -554,7 +515,8 @@ public class SudokuBoard : Object
}
}
-public enum House {
+public enum House
+{
ROW,
COLUMN,
BLOCK
@@ -571,11 +533,13 @@ public struct Coord
this.col = col;
}
- public static int hash (Coord coord) {
+ public static int hash (Coord coord)
+ {
return (coord.row * 33) ^ coord.col;
}
- public static bool equal (Coord a, Coord b) {
+ public static bool equal (Coord a, Coord b)
+ {
return ((a.row == b.row) && (a.col == b.col));
}
}
@@ -591,16 +555,19 @@ public struct Cell
this.val = val;
}
- public static int hash (Cell cell) {
+ public static int hash (Cell cell)
+ {
return (Coord.hash(cell.coord) * 33) ^ cell.val;
}
- public static bool equal (Cell a, Cell b) {
+ public static bool equal (Cell a, Cell b)
+ {
return (Coord.equal(a.coord, b.coord) && (a.val == b.val));
}
}
-public enum DifficultyCategory {
+public enum DifficultyCategory
+{
UNKNOWN,
EASY,
MEDIUM,
diff --git a/lib/sudoku-game.vala b/lib/sudoku-game.vala
index 1e6ce4c..36dd139 100644
--- a/lib/sudoku-game.vala
+++ b/lib/sudoku-game.vala
@@ -78,7 +78,8 @@ public class SudokuGame : Object
var old_val = board[row, col];
update_undo (row, col, old_val, val);
- if (mode == GameMode.CREATE) {
+ if (mode == GameMode.CREATE)
+ {
board.insert (row, col, val, true);
board.is_fixed[row, col] = true;
}
@@ -93,7 +94,8 @@ public class SudokuGame : Object
int old_val = board[row, col];
update_undo (row, col, old_val, 0);
- if (mode == GameMode.CREATE) {
+ if (mode == GameMode.CREATE)
+ {
board.remove (row, col, true);
board.is_fixed[row, col] = false;
}
@@ -168,15 +170,18 @@ public class SudokuGame : Object
int old_val = board [top.row, top.col];
add_to_stack (to, top.row, top.col, old_val);
- if (mode == GameMode.CREATE) {
+ if (mode == GameMode.CREATE)
+ {
board.remove (top.row, top.col, board.is_fixed[top.row, top.col]);
board.is_fixed[top.row, top.col] = false;
}
else
board.remove (top.row, top.col);
- if (top.val != 0) {
- if (mode == GameMode.CREATE) {
+ if (top.val != 0)
+ {
+ if (mode == GameMode.CREATE)
+ {
board.insert (top.row, top.col, top.val, board.is_fixed[top.row, top.col]);
board.is_fixed[top.row, top.col] = true;
}
@@ -228,7 +233,8 @@ public class SudokuGame : Object
}
}
-public enum GameMode {
+public enum GameMode
+{
PLAY,
CREATE;
}
diff --git a/lib/sudoku-generator.vala b/lib/sudoku-generator.vala
index 1c1db49..06f2007 100644
--- a/lib/sudoku-generator.vala
+++ b/lib/sudoku-generator.vala
@@ -60,7 +60,8 @@ public class SudokuGenerator : Object
}
}
- private SudokuGenerator () {
+ private SudokuGenerator ()
+ {
}
private static SudokuBoard generate_board (DifficultyCategory category)
@@ -89,7 +90,8 @@ public class SudokuGenerator : Object
worker.run ();
}, (int) get_num_processors (), false);
- if (cancellable != null) {
+ if (cancellable != null)
+ {
cancellable.connect(() => {
ThreadPool.free((owned) pool, true, false);
generate_boards_async.callback();
@@ -97,9 +99,7 @@ public class SudokuGenerator : Object
}
for (var i = 0; i < nboards; i++)
- {
pool.add (new Worker (nboards, category, boards, generate_boards_async.callback));
- }
yield;
diff --git a/lib/sudoku-saver.vala b/lib/sudoku-saver.vala
index 31f78df..ef1588d 100644
--- a/lib/sudoku-saver.vala
+++ b/lib/sudoku-saver.vala
@@ -26,24 +26,25 @@ public class SudokuSaver : Object
public static string savegame_file { get; private set; default = ""; }
public static string finishgame_dir { get; private set; default = ""; }
- public SudokuSaver() {
- try {
+ public SudokuSaver()
+ {
+ try
+ {
var config_dir = Environment.get_user_data_dir ();
var sudoku_data_dir = Path.build_path (Path.DIR_SEPARATOR_S, config_dir, "gnome-sudoku");
savegame_file = Path.build_path (Path.DIR_SEPARATOR_S, sudoku_data_dir, "savefile");
finishgame_dir = Path.build_path (Path.DIR_SEPARATOR_S, sudoku_data_dir, "finished");
+ var file = File.new_for_path (sudoku_data_dir);
+ if (!file.query_exists ())
+ file.make_directory ();
- {
- var file = File.new_for_path (sudoku_data_dir);
- if (!file.query_exists ())
- file.make_directory ();
-
- file = File.new_for_path (finishgame_dir);
- if (!file.query_exists ())
- file.make_directory ();
- }
- } catch (Error e) {
+ file = File.new_for_path (finishgame_dir);
+ if (!file.query_exists ())
+ file.make_directory ();
+ }
+ catch (Error e)
+ {
warning ("%s", e.message);
}
}
@@ -90,9 +91,12 @@ public class SudokuSaver : Object
{
var json_str = serialize_game_to_json (game);
- try {
+ try
+ {
FileUtils.set_contents (file_name, json_str);
- } catch (Error e) {
+ }
+ catch (Error e)
+ {
warning ("%s", e.message);
}
}
@@ -165,9 +169,12 @@ public class SudokuSaver : Object
private SudokuGame? parse_json_to_game (string file_path)
{
Json.Parser parser = new Json.Parser ();
- try {
+ try
+ {
parser.load_from_file (file_path);
- } catch (Error e) {
+ }
+ catch (Error e)
+ {
return null;
}
diff --git a/src/gnome-sudoku.vala b/src/gnome-sudoku.vala
index 4a31d0e..bc4e411 100644
--- a/src/gnome-sudoku.vala
+++ b/src/gnome-sudoku.vala
@@ -209,7 +209,8 @@ public class Sudoku : Gtk.Application
saver = new SudokuSaver ();
var savegame = saver.get_savedgame ();
- if (savegame != null) {
+ if (savegame != null)
+ {
if (savegame.board.difficulty_category == DifficultyCategory.CUSTOM)
current_game_mode = savegame.board.filled == savegame.board.fixed ? GameMode.CREATE :
GameMode.PLAY;
start_game (savegame.board);
@@ -295,9 +296,7 @@ public class Sudoku : Gtk.Application
{
int solutions = game.board.count_solutions_limited ();
if (solutions == 1)
- {
start_custom_game (game.board);
- }
else if (solutions == 0)
{
// Error dialog shown when starting a custom game that is not valid.
@@ -317,9 +316,8 @@ public class Sudoku : Gtk.Application
dialog.response.connect ((response_id) => {
if (response_id == Gtk.ResponseType.ACCEPT)
- {
start_custom_game (game.board);
- }
+
dialog.destroy ();
});
@@ -462,10 +460,13 @@ public class Sudoku : Gtk.Application
{
current_game_mode = GameMode.CREATE;
SudokuGenerator.generate_boards_async.begin (1, DifficultyCategory.CUSTOM, null, (obj, res) => {
- try {
+ try
+ {
var gen_boards = SudokuGenerator.generate_boards_async.end (res);
start_game (gen_boards[0]);
- } catch (Error e) {
+ }
+ catch (Error e)
+ {
error ("Error: %s", e.message);
}
});
@@ -483,11 +484,14 @@ public class Sudoku : Gtk.Application
current_game_mode = GameMode.PLAY;
SudokuGenerator.generate_boards_async.begin (1, selected_difficulty, null, (obj, res) => {
- try {
+ try
+ {
var gen_boards = SudokuGenerator.generate_boards_async.end (res);
back_button.sensitive = true;
start_game (gen_boards[0]);
- } catch (Error e) {
+ }
+ catch (Error e)
+ {
error ("Error: %s", e.message);
}
});
@@ -523,11 +527,13 @@ public class Sudoku : Gtk.Application
if (game != null)
game.resume_clock ();
- if (current_game_mode == GameMode.PLAY) {
+ if (current_game_mode == GameMode.PLAY)
+ {
play_custom_game_button.visible = false;
play_pause_button.visible = true;
}
- else {
+ else
+ {
clock_label.hide ();
clock_image.hide ();
play_custom_game_button.visible = true;
diff --git a/src/number-picker.vala b/src/number-picker.vala
index e16f385..0e0bac5 100644
--- a/src/number-picker.vala
+++ b/src/number-picker.vala
@@ -33,7 +33,8 @@ private class NumberPicker : Gtk.Grid
private static const int EARMARKS_MAX_ALLOWED = 5;
private int earmarks_active;
- public NumberPicker (ref SudokuBoard board, bool earmark = false) {
+ public NumberPicker (ref SudokuBoard board, bool earmark = false)
+ {
this.board = board;
earmarks_active = 0;
diff --git a/src/print-dialog.vala b/src/print-dialog.vala
index b1166cd..fdc002c 100644
--- a/src/print-dialog.vala
+++ b/src/print-dialog.vala
@@ -55,9 +55,8 @@ public class PrintDialog : Gtk.Dialog
settings = new GLib.Settings ("org.gnome.sudoku");
this.response.connect ((response_id) => {
- if (response_id == Gtk.ResponseType.CANCEL || response_id == Gtk.ResponseType.DELETE_EVENT) {
+ if (response_id == Gtk.ResponseType.CANCEL || response_id == Gtk.ResponseType.DELETE_EVENT)
cancellable.cancel ();
- }
});
set_transient_for (window);
@@ -162,9 +161,7 @@ public class PrintDialog : Gtk.Dialog
catch (IOError e)
{
if (!(e is IOError.CANCELLED))
- {
warning ("Error: %s\n", e.message);
- }
}
destroy ();
diff --git a/src/sudoku-view.vala b/src/sudoku-view.vala
index c4b161e..ac3edde 100644
--- a/src/sudoku-view.vala
+++ b/src/sudoku-view.vala
@@ -420,7 +420,8 @@ private class SudokuCellView : Gtk.DrawingArea
{
this.value = new_val;
- if (game.mode == GameMode.CREATE) {
+ if (game.mode == GameMode.CREATE)
+ {
if (_selected)
background_color = selected_bg_color;
else
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]