gpt4 book ai didi

dart - 这段使用Streams的代码是错误的还是我应该提交错误?

转载 作者:行者123 更新时间:2023-12-03 03:09:07 28 4
gpt4 key购买 nike

我编写的代码可能会很长一个问题,但是,我想看看是否存在错误,因此可以将其提交给dartbug.com。如果没有错误,我想知道我做错了什么。

该代码的想法是使点的位置与网格同步(分别为Square类和Grid类)。

Square.onPosChange是发送前一个位置的Stream。

将正方形添加到Grid(Grid.add)后,将订阅onPosChange,它的作用是更改正方形在Grid中的位置;首先,它将其从上一个位置移除(仅分配null),然后将其分配到当前位置。

当我期望先前的位置为空时,我的测试失败。现在下面是代码,请注意我认为可能是问题所在的注释行。

编辑:以防万一,这不是愚人节问题:P

import 'dart:async';
import 'package:unittest/unittest.dart';

void main() {
test('grid',(){
Square s = new Square(3, 5);
Grid g = new Grid(10,10);
g.add(s);
expect(g._cols[3].squares[5], s);//pass
s.x = 6;
expect(g._cols[6].squares[5], s);//pass
expect(g._cols[3].squares[5], isNull);//fails

});
}

class Grid{
List<GridCol> _cols;
int w, h;
Grid(this.w, this.h){
_cols = new List<GridCol>.filled(w, new GridCol(h));
}
add(Square square){
if(!isOut(square)){
//add square to grid
_cols[square.x].squares[square.y] = square;
//listen to onPosChanged event stream
square.onPosChanged.listen((Point previousPos){
//remove from previous position
_cols[previousPos.x].squares[previousPos.y] = null;
//if is not out of bounds, add in new position
if(!isOut(square)){
/*
* Up until this point, (3,5) is null, as was set earlier.
* (6,5) is also null since it was never set.
*
* But after the following line, strangely,
* both (3,5) and (6,5) are set to the square.
*/
_cols[square.x].squares[square.y] = square;

print("(3,5): ${_cols[3].squares[5]}");//(3,5): (6,5)
print("(6,5): ${_cols[6].squares[5]}");//(6,5): (6,5)
}

});
}
}

isOut(Point p) =>
p.x < 0 || p.y < 0 || p.x >= w || p.y >= h;
}

class GridCol{
List<Square> squares;
GridCol(int h): squares = new List<Square>(h);
}

class Square extends Point{
int get x => super.x;
set x (value){
var prev = super.x;
super.x = value;
_sc.add(p(prev, y));
}
int get y => super.y;
set y (value){
var prev = super.y;
super.y = value;
_sc.add(p(x,prev));
}

StreamController<Point> _sc;
Stream get onPosChanged => _sc.stream;
Square(x, y)
{
super.x = x;
super.y = y;
_sc = new StreamController<Point>.broadcast();
}
}

class Point{
int x = 0;
int y = 0;
Point([this.x, this.y]);
String toString() => '($x,$y)';
}

p(x, y) => new Point(x, y);

最佳答案

这是很难找到的,但是这是您的解决办法,与Streams无关。

Grid的构造函数如下所示:

Grid(this.w, this.h){ 
_cols = new List<GridCol>.filled(w, new GridCol(h));
}

您希望 _cols中的每个元素都用 new GridCol(h);填充。实际上,您得到的内容(以及代码的实际含义)是用 GridCol的相同实例填充每个元素。稍微改写为:

Grid(this.w, this.h){
var gridCol = new GridCol(h); // create a single instance of a GridCol
_cols = new List<GridCol>.filled(w, gridCol); // add that instance to every element
}

这意味着,当您添加第一个正方形时,每个 GridCol都是相同的单个实例,最终得到一个看起来像这样的网格(其中 -为null,而 S是您的正方形):
---S------
---S------
---S------
---S------
---S------
---S------
---S------
---S------
---S------
---S------

这种情况就发生在 add函数的开头,在涉及任何流之前,紧跟在注释之后: //add square to grid

如果使用以下代码初始化网格中的每个元素,则测试将通过。

Grid(this.w, this.h){
_cols = new List<GridCol>(w); // create the list
// fill each element with a different GridCol
for (int i = 0; i < _cols.length; i++) {
_cols[i] = new GridCol(h); // insert a new GridCol for every element.
}
}

关于dart - 这段使用Streams的代码是错误的还是我应该提交错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15739298/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com