Skip to content

Draw the board ​

The first version of the view of Crosses connects to the world and draws the nine squares of the board with the marks already played on them.

The board is data ​

A world passes the values it shows to your view as named entries of data. Crosses sends its board under the name board as three rows of three squares. Each square holds 'X', 'O' or '' for an empty square.

The fake world of this step starts in the middle of a round:

js
const crosses = {
  name: 'Crosses',
  profile: { description: 'Tic-tac-toe: three in a row wins.' },
  data: {
    board: [
      ['X', '', ''],
      ['', 'O', ''],
      ['', '', ''],
    ],
  },
};

Nine squares ​

The view flattens the rows into nine marks with flat() and draws a square for each one. The squares are numbered 1 to 9 row by row (square 1 is the top left corner, 5 the middle and 9 the bottom right).

jsx
const marks = world.data.board?.flat() ?? ['', '', '', '', '', '', '', '', ''];

Until the world sends board, the fallback after ?? gives nine empty squares that keep the board on the page from the first render.

Each square is a <button> that shows its mark (X in blue and O in red) and carries an aria-label with its number and mark such as "Square 5, O" or "Square 6, empty". A click does nothing yet because the squares become moves only in step 3.

The plain JavaScript version creates the nine buttons once and at every change of the world rewrites only their marks. A button created again at every change could lose a click once the squares become moves.

The code ​

jsx
import { useWorld } from './useWorld.js';

// A fake world to build with, a game of tic-tac-toe that needs no account and no network.
const crosses = {
	name: 'Crosses',
	profile: { description: 'Tic-tac-toe: three in a row wins.' },
	// What the world shows, by name. The board: three rows of three squares, a round already started.
	data: {
		board: [
			['X', '', ''],
			['', 'O', ''],
			['', '', ''],
		],
	},
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: crosses } });

	// The three states of every view: it failed, it is connecting, it is here.
	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	return (
		<main>
			<h1>{world.name}</h1>
			<p>{world.profile.description}</p>
			<Board world={world} />
		</main>
	);
}

// The board from the data of the world: nine squares, numbered 1 to 9 row by row.
function Board({ world }) {
	// Until the world sends its board, the squares are empty.
	const marks = world.data.board?.flat() ?? ['', '', '', '', '', '', '', '', ''];
	return (
		<div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 72px)', gap: 6 }}>
			{marks.map((mark, index) => (
				<button
					key={index}
					aria-label={`Square ${index + 1}, ${mark || 'empty'}`}
					style={{ height: 72, fontSize: 32, fontWeight: 700, color: mark === 'X' ? '#3b82f6' : '#ef4444' }}
				>
					{mark}
				</button>
			))}
		</div>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake world to build with, a game of tic-tac-toe that needs no account and no network.
const crosses = {
	name: 'Crosses',
	profile: { description: 'Tic-tac-toe: three in a row wins.' },
	// What the world shows, by name. The board: three rows of three squares, a round already started.
	data: {
		board: [
			['X', '', ''],
			['', 'O', ''],
			['', '', ''],
		],
	},
};

// The page: one element that holds the whole view.
const page = document.createElement('main');
page.textContent = 'Connecting…';
document.body.append(page);

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: crosses } });
page.textContent = '';
header(page, world);
board(page, world);

// The name of the world and what it is.
function header(container, world) {
	const title = document.createElement('h1');
	const about = document.createElement('p');
	container.append(title, about);

	// Called at once, then after every change of the world.
	return world.subscribe((node) => {
		title.textContent = node.name;
		about.textContent = node.profile.description ?? '';
	});
}

// The board from the data of the world: nine squares, numbered 1 to 9 row by row.
function board(container, world) {
	const grid = document.createElement('div');
	grid.style.cssText = 'display: grid; grid-template-columns: repeat(3, 72px); gap: 6px';
	// The nine squares are made once, and every change of the world writes their marks.
	const squares = [];
	for (let index = 0; index < 9; index += 1) {
		const square = document.createElement('button');
		square.style.cssText = 'height: 72px; font-size: 32px; font-weight: 700';
		squares.push(square);
	}
	grid.append(...squares);
	container.append(grid);

	return world.subscribe((node) => {
		// Until the world sends its board, the squares are empty.
		const marks = node.data.board?.flat() ?? [];
		squares.forEach((square, index) => {
			const mark = marks[index] ?? '';
			square.textContent = mark;
			square.setAttribute('aria-label', `Square ${index + 1}, ${mark || 'empty'}`);
			square.style.color = mark === 'X' ? '#3b82f6' : '#ef4444';
		});
	});
}
Live, on a fake world
Open

Next steps ​

Step 2: Players and turns adds who plays what and whose turn it is.

Every kind of value that a world can show is in The data of the world. A board like this one is also ready as the Grid component.

The UNaIVERSE GUI kit, released under the Apache License 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.