yo35 / kokopu

A JavaScript/TypeScript library implementing the chess game rules and providing tools to read/write the standard chess file formats.
https://www.npmjs.com/package/kokopu
GNU Lesser General Public License v3.0
27 stars 5 forks source link

Previous node for first node of child variations #25

Closed mbutsykin closed 2 years ago

mbutsykin commented 2 years ago

Maybe it's better to return "parent's" previous node for children variations?

const game = new kokopu.Game();

const e4 = game.mainVariation().play('e4');
const e5 = e4.play('e5')

const e6 = e5.addVariation().play('e6')

e6.previous(); // for now - it's undefined but maybe return e4?
yo35 commented 2 years ago

I considered implementing it like this, but I wanted to have an API that exposes clearly that the beginning of a sub-variation has been reached. Also, in this way, .next() and .previous() are "symmetric", in the sense that aNode.next().previous() === aNode aNode.previous().next() === aNode (assuming that aNode has a parent and a child). With the implementation you propose, it would be possible to have aNode.previous().next() returns something different from aNode.

mbutsykin commented 2 years ago

i see, sounds logical) but after you implemented "id" for nodes - imho there is no need in comparing instances directly.

for example here is my UI

Screenshot 2022-04-11 at 12 57 00

you can navigate "back" and "forward" with arrows at the bottom

for current implementation

for "forward" you can do as simple as that:

const [current, setCurrent] = useState<Node>()

function handleNextClick() {
  setCurrent(current.next());
}

for "back":

const [current, setCurrent] = useState<Node>()

function handlePrevClick() {
  setCurrent(getPrevious(current));
}

function getPrevious(node) {

  let previous = node.previous()

  if (previous) {
    return previous;
  }

  const parent = node.parentVariation().parentNode();

  if (parent) {
    return getPrevious(parent);  
  }

  return undefined;
}

seems a bit overwhelmed =/ but i guess it's still fine, just wanted to have it somehow "consistent" your point is definitely valid and it's up to you of course)

mbutsykin commented 2 years ago

btw, i appreciate your work a lot! i have an app for iOS (for iPhone & iPad) that's using your library a lot) can i somehow gift it for you? i can generate "promo code" (redeem in appstore) and send it to you somewhere

yo35 commented 2 years ago

Thanks for your enthusiasm and your proposal. Unfortunately, I'm on the Android-side of the Force, thus I won't be able to test your app. ;-)