Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/iframe/life-game.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ const cellSize = 600 / boardSize; //セルの大きさ(px)
function isNextAlive(around, self) {
// 自身が生きている & 周囲が 2 か 3 で生存
if (self && 2 <= around && around <= 3) {
return true;
return self;
}
// 自身が死んでいる & 周囲が 3 で誕生
if (!self && around === 3) {
return true;
return 1;
}
return false;
return 0;
}

// cellの状態に応じた色を返す関数
function getStyle(cell) {
// cellがtrueなら黒、falseなら白を返す
return cell ? "black" : "white";
if (cell === 0) return "white";
// cellの値に応じて色を返す場合はここに追加
return "black"; // デフォルトは黒
}

//Boardの初期化
let board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => false));
let board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => 0));
const table = document.getElementById("game-board");

//盤面をBoardに従って変更する関数達(Boardを変更したら実行する)
Expand Down Expand Up @@ -209,7 +210,7 @@ function progressBoard() {
for (let ii = 0; ii < tate.length; ii++) {
for (let jj = 0; jj < yoko.length; jj++) {
if (tate[ii] !== 0 || yoko[jj] !== 0) {
around += board[i + tate[ii]][j + yoko[jj]] ? 1 : 0;
around += board[i + tate[ii]][j + yoko[jj]] !== 0 ? 1 : 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+= board[ii][jj] でもいいかも

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

現行の0or1しかとりえない状況だと動くと思いますが、numberを活用しだすとエラー吐きそうなので触れずに行こうと思ってます。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

濃度つきセルのコードを書くときに変えればいいので、よさそう

}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/api/board.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export async function saveBoard(data: { board: boolean[][]; name: string }, isJapanese: boolean) {
export async function saveBoard(data: { board: number[][]; name: string }, isJapanese: boolean) {
try {
const response = await fetch("/api/board", {
method: "POST",
Expand Down Expand Up @@ -31,7 +31,7 @@ export type BoardListItem = {
id: number;
name: string;
createdAt: string;
preview: boolean[][];
preview: number[][];
};

export async function fetchBoardList(isJapanese: boolean): Promise<BoardListItem[] | undefined> {
Expand Down Expand Up @@ -62,7 +62,7 @@ export async function fetchBoardList(isJapanese: boolean): Promise<BoardListItem
export async function loadBoardById(
id: number,
isJapanese: boolean,
): Promise<boolean[][] | undefined> {
): Promise<number[][] | undefined> {
try {
const response = await fetch(`/api/board?id=${id}`);

Expand All @@ -76,7 +76,7 @@ export async function loadBoardById(

const loadedBoard = await response.json();

return loadedBoard as boolean[][];
return loadedBoard as number[][];
} catch (err) {
console.error("Load error", err);
if (isJapanese) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/board-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const PREVIEW_SIZE = 20;
* 任意のサイズの盤面データから、中央 20x20 のプレビューを生成します。
* 20x20 に満たない場合は、中央に配置し、周囲を false (空白) で埋めます。
*/
export function createBoardPreview(boardData: boolean[][]): boolean[][] {
export function createBoardPreview(boardData: number[][]): number[][] {
const boardHeight = boardData.length;
const boardWidth = boardData[0]?.length || 0;

const finalPreview: boolean[][] = Array.from({ length: PREVIEW_SIZE }, () =>
Array(PREVIEW_SIZE).fill(false),
const finalPreview: number[][] = Array.from({ length: PREVIEW_SIZE }, () =>
Array(PREVIEW_SIZE).fill(0),
);

const sourceStartRow = Math.max(0, Math.floor((boardHeight - PREVIEW_SIZE) / 2));
Expand Down
6 changes: 3 additions & 3 deletions src/lib/models/BoardManager.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { saveBoard, fetchBoardList, loadBoardById, type BoardListItem } from "$l

type SaveState =
| { saving: false }
| { saving: true; data: boolean[][]; name: string; preview: boolean[][] };
| { saving: true; data: number[][]; name: string; preview: number[][] };

type LoadState =
| { state: "closed" }
Expand All @@ -16,7 +16,7 @@ export class BoardManager {

constructor() {}

openSaveModal(board: boolean[][]) {
openSaveModal(board: number[][]) {
const preview = createBoardPreview(board);
this.saveState = { saving: true, data: board, name: "", preview: preview };
}
Expand Down Expand Up @@ -46,7 +46,7 @@ export class BoardManager {
this.loadState = { state: "closed" };
}

async load(id: number, isJapanese: boolean): Promise<boolean[][] | undefined> {
async load(id: number, isJapanese: boolean): Promise<number[][] | undefined> {
this.closeLoadModal();
return await loadBoardById(id, isJapanese);
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
break;
}
case "save_board": {
boardManager.openSaveModal(event.data.data as boolean[][]);
boardManager.openSaveModal(event.data.data as number[][]);
break;
}
default: {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/board/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createBoardPreview } from "@/lib/board-preview.js";
import * as v from "valibot";

const BoardSchema = v.object({
board: v.array(v.array(v.boolean())),
board: v.array(v.array(v.number())),
name: v.pipe(v.string(), v.minLength(1, "盤面名は必須です。")),
});

Expand Down