xref: /haiku/src/apps/sudoku/SudokuField.h (revision 1b8f7f13a3dc70e0e903cb94248220b40b732204)
1 /*
2  * Copyright 2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef SUDOKU_FIELD_H
6 #define SUDOKU_FIELD_H
7 
8 
9 #include <Archivable.h>
10 #include <SupportDefs.h>
11 
12 
13 enum {
14 	kInitialValue	= 0x01,
15 };
16 
17 class SudokuField : public BArchivable {
18 public:
19 	SudokuField(uint32 size);
20 	SudokuField(const BMessage* archive);
21 	SudokuField(const SudokuField& other);
22 	virtual ~SudokuField();
23 
24 	status_t InitCheck();
25 
26 	virtual status_t Archive(BMessage* archive, bool deep) const;
27 	static SudokuField* Instantiate(BMessage* archive);
28 
29 	status_t SetTo(char base, const char* data);
30 	void SetTo(const SudokuField* other);
31 	void Reset();
32 
33 	bool IsSolved();
34 
35 	uint32 Size() const { return fSize; }
36 	uint32 BlockSize() const { return fBlockSize; }
37 
38 	void SetHintMaskAt(uint32 x, uint32 y, uint32 hintMask);
39 	uint32 HintMaskAt(uint32 x, uint32 y) const;
40 
41 	void SetValidMaskAt(uint32 x, uint32 y, uint32 validMask);
42 	uint32 ValidMaskAt(uint32 x, uint32 y) const;
43 
44 	void SetFlagsAt(uint32 x, uint32 y, uint32 flags);
45 	uint32 FlagsAt(uint32 x, uint32 y) const;
46 
47 	void SetValueAt(uint32 x, uint32 y, uint32 value, bool setSolved = false);
48 	uint32 ValueAt(uint32 x, uint32 y) const;
49 
50 	void Dump();
51 
52 private:
53 	struct field {
54 		field();
55 
56 		uint32		hint_mask;
57 		uint32		valid_mask;
58 		uint32		flags;
59 		uint32		value;
60 	};
61 
62 	bool _ValidValueAt(uint32 x, uint32 y) const;
63 	void _ComputeValidMask(uint32 x, uint32 y, bool setSolved);
64 	void _UpdateValidMaskChanged(uint32 x, uint32 y, bool setSolved);
65 	const field& _FieldAt(uint32 x, uint32 y) const;
66 	field& _FieldAt(uint32 x, uint32 y);
67 
68 	uint32	fSize;
69 	uint32	fBlockSize;
70 	uint32	fMaxMask;
71 	field*	fFields;
72 };
73 
74 #endif	// SUDOKU_FIELD_H
75