2005-11-25 23:40:16 +03:00
|
|
|
//
|
|
|
|
// Sudoku game using the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2018-02-01 00:17:17 +03:00
|
|
|
// Copyright 2005-2018 by Michael Sweet.
|
2021-12-19 00:42:30 +03:00
|
|
|
// Copyright 2019-2021 by Bill Spitzak and others.
|
2005-11-25 23:40:16 +03:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2005-11-25 23:40:16 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-11-25 23:40:16 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2005-11-25 23:40:16 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Enumerations.H>
|
2009-04-21 13:09:37 +04:00
|
|
|
#include <FL/Fl_Double_Window.H>
|
2005-11-25 23:40:16 +03:00
|
|
|
#include <FL/Fl_Button.H>
|
|
|
|
#include <FL/Fl_Group.H>
|
|
|
|
#include <FL/fl_ask.H>
|
|
|
|
#include <FL/fl_draw.H>
|
2005-11-27 08:15:47 +03:00
|
|
|
#include <FL/Fl_Help_Dialog.H>
|
2005-11-25 23:40:16 +03:00
|
|
|
#include <FL/Fl_Preferences.H>
|
|
|
|
#include <FL/Fl_Sys_Menu_Bar.H>
|
2018-02-01 00:17:17 +03:00
|
|
|
#include <FL/platform.H>
|
2020-01-18 15:03:29 +03:00
|
|
|
#include <FL/Fl_Image_Surface.H>
|
|
|
|
#include <FL/Fl_Bitmap.H>
|
2005-11-25 23:40:16 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2005-12-06 07:35:04 +03:00
|
|
|
#include <FL/math.h>
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2020-01-18 15:03:29 +03:00
|
|
|
#include "pixmaps/sudoku.xbm"
|
2005-11-27 17:45:48 +03:00
|
|
|
|
2023-08-15 23:59:07 +03:00
|
|
|
// Audio headers...
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif // !_WIN32
|
2023-08-16 11:27:41 +03:00
|
|
|
#if defined __APPLE__ && !defined(FLTK_USE_X11)
|
|
|
|
# define USE_MACOS 1
|
|
|
|
#endif
|
2023-08-15 23:59:07 +03:00
|
|
|
|
|
|
|
#ifdef HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
# define ALSA_PCM_NEW_HW_PARAMS_API
|
|
|
|
# include <alsa/asoundlib.h>
|
|
|
|
#endif // HAVE_ALSA_ASOUNDLIB_H
|
2023-08-16 12:18:19 +03:00
|
|
|
#ifdef __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
# include <CoreAudio/AudioHardware.h>
|
2023-08-16 12:18:19 +03:00
|
|
|
#endif // __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <mmsystem.h>
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
//
|
|
|
|
// Default sizes...
|
|
|
|
//
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
#define GROUP_SIZE 160
|
|
|
|
#define CELL_SIZE 50
|
|
|
|
#define CELL_OFFSET 5
|
2023-08-16 11:27:41 +03:00
|
|
|
#ifdef USE_MACOS
|
2020-07-01 19:03:10 +03:00
|
|
|
# define MENU_OFFSET 0
|
2005-11-25 23:40:16 +03:00
|
|
|
#else
|
2020-07-01 19:03:10 +03:00
|
|
|
# define MENU_OFFSET 25
|
2023-08-16 11:27:41 +03:00
|
|
|
#endif // USE_MACOS
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2023-08-15 23:59:07 +03:00
|
|
|
// Sound class for Sudoku...
|
|
|
|
//
|
|
|
|
// There are MANY ways to implement sound in a FLTK application.
|
|
|
|
// The approach we are using here is to conditionally compile OS-
|
|
|
|
// specific code into the application - CoreAudio for MacOS X, the
|
|
|
|
// standard Win32 API stuff for Windows, ALSA or X11 for Linux, and
|
|
|
|
// X11 for all others. We have to support ALSA on Linux because the
|
|
|
|
// current Xorg releases no longer support XBell() or the PC speaker.
|
|
|
|
//
|
|
|
|
// There are several good cross-platform audio libraries we could also
|
|
|
|
// use, such as OpenAL, PortAudio, and SDL, however they were not chosen
|
|
|
|
// for this application because of our limited use of sound.
|
|
|
|
//
|
|
|
|
// Many thanks to Ian MacArthur who provided sample code that led to
|
|
|
|
// the CoreAudio implementation you see here!
|
|
|
|
class SudokuSound {
|
|
|
|
// Private, OS-specific data...
|
2023-08-16 12:18:19 +03:00
|
|
|
#ifdef __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
AudioDeviceID device;
|
|
|
|
# if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
|
|
|
AudioDeviceIOProcID audio_proc_id;
|
|
|
|
# endif
|
|
|
|
AudioStreamBasicDescription format;
|
|
|
|
short *data;
|
|
|
|
int remaining;
|
|
|
|
|
|
|
|
static OSStatus audio_cb(AudioDeviceID device,
|
|
|
|
const AudioTimeStamp *current_time,
|
|
|
|
const AudioBufferList *data_in,
|
|
|
|
const AudioTimeStamp *time_in,
|
|
|
|
AudioBufferList *data_out,
|
|
|
|
const AudioTimeStamp *time_out,
|
|
|
|
void *client_data);
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
HWAVEOUT device;
|
|
|
|
HGLOBAL header_handle;
|
|
|
|
LPWAVEHDR header_ptr;
|
|
|
|
HGLOBAL data_handle;
|
|
|
|
LPSTR data_ptr;
|
|
|
|
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
snd_pcm_t *handle;
|
|
|
|
# endif // HAVE_ALSA_ASOUNDLIB_H
|
2023-08-16 12:18:19 +03:00
|
|
|
#endif // __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
|
|
|
|
// Common data...
|
|
|
|
static int frequencies[9];
|
|
|
|
static short *sample_data[9];
|
|
|
|
static int sample_size;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
SudokuSound();
|
|
|
|
~SudokuSound();
|
|
|
|
|
|
|
|
void play(char note);
|
|
|
|
};
|
|
|
|
|
2024-08-05 00:42:03 +03:00
|
|
|
typedef unsigned int State;
|
|
|
|
typedef State GameState[81];
|
2023-08-15 23:59:07 +03:00
|
|
|
|
|
|
|
// Sudoku cell class...
|
|
|
|
class SudokuCell : public Fl_Widget {
|
2024-08-04 18:28:35 +03:00
|
|
|
int row_;
|
|
|
|
int col_;
|
2023-08-15 23:59:07 +03:00
|
|
|
bool readonly_;
|
|
|
|
int value_;
|
2024-08-04 18:28:35 +03:00
|
|
|
int hint_map_;
|
2023-08-15 23:59:07 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2024-08-04 18:28:35 +03:00
|
|
|
SudokuCell(int row, int col, int X, int Y, int W, int H);
|
|
|
|
int row() { return row_; }
|
|
|
|
int col() { return col_; }
|
|
|
|
void draw() FL_OVERRIDE;
|
|
|
|
int handle(int event) FL_OVERRIDE;
|
2023-08-15 23:59:07 +03:00
|
|
|
void readonly(bool r) { readonly_ = r; redraw(); }
|
|
|
|
bool readonly() const { return readonly_; }
|
2024-08-04 18:28:35 +03:00
|
|
|
void set_hint(int n) { hint_map_ |= (1<<n); redraw(); }
|
|
|
|
void clear_hint(int n) { hint_map_ &= ~(1<<n); redraw(); }
|
|
|
|
void clear_hints() { hint_map_ = 0; redraw(); }
|
|
|
|
void set_hint_map(int v) { hint_map_ = v; }
|
|
|
|
int get_hint_map() { return hint_map_; }
|
|
|
|
bool hint_set(int n) const { return ((hint_map_&(1<<n))!=0); }
|
|
|
|
void value(int v) { value_ = v; redraw(); }
|
2023-08-15 23:59:07 +03:00
|
|
|
int value() const { return value_; }
|
2024-08-05 00:42:03 +03:00
|
|
|
State state() { return (hint_map_>>1) | (value_<<12) | (readonly_<<9); }
|
|
|
|
void state(State s) {
|
|
|
|
hint_map_ = (s & 0x000001ff) << 1;
|
|
|
|
readonly_ = (s & 0x00000200) >> 9;
|
|
|
|
value_ = (s & 0x0000f000) >> 12;
|
|
|
|
if (readonly_) color(FL_GRAY); else color(FL_LIGHT3);
|
|
|
|
redraw();
|
|
|
|
}
|
2023-08-15 23:59:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Sudoku window class...
|
|
|
|
class Sudoku : public Fl_Double_Window {
|
|
|
|
Fl_Sys_Menu_Bar *menubar_;
|
|
|
|
Fl_Group *grid_;
|
|
|
|
time_t seed_;
|
|
|
|
char grid_values_[9][9];
|
|
|
|
SudokuCell *grid_cells_[9][9];
|
|
|
|
Fl_Group *grid_groups_[3][3];
|
|
|
|
int difficulty_;
|
|
|
|
SudokuSound *sound_;
|
2024-08-05 00:42:03 +03:00
|
|
|
GameState undo_stack[64];
|
|
|
|
int undo_head_, undo_tail_, redo_head_;
|
2023-08-15 23:59:07 +03:00
|
|
|
|
|
|
|
static void check_cb(Fl_Widget *widget, void *);
|
|
|
|
static void close_cb(Fl_Widget *widget, void *);
|
|
|
|
static void diff_cb(Fl_Widget *widget, void *d);
|
|
|
|
static void update_helpers_cb(Fl_Widget *, void *);
|
|
|
|
static void help_cb(Fl_Widget *, void *);
|
|
|
|
static void mute_cb(Fl_Widget *widget, void *);
|
|
|
|
static void new_cb(Fl_Widget *widget, void *);
|
|
|
|
static void reset_cb(Fl_Widget *widget, void *);
|
|
|
|
static void restart_cb(Fl_Widget *widget, void *);
|
|
|
|
void set_title();
|
|
|
|
static void solve_cb(Fl_Widget *widget, void *);
|
|
|
|
|
|
|
|
static Fl_Help_Dialog *help_dialog_;
|
|
|
|
static Fl_Preferences prefs_;
|
|
|
|
public:
|
|
|
|
|
|
|
|
Sudoku();
|
|
|
|
~Sudoku();
|
|
|
|
|
|
|
|
void check_game(bool highlight = true);
|
|
|
|
void load_game();
|
|
|
|
void new_game(time_t seed);
|
|
|
|
int next_value(SudokuCell *c);
|
2024-08-04 18:28:35 +03:00
|
|
|
void resize(int X, int Y, int W, int H) FL_OVERRIDE;
|
2023-08-15 23:59:07 +03:00
|
|
|
void save_game();
|
|
|
|
void solve_game();
|
|
|
|
void update_helpers();
|
2024-08-04 18:28:35 +03:00
|
|
|
void clear_hints_for(int row, int col, int val);
|
2024-08-05 00:42:03 +03:00
|
|
|
void save_state(GameState &s);
|
|
|
|
void load_state(GameState &s);
|
|
|
|
void undo();
|
|
|
|
void redo();
|
|
|
|
void clear_undo();
|
|
|
|
void undo_checkpoint();
|
2023-08-15 23:59:07 +03:00
|
|
|
};
|
|
|
|
|
2024-08-04 17:46:04 +03:00
|
|
|
Sudoku *sudoku = NULL;
|
2023-08-15 23:59:07 +03:00
|
|
|
|
|
|
|
// Sound class globals...
|
|
|
|
int SudokuSound::frequencies[9] = {
|
|
|
|
880, // A(5)
|
|
|
|
988, // B(5)
|
|
|
|
1046, // C(5)
|
|
|
|
1174, // D(5)
|
|
|
|
1318, // E(5)
|
|
|
|
1396, // F(5)
|
|
|
|
1568, // G(5)
|
|
|
|
1760, // H (A6)
|
|
|
|
1976 // I (B6)
|
|
|
|
};
|
|
|
|
short *SudokuSound::sample_data[9] = { 0 };
|
|
|
|
int SudokuSound::sample_size = 0;
|
|
|
|
|
|
|
|
// Initialize the SudokuSound class
|
|
|
|
SudokuSound::SudokuSound() {
|
|
|
|
sample_size = 0;
|
|
|
|
|
2023-08-16 12:18:19 +03:00
|
|
|
#ifdef __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
remaining = 0;
|
|
|
|
|
|
|
|
UInt32 size = sizeof(device);
|
|
|
|
|
|
|
|
if (AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
&size, (void *)&device) != noErr) return;
|
|
|
|
|
|
|
|
size = sizeof(format);
|
|
|
|
if (AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyStreamFormat,
|
|
|
|
&size, &format) != noErr) return;
|
|
|
|
|
|
|
|
// Set up a format we like...
|
|
|
|
format.mSampleRate = 44100.0; // 44.1kHz
|
|
|
|
format.mChannelsPerFrame = 2; // stereo
|
|
|
|
|
|
|
|
if (AudioDeviceSetProperty(device, NULL, 0, false,
|
|
|
|
kAudioDevicePropertyStreamFormat,
|
|
|
|
sizeof(format), &format) != noErr) return;
|
|
|
|
|
|
|
|
// Check we got linear pcm - what to do if we did not ???
|
|
|
|
if (format.mFormatID != kAudioFormatLinearPCM) return;
|
|
|
|
|
|
|
|
// Attach the callback and start the device
|
|
|
|
# if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
|
|
|
if (AudioDeviceCreateIOProcID(device, audio_cb, (void *)this, &audio_proc_id) != noErr) return;
|
|
|
|
AudioDeviceStart(device, audio_proc_id);
|
|
|
|
# else
|
|
|
|
if (AudioDeviceAddIOProc(device, audio_cb, (void *)this) != noErr) return;
|
|
|
|
AudioDeviceStart(device, audio_cb);
|
|
|
|
# endif
|
|
|
|
|
|
|
|
sample_size = (int)format.mSampleRate / 20;
|
|
|
|
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
WAVEFORMATEX format;
|
|
|
|
|
|
|
|
memset(&format, 0, sizeof(format));
|
|
|
|
format.cbSize = sizeof(format);
|
|
|
|
format.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
format.nChannels = 2;
|
|
|
|
format.nSamplesPerSec = 44100;
|
|
|
|
format.nAvgBytesPerSec = 44100 * 4;
|
|
|
|
format.nBlockAlign = 4;
|
|
|
|
format.wBitsPerSample = 16;
|
|
|
|
|
|
|
|
data_handle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, format.nSamplesPerSec / 5);
|
|
|
|
if (!data_handle) return;
|
|
|
|
|
|
|
|
data_ptr = (LPSTR)GlobalLock(data_handle);
|
|
|
|
|
|
|
|
header_handle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, sizeof(WAVEHDR));
|
|
|
|
if (!header_handle) return;
|
|
|
|
|
|
|
|
header_ptr = (WAVEHDR *)GlobalLock(header_handle);
|
|
|
|
|
|
|
|
header_ptr->lpData = data_ptr;
|
|
|
|
header_ptr->dwBufferLength = format.nSamplesPerSec / 5;
|
|
|
|
header_ptr->dwFlags = 0;
|
|
|
|
header_ptr->dwLoops = 0;
|
|
|
|
|
|
|
|
if (waveOutOpen(&device, WAVE_MAPPER, &format, 0, 0, WAVE_ALLOWSYNC)
|
|
|
|
!= MMSYSERR_NOERROR) return;
|
|
|
|
|
|
|
|
waveOutPrepareHeader(device, header_ptr, sizeof(WAVEHDR));
|
|
|
|
|
|
|
|
sample_size = 44100 / 20;
|
|
|
|
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
handle = NULL;
|
|
|
|
|
|
|
|
if (snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0) >= 0) {
|
|
|
|
// Initialize PCM sound stuff...
|
|
|
|
snd_pcm_hw_params_t *params;
|
|
|
|
|
|
|
|
snd_pcm_hw_params_alloca(¶ms);
|
|
|
|
snd_pcm_hw_params_any(handle, params);
|
|
|
|
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
|
|
|
|
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16);
|
|
|
|
snd_pcm_hw_params_set_channels(handle, params, 2);
|
|
|
|
unsigned rate = 44100;
|
|
|
|
int dir;
|
|
|
|
snd_pcm_hw_params_set_rate_near(handle, params, &rate, &dir);
|
|
|
|
snd_pcm_uframes_t period = (int)rate / 4;
|
|
|
|
snd_pcm_hw_params_set_period_size_near(handle, params, &period, &dir);
|
|
|
|
|
|
|
|
sample_size = rate / 20;
|
|
|
|
|
|
|
|
if (snd_pcm_hw_params(handle, params) < 0) {
|
|
|
|
sample_size = 0;
|
|
|
|
snd_pcm_close(handle);
|
|
|
|
handle = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif // HAVE_ALSA_ASOUNDLIB_H
|
2023-08-16 12:18:19 +03:00
|
|
|
#endif // __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
|
|
|
|
if (sample_size) {
|
|
|
|
// Make each of the notes using a combination of sine and sawtooth waves
|
|
|
|
int attack = sample_size / 10;
|
|
|
|
int decay = 4 * sample_size / 5;
|
|
|
|
|
|
|
|
for (int i = 0; i < 9; i ++) {
|
|
|
|
sample_data[i] = new short[2 * sample_size];
|
|
|
|
|
|
|
|
short *sample_ptr = sample_data[i];
|
|
|
|
|
|
|
|
for (int j = 0; j < sample_size; j ++, sample_ptr += 2) {
|
|
|
|
double theta = 0.05 * frequencies[i] * j / sample_size;
|
|
|
|
double val = 0.5 * sin(2.0 * M_PI * theta) + theta - (int)theta - 0.5;
|
|
|
|
|
|
|
|
if (j < attack) {
|
|
|
|
*sample_ptr = (int)(32767 * val * j / attack);
|
|
|
|
} else if (j > decay) {
|
|
|
|
*sample_ptr = (int)(32767 * val * (sample_size - j + decay) /
|
|
|
|
sample_size);
|
|
|
|
} else *sample_ptr = (int)(32767 * val);
|
|
|
|
|
|
|
|
sample_ptr[1] = *sample_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Cleanup the SudokuSound class
|
|
|
|
SudokuSound::~SudokuSound() {
|
2023-08-16 12:18:19 +03:00
|
|
|
#ifdef __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
if (sample_size) {
|
|
|
|
# if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
|
|
|
AudioDeviceStop(device, audio_proc_id);
|
|
|
|
AudioDeviceDestroyIOProcID(device, audio_proc_id);
|
|
|
|
# else
|
|
|
|
AudioDeviceStop(device, audio_cb);
|
|
|
|
AudioDeviceRemoveIOProc(device, audio_cb);
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
if (sample_size) {
|
|
|
|
waveOutClose(device);
|
|
|
|
|
|
|
|
GlobalUnlock(header_handle);
|
|
|
|
GlobalFree(header_handle);
|
|
|
|
|
|
|
|
GlobalUnlock(data_handle);
|
|
|
|
GlobalFree(data_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
if (handle) {
|
|
|
|
snd_pcm_drain(handle);
|
|
|
|
snd_pcm_close(handle);
|
|
|
|
}
|
|
|
|
# endif // HAVE_ALSA_ASOUNDLIB_H
|
2023-08-16 12:18:19 +03:00
|
|
|
#endif // __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
|
|
|
|
if (sample_size) {
|
|
|
|
for (int i = 0; i < 9; i ++) {
|
|
|
|
delete[] sample_data[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-16 12:18:19 +03:00
|
|
|
#ifdef __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
// Callback function for writing audio data...
|
|
|
|
OSStatus
|
|
|
|
SudokuSound::audio_cb(AudioDeviceID device,
|
|
|
|
const AudioTimeStamp *current_time,
|
|
|
|
const AudioBufferList *data_in,
|
|
|
|
const AudioTimeStamp *time_in,
|
|
|
|
AudioBufferList *data_out,
|
|
|
|
const AudioTimeStamp *time_out,
|
|
|
|
void *client_data) {
|
|
|
|
SudokuSound *ss = (SudokuSound *)client_data;
|
|
|
|
int count;
|
|
|
|
float *buffer;
|
|
|
|
|
|
|
|
if (!ss->remaining) return noErr;
|
|
|
|
|
|
|
|
for (count = data_out->mBuffers[0].mDataByteSize / sizeof(float),
|
|
|
|
buffer = (float*) data_out->mBuffers[0].mData;
|
|
|
|
ss->remaining > 0 && count > 0;
|
|
|
|
count --, ss->data ++, ss->remaining --) {
|
|
|
|
*buffer++ = *(ss->data) / 32767.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
*buffer++ = 0.0;
|
|
|
|
count --;
|
|
|
|
}
|
|
|
|
|
|
|
|
return noErr;
|
|
|
|
}
|
2023-08-16 12:18:19 +03:00
|
|
|
#endif // __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
|
|
|
|
#define NOTE_DURATION 50
|
|
|
|
|
|
|
|
// Play a note for <NOTE_DURATION> ms...
|
|
|
|
void SudokuSound::play(char note) {
|
|
|
|
Fl::check();
|
|
|
|
|
2023-08-16 12:18:19 +03:00
|
|
|
#ifdef __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
// Point to the next note...
|
|
|
|
data = sample_data[note - 'A'];
|
|
|
|
remaining = sample_size * 2;
|
|
|
|
|
|
|
|
// Wait for the sound to complete...
|
|
|
|
usleep(NOTE_DURATION*1000);
|
|
|
|
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
if (sample_size) {
|
|
|
|
memcpy(data_ptr, sample_data[note - 'A'], sample_size * 4);
|
|
|
|
|
|
|
|
waveOutWrite(device, header_ptr, sizeof(WAVEHDR));
|
|
|
|
|
|
|
|
Sleep(NOTE_DURATION);
|
|
|
|
} else Beep(frequencies[note - 'A'], NOTE_DURATION);
|
|
|
|
|
|
|
|
#elif defined(FLTK_USE_X11)
|
|
|
|
# ifdef HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
if (handle) {
|
|
|
|
// Use ALSA to play the sound...
|
|
|
|
if (snd_pcm_writei(handle, sample_data[note - 'A'], sample_size) < 0) {
|
|
|
|
snd_pcm_prepare(handle);
|
|
|
|
snd_pcm_writei(handle, sample_data[note - 'A'], sample_size);
|
|
|
|
}
|
|
|
|
usleep(NOTE_DURATION*1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
# endif // HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
|
|
|
|
// Just use standard X11 stuff...
|
|
|
|
XKeyboardState state;
|
|
|
|
XKeyboardControl control;
|
|
|
|
|
|
|
|
// Get original pitch and duration...
|
|
|
|
XGetKeyboardControl(fl_display, &state);
|
|
|
|
|
|
|
|
// Sound a tone for the given note...
|
|
|
|
control.bell_percent = 100;
|
|
|
|
control.bell_pitch = frequencies[note - 'A'];
|
|
|
|
control.bell_duration = NOTE_DURATION;
|
|
|
|
|
|
|
|
XChangeKeyboardControl(fl_display,
|
|
|
|
KBBellPercent | KBBellPitch | KBBellDuration,
|
|
|
|
&control);
|
|
|
|
XBell(fl_display, 100);
|
|
|
|
XFlush(fl_display);
|
|
|
|
|
|
|
|
// Restore original pitch and duration...
|
|
|
|
control.bell_percent = state.bell_percent;
|
|
|
|
control.bell_pitch = state.bell_pitch;
|
|
|
|
control.bell_duration = state.bell_duration;
|
|
|
|
|
|
|
|
XChangeKeyboardControl(fl_display,
|
|
|
|
KBBellPercent | KBBellPitch | KBBellDuration,
|
|
|
|
&control);
|
2023-08-16 12:18:19 +03:00
|
|
|
#endif // __APPLE__
|
2023-08-15 23:59:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create a cell widget
|
2024-08-04 18:28:35 +03:00
|
|
|
SudokuCell::SudokuCell(int row, int col, int X, int Y, int W, int H)
|
|
|
|
: Fl_Widget(X, Y, W, H, 0), row_(row), col_(col)
|
|
|
|
{
|
2023-08-15 23:59:07 +03:00
|
|
|
value(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Draw cell
|
|
|
|
void
|
|
|
|
SudokuCell::draw() {
|
2024-08-04 18:28:35 +03:00
|
|
|
static Fl_Align align[9] = {
|
2023-08-15 23:59:07 +03:00
|
|
|
FL_ALIGN_TOP_LEFT,
|
|
|
|
FL_ALIGN_TOP,
|
|
|
|
FL_ALIGN_TOP_RIGHT,
|
2024-08-04 18:28:35 +03:00
|
|
|
FL_ALIGN_LEFT,
|
|
|
|
0,
|
2023-08-15 23:59:07 +03:00
|
|
|
FL_ALIGN_RIGHT,
|
|
|
|
FL_ALIGN_BOTTOM_LEFT,
|
2024-08-04 18:28:35 +03:00
|
|
|
FL_ALIGN_BOTTOM,
|
|
|
|
FL_ALIGN_BOTTOM_RIGHT,
|
2023-08-15 23:59:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Draw the cell box...
|
|
|
|
if (readonly()) fl_draw_box(FL_UP_BOX, x(), y(), w(), h(), color());
|
|
|
|
else fl_draw_box(FL_DOWN_BOX, x(), y(), w(), h(), color());
|
|
|
|
|
|
|
|
// Draw the cell background...
|
|
|
|
if (Fl::focus() == this) {
|
|
|
|
Fl_Color c = fl_color_average(FL_SELECTION_COLOR, color(), 0.5f);
|
|
|
|
fl_color(c);
|
|
|
|
fl_rectf(x() + 4, y() + 4, w() - 8, h() - 8);
|
|
|
|
fl_color(fl_contrast(labelcolor(), c));
|
|
|
|
} else fl_color(labelcolor());
|
|
|
|
|
|
|
|
// Draw the cell value...
|
|
|
|
char s[2];
|
|
|
|
|
|
|
|
s[1] = '\0';
|
|
|
|
|
|
|
|
if (value_) {
|
|
|
|
s[0] = value_ + '0';
|
|
|
|
fl_font(FL_HELVETICA_BOLD, h() - 10);
|
|
|
|
fl_draw(s, x(), y(), w(), h(), FL_ALIGN_CENTER);
|
2024-08-04 18:28:35 +03:00
|
|
|
} else {
|
|
|
|
fl_font(FL_HELVETICA_BOLD, h() / 5);
|
|
|
|
for (int i = 1; i <= 9; i ++) {
|
|
|
|
if (hint_set(i)) {
|
|
|
|
s[0] = i + '0';
|
|
|
|
fl_draw(s, x() + 5, y() + 5, w() - 10, h() - 10, align[i-1]);
|
|
|
|
}
|
2023-08-15 23:59:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handle events in cell
|
|
|
|
int
|
|
|
|
SudokuCell::handle(int event) {
|
|
|
|
switch (event) {
|
|
|
|
case FL_FOCUS :
|
|
|
|
Fl::focus(this);
|
|
|
|
redraw();
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case FL_UNFOCUS :
|
|
|
|
redraw();
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case FL_PUSH :
|
|
|
|
if (!readonly() && Fl::event_inside(this)) {
|
|
|
|
if (Fl::event_clicks()) {
|
|
|
|
// 2+ clicks increments/sets value
|
|
|
|
if (value()) {
|
|
|
|
if (value() < 9) value(value() + 1);
|
|
|
|
else value(1);
|
2024-08-04 17:46:04 +03:00
|
|
|
} else value(sudoku->next_value(this));
|
2023-08-15 23:59:07 +03:00
|
|
|
}
|
2024-08-05 00:42:03 +03:00
|
|
|
// TODO: add this to the undo process
|
2023-08-15 23:59:07 +03:00
|
|
|
Fl::focus(this);
|
|
|
|
redraw();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FL_KEYDOWN :
|
|
|
|
if (Fl::event_state() & FL_CTRL) break;
|
|
|
|
int key = Fl::event_key() - '0';
|
|
|
|
if (key < 0 || key > 9) key = Fl::event_key() - FL_KP - '0';
|
|
|
|
if (key > 0 && key <= 9) {
|
|
|
|
if (readonly()) {
|
|
|
|
fl_beep(FL_BEEP_ERROR);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Fl::event_state() & (FL_SHIFT | FL_CAPS_LOCK)) {
|
2024-08-04 18:28:35 +03:00
|
|
|
if (hint_set(key)) {
|
|
|
|
clear_hint(key);
|
|
|
|
} else {
|
|
|
|
set_hint(key);
|
2023-08-15 23:59:07 +03:00
|
|
|
}
|
2024-08-05 00:42:03 +03:00
|
|
|
sudoku->undo_checkpoint();
|
2023-08-15 23:59:07 +03:00
|
|
|
redraw();
|
|
|
|
} else {
|
|
|
|
value(key);
|
2024-08-04 18:28:35 +03:00
|
|
|
sudoku->clear_hints_for(row(), col(), key);
|
2024-08-05 00:42:03 +03:00
|
|
|
sudoku->undo_checkpoint();
|
2023-08-15 23:59:07 +03:00
|
|
|
do_callback();
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
} else if (key == 0 || Fl::event_key() == FL_BackSpace ||
|
|
|
|
Fl::event_key() == FL_Delete) {
|
|
|
|
if (readonly()) {
|
|
|
|
fl_beep(FL_BEEP_ERROR);
|
|
|
|
return 1;
|
|
|
|
}
|
2024-08-04 18:28:35 +03:00
|
|
|
if (Fl::event_state() & (FL_SHIFT | FL_CAPS_LOCK)) {
|
|
|
|
clear_hints();
|
2024-08-05 00:42:03 +03:00
|
|
|
sudoku->undo_checkpoint();
|
2024-08-04 18:28:35 +03:00
|
|
|
} else {
|
|
|
|
value(0);
|
|
|
|
do_callback();
|
2024-08-05 00:42:03 +03:00
|
|
|
sudoku->undo_checkpoint();
|
2024-08-04 18:28:35 +03:00
|
|
|
}
|
2023-08-15 23:59:07 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Fl_Widget::handle(event);
|
|
|
|
}
|
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2005-11-27 08:15:47 +03:00
|
|
|
// Sudoku class globals...
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Help_Dialog *Sudoku::help_dialog_ = (Fl_Help_Dialog *)0;
|
2022-01-19 18:08:29 +03:00
|
|
|
Fl_Preferences Sudoku::prefs_(Fl_Preferences::USER_L, "fltk.org", "sudoku");
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2024-08-05 00:42:03 +03:00
|
|
|
static void undo_cb(Fl_Widget*, void*) {
|
|
|
|
sudoku->undo();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void redo_cb(Fl_Widget*, void*) {
|
|
|
|
sudoku->redo();
|
|
|
|
}
|
2005-11-27 08:15:47 +03:00
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
// Create a Sudoku game window...
|
|
|
|
Sudoku::Sudoku()
|
2024-08-05 00:42:03 +03:00
|
|
|
: Fl_Double_Window(GROUP_SIZE * 3, GROUP_SIZE * 3 + MENU_OFFSET, "Sudoku"),
|
|
|
|
undo_head_(0), undo_tail_(0), redo_head_(0)
|
2005-11-25 23:40:16 +03:00
|
|
|
{
|
2007-05-20 04:01:06 +04:00
|
|
|
int j, k;
|
2005-11-25 23:40:16 +03:00
|
|
|
Fl_Group *g;
|
|
|
|
SudokuCell *cell;
|
2020-07-01 19:03:10 +03:00
|
|
|
static Fl_Menu_Item items[] = {
|
2005-11-25 23:40:16 +03:00
|
|
|
{ "&Game", 0, 0, 0, FL_SUBMENU },
|
2024-08-05 00:42:03 +03:00
|
|
|
{ "&New Game", FL_COMMAND | 'n', new_cb, 0, FL_MENU_DIVIDER },
|
|
|
|
{ "&Check Game", FL_COMMAND | 'c', check_cb, 0, 0 },
|
|
|
|
{ "&Restart Game", FL_COMMAND | 'r', restart_cb, 0, 0 },
|
|
|
|
{ "&Solve Game", FL_COMMAND | 's', solve_cb, 0, FL_MENU_DIVIDER },
|
|
|
|
{ "&Update Helpers", 0, update_helpers_cb, 0, 0 },
|
|
|
|
{ "&Mute Sound", FL_COMMAND | 'm', mute_cb, 0, FL_MENU_TOGGLE | FL_MENU_DIVIDER },
|
2023-08-16 11:27:41 +03:00
|
|
|
#ifndef USE_MACOS
|
2024-08-05 00:42:03 +03:00
|
|
|
{ "&Quit", FL_COMMAND | 'q', close_cb, 0, 0 },
|
2023-08-16 11:27:41 +03:00
|
|
|
#endif
|
2024-08-05 00:42:03 +03:00
|
|
|
{ 0 },
|
|
|
|
{ "&Edit", 0, 0, 0, FL_SUBMENU },
|
|
|
|
{ "&Undo", FL_COMMAND | 'z', undo_cb },
|
|
|
|
{ "&Redo", FL_COMMAND | 'Z', redo_cb },
|
|
|
|
{ 0 },
|
2005-11-25 23:40:16 +03:00
|
|
|
{ "&Difficulty", 0, 0, 0, FL_SUBMENU },
|
2024-08-05 00:42:03 +03:00
|
|
|
{ "&Easy", 0, diff_cb, (void *)"0", FL_MENU_RADIO },
|
|
|
|
{ "&Medium", 0, diff_cb, (void *)"1", FL_MENU_RADIO },
|
|
|
|
{ "&Hard", 0, diff_cb, (void *)"2", FL_MENU_RADIO },
|
|
|
|
{ "&Impossible", 0, diff_cb, (void *)"3", FL_MENU_RADIO },
|
|
|
|
{ 0 },
|
|
|
|
#ifndef USE_MACOS
|
2005-11-27 08:15:47 +03:00
|
|
|
{ "&Help", 0, 0, 0, FL_SUBMENU },
|
2024-08-05 00:42:03 +03:00
|
|
|
{ "&About Sudoku", FL_F + 1, help_cb, 0, 0 },
|
|
|
|
{ 0 },
|
|
|
|
#endif
|
2005-11-25 23:40:16 +03:00
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-12-05 03:55:01 +03:00
|
|
|
// Setup sound output...
|
2007-05-20 04:01:06 +04:00
|
|
|
prefs_.get("mute_sound", j, 0);
|
|
|
|
if (j) {
|
2007-02-08 19:37:03 +03:00
|
|
|
// Mute sound?
|
|
|
|
sound_ = NULL;
|
|
|
|
items[6].flags |= FL_MENU_VALUE;
|
|
|
|
} else sound_ = new SudokuSound();
|
2005-12-05 03:55:01 +03:00
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
// Menubar...
|
2005-11-27 04:56:57 +03:00
|
|
|
prefs_.get("difficulty", difficulty_, 0);
|
|
|
|
if (difficulty_ < 0 || difficulty_ > 3) difficulty_ = 0;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2009-12-21 19:28:58 +03:00
|
|
|
items[10 + difficulty_].flags |= FL_MENU_VALUE;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
|
|
|
menubar_ = new Fl_Sys_Menu_Bar(0, 0, 3 * GROUP_SIZE, 25);
|
|
|
|
menubar_->menu(items);
|
2024-08-05 00:42:03 +03:00
|
|
|
#ifdef USE_MACOS
|
|
|
|
menubar_->about(help_cb, NULL);
|
|
|
|
#endif
|
2005-11-25 23:40:16 +03:00
|
|
|
|
|
|
|
// Create the grids...
|
2023-08-15 23:59:07 +03:00
|
|
|
grid_ = new Fl_Group(0, MENU_OFFSET, 3 * GROUP_SIZE, 3 * GROUP_SIZE);
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 3; j ++)
|
|
|
|
for (k = 0; k < 3; k ++) {
|
2023-08-15 23:59:07 +03:00
|
|
|
g = new Fl_Group(k * GROUP_SIZE, j * GROUP_SIZE + MENU_OFFSET,
|
|
|
|
GROUP_SIZE, GROUP_SIZE);
|
|
|
|
g->box(FL_BORDER_BOX);
|
|
|
|
if ((int)(j == 1) ^ (int)(k == 1)) g->color(FL_DARK3);
|
|
|
|
else g->color(FL_DARK2);
|
|
|
|
g->end();
|
|
|
|
|
|
|
|
grid_groups_[j][k] = g;
|
2005-11-27 08:15:47 +03:00
|
|
|
}
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 9; j ++)
|
|
|
|
for (k = 0; k < 9; k ++) {
|
2024-08-04 18:28:35 +03:00
|
|
|
cell = new SudokuCell(j, k,
|
|
|
|
k * CELL_SIZE + CELL_OFFSET +
|
2023-08-15 23:59:07 +03:00
|
|
|
(k / 3) * (GROUP_SIZE - 3 * CELL_SIZE),
|
|
|
|
j * CELL_SIZE + CELL_OFFSET + MENU_OFFSET +
|
|
|
|
(j / 3) * (GROUP_SIZE - 3 * CELL_SIZE),
|
|
|
|
CELL_SIZE, CELL_SIZE);
|
2005-11-27 08:15:47 +03:00
|
|
|
cell->callback(reset_cb);
|
2007-05-20 04:01:06 +04:00
|
|
|
grid_cells_[j][k] = cell;
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:03:29 +03:00
|
|
|
// Set icon for window
|
|
|
|
Fl_Bitmap bm(sudoku_bits, sudoku_width, sudoku_height);
|
|
|
|
Fl_Image_Surface surf(sudoku_width, sudoku_height, 1);
|
|
|
|
Fl_Surface_Device::push_current(&surf);
|
|
|
|
fl_color(FL_WHITE);
|
|
|
|
fl_rectf(0, 0, sudoku_width, sudoku_height);
|
|
|
|
fl_color(FL_BLACK);
|
|
|
|
bm.draw(0, 0);
|
|
|
|
Fl_Surface_Device::pop_current();
|
|
|
|
icon(surf.image());
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2005-11-27 17:45:48 +03:00
|
|
|
// Catch window close events...
|
2005-11-25 23:40:16 +03:00
|
|
|
callback(close_cb);
|
2005-11-27 17:45:48 +03:00
|
|
|
|
|
|
|
// Make the window resizable...
|
2005-11-25 23:40:16 +03:00
|
|
|
resizable(grid_);
|
2005-11-27 17:45:48 +03:00
|
|
|
size_range(3 * GROUP_SIZE, 3 * GROUP_SIZE + MENU_OFFSET, 0, 0, 5, 5, 1);
|
2005-11-25 23:40:16 +03:00
|
|
|
|
|
|
|
// Restore the previous window dimensions...
|
|
|
|
int X, Y, W, H;
|
|
|
|
|
|
|
|
if (prefs_.get("x", X, -1)) {
|
|
|
|
prefs_.get("y", Y, -1);
|
|
|
|
prefs_.get("width", W, 3 * GROUP_SIZE);
|
|
|
|
prefs_.get("height", H, 3 * GROUP_SIZE + MENU_OFFSET);
|
|
|
|
|
2006-08-23 15:34:36 +04:00
|
|
|
resize(X, Y, W, H);
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
2005-11-27 04:56:57 +03:00
|
|
|
set_title();
|
2024-08-05 00:42:03 +03:00
|
|
|
clear_undo();
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-05 03:55:01 +03:00
|
|
|
// Destroy the sudoku window...
|
|
|
|
Sudoku::~Sudoku() {
|
2007-02-08 19:37:03 +03:00
|
|
|
if (sound_) delete sound_;
|
2005-12-05 03:55:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
// Check for a solution to the game...
|
|
|
|
void
|
|
|
|
Sudoku::check_cb(Fl_Widget *widget, void *) {
|
2024-08-04 17:46:04 +03:00
|
|
|
sudoku->check_game();
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check if the user has correctly solved the game...
|
|
|
|
void
|
2005-11-26 05:17:04 +03:00
|
|
|
Sudoku::check_game(bool highlight) {
|
2005-11-25 23:40:16 +03:00
|
|
|
bool empty = false;
|
|
|
|
bool correct = true;
|
2007-05-20 04:01:06 +04:00
|
|
|
int j, k, m;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
|
|
|
// Check the game for right/wrong answers...
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 9; j ++)
|
|
|
|
for (k = 0; k < 9; k ++) {
|
|
|
|
SudokuCell *cell = grid_cells_[j][k];
|
2005-11-25 23:40:16 +03:00
|
|
|
int val = cell->value();
|
|
|
|
|
2005-12-04 04:43:22 +03:00
|
|
|
if (cell->readonly()) continue;
|
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
if (!val) empty = true;
|
2005-12-02 04:34:25 +03:00
|
|
|
else {
|
2007-05-20 04:01:06 +04:00
|
|
|
for (m = 0; m < 9; m ++)
|
|
|
|
if ((j != m && grid_cells_[m][k]->value() == val) ||
|
2020-07-01 19:03:10 +03:00
|
|
|
(k != m && grid_cells_[j][m]->value() == val)) break;
|
2005-12-02 04:34:25 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
if (m < 9) {
|
2005-12-02 04:34:25 +03:00
|
|
|
if (highlight) {
|
2020-07-01 19:03:10 +03:00
|
|
|
cell->color(FL_YELLOW);
|
|
|
|
cell->redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
correct = false;
|
|
|
|
} else if (highlight) {
|
|
|
|
cell->color(FL_LIGHT3);
|
|
|
|
cell->redraw();
|
|
|
|
}
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-13 21:53:38 +03:00
|
|
|
// Check subgrids for duplicate numbers...
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 9; j += 3)
|
|
|
|
for (k = 0; k < 9; k += 3)
|
|
|
|
for (int jj = 0; jj < 3; jj ++)
|
|
|
|
for (int kk = 0; kk < 3; kk ++) {
|
2020-07-01 19:03:10 +03:00
|
|
|
SudokuCell *cell = grid_cells_[j + jj][k + kk];
|
|
|
|
int val = cell->value();
|
2006-01-13 21:53:38 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
if (cell->readonly() || !val) continue;
|
2006-01-13 21:53:38 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
int jjj;
|
2006-01-13 21:53:38 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (jjj = 0; jjj < 3; jjj ++) {
|
2020-07-01 19:03:10 +03:00
|
|
|
int kkk;
|
2006-01-13 21:53:38 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
for (kkk = 0; kkk < 3; kkk ++)
|
2007-05-20 04:01:06 +04:00
|
|
|
if (jj != jjj && kk != kkk &&
|
2020-07-01 19:03:10 +03:00
|
|
|
grid_cells_[j + jjj][k + kkk]->value() == val) break;
|
2006-01-13 21:53:38 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
if (kkk < 3) break;
|
2020-07-01 19:03:10 +03:00
|
|
|
}
|
2006-01-13 21:53:38 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
if (jjj < 3) {
|
2006-01-13 21:53:38 +03:00
|
|
|
if (highlight) {
|
2020-07-01 19:03:10 +03:00
|
|
|
cell->color(FL_YELLOW);
|
|
|
|
cell->redraw();
|
|
|
|
}
|
2006-01-13 21:53:38 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
correct = false;
|
|
|
|
}
|
|
|
|
}
|
2006-01-13 21:53:38 +03:00
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
if (!empty && correct) {
|
|
|
|
// Success!
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 9; j ++) {
|
|
|
|
for (k = 0; k < 9; k ++) {
|
2020-07-01 19:03:10 +03:00
|
|
|
SudokuCell *cell = grid_cells_[j][k];
|
|
|
|
cell->color(FL_GREEN);
|
|
|
|
cell->readonly(1);
|
2005-11-27 04:56:57 +03:00
|
|
|
}
|
2005-12-02 04:34:25 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
if (sound_) sound_->play('A' + grid_cells_[j][8]->value() - 1);
|
2005-12-06 06:55:45 +03:00
|
|
|
}
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Close the window, saving the game first...
|
|
|
|
void
|
|
|
|
Sudoku::close_cb(Fl_Widget *widget, void *) {
|
2024-08-04 17:46:04 +03:00
|
|
|
sudoku->save_game();
|
|
|
|
sudoku->hide();
|
2005-11-27 08:15:47 +03:00
|
|
|
|
|
|
|
if (help_dialog_) help_dialog_->hide();
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set the level of difficulty...
|
|
|
|
void
|
|
|
|
Sudoku::diff_cb(Fl_Widget *widget, void *d) {
|
2005-12-09 21:31:34 +03:00
|
|
|
int diff = atoi((char *)d);
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2024-08-04 17:46:04 +03:00
|
|
|
if (diff != sudoku->difficulty_) {
|
|
|
|
sudoku->difficulty_ = diff;
|
2024-08-05 00:42:03 +03:00
|
|
|
sudoku->new_cb(widget, NULL);
|
2024-08-04 17:46:04 +03:00
|
|
|
sudoku->set_title();
|
2005-11-27 04:56:57 +03:00
|
|
|
|
2006-08-24 22:09:44 +04:00
|
|
|
if (diff > 1)
|
|
|
|
{
|
|
|
|
// Display a message about the higher difficulty levels for the
|
|
|
|
// Sudoku zealots of the world...
|
|
|
|
int val;
|
|
|
|
|
|
|
|
prefs_.get("difficulty_warning", val, 0);
|
|
|
|
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
prefs_.set("difficulty_warning", 1);
|
2020-07-01 19:03:10 +03:00
|
|
|
fl_alert("Note: 'Hard' and 'Impossible' puzzles may have more than "
|
|
|
|
"one possible solution.\n"
|
|
|
|
"This is not an error or bug.");
|
2006-08-24 22:09:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-04 17:46:04 +03:00
|
|
|
prefs_.set("difficulty", sudoku->difficulty_);
|
2005-12-09 21:31:34 +03:00
|
|
|
}
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
2007-01-24 15:03:36 +03:00
|
|
|
// Update the little marker numbers in all cells
|
|
|
|
void
|
|
|
|
Sudoku::update_helpers_cb(Fl_Widget *widget, void *) {
|
2024-08-04 17:46:04 +03:00
|
|
|
sudoku->update_helpers();
|
2007-01-24 15:03:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sudoku::update_helpers() {
|
2007-05-20 04:01:06 +04:00
|
|
|
int j, k, m;
|
|
|
|
|
|
|
|
// First we delete any entries that the user may have made
|
|
|
|
for (j = 0; j < 9; j ++) {
|
|
|
|
for (k = 0; k < 9; k ++) {
|
|
|
|
SudokuCell *cell = grid_cells_[j][k];
|
2024-08-04 18:28:35 +03:00
|
|
|
cell->clear_hints();
|
2007-01-24 15:03:36 +03:00
|
|
|
}
|
|
|
|
}
|
2007-05-20 04:01:06 +04:00
|
|
|
|
|
|
|
// Now go through all cells and find out, what we can not be
|
|
|
|
for (j = 0; j < 81; j ++) {
|
2007-01-24 15:03:36 +03:00
|
|
|
char taken[10] = { 0 };
|
2007-05-20 04:01:06 +04:00
|
|
|
// Find our destination cell
|
|
|
|
int row = j / 9;
|
|
|
|
int col = j % 9;
|
2007-01-24 15:03:36 +03:00
|
|
|
SudokuCell *dst_cell = grid_cells_[row][col];
|
|
|
|
if (dst_cell->value()) continue;
|
2007-05-20 04:01:06 +04:00
|
|
|
// Find all values already taken in this row
|
|
|
|
for (k = 0; k < 9; k ++) {
|
|
|
|
SudokuCell *cell = grid_cells_[row][k];
|
2007-01-24 15:03:36 +03:00
|
|
|
int v = cell->value();
|
|
|
|
if (v) taken[v] = 1;
|
|
|
|
}
|
2007-05-20 04:01:06 +04:00
|
|
|
// Find all values already taken in this column
|
|
|
|
for (k = 0; k < 9; k ++) {
|
|
|
|
SudokuCell *cell = grid_cells_[k][col];
|
2007-01-24 15:03:36 +03:00
|
|
|
int v = cell->value();
|
|
|
|
if (v) taken[v] = 1;
|
|
|
|
}
|
2007-05-20 04:01:06 +04:00
|
|
|
// Now find all values already taken in this square
|
|
|
|
int ro = (row / 3) * 3;
|
|
|
|
int co = (col / 3) * 3;
|
|
|
|
for (k = 0; k < 3; k ++) {
|
|
|
|
for (m = 0; m < 3; m ++) {
|
2008-02-26 23:37:01 +03:00
|
|
|
SudokuCell *cell = grid_cells_[ro + k][co + m];
|
2007-01-24 15:03:36 +03:00
|
|
|
int v = cell->value();
|
|
|
|
if (v) taken[v] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// transfer our findings to the markers
|
2024-08-04 18:28:35 +03:00
|
|
|
for (m = 1; m <= 9; m ++) {
|
2008-02-26 23:37:01 +03:00
|
|
|
if (!taken[m])
|
2024-08-04 18:28:35 +03:00
|
|
|
dst_cell->set_hint(m);
|
2007-01-24 15:03:36 +03:00
|
|
|
}
|
|
|
|
}
|
2024-08-05 00:42:03 +03:00
|
|
|
undo_checkpoint();
|
2007-01-24 15:03:36 +03:00
|
|
|
}
|
|
|
|
|
2024-08-04 18:28:35 +03:00
|
|
|
void Sudoku::clear_hints_for(int row, int col, int val) {
|
|
|
|
int i, j;
|
|
|
|
// clear row
|
|
|
|
for (i = 0; i < 9; ++i)
|
|
|
|
grid_cells_[row][i]->clear_hint(val);
|
|
|
|
// clear column
|
|
|
|
for (i = 0; i < 9; ++i)
|
|
|
|
grid_cells_[i][col]->clear_hint(val);
|
|
|
|
// clear block
|
|
|
|
row = (row / 3) * 3;
|
|
|
|
col = (col / 3) * 3;
|
|
|
|
for (i = 0; i < 3; ++i)
|
|
|
|
for (j = 0; j < 3; ++j)
|
|
|
|
grid_cells_[row+i][col+j]->clear_hint(val);
|
|
|
|
}
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2005-11-27 08:15:47 +03:00
|
|
|
// Show the on-line help...
|
|
|
|
void
|
|
|
|
Sudoku::help_cb(Fl_Widget *, void *) {
|
|
|
|
if (!help_dialog_) {
|
|
|
|
help_dialog_ = new Fl_Help_Dialog();
|
|
|
|
|
|
|
|
help_dialog_->value(
|
2020-07-01 19:03:10 +03:00
|
|
|
"<HTML>\n"
|
|
|
|
"<HEAD>\n"
|
|
|
|
"<TITLE>Sudoku Help</TITLE>\n"
|
|
|
|
"</HEAD>\n"
|
|
|
|
"<BODY BGCOLOR='#ffffff'>\n"
|
2005-11-27 08:15:47 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
"<H2>About the Game</H2>\n"
|
2005-11-27 08:15:47 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
"<P>Sudoku (pronounced soo-dough-coo with the emphasis on the\n"
|
2005-11-27 08:15:47 +03:00
|
|
|
"first syllable) is a simple number-based puzzle/game played on a\n"
|
2020-07-01 19:03:10 +03:00
|
|
|
"9x9 grid that is divided into 3x3 subgrids. The goal is to enter\n"
|
|
|
|
"a number from 1 to 9 in each cell so that each number appears\n"
|
|
|
|
"only once in each column and row. In addition, each 3x3 subgrid\n"
|
|
|
|
"may only contain one of each number.</P>\n"
|
|
|
|
|
|
|
|
"<P>This version of the puzzle is copyright 2005-2010 by Michael R\n"
|
|
|
|
"Sweet.</P>\n"
|
|
|
|
|
|
|
|
"<P><B>Note:</B> The 'Hard' and 'Impossible' difficulty\n"
|
|
|
|
"levels generate Sudoku puzzles with multiple possible solutions.\n"
|
|
|
|
"While some purists insist that these cannot be called 'Sudoku'\n"
|
|
|
|
"puzzles, the author (me) has personally solved many such puzzles\n"
|
|
|
|
"in published/printed Sudoku books and finds them far more\n"
|
|
|
|
"interesting than the simple single solution variety. If you don't\n"
|
|
|
|
"like it, don't play with the difficulty set to 'High' or\n"
|
|
|
|
"'Impossible'.</P>\n"
|
|
|
|
|
|
|
|
"<H2>How to Play the Game</H2>\n"
|
|
|
|
|
|
|
|
"<P>At the start of a new game, Sudoku fills in a random selection\n"
|
|
|
|
"of cells for you - the number of cells depends on the difficulty\n"
|
|
|
|
"level you use. Click in any of the empty cells or use the arrow\n"
|
|
|
|
"keys to highlight individual cells and press a number from 1 to 9\n"
|
|
|
|
"to fill in the cell. To clear a cell, press 0, Delete, or\n"
|
|
|
|
"Backspace. When you have successfully completed all subgrids, the\n"
|
|
|
|
"entire puzzle is highlighted in green until you start a new\n"
|
|
|
|
"game.</P>\n"
|
|
|
|
|
|
|
|
"<P>As you work to complete the puzzle, you can display possible\n"
|
|
|
|
"solutions inside each cell by holding the Shift key and pressing\n"
|
|
|
|
"each number in turn. Repeat the process to remove individual\n"
|
|
|
|
"numbers, or press a number without the Shift key to replace them\n"
|
|
|
|
"with the actual number to use.</P>\n"
|
|
|
|
"</BODY>\n"
|
2005-11-27 08:15:47 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
help_dialog_->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
// Load the game from saved preferences...
|
|
|
|
void
|
|
|
|
Sudoku::load_game() {
|
|
|
|
// Load the current values and state of each grid...
|
|
|
|
memset(grid_values_, 0, sizeof(grid_values_));
|
|
|
|
|
2005-11-26 09:23:13 +03:00
|
|
|
bool solved = true;
|
|
|
|
|
2023-08-15 23:59:07 +03:00
|
|
|
for (int j = 0; j < 9; j ++)
|
2007-05-20 04:01:06 +04:00
|
|
|
for (int k = 0; k < 9; k ++) {
|
2023-08-15 23:59:07 +03:00
|
|
|
char name[255];
|
|
|
|
int val;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
SudokuCell *cell = grid_cells_[j][k];
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2023-08-15 23:59:07 +03:00
|
|
|
snprintf(name, sizeof(name), "value%d.%d", j, k);
|
|
|
|
if (!prefs_.get(name, val, 0)) {
|
|
|
|
j = 9;
|
|
|
|
grid_values_[0][0] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
grid_values_[j][k] = val;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2023-08-15 23:59:07 +03:00
|
|
|
snprintf(name, sizeof(name), "state%d.%d", j, k);
|
|
|
|
prefs_.get(name, val, 0);
|
|
|
|
cell->value(val);
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2023-08-15 23:59:07 +03:00
|
|
|
snprintf(name, sizeof(name), "readonly%d.%d", j, k);
|
|
|
|
prefs_.get(name, val, 0);
|
|
|
|
cell->readonly(val != 0);
|
|
|
|
|
|
|
|
if (val) cell->color(FL_GRAY);
|
|
|
|
else {
|
2005-11-26 09:23:13 +03:00
|
|
|
cell->color(FL_LIGHT3);
|
2020-07-01 19:03:10 +03:00
|
|
|
solved = false;
|
2005-11-26 09:23:13 +03:00
|
|
|
}
|
2005-11-26 01:43:33 +03:00
|
|
|
|
2024-08-04 18:28:35 +03:00
|
|
|
snprintf(name, sizeof(name), "hint%d.%d", j, k);
|
|
|
|
prefs_.get(name, val, 0);
|
|
|
|
cell->set_hint_map(val);
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
2005-11-26 09:23:13 +03:00
|
|
|
// If we didn't load any values or the last game was solved, then
|
|
|
|
// create a new game automatically...
|
2005-12-02 17:57:39 +03:00
|
|
|
if (solved || !grid_values_[0][0]) new_game(time(NULL));
|
2005-11-27 08:15:47 +03:00
|
|
|
else check_game(false);
|
2024-08-05 00:42:03 +03:00
|
|
|
clear_undo();
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-08 19:37:03 +03:00
|
|
|
// Mute/unmute sound...
|
|
|
|
void
|
|
|
|
Sudoku::mute_cb(Fl_Widget *widget, void *) {
|
2024-08-04 17:46:04 +03:00
|
|
|
if (sudoku->sound_) {
|
|
|
|
delete sudoku->sound_;
|
|
|
|
sudoku->sound_ = NULL;
|
2007-02-08 19:37:03 +03:00
|
|
|
prefs_.set("mute_sound", 1);
|
|
|
|
} else {
|
2024-08-04 17:46:04 +03:00
|
|
|
sudoku->sound_ = new SudokuSound();
|
2007-02-08 19:37:03 +03:00
|
|
|
prefs_.set("mute_sound", 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
// Create a new game...
|
|
|
|
void
|
|
|
|
Sudoku::new_cb(Fl_Widget *widget, void *) {
|
2024-08-04 17:46:04 +03:00
|
|
|
if (sudoku->grid_cells_[0][0]->color() != FL_GREEN) {
|
2023-08-15 23:59:07 +03:00
|
|
|
if (!fl_choice("Are you sure you want to change the difficulty level and "
|
|
|
|
"discard the current game?", "Keep Current Game", "Start New Game",
|
|
|
|
NULL)) return;
|
|
|
|
}
|
2024-08-04 17:46:04 +03:00
|
|
|
sudoku->new_game(time(NULL));
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
2023-08-15 23:59:07 +03:00
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
// Create a new game...
|
|
|
|
void
|
2005-12-02 17:57:39 +03:00
|
|
|
Sudoku::new_game(time_t seed) {
|
2007-05-20 04:01:06 +04:00
|
|
|
int j, k, m, n, t, count;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
// Generate a new (valid) Sudoku grid...
|
2005-12-02 17:57:39 +03:00
|
|
|
seed_ = seed;
|
2016-04-06 00:15:45 +03:00
|
|
|
srand((unsigned int)seed);
|
2005-11-25 23:40:16 +03:00
|
|
|
|
|
|
|
memset(grid_values_, 0, sizeof(grid_values_));
|
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 9; j += 3) {
|
|
|
|
for (k = 0; k < 9; k += 3) {
|
2005-11-25 23:40:16 +03:00
|
|
|
for (t = 1; t <= 9; t ++) {
|
2020-07-01 19:03:10 +03:00
|
|
|
for (count = 0; count < 20; count ++) {
|
|
|
|
m = j + (rand() % 3);
|
|
|
|
n = k + (rand() % 3);
|
|
|
|
if (!grid_values_[m][n]) {
|
|
|
|
int mm;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
for (mm = 0; mm < m; mm ++)
|
|
|
|
if (grid_values_[mm][n] == t) break;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
if (mm < m) continue;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
int nn;
|
2007-05-20 04:01:06 +04:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
for (nn = 0; nn < n; nn ++)
|
|
|
|
if (grid_values_[m][nn] == t) break;
|
2007-05-20 04:01:06 +04:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
if (nn < n) continue;
|
2007-05-20 04:01:06 +04:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
grid_values_[m][n] = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
if (count == 20) {
|
|
|
|
// Unable to find a valid puzzle so far, so start over...
|
|
|
|
k = 9;
|
|
|
|
j = -3;
|
|
|
|
memset(grid_values_, 0, sizeof(grid_values_));
|
|
|
|
}
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start by making all cells editable
|
|
|
|
SudokuCell *cell;
|
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 9; j ++)
|
|
|
|
for (k = 0; k < 9; k ++) {
|
|
|
|
cell = grid_cells_[j][k];
|
2005-11-25 23:40:16 +03:00
|
|
|
cell->value(0);
|
|
|
|
cell->readonly(0);
|
|
|
|
cell->color(FL_LIGHT3);
|
2024-08-05 00:42:03 +03:00
|
|
|
cell->clear_hints();
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
2005-11-27 04:56:57 +03:00
|
|
|
// Show N cells...
|
2006-08-24 22:09:44 +04:00
|
|
|
count = 11 * (5 - difficulty_);
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2005-11-26 17:25:01 +03:00
|
|
|
int numbers[9];
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 9; j ++) numbers[j] = j + 1;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2005-11-26 17:25:01 +03:00
|
|
|
while (count > 0) {
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 20; j ++) {
|
2005-11-26 17:25:01 +03:00
|
|
|
k = rand() % 9;
|
|
|
|
m = rand() % 9;
|
|
|
|
t = numbers[k];
|
|
|
|
numbers[k] = numbers[m];
|
|
|
|
numbers[m] = t;
|
|
|
|
}
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; count > 0 && j < 9; j ++) {
|
|
|
|
t = numbers[j];
|
2005-11-26 17:25:01 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (k = 0; count > 0 && k < 9; k ++) {
|
|
|
|
cell = grid_cells_[j][k];
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
if (grid_values_[j][k] == t && !cell->readonly()) {
|
2020-07-01 19:03:10 +03:00
|
|
|
cell->value(grid_values_[j][k]);
|
|
|
|
cell->readonly(1);
|
|
|
|
cell->color(FL_GRAY);
|
2005-11-26 09:23:13 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
count --;
|
|
|
|
break;
|
|
|
|
}
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
2005-11-26 17:25:01 +03:00
|
|
|
}
|
2005-11-27 04:56:57 +03:00
|
|
|
}
|
2024-08-05 00:42:03 +03:00
|
|
|
clear_undo();
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-12 07:23:05 +03:00
|
|
|
// Return the next available value for a cell...
|
|
|
|
int
|
|
|
|
Sudoku::next_value(SudokuCell *c) {
|
2020-07-01 19:03:10 +03:00
|
|
|
int j = 0, k = 0, m = 0, n = 0;
|
2005-12-12 07:23:05 +03:00
|
|
|
|
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 9; j ++) {
|
|
|
|
for (k = 0; k < 9; k ++)
|
|
|
|
if (grid_cells_[j][k] == c) break;
|
2005-12-12 07:23:05 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
if (k < 9) break;
|
2005-12-12 07:23:05 +03:00
|
|
|
}
|
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
if (j == 9) return 1;
|
2005-12-12 07:23:05 +03:00
|
|
|
|
|
|
|
j -= j % 3;
|
2007-05-20 04:01:06 +04:00
|
|
|
k -= k % 3;
|
2005-12-12 07:23:05 +03:00
|
|
|
|
|
|
|
int numbers[9];
|
|
|
|
|
|
|
|
memset(numbers, 0, sizeof(numbers));
|
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (m = 0; m < 3; m ++)
|
|
|
|
for (n = 0; n < 3; n ++) {
|
|
|
|
c = grid_cells_[j + m][k + n];
|
2005-12-12 07:23:05 +03:00
|
|
|
if (c->value()) numbers[c->value() - 1] = 1;
|
|
|
|
}
|
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 9; j ++)
|
|
|
|
if (!numbers[j]) return j + 1;
|
2005-12-12 07:23:05 +03:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
// Reset widget color to gray...
|
|
|
|
void
|
|
|
|
Sudoku::reset_cb(Fl_Widget *widget, void *) {
|
|
|
|
widget->color(FL_LIGHT3);
|
|
|
|
widget->redraw();
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2005-11-26 05:17:04 +03:00
|
|
|
((Sudoku *)(widget->window()))->check_game(false);
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Resize the window...
|
|
|
|
void
|
|
|
|
Sudoku::resize(int X, int Y, int W, int H) {
|
|
|
|
// Resize the window...
|
2009-04-21 13:09:37 +04:00
|
|
|
Fl_Double_Window::resize(X, Y, W, H);
|
2005-11-25 23:40:16 +03:00
|
|
|
|
|
|
|
// Save the new window geometry...
|
|
|
|
prefs_.set("x", X);
|
|
|
|
prefs_.set("y", Y);
|
|
|
|
prefs_.set("width", W);
|
|
|
|
prefs_.set("height", H);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-12 07:23:05 +03:00
|
|
|
// Restart game from beginning...
|
|
|
|
void
|
|
|
|
Sudoku::restart_cb(Fl_Widget *widget, void *) {
|
2006-01-13 21:53:38 +03:00
|
|
|
bool solved = true;
|
2005-12-12 07:23:05 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (int j = 0; j < 9; j ++)
|
|
|
|
for (int k = 0; k < 9; k ++) {
|
2024-08-04 17:46:04 +03:00
|
|
|
SudokuCell *cell = sudoku->grid_cells_[j][k];
|
2024-08-05 00:42:03 +03:00
|
|
|
cell->clear_hints();
|
2005-12-12 07:23:05 +03:00
|
|
|
if (!cell->readonly()) {
|
2006-01-13 21:53:38 +03:00
|
|
|
solved = false;
|
2005-12-12 07:23:05 +03:00
|
|
|
int v = cell->value();
|
2020-07-01 19:03:10 +03:00
|
|
|
cell->value(0);
|
|
|
|
cell->color(FL_LIGHT3);
|
2024-08-04 17:46:04 +03:00
|
|
|
if (v && sudoku->sound_) sudoku->sound_->play('A' + v - 1);
|
2005-12-12 07:23:05 +03:00
|
|
|
}
|
|
|
|
}
|
2006-01-13 21:53:38 +03:00
|
|
|
|
2024-08-10 21:32:19 +03:00
|
|
|
if (solved)
|
2024-08-05 00:42:03 +03:00
|
|
|
sudoku->new_game(sudoku->seed_);
|
|
|
|
else
|
|
|
|
sudoku->clear_undo();
|
2005-12-12 07:23:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
// Save the current game state...
|
|
|
|
void
|
|
|
|
Sudoku::save_game() {
|
|
|
|
// Save the current values and state of each grid...
|
2023-08-15 23:59:07 +03:00
|
|
|
for (int j = 0; j < 9; j ++)
|
2007-05-20 04:01:06 +04:00
|
|
|
for (int k = 0; k < 9; k ++) {
|
2005-11-25 23:40:16 +03:00
|
|
|
char name[255];
|
2007-05-20 04:01:06 +04:00
|
|
|
SudokuCell *cell = grid_cells_[j][k];
|
2023-08-15 23:59:07 +03:00
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "value%d.%d", j, k);
|
|
|
|
prefs_.set(name, grid_values_[j][k]);
|
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "state%d.%d", j, k);
|
|
|
|
prefs_.set(name, cell->value());
|
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "readonly%d.%d", j, k);
|
|
|
|
prefs_.set(name, cell->readonly());
|
|
|
|
|
|
|
|
for (int m = 0; m < 8; m ++) {
|
2024-08-04 18:28:35 +03:00
|
|
|
snprintf(name, sizeof(name), "hint%d.%d", j, k);
|
|
|
|
prefs_.set(name, cell->get_hint_map());
|
2005-11-26 01:43:33 +03:00
|
|
|
}
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-05 00:42:03 +03:00
|
|
|
void Sudoku::save_state(GameState &s) {
|
|
|
|
for (int j = 0; j < 9; j ++) {
|
|
|
|
for (int k = 0; k < 9; k ++) {
|
|
|
|
s[j*9+k] = grid_cells_[j][k]->state();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sudoku::load_state(GameState &s) {
|
|
|
|
for (int j = 0; j < 9; j ++) {
|
|
|
|
for (int k = 0; k < 9; k ++) {
|
|
|
|
grid_cells_[j][k]->state(s[j*9+k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sudoku::undo() {
|
|
|
|
if (undo_head_ != undo_tail_) {
|
|
|
|
undo_head_ = ((undo_head_-1) & 63);
|
|
|
|
load_state(undo_stack[undo_head_]);
|
|
|
|
redraw();
|
|
|
|
} else {
|
|
|
|
fl_beep(FL_BEEP_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sudoku::redo() {
|
|
|
|
if (undo_head_ != redo_head_) {
|
|
|
|
undo_head_ = ((undo_head_+1) & 63);
|
|
|
|
load_state(undo_stack[undo_head_]);
|
|
|
|
redraw();
|
|
|
|
} else {
|
|
|
|
fl_beep(FL_BEEP_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sudoku::clear_undo() {
|
|
|
|
undo_head_ = undo_tail_ = redo_head_ = 0;
|
|
|
|
save_state(undo_stack[undo_head_]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sudoku::undo_checkpoint() {
|
|
|
|
if (undo_head_ == redo_head_) {
|
|
|
|
redo_head_ = ((redo_head_+1) & 63);
|
|
|
|
}
|
|
|
|
undo_head_ = ((undo_head_+1) & 63);
|
|
|
|
if (undo_head_ == undo_tail_) {
|
|
|
|
undo_tail_ = ((undo_tail_+1) & 63);
|
|
|
|
}
|
|
|
|
save_state(undo_stack[undo_head_]);
|
|
|
|
}
|
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2005-11-27 04:56:57 +03:00
|
|
|
// Set title of window...
|
|
|
|
void
|
|
|
|
Sudoku::set_title() {
|
|
|
|
static const char * const titles[] = {
|
|
|
|
"Sudoku - Easy",
|
|
|
|
"Sudoku - Medium",
|
|
|
|
"Sudoku - Hard",
|
|
|
|
"Sudoku - Impossible"
|
|
|
|
};
|
|
|
|
|
|
|
|
label(titles[difficulty_]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-25 23:40:16 +03:00
|
|
|
// Solve the puzzle...
|
|
|
|
void
|
|
|
|
Sudoku::solve_cb(Fl_Widget *widget, void *) {
|
2024-08-04 17:46:04 +03:00
|
|
|
sudoku->solve_game();
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Solve the puzzle...
|
|
|
|
void
|
|
|
|
Sudoku::solve_game() {
|
2007-05-20 04:01:06 +04:00
|
|
|
int j, k;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = 0; j < 9; j ++) {
|
|
|
|
for (k = 0; k < 9; k ++) {
|
|
|
|
SudokuCell *cell = grid_cells_[j][k];
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
cell->value(grid_values_[j][k]);
|
2005-11-26 01:43:33 +03:00
|
|
|
cell->readonly(1);
|
2005-12-06 06:55:45 +03:00
|
|
|
cell->color(FL_GRAY);
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
2005-12-06 06:55:45 +03:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
if (sound_) sound_->play('A' + grid_cells_[j][8]->value() - 1);
|
2005-12-06 06:55:45 +03:00
|
|
|
}
|
2024-08-05 00:42:03 +03:00
|
|
|
undo_checkpoint();
|
2005-11-25 23:40:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Main entry for game...
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) {
|
|
|
|
Sudoku s;
|
2024-08-04 17:46:04 +03:00
|
|
|
sudoku = &s;
|
2005-11-25 23:40:16 +03:00
|
|
|
|
2005-12-05 03:55:01 +03:00
|
|
|
// Show the game...
|
2005-11-25 23:40:16 +03:00
|
|
|
s.show(argc, argv);
|
2005-12-05 03:55:01 +03:00
|
|
|
|
|
|
|
// Load the previous game...
|
|
|
|
s.load_game();
|
|
|
|
|
|
|
|
// Run until the user quits...
|
2005-11-25 23:40:16 +03:00
|
|
|
return (Fl::run());
|
|
|
|
}
|