Skip to content

Commit

Permalink
feat: adapt placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
sor4chi committed Jul 1, 2024
1 parent 03d413d commit 1be75a1
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 25 deletions.
53 changes: 41 additions & 12 deletions visualizer-3d-neo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ function Cell(props: ICell) {

return (
<div
className="cell-container"
className={[
"cell-container",
!isNaN(parseInt(props.char)) ? "cell-number" : "",
].join(" ")}
onClick={() => setEditing(true)}
onBlur={() => setEditing(false)}
>
Expand Down Expand Up @@ -65,6 +68,8 @@ function App() {
const [game, setGame] = useState<Game | null>(null);
const [totalTurn, setTotalTurn] = useState(1);
const [s, setS] = useState(1000);
const [a, setA] = useState(0);
const [b, setB] = useState(0);
const [submitted, setSubmitted] = useState<number | null>(null);
const [playing, setPlaying] = useState(false);

Expand Down Expand Up @@ -94,10 +99,10 @@ function App() {
const newMatrix = game?.matrix.map((row) =>
row.concat(Array.from({ length: w - (game?.w || 0) }, () => "."))
);
setGame(new Game(game?.h || 0, w, newMatrix));
setGame(new Game(game?.h || 0, w, a, b, newMatrix));
} else {
const newMatrix = game?.matrix.map((row) => row.slice(0, w));
setGame(new Game(game?.h || 0, w, newMatrix));
setGame(new Game(game?.h || 0, w, a, b, newMatrix));
}
};

Expand All @@ -108,23 +113,33 @@ function App() {
Array.from({ length: game?.w || 0 }, () => ".")
)
);
setGame(new Game(h, game?.w || 0, newMatrix));
setGame(new Game(h, game?.w || 0, a, b, newMatrix));
} else {
const newMatrix = game?.matrix.slice(0, h);
setGame(new Game(h, game?.w || 0, newMatrix));
setGame(new Game(h, game?.w || 0, a, b, newMatrix));
}
};

const updateChar = (i: number, j: number, char: string) => {
const newMatrix = game?.matrix.map((row, r) =>
row.map((cell, c) => (r === i && c === j ? char : cell))
);
setGame(new Game(game?.h || 0, game?.w || 0, newMatrix));
setGame(new Game(game?.h || 0, game?.w || 0, a, b, newMatrix));
};

const updateTextarea = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
const newMatrix = e.target.value.split("\n").map((row) => row.split(" "));
setGame(new Game(newMatrix.length, newMatrix[0].length, newMatrix));
setGame(new Game(newMatrix.length, newMatrix[0].length, a, b, newMatrix));
};

const updatePlaceholder = (value: number, placeholder: "a" | "b") => {
if (placeholder === "a") {
setA(value);
setGame(new Game(game?.h || 0, game?.w || 0, value, b, game?.matrix));
} else {
setB(value);
setGame(new Game(game?.h || 0, game?.w || 0, a, value, game?.matrix));
}
};

const shift = (dir: "up" | "down" | "left" | "right") => {
Expand All @@ -145,7 +160,7 @@ function App() {
return ".";
})
);
setGame(new Game(game?.h || 0, game?.w || 0, newMatrix));
setGame(new Game(game?.h || 0, game?.w || 0, a, b, newMatrix));
};

const computeMatrix = () => {
Expand All @@ -164,16 +179,16 @@ function App() {

useEffect(() => {
if (game) {
localStorage.setItem("game", game.to_string());
localStorage.setItem("game", game.to_json());
}
}, [game]);

useEffect(() => {
const savedGame = localStorage.getItem("game");
if (savedGame) {
setGame(Game.from_string(savedGame));
setGame(Game.from_json(savedGame));
} else {
setGame(new Game(10, 10));
setGame(new Game(10, 10, 0, 0));
}
}, []);

Expand All @@ -199,6 +214,20 @@ function App() {
onChange={(e) => updateWidth(parseInt(e.target.value))}
id="w"
/>
<label htmlFor="a">A: </label>
<input
type="number"
value={a}
onChange={(e) => updatePlaceholder(parseInt(e.target.value), "a")}
id="a"
/>
<label htmlFor="b">B: </label>
<input
type="number"
value={b}
onChange={(e) => updatePlaceholder(parseInt(e.target.value), "b")}
id="b"
/>
</p>
<p className="form">
<label htmlFor="t">Time: </label>
Expand Down Expand Up @@ -230,7 +259,7 @@ function App() {
<p>Matrix:</p>
<textarea
className="matrix"
value={game?.to_string()}
value={game?.matrix.map((row) => row.join(" ")).join("\n")}
onChange={updateTextarea}
/>
<p>Shifter:</p>
Expand Down
58 changes: 45 additions & 13 deletions visualizer-3d-neo/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const AVAILABLE_CHARS = ".0123456789<>^v+-*/%@=#SAB\n".split("");
const ERRORS_KEYS = {
CHAR_ERROR: (i: number, j: number) => `CHAR_ERROR(${i}-${j})`,
CONFLICT_ERROR: (i: number, j: number) => `CONFLICT_ERROR(${i}-${j})`,
NUMBER_CELL_ERROR: (i: number, j: number) => `NUMBER_CELL_ERROR(${i}-${j})`,
WARP_ERROR: "WARP_ERROR",
};

Expand All @@ -12,6 +13,8 @@ const ERROR_MESSAGES = {
CHAR_ERROR: (i: number, j: number) => `Invalid character in (${i}-${j})`,
CONFLICT_ERROR: (i: number, j: number, inserted: string, existing: string) =>
`Conflict in (${i}-${j}): inserted ${inserted}, existing ${existing}`,
NUMBER_CELL_ERROR: (i: number, j: number) =>
`Number cell in (${i}-${j})'s value must be from -99 to 99`,
WARP_ERROR: "Multiple dt warps are not allowed",
};

Expand Down Expand Up @@ -88,12 +91,16 @@ const isNumber = (s: string): boolean => {
export class Game {
private _h: number;
private _w: number;
private _a: number;
private _b: number;
private _matrix: string[][];
private _errors: Map<string, string> = new Map();

constructor(h: number, w: number, matrix?: string[][]) {
constructor(h: number, w: number, a: number, b: number, matrix?: string[][]) {
this._h = h;
this._w = w;
this._a = a;
this._b = b;
this._matrix =
matrix ??
Array.from({ length: h }, () => Array.from({ length: w }, () => "."));
Expand Down Expand Up @@ -130,6 +137,21 @@ export class Game {
}
});
});

// 初期盤面には-99から99までの範囲外の数字は含められない
this.matrix.forEach((row, i) => {
row.forEach((cell, j) => {
if (isNumber(cell)) {
const n = parseInt(cell, 10);
if (n < -99 || n > 99) {
this._errors.set(
ERRORS_KEYS.NUMBER_CELL_ERROR(i, j),
ERROR_MESSAGES.NUMBER_CELL_ERROR(i, j)
);
}
}
});
});
}

private copyMatrix(): string[][] {
Expand All @@ -156,7 +178,17 @@ export class Game {
}
}
let current_matrix = this.copyMatrix();
let history: string[][][] = [this.copyMatrix()];
// 最初にA,Bを適用
current_matrix.forEach((row, i) => {
row.forEach((cell, j) => {
if (cell === "A") {
current_matrix[i][j] = this._a.toString();
} else if (cell === "B") {
current_matrix[i][j] = this._b.toString();
}
});
});
let history: string[][][] = [current_matrix.map((row) => row.slice())];

let curT = 1;
while (curT < t) {
Expand Down Expand Up @@ -410,18 +442,18 @@ export class Game {
return current_matrix;
}

to_string(): string {
return this.matrix.map((row) => row.join("")).join("\n");
to_json() {
return JSON.stringify({
h: this.h,
w: this.w,
a: this._a,
b: this._b,
matrix: this.matrix,
});
}

static from_string(s: string): Game {
const lines = s.split("\n");
const h = lines.length;
const w = lines[0].length;
const matrix = Array.from({ length: h }, (_, i) =>
Array.from({ length: w }, (_, j) => lines[i][j])
);

return new Game(h, w, matrix);
static from_json(json: string) {
const obj = JSON.parse(json);
return new Game(obj.h, obj.w, obj.a, obj.b, obj.matrix);
}
}

0 comments on commit 1be75a1

Please sign in to comment.