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
2 changes: 1 addition & 1 deletion src/experiments/pathfinding/pathfinding.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="description" content="先跑一遍,再改一格。在同一张地图上对比广度优先搜索和 A*,保留前后结果,亲手试出寻路的不同。">
<meta name="color-scheme" content="light dark"><link rel="icon" href="data:,">
<link rel="stylesheet" href="./style.css"><link rel="stylesheet" href="./pathfinding.css">
<script type="module" src="./pathfinding.mjs?v=cancel-results-v1"></script>
<script type="module" src="./pathfinding.mjs?v=cell-render-v1"></script>
<title>算法游乐场 · 寻路竞技场</title>
<link rel="stylesheet" href="./navigation.css?v=three-cat-brand-v1"><script type="module" src="./navigation.mjs?v=cats-in-scenes-v1"></script>
<link rel="stylesheet" href="./pathfinding-cats.css?v=snack-predict-v1">
Expand Down
12 changes: 8 additions & 4 deletions src/experiments/pathfinding/pathfinding.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,16 @@ function render(){
const visited=new Set(frame.visited),frontier=new Set(frame.frontier),path=new Set(frame.path);
cells[name].forEach((cell,index)=>{
const state=index===map.start?'start':index===map.goal?'goal':walls.has(index)?'wall':path.has(index)?'path':visited.has(index)?'visited':frontier.has(index)?'frontier':'';
cell.className=`cell ${state}${index===frame.current&&!frame.done?' current':''}`;
cell.textContent=index===map.start?'S':index===map.goal?'G':'';
const className=`cell ${state}${index===frame.current&&!frame.done?' current':''}`;
if(cell.className!==className)cell.className=className;
const text=index===map.start?'S':index===map.goal?'G':'';
if(cell.textContent!==text)cell.textContent=text;
const label={start:'起点',goal:'终点',wall:'墙',path:'最终路径',visited:'已探索',frontier:'待探索','':'空地'}[state];
cell.setAttribute('aria-label',`第 ${Math.floor(index/map.cols)+1} 行,第 ${index%map.cols+1} 列,${label}`);
const ariaLabel=`第 ${Math.floor(index/map.cols)+1} 行,第 ${index%map.cols+1} 列,${label}`;
if(cell.getAttribute('aria-label')!==ariaLabel)cell.setAttribute('aria-label',ariaLabel);
const candidate=guessing&&name===guessAlgorithm?guessRound?.decision.candidates.find(c=>c.index===index):null;
cell.setAttribute('aria-pressed',String(!!candidate&&guessRound.choice===index));
const pressed=String(!!candidate&&guessRound.choice===index);
if(cell.getAttribute('aria-pressed')!==pressed)cell.setAttribute('aria-pressed',pressed);
if(candidate){
cell.classList.toggle('guess-candidate',true);
cell.classList.toggle('guess-picked',guessRound.choice===index);
Expand Down
53 changes: 52 additions & 1 deletion tests/experiments/pathfinding/pathfinding-ui.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ async function boot(){
}
append(...children){for(const child of children){if(child.parent)child.parent.children=child.parent.children.filter(c=>c!==child);this.children.push(child);child.parent=this;}}
replaceChildren(...children){this.children=[];this.append(...children);}
setAttribute(k,v){this.attributes.set(k,v);}getAttribute(k){return this.attributes.get(k);}
set className(value){this.writes=(this.writes??0)+1;this._className=value;}get className(){return this._className;}
set textContent(value){this.writes=(this.writes??0)+1;this._textContent=value;}get textContent(){return this._textContent;}
setAttribute(k,v){this.writes=(this.writes??0)+1;this.attributes.set(k,v);}getAttribute(k){return this.attributes.get(k);}
closest(selector){return selector==='.cell'&&this.className.split(' ').includes('cell')?this:null;}
contains(child){return this.children.includes(child);}
getBoundingClientRect(){return {left:0,top:0,right:300,bottom:220,width:300,height:220};}
Expand Down Expand Up @@ -44,6 +46,55 @@ async function boot(){
selected(){return presets.find(b=>b.getAttribute('aria-pressed')==='true')?.dataset.map;}};
}

function gridSnapshot(u,names=['bfs','astar']){
return names.flatMap(name=>u.els.get(name+'-grid').children.map(cell=>[
cell.className,cell.textContent,cell.getAttribute('aria-label'),cell.getAttribute('aria-pressed')
]));
}
function gridWrites(u,names=['bfs','astar']){
return names.flatMap(name=>u.els.get(name+'-grid').children).reduce((sum,cell)=>sum+cell.writes,0);
}

test('playback writes only grid properties that actually change on each search step',async()=>{
const u=await boot();try{
u.click('play');
for(let step=1;step<=5;step++){
const before=gridSnapshot(u),writes=gridWrites(u);
const [id,tick]=u.timers.entries().next().value;u.timers.delete(id);tick();
const after=gridSnapshot(u),changes=after.reduce((sum,cell,i)=>sum+cell.filter((value,j)=>value!==before[i][j]).length,0);
assert.equal(u.els.get('bfs-explored').textContent,step);
assert.equal(u.els.get('astar-explored').textContent,step);
assert.ok(changes>0,'the visible search must advance');
assert.equal(gridWrites(u)-writes,changes,'unchanged cells must not receive redundant writes');
}
}finally{u.restore();}
});

test('a completed A-star board receives no writes while BFS keeps exploring',async()=>{
const u=await boot();try{
for(let i=0;i<60;i++)u.click('step');
assert.equal(u.els.get('astar-distance').textContent,20);
const before=gridSnapshot(u,['astar']),writes=gridWrites(u,['astar']);
u.click('step');
assert.equal(u.els.get('bfs-explored').textContent,61);
assert.deepEqual(gridSnapshot(u,['astar']),before);
assert.equal(gridWrites(u,['astar']),writes);
}finally{u.restore();}
});

test('leaving a revealed guess restores every normal grid property at the same step',async()=>{
let expected;const normal=await boot();try{
normal.click('step');normal.click('step');expected=gridSnapshot(normal);
}finally{normal.restore();}
const u=await boot();try{
u.click('guess-toggle');
const decision=decisionAt(search(makeMap(),'astar'),1);
u.edit(decision.candidates.find(c=>c.index!==decision.current).index,'astar');
u.click('guess-reveal');u.click('guess-toggle');
assert.deepEqual(gridSnapshot(u),expected,'candidate numbers, classes, labels and selection must all be cleared');
}finally{u.restore();}
});

test('first-run guidance waits for a complete real baseline before inviting a map edit',async()=>{
const u=await boot();try{
assert.match(u.els.get('play-status').textContent,/先.*跑.*对照/);
Expand Down
Loading