2

Help with Function for turn based game

hogbits's Avatar hogbits10/5/21 7:47 pm
10/5/2021 11:32 pm
hogbits's Avatar hogbits
Hello,
I have an idea for a turn based game that I wanted to prototype. But I am very much out of practice with command/functions. How would you suggest I write a function that handles the following:

  • Supports 2 to 8 players
  • Tracks the order so that players take turns in sequence
  • Allows each player to perform an action when it's their turn
  • If a player dies or is removed from the game (e.g. removed via function or disconnects) that player does not prevent other players from playing.

Back in ~v1.10 We used armor stands and scoreboards. But I think that old methodology is mostly defunct now. If I had to guess (conceptually), I would need to create a few scoreboards. One to track the current turn, and another for each player that joins the game. However I am not really sure how to go about tracking and selecting the current player whose turn it is. Also, not sure how to skip those players whom are no longer in play.

Any thoughts, suggestions, or code samples would be very much appreciated.
Posted by hogbits's Avatar
hogbits
Level 34 : Artisan Pig
20

Create an account or sign in to comment.

2

garlicbreathinator
10/05/2021 8:43 pm
Level 31 : Artisan Engineer
garlicbreathinator's Avatar
Depending on what it is, I would probably create a global scoreboard that gives each player a unique id (counting up from 0) which would be used to identify them. The game would use an NBT list (GameState:[]} to store data, either in storage (for a single game instance per world) or in an item in a chest or equipped to an entity (for a game that exists as a physical place, with multiple game boards per world). Each element of this list would be a compound which stores their game data, something like: {PlayerID:0,Lives:3,Score:150}.
As players join the game, a template for this data is added to the end of the list with /data modify ... append ... and the player's id is copied into it.

The main game loop would read this data and save the variables to scoreboards. Then, it would test if the player was online by setting a score somewhere (let's call it playerFound) to 0 and then execute as @a if score @s playerid = <wherever the PlayerID from nbt was copied to> run function ... This function will set the score playerFound to 1 and announce to the player that it is their turn. If the score is still 0 after this, they are not online and the GameState[​0], or their place in the game data, is deleted and the next turn is started.



If they are on, they do their turn and then their data is moved to the back by appending a copy to the list with /data modify and deleting the original.

Now the next player is up and the process repeats.

If a player "dies" or is otherwise out of the game during their turn, you can just delete their data without appending it to the list first. If you want to manually kick a player out of the game, append a sentinel value (a known value that represents the end of the list) to the list and run a function that will loop itself as follows:

If the first value of the list's PlayerID matches the player you want to kick's id (if it doesn't have an id, it automatically will not match), and if it is then delete it. Otherwise, move it to the back of the list by appending it and then deleting it. If the first value of the list now (after shifting whatever was in it) does not equal the sentinel value, then run the function again. Delete the first element of the list (and exit the loop).

That last command will only trigger after every element of the list was scanned and moved to the end, and removes the sentinel value we used. Now the list is the same as it was before, but with the removed player's turn removed.
2
hogbits
10/05/2021 11:32 pm
Level 34 : Artisan Pig
hogbits's Avatar
Thanks for the response! the /data command is new to me. I don't think it existed last time I wrote any commands (mc v1.11 ish) It looks really powerful and complex so I will need to do some "light" reading :D

If I understand the general concept here it is that the data store holds an array of players, as each player finishes their turn they are cycled to the end of the array or removed as needed. If so It sounds pretty straight forward. At least it gives me a good place to start.

Thank you!
1
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome