Skip to content

Players and turns ​

Before a move the person needs three facts: (1) which of the two players they are, (2) who plays against them and (3) whose turn it is. The view now shows all three above the board.

Roles ​

The person and every member of a world each have a role. Of the two roles of Crosses (X and O) the fake world gives X to the person and O to the member Otto:

js
members: [{ name: 'Otto', role: 'O' }],
roles: [
  { name: 'X', description: 'Plays crosses, and moves first.' },
  { name: 'O', description: 'Plays noughts.' },
],
myRole: 'X',

Your view reads them as world.myRole, world.members and world.roles. The other player is the member whose role differs from the role of the person:

js
function opponentOf(world) {
  return world.members?.find((member) => member.role !== world.myRole) ?? null;
}

Whose turn ​

The kit has no field for the turn because each world decides where to keep it. Crosses stores the role whose turn it is in data.turn next to the board. When turn equals myRole, it is the turn of the person:

jsx
const isMyTurn = world.data.turn === world.myRole;
// ...
<strong>{isMyTurn ? 'Your turn.' : `${opponent?.name ?? 'The other player'} is thinking…`}</strong>

The fake world keeps turn fixed at 'X' until step 3 makes it change at every move.

The code ​

The new lines are highlighted.

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', ''],
			['', '', ''],
		],
		// Whose turn it is: the role that plays now.
		turn: 'X',
	},
	// Who is in the world: Otto, a fake agent who plays O. The person plays X.
	members: [{ name: 'Otto', role: 'O' }],
	roles: [
		{ name: 'X', description: 'Plays crosses, and moves first.' },
		{ name: 'O', description: 'Plays noughts.' },
	],
	myRole: 'X',
};

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>
			<Players world={world} />
			<Board world={world} />
		</main>
	);
}

// Who plays what and whose turn it is.
function Players({ world }) {
	const opponent = opponentOf(world);
	const isMyTurn = world.data.turn === world.myRole;
	return (
		<>
			<p>
				You play {world.myRole}. {opponent ? `${opponent.name} plays ${opponent.role}.` : 'Waiting for a second player.'}
			</p>
			<p>
				<strong>{isMyTurn ? 'Your turn.' : `${opponent?.name ?? 'The other player'} is thinking…`}</strong>
			</p>
		</>
	);
}

// The other player: the member whose role is not the person's.
function opponentOf(world) {
	return world.members?.find((member) => member.role !== world.myRole) ?? null;
}

// 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', ''],
			['', '', ''],
		],
		// Whose turn it is: the role that plays now.
		turn: 'X',
	},
	// Who is in the world: Otto, a fake agent who plays O. The person plays X.
	members: [{ name: 'Otto', role: 'O' }],
	roles: [
		{ name: 'X', description: 'Plays crosses, and moves first.' },
		{ name: 'O', description: 'Plays noughts.' },
	],
	myRole: 'X',
};

// 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);
players(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 ?? '';
	});
}

// Who plays what and whose turn it is.
function players(container, world) {
	const roles = document.createElement('p');
	const turn = document.createElement('p');
	turn.style.fontWeight = 'bold';
	container.append(roles, turn);

	return world.subscribe((node) => {
		const opponent = opponentOf(node);
		const isMyTurn = node.data.turn === node.myRole;
		roles.textContent = `You play ${node.myRole}. ${opponent ? `${opponent.name} plays ${opponent.role}.` : 'Waiting for a second player.'}`;
		turn.textContent = isMyTurn ? 'Your turn.' : `${opponent?.name ?? 'The other player'} is thinking…`;
	});
}

// The other player: the member whose role is not the person's.
function opponentOf(node) {
	return node.members?.find((member) => member.role !== node.myRole) ?? null;
}

// 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 ​

When it is the turn of the person, a click on a square still does nothing. Step 3: Make a move sends the moves to the world and lets Otto answer them.

Members and roles are covered in full in People and roles. The TurnIndicator component renders a ready-made line that says whose turn it is.

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