Skip to content

Commit

Permalink
refactor: use board instead of hands
Browse files Browse the repository at this point in the history
  • Loading branch information
S-N-O-R-L-A-X committed Oct 20, 2024
1 parent 213100e commit 3c01264
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Components/ShowAllBoards/ShowAllBoards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function ShowAllBoards(props: ShowAllBoardsProps) {
(context) =>
(
<div className="all-boards">
{context.all_boards.map((board: CompleteBoard, idx: number) => <ShowCards key={idx} all_hands={board.board} board_number={idx + 1} dds={context.dds} {...props} />)}
{context.all_boards.map((board: CompleteBoard, idx: number) => <ShowCards key={idx} board={board.board} dds={context.dds} {...props} />)}
</div>
)
}
Expand Down
13 changes: 7 additions & 6 deletions src/Components/ShowCards/ShowCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import "./index.css";
import { PROGRAM_POSITIONS } from "../../Utils/maps";
import React, { PropsWithChildren, Suspense } from "react";
import exportPBN from "../../Utils/PBN";
import Board from "../../models/Board";

interface ShowCardsProps extends PropsWithChildren {
all_hands: Hand[];
board_number: number;
board: Board;
dds: boolean;
canClick?: boolean;
beautify?: boolean;
Expand All @@ -17,18 +17,19 @@ interface ShowCardsProps extends PropsWithChildren {
const OfflineBridgeSolver = React.lazy(() => import("../../views/Analysis/OfflineBridgeSolver"))

export default function ShowCards(props: ShowCardsProps) {
const { all_hands, board_number, children, dds, ...rest } = props;
const { board, children, dds, ...rest } = props;
const allHands = board.getAllHands();
return (
<div className="board-container">
<div className="board-number"><div>{board_number}</div>{dds && <button className="export" onClick={() => exportPBN(all_hands, board_number)}>export to PBN</button>}</div>
<div className="board-number"><div>{board.boardnum}</div>{dds && <button className="export" onClick={() => exportPBN(board)}>export to PBN</button>}</div>
<div className="predicted">{children}</div>
{dds &&
<div className="double-dummy">
<Suspense fallback={<div>Loading...</div>}> <OfflineBridgeSolver allHands={all_hands} /> </Suspense>
<Suspense fallback={<div>Loading...</div>}> <OfflineBridgeSolver allHands={allHands} /> </Suspense>
</div>
}

{all_hands.map((all_hand, idx) => (
{allHands.map((all_hand, idx) => (
<div key={idx} className={PROGRAM_POSITIONS[idx] + "hand"}>
<ShowOneHand position={PROGRAM_POSITIONS[idx]} hand={all_hand} {...rest}></ShowOneHand>
</div>
Expand Down
11 changes: 6 additions & 5 deletions src/Utils/PBN.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Board from "../models/Board";
import Hand from "../models/Hand";

export function parseHand(hand: Hand) {
Expand Down Expand Up @@ -28,22 +29,22 @@ export function convertAllHandsToPBN(allHands: Hand[]) {
return str;
}

export default function exportPBN(allHands: Hand[], boardNumber: number) {
const date=new Date();
export default function exportPBN(board: Board) {
const date = new Date();
const pbn = `% PBN 2.1
% EXPORT
%Content-type: text/x-pbn; charset=ISO-8859-1
[Event ""]
[Site ""]
[Date "${date.getFullYear()}.${date.getMonth()+1}.${date.getDate()}"]
[Board "${boardNumber}"]
[Date "${date.getFullYear()}.${date.getMonth() + 1}.${date.getDate()}"]
[Board "${board.boardnum}"]
[West "?"]
[North "?"]
[East "?"]
[South "?"]
[Dealer "N"]
[Vulnerable "None"]
[Deal "${convertAllHandsToPBN(allHands)}"]
[Deal "${convertAllHandsToPBN(board.getAllHands())}"]
[Scoring ""]
[Declarer ""]
[Contract ""]
Expand Down
4 changes: 4 additions & 0 deletions src/models/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,8 @@ export default class Board {
// 保存发牌结果
[this.Nhand, this.Shand, this.Ehand, this.Whand] = hands;
}

public getAllHands(): Hand[] {
return [this.Nhand, this.Shand, this.Ehand, this.Whand];
}
}
1 change: 0 additions & 1 deletion src/models/Hand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,5 @@ export default class Hand {
}

return show;

}
}
2 changes: 1 addition & 1 deletion src/views/Analysis/Analysis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Analysis() {
async function countTricks() {
for (const board of all_boards) {
if (!board.ddtricks) {
board.ddtricks = await analyzeOffline(board.board);
board.ddtricks = await analyzeOffline(board.board.getAllHands());
}
tmp = MatrixAdd(tmp, board.ddtricks as (string | number)[][]);
}
Expand Down
48 changes: 32 additions & 16 deletions src/views/AnalysisOnline(abandoned)/DealWithAnalysis.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import { useState, ChangeEvent, useRef, useCallback, useMemo } from "react";

// import models
import Hand from "../../models/Hand";
import Board from "../../models/Board";
import handFilter, { OneFilterProps } from "../../models/HandFilter";
import { useState, ChangeEvent, useRef, useCallback } from "react";
import HandSetting from "../../Components/HandSetting/HandSetting";

import "./index.css";
import Card from "../../models/Card";

// import utils
import { idx2card } from "../../Utils/utils";
import { Position, PROGRAM_POSITIONS } from "../../Utils/maps";
import { DealContext } from "../Deal/DealContext";
import AllBridgeSolverOnline from "./AllBridgeSolverOnline";

// import other components
import HandSetting from "../../Components/HandSetting/HandSetting";
import { DealContext } from "./DealContext";
import ShowResults from "../Show/ShowResults";

// import css
import "./index.css";

function deal(boardSize: number, hand_filter: Record<string, OneFilterProps>) {
const boards: Hand[][] = [];
while (boardSize--) {
const boards: Board[] = [];
for (let boardNum = 1; boardNum <= boardSize; ++boardNum) {
while (true) {
const board = new Board(boardNum);
const players: Hand[] = [new Hand(), new Hand(), new Hand(), new Hand()];
const B = new Board(Math.floor(Math.random() * 16));
const { N, S, W, E } = hand_filter;

let known_cards: Record<string, Card[]> | undefined = undefined;
if (N?.cards || S?.cards || W?.cards || E?.cards) {
known_cards = {};
Expand All @@ -42,20 +50,21 @@ function deal(boardSize: number, hand_filter: Record<string, OneFilterProps>) {
"W": { hand: players[3], ...hand_filter["W"] },
}

B.deal(players, known_cards);
board.deal(players, known_cards);
if (handFilter(to_pass_filter)) {
boards.push(players);
boards.push(board);
break;
}
}
}
return boards;
}

export default function DealWithAnalysis() {
export default function DealWithHands() {
const [board_size, setBoard_size] = useState<number>(1);
const [boards, setBoards] = useState<Hand[][]>([]);
const [boards, setBoards] = useState<Board[]>([]);
const [beautify, setBeautify] = useState<boolean>(false);
const [DDS, setDDS] = useState<boolean>(false);
const [known_cards, setKnown_cards] = useState<number[]>(new Array(52).fill(-1)); // all cards
const [allFilters, setAllFilters] = useState<Record<string, OneFilterProps>>({});

Expand Down Expand Up @@ -98,6 +107,10 @@ export default function DealWithAnalysis() {
setBeautify((e.target as HTMLInputElement).checked);
}

function handleDDS(e: ChangeEvent) {
setDDS((e.target as HTMLInputElement).checked);
}

return (
<>
<div className="deal-setting">
Expand All @@ -107,8 +120,9 @@ export default function DealWithAnalysis() {

<div>
<input type="checkbox" id="beautify" name="beautify" onChange={handleBeautify} />是否需要美化?
<input type="checkbox" id="dds" name="dds" onChange={handleDDS} />是否需要四明手分析?(会更花费时间)
</div>
<DealContext.Provider value={{ known_cards, changeKnown_cards, contextType: "analysis" }}>
<DealContext.Provider value={{ known_cards, changeKnown_cards, contextType: "multi" }}>
<HandSetting ref={Nref} position="N" getData={getData} />
<HandSetting ref={Sref} position="S" getData={getData} />
<HandSetting ref={Eref} position="E" getData={getData} />
Expand All @@ -119,8 +133,10 @@ export default function DealWithAnalysis() {
<button onClick={handleClick}>Get new boards</button>

</div>
{boards[0] && <AllBridgeSolverOnline all_boards={boards} />}

{useMemo(
() => <ShowResults all_boards={boards.map((b: Board) => { return { board: b } })} beautify={beautify} dds={DDS} />,
[DDS, beautify, boards]
)}
</>
)
}
14 changes: 7 additions & 7 deletions src/views/Deal/DealWithHands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import ShowResults from "../Show/ShowResults";
import "./index.css";

function deal(boardSize: number, hand_filter: Record<string, OneFilterProps>) {
const boards: Hand[][] = [];
while (boardSize--) {
const boards: Board[] = [];
for (let boardNum = 1; boardNum <= boardSize; ++boardNum) {
while (true) {
const board = new Board(boardNum);
const players: Hand[] = [new Hand(), new Hand(), new Hand(), new Hand()];
const B = new Board(Math.floor(Math.random() * 16));
const { N, S, W, E } = hand_filter;

let known_cards: Record<string, Card[]> | undefined = undefined;
Expand All @@ -50,9 +50,9 @@ function deal(boardSize: number, hand_filter: Record<string, OneFilterProps>) {
"W": { hand: players[3], ...hand_filter["W"] },
}

B.deal(players, known_cards);
board.deal(players, known_cards);
if (handFilter(to_pass_filter)) {
boards.push(players);
boards.push(board);
break;
}
}
Expand All @@ -62,7 +62,7 @@ function deal(boardSize: number, hand_filter: Record<string, OneFilterProps>) {

export default function DealWithHands() {
const [board_size, setBoard_size] = useState<number>(1);
const [boards, setBoards] = useState<Hand[][]>([]);
const [boards, setBoards] = useState<Board[]>([]);
const [beautify, setBeautify] = useState<boolean>(false);
const [DDS, setDDS] = useState<boolean>(false);
const [known_cards, setKnown_cards] = useState<number[]>(new Array(52).fill(-1)); // all cards
Expand Down Expand Up @@ -134,7 +134,7 @@ export default function DealWithHands() {

</div>
{useMemo(
() => <ShowResults all_boards={boards.map((x) => { return { board: x } })} beautify={beautify} dds={DDS} />,
() => <ShowResults all_boards={boards.map((b: Board) => { return { board: b } })} beautify={beautify} dds={DDS} />,
[DDS, beautify, boards]
)}
</>
Expand Down
4 changes: 2 additions & 2 deletions src/views/Show/ShowResults.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createContext } from "react";
import ShowAllBoards from "../../Components/ShowAllBoards/ShowAllBoards";
import Hand from "../../models/Hand";
import Analysis from "../Analysis/Analysis";
import Board from "../../models/Board";

export interface CompleteBoard {
board: Hand[];
board: Board;
ddtricks?: (string | number)[][] | string;
}

Expand Down

0 comments on commit 3c01264

Please sign in to comment.