Skip to content

Commit

Permalink
支持显示UTF8字符
Browse files Browse the repository at this point in the history
  • Loading branch information
jsc723 committed Oct 29, 2023
1 parent 0400d5d commit 009841e
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 47 deletions.
5 changes: 3 additions & 2 deletions game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ namespace YGO {

}


void Game::run() {
void Game::runHeader() {
if (m_header.size()) {
Executor e(this, m_collections["H"], m_collections["H"]->end(), 0);
e.run_program(m_header);
}
}
void Game::run() {
if (m_debug) {
cout << "-----------run-------------" << endl;
}
Expand Down
1 change: 1 addition & 0 deletions game.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ namespace YGO {
public:
Game(const Deck &deck_template, const Simulator::Topic &topic, bool debug);
void run();
void runHeader();
bool execute_card(std::shared_ptr<CardCollection> col, CardNode it, int opt);
int compute_number(t_string program);
std::shared_ptr<CardCollection> find_card_position(CardNode it);
Expand Down
35 changes: 34 additions & 1 deletion global.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "global.h"
#include <random>
#include <Windows.h>

//ref: https://leetcode.com/problems/wildcard-matching/solution/
bool YGO::wildCardMatch(const t_string& s, const t_string& p)
Expand Down Expand Up @@ -130,4 +131,36 @@ double YGO::compute_std(const std::vector<double> data) {
sum += (x - mean) * (x - mean);
}
return sqrt(sum / (n - 1));
}
}

std::string YGO::utf8_to_local_encoding(const std::string& utf8String) {

// Calculate the required buffer size for the converted string
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, utf8String.c_str(), -1, nullptr, 0);

// Allocate a wide-character buffer
std::wstring wideString(requiredSize, 0);

// Convert the UTF-8 string to wide-character
MultiByteToWideChar(CP_UTF8, 0, utf8String.c_str(), -1, &wideString[0], requiredSize);

// Calculate the required buffer size for the console encoding
int consoleSize = WideCharToMultiByte(CP_ACP, 0, wideString.c_str(), -1, nullptr, 0, nullptr, nullptr);

// Allocate a buffer for the console-encoded string
std::string consoleString(consoleSize, 0);

// Convert the wide-character string to the console encoding
WideCharToMultiByte(CP_ACP, 0, wideString.c_str(), -1, &consoleString[0], consoleSize, nullptr, nullptr);

return consoleString;
}

YGO::UTF8CodePage::UTF8CodePage() : m_old_code_page(::GetConsoleOutputCP()) {
::SetConsoleOutputCP(CP_UTF8);
}

YGO::UTF8CodePage::~UTF8CodePage()
{
::SetConsoleOutputCP(m_old_code_page);
}
13 changes: 13 additions & 0 deletions global.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <functional>
#include <string>
#include <cmath>
#include <Windows.h>

namespace YGO
{
Expand All @@ -31,5 +32,17 @@ namespace YGO
double compute_ci(double p, int n);
double compute_mean(const std::vector<double> data);
double compute_std(const std::vector<double> data);

std::string utf8_to_local_encoding(const std::string& utf8String);

class UTF8CodePage {
public:
UTF8CodePage();
~UTF8CodePage();

private:
UINT m_old_code_page;
};


}
3 changes: 2 additions & 1 deletion simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ void YGO::Simulator::run(const Deck& deck_template, Context& context)
for (int i = 0; i < m_topics.size(); i++)
{
Game g(deck_template, m_topics[i], m_debug);
g.runHeader();
if (m_topics[i].m_exec_program) {
g.run();
}
Expand All @@ -143,7 +144,7 @@ void YGO::Simulator::run(const Deck& deck_template, Context& context)
if (combo_score > 0) {
success[i][j]++;
any_success = true;
max_topic_score = std::max(max_topic_score, combo_score);
max_topic_score = std::max<int>(max_topic_score, combo_score);
}
}
total_success[i] += any_success;
Expand Down
231 changes: 188 additions & 43 deletions test/kowakuma.yml
Original file line number Diff line number Diff line change
@@ -1,76 +1,221 @@
deck:
cards:
kowakuma-trion:
トリオンの蟲惑魔:
count: 3
attribute: ['monstor', 'kowakuma']
description: '特里恩虫惑魔'
kowakuma-tio:
count: 3
ティオの蟲惑魔:
count: 2
attribute: ['monstor', 'kowakuma']
description: '蒂奥虫惑魔'
kowakuma-ranka:
ランカの蟲惑魔:
count: 3
attribute: ['monstor', 'kowakuma']
description: '兰卡虫惑魔'
kowakuma-jiner:
プティカの蟲惑魔:
count: 3
attribute: ['monstor', 'kowakuma']
description: '吉娜虫惑魔'
kowakuma-atora:
attribute: ['monstor', 'kowakuma']
description: '阿特拉虫惑魔'
urara:
キノの蟲惑魔:
count: 3
attribute: ['monstor', 'hand-trap']
description: '灰流丽'
zou-g:
count: 3
attribute: ['monstor', 'hand-trap']
description: '增殖G'
bu-mian-jia:
attribute: ['monstor', 'kowakuma']
蟲惑の園:
count: 1
attribute: []
description: '虫妹场地'
灰流丽:
count: 2
attribute: ['monstor', 'hand-trap', 'k', 'x']
# 增殖的G:
# count: 2
# attribute: ['monstor', 'hand-trap', 'k', 'x']

ホールティアの蟲惑魔:
count: 2
attribute: ['trap', 'kowakuma-trap']
description: 'ホールティアの蟲惑魔'
锁鸟:
count: 2
attribute: ['trap', 'hand-trap', 'k', 'x']
天童:
count: 2
attribute: ['kk']
:
count: 2
attribute: ['x', 'k']
白银姬:
count: 1
三战:
count: 1
attribute: ['kk']
胜负:
count: 2
attribute: ['kk', 'trap']
泡影:
count: 2
attribute: ['k', 'trap', 'x']
本家坑:
count: 7
attribute: ['trap', 'kowakuma-trap', 'xx']
其他坑:
count: 3
attribute: ['trap']
description: '布面甲'
houyou:
attribute: ['trap', 'xx']
反击陷阱:
count: 3
attribute: ['trap', 'hand-trap']
description: '泡影'
other-trap:
count: 15
attribute: ['trap']
description: '红坑'
attribute: ['fanji-trap', 'xxx']

alias:
HandTrap: a:hand-trap
simulate:
count: 1000
count: 100
confidence-interval: true
tests:
test-expend:
header: >
(= sono 5);
(= frsa 20);
(= strs 10);
(= trap 15);
(= kino 5);
(= other 9);
(= other2 (* 2 other));
(= other3 (* 3 other));
(= other4 (* 4 other));
combos:
A1:
score: "|H.a:trap|"
Kowakuma0:
score: "other4"
hand:
- a:kowakuma
Trion0:
score: "(+ trap other4)"
hand:
- トリオンの蟲惑魔
Trion-NTrap:
score: "(+ trap frsa other3)"
hand:
- トリオンの蟲惑魔
- a:trap
Trion-KTrap:
score: "(+ trap trap frsa other3)"
hand:
- トリオンの蟲惑魔
- a:kowakuma-trap
Ranka0:
score: "(+ trap other4)"
hand:
- ランカの蟲惑魔
Ranka-KTrap:
score: "(+ trap frsa other3)"
hand:
- ランカの蟲惑魔
- a:kowakuma-trap
Putika-Trion:
score: "(+ trap trap frsa other3)"
hand:
- プティカの蟲惑魔
- トリオンの蟲惑魔
Putika-Tio:
score: "(+ trap frsa other3)"
hand:
- プティカの蟲惑魔
- ティオの蟲惑魔
Putika-Tio-KTrap:
score: "(+ trap trap frsa other2)"
hand:
- プティカの蟲惑魔
- ティオの蟲惑魔
- a:kowakuma-trap
Putika-Ranka:
score: "(+ trap frsa other3)"
hand:
- プティカの蟲惑魔
- ランカの蟲惑魔
Putika-Ranka-KTrap:
score: "(+ trap trap frsa other3)"
hand:
- プティカの蟲惑魔
- ランカの蟲惑魔
- a:kowakuma-trap
Putika-Kino-Trap:
score: "(+ trap trap trap other2)"
hand:
- プティカの蟲惑魔
- キノの蟲惑魔
- a:trap
Putika-Kino-KTrap:
score: "(+ trap trap frsa sono kino other2)"
hand:
- プティカの蟲惑魔
- キノの蟲惑魔
- a:kowakuma-trap
Holetia0:
score: "other3"
hand:
- ホールティアの蟲惑魔
- a:trap
Holetia-Kowakuma-Trap:
score: "(+ kino frsa strs trap other2)"
hand:
- ホールティアの蟲惑魔
- a:trap
- a:kowakuma
Sono-Ranka-Trap:
score: "(+ sono kino frsa strs other2)"
hand:
- ランカの蟲惑魔
- 蟲惑の園
Sono-Ranka-KTrap:
score: "(+ sono kino frsa strs trap other2)"
hand:
- ランカの蟲惑魔
- 蟲惑の園
Sono-ティオの蟲惑魔-Trap:
score: "(+ sono strs frsa other2)"
hand:
- 蟲惑の園
- ティオの蟲惑魔
- a:kowakuma
- a:trap
A2:
score: "(+ 2 |H.a:trap|)"
Sono-ティオの蟲惑魔-KTrap:
score: "(+ sono strs frsa trap other2)"
hand:
- 蟲惑の園
- a:ティオの蟲惑魔
- a:kowakuma
- a:kowakuma-trap
Sono-トリオンの蟲惑魔:
score: "(+ sono frsa strs trap other2)"
hand:
- 蟲惑の園
- トリオンの蟲惑魔
- a:kowakuma
Sono-キノの蟲惑魔-Trap:
score: "(+ sono frsa strs trap other2)"
hand:
- 蟲惑の園
- キノの蟲惑魔
- a:kowakuma
- bu-mian-jia
- a:trap
Sono-キノの蟲惑魔-KTrap:
score: "(+ sono frsa strs trap trap other2)"
hand:
- 蟲惑の園
- キノの蟲惑魔
- a:kowakuma
- a:kowakuma-trap
test-hand-trap:
start-card: 5
combos:
HT1:
score: '|H.a:hand-trap|' #计算手上手坑的个数
hand:
- HandTrap
HT2:
HT:
score: '|H.a:hand-trap|'
hand:
- HandTrap
- HandTrap
urara-2:
condition: '(== |H.urara| 2)' #也可以编程使用表达式
test-kk:
start-card: 6
combos:
K:
score: '(+ |H.a:k| (* 2 |H.a:kk|))'
hand:
- a:k
KK:
score: '(+ |H.a:k| (* 2 |H.a:kk|))'
hand:
- a:kk

1 change: 1 addition & 0 deletions ygo-calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void checkPath(const string& path)

int main(int argc, char **argv)
{
UTF8CodePage use_utf8;
if (argc > 2)
{
panic("Usage: ./ygo-calc.exe <deck.yml>");
Expand Down

0 comments on commit 009841e

Please sign in to comment.