37 lines
807 B
TypeScript
37 lines
807 B
TypeScript
export type State = {
|
|
board: ('X' | 'O' | null)[][],
|
|
};
|
|
|
|
export const start = {
|
|
board: [
|
|
[null, null, null],
|
|
[null, null, null],
|
|
[null, null, null],
|
|
],
|
|
};
|
|
|
|
export type Action = {
|
|
type: 'reset',
|
|
} | {
|
|
type: 'play',
|
|
symbol: 'X' | 'O',
|
|
x: number,
|
|
y: number,
|
|
};
|
|
|
|
export function reducer(state: State, action: Action): State {
|
|
switch (action.type) {
|
|
case 'reset':
|
|
return start;
|
|
case 'play':
|
|
if (!state.board[action.x][action.y]) {
|
|
const row = [...state.board[action.x]];
|
|
row[action.y] = action.symbol;
|
|
const board = [...state.board];
|
|
board[action.x] = row;
|
|
return { ...state, board }
|
|
}
|
|
return state;
|
|
}
|
|
}
|