Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
jlib
Commits
89492d86
Commit
89492d86
authored
Dec 21, 2020
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sudoku client
parent
de679daa
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
161 additions
and
26 deletions
+161
-26
sudoku.h
jlib/misc/sudoku.h
+8
-5
sudoku_client.cpp
test/sudoku_client/sudoku_client.cpp
+125
-0
sudoku_client.vcxproj
test/sudoku_client/sudoku_client.vcxproj
+18
-20
sudoku_client.vcxproj.filters
test/sudoku_client/sudoku_client.vcxproj.filters
+5
-0
sudoku_client.vcxproj.user
test/sudoku_client/sudoku_client.vcxproj.user
+5
-1
No files found.
jlib/misc/sudoku.h
View file @
89492d86
...
...
@@ -6,6 +6,7 @@
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
namespace
jlib
{
namespace
misc
{
...
...
@@ -21,6 +22,8 @@ struct Helper {
int
groups_of
[
N
][
GROUPS_OF
];
// 每格属于3个组
int
groups
[
GROUPS
][
9
];
// 27个组 = 9行+9列+9块
std
::
default_random_engine
rng
{};
// 初始化辅助结构体,用户调用 solve 之前手动调用一次即可
Helper
()
{
memset
(
neighbors
,
-
1
,
sizeof
(
neighbors
));
...
...
@@ -280,7 +283,7 @@ inline int solve_n(const std::string& grid, OnSolved on_solved, int max_solves =
////////////////////////// 生成数独 ///////////////////////////////
// 随机获取可选数量最少的 cell
inline
int
random_least_cell_count
(
bool
cells
[
N
][
9
])
{
inline
int
random_least_cell_count
(
bool
cells
[
N
][
9
]
,
Helper
*
helper
)
{
int
ks
[
N
],
ki
=
0
,
n
=
9
;
ks
[
0
]
=
0
;
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
int
nn
=
cell_count
(
cells
,
i
);
...
...
@@ -292,13 +295,13 @@ inline int random_least_cell_count(bool cells[N][9]) {
}
}
}
std
::
shuffle
(
ks
,
ks
+
ki
,
std
::
default_random_engine
()
);
std
::
shuffle
(
ks
,
ks
+
ki
,
helper
->
rng
);
return
ks
[
0
];
}
inline
bool
random_search
(
bool
cells
[
N
][
9
],
Helper
*
helper
)
{
if
(
solved
(
cells
))
{
return
true
;
}
int
k
=
random_least_cell_count
(
cells
);
int
k
=
random_least_cell_count
(
cells
,
helper
);
for
(
int
val
=
1
;
val
<=
9
;
val
++
)
{
if
(
cell_on
(
cells
,
k
,
val
))
{
bool
cells1
[
N
][
9
];
...
...
@@ -332,10 +335,10 @@ inline std::string random_puzzle(Helper* helper = nullptr) {
helper
=
&
h
;
}
auto
solved_grid
=
random_solved_puzzle
(
helper
);
std
::
vector
<
int
>
random_N
=
[]()
{
std
::
vector
<
int
>
random_N
=
[
&
helper
]()
{
std
::
vector
<
int
>
n
(
N
,
0
);
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
n
[
i
]
=
i
;
}
std
::
shuffle
(
n
.
begin
(),
n
.
end
(),
std
::
default_random_engine
()
);
std
::
shuffle
(
n
.
begin
(),
n
.
end
(),
helper
->
rng
);
return
n
;
}();
...
...
test/sudoku_client/sudoku_client.cpp
0 → 100644
View file @
89492d86
#include "../../jlib/net/simple_libevent_client.h"
#include "../../jlib/misc/sudoku.h"
#include "../../jlib/log2.h"
using
namespace
jlib
::
net
;
using
namespace
jlib
::
misc
::
sudoku
;
// proto: 89 bytes
// [id:] puzzle \r\n
// n 81 2
Helper
helper
{};
int
puzzles
=
1000
;
int
npuzzle
=
0
;
bool
done
=
false
;
void
sendPuzzle
(
simple_libevent_client
*
client
)
{
if
(
npuzzle
++
<
puzzles
)
{
auto
puzzle
=
random_puzzle
(
&
helper
);
std
::
string
request
=
std
::
to_string
(
npuzzle
)
+
":"
+
puzzle
;
JLOG_INFO
(
request
);
request
+=
"
\r\n
"
;
client
->
send
(
request
.
c_str
(),
request
.
size
());
}
else
{
//client->stop();
done
=
true
;
}
}
void
onConnectinoCallback
(
bool
up
,
const
std
::
string
&
msg
,
void
*
user_data
)
{
if
(
up
)
{
sendPuzzle
(
reinterpret_cast
<
simple_libevent_client
*>
(
user_data
));
}
}
bool
processResponse
(
simple_libevent_client
*
client
,
const
std
::
string
&
response
)
{
std
::
string
id
,
result
,
info
;
std
::
string
::
const_iterator
colon
=
std
::
find
(
response
.
begin
(),
response
.
end
(),
':'
);
if
(
colon
!=
response
.
end
())
{
id
.
assign
(
response
.
cbegin
(),
colon
);
result
.
assign
(
colon
+
1
,
response
.
cend
());
}
else
{
result
=
response
;
}
if
(
result
.
size
()
==
81
)
{
if
(
id
.
empty
())
{
info
=
result
;
}
else
{
info
=
id
+
":"
+
result
;
}
JLOG_INFO
(
info
);
sendPuzzle
(
client
);
return
true
;
}
else
{
JLOG_ERRO
(
response
);
return
false
;
}
}
size_t
onMessageCallback
(
const
char
*
data
,
size_t
len
,
void
*
user_data
)
{
auto
client
=
reinterpret_cast
<
simple_libevent_client
*>
(
user_data
);
size_t
ate
=
0
;
while
(
len
-
ate
>=
81
+
2
)
{
const
char
*
crlf
=
strstr
(
data
+
ate
,
"
\r\n
"
);
if
(
crlf
)
{
std
::
string
response
(
data
+
ate
,
crlf
);
ate
=
crlf
-
data
+
2
;
if
(
!
processResponse
(
client
,
response
))
{
//client->stop();
done
=
true
;
break
;
}
}
else
if
(
len
-
ate
>
100
)
{
//client->stop();
done
=
true
;
break
;
}
else
{
break
;
}
}
return
ate
;
}
int
main
(
int
argc
,
char
**
argv
)
{
const
char
*
ip
=
"127.0.0.1"
;
int
port
=
9981
;
if
(
argc
>
1
)
{
ip
=
argv
[
1
];
}
if
(
argc
>
2
)
{
port
=
atoi
(
argv
[
2
]);
}
if
(
argc
>
3
)
{
puzzles
=
atoi
(
argv
[
3
]);
}
jlib
::
init_logger
(
L"sudoku_server"
);
simple_libevent_client
client
;
client
.
setUserData
(
&
client
);
client
.
setOnConnectionCallback
(
onConnectinoCallback
);
client
.
setOnMsgCallback
(
onMessageCallback
);
std
::
string
msg
;
if
(
!
client
.
start
(
ip
,
port
,
msg
,
true
))
{
JLOG_CRTC
(
msg
);
return
-
1
;
}
while
(
!
done
)
{
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
1
));
}
}
\ No newline at end of file
test/sudoku_client/sudoku_client.vcxproj
View file @
89492d86
...
...
@@ -17,7 +17,6 @@
<Configuration>
Release
</Configuration>
<Platform>
x64
</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup
Label=
"Globals"
>
<VCProjectVersion>
16.0
</VCProjectVersion>
...
...
@@ -53,11 +52,10 @@
<WholeProgramOptimization>
true
</WholeProgramOptimization>
<CharacterSet>
Unicode
</CharacterSet>
</PropertyGroup>
<Import
Project=
"$(VCTargetsPath)\Microsoft.Cpp.props"
/>
<ImportGroup
Label=
"ExtensionSettings"
>
</ImportGroup>
<ImportGroup
Label=
"Shared"
>
<ImportGroup
Label=
"Shared"
>
</ImportGroup>
<ImportGroup
Label=
"PropertySheets"
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
<Import
Project=
"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props"
Condition=
"exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')"
Label=
"LocalAppDataPlatform"
/>
...
...
@@ -71,7 +69,6 @@
<ImportGroup
Label=
"PropertySheets"
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
<Import
Project=
"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props"
Condition=
"exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')"
Label=
"LocalAppDataPlatform"
/>
</ImportGroup>
<PropertyGroup
Label=
"UserMacros"
/>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
<LinkIncremental>
true
</LinkIncremental>
...
...
@@ -85,7 +82,6 @@
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
<LinkIncremental>
false
</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
<ClCompile>
<WarningLevel>
Level3
</WarningLevel>
...
...
@@ -96,6 +92,7 @@
<Link>
<SubSystem>
Console
</SubSystem>
<GenerateDebugInformation>
true
</GenerateDebugInformation>
<AdditionalDependencies>
$(SolutionDir)$(Configuration)\simple_libevent_client_md.lib;%(AdditionalDependencies)
</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
...
...
@@ -142,8 +139,9 @@
<GenerateDebugInformation>
true
</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup></ItemGroup>
<ItemGroup>
<ClCompile
Include=
"sudoku_client.cpp"
/>
</ItemGroup>
<Import
Project=
"$(VCTargetsPath)\Microsoft.Cpp.targets"
/>
<ImportGroup
Label=
"ExtensionTargets"
>
</ImportGroup>
...
...
test/sudoku_client/sudoku_client.vcxproj.filters
View file @
89492d86
...
...
@@ -14,4 +14,9 @@
<Extensions>
rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile
Include=
"sudoku_client.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
test/sudoku_client/sudoku_client.vcxproj.user
View file @
89492d86
<?xml version="1.0" encoding="utf-8"?>
<Project
ToolsVersion=
"Current"
xmlns=
"http://schemas.microsoft.com/developer/msbuild/2003"
>
<PropertyGroup
/>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
<LocalDebuggerCommandArguments>
</LocalDebuggerCommandArguments>
<DebuggerFlavor>
WindowsLocalDebugger
</DebuggerFlavor>
</PropertyGroup>
</Project>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment