#Turn Based Implementation

1 messages ยท Page 1 of 1 (latest)

lethal burrow
#

Turn Based Implementation

junior bough
#

so what is placement for

lethal burrow
#

it is for placing the gameobject on nodes

#

these two are for movement after being placed on the nodes

junior bough
#

alright

dusk summit
#

you have any idea about it @junior bough how to implement ?

junior bough
lethal burrow
junior bough
#

so check if the piece you trying to pick up is only the one that matches the TurnManager

tawdry sigil
#

@lethal burrow Did I not tell you NOT to just copy/paste mine. This way you learn nothing
btw. All done

lethal burrow
tawdry sigil
#

what? The video you posted is my work. Not yours

lethal burrow
tawdry sigil
#

but you need to implement what I did into your own game so you can understand what/how/why it is working. This way you learn absolutely nothing

lethal burrow
tawdry sigil
#

As @junior bough said. set up an enum you can switch between Tigers and Goats and locate everywhere in the code where it needs to be checked and check it

#

this is why you need to take the time to understand the code first so you know where to add the checks

lethal burrow
#

I used a bool instead. And checking if the placed object is goat or tiger using tags. but unfortunately it is working only the first time.

tawdry sigil
#

So that is the mouse up. what do you check in mouse down?

lethal burrow
#

why would i check in mousedown. It is just instantiation.

tawdry sigil
#

dont you think it's a good idea to check if it is someone's turn at the moment of selection? That is in mouse down

dusk summit
#

you are saying that first we should actually check whether the selection is tiger or goat and then the script should compare it with whatever tag we have written and then it should execute what we have written ?

tawdry sigil
#

Think about it.
If you are playing a turn based board game, at which point in a players move do you decide 2 things. 1 Is it his move and 2. what pieces can he move?

dusk summit
#

we will check it by if he is clicking and compare it by other ones

tawdry sigil
#

I asked at which point

dusk summit
#

after player 1 will play

tawdry sigil
#

let me make it simple for you.
At which point in a player move do you decide if it is his move
The start of a move
During the move
The End of the move
?

dusk summit
#

Start of the move

tawdry sigil
#

exactly. And in this case what signifys the start of a move?

dusk summit
#

whether we are selecting it or not

tawdry sigil
#

no

#

what action is taken by the player at the start of every move?

lethal burrow
tawdry sigil
#

no, physical action

lethal burrow
#

mouseclick?

tawdry sigil
#

no

#

mouse down

dusk summit
#

yeah

tawdry sigil
#

so, where ever you have a mouse down event you need to check, is it this players turn

lethal burrow
#

But how can we check it. We are using the same script for both goat and tiger.

tawdry sigil
#

yes, but in the placement script, for example, you have the prefab variable and the tag/name of that prefab tells you what is trying to be selected

tawdry sigil
#

the same thing can be applied to the Movement script by using the node.occupant

lethal burrow
#

@tawdry sigil we are enabling and disabling placement script based upon the turn. is this wrong?

tawdry sigil
#

not necessary. A simple if in MouseDown is sufficient.
this is mine

    private void OnMouseDown()
    {
        if (max > 0)
        {
            if (GameManager.instance.turn.ToString() == prefab.name)
            {
                _isSelected = true;
                toPlace = Instantiate(prefab);
                toPlace.name = prefab.name;
                toPlace.transform.position = transform.position;
            }
        }
    }
lethal burrow
#

we made three tigerHomes as the position of them should be fixed when the game starts.

tawdry sigil
#

that should not be tigerhome just tiger

#

you only need the Home prefabs for manual placement. That is what they are for

dusk summit
tawdry sigil
#

yes

#

mine

public enum Turn
{
    Tiger, Goat
}
dusk summit
#

so i will use them in onmousedown and create a condition

tawdry sigil
#

yes

dusk summit
#

okok

#

but why are they not accessible ?

tawdry sigil
#

show code

#

you have the enum defined inside TurnManager

dusk summit
#

yeah

#

inside

tawdry sigil
#

so you access it as
TurnManager.PlayerType

dusk summit
#

yes

#

exactly

#

i am not able to do that

#

-_-

#

ooo ok got it

tawdry sigil
#

this
private PlayerType currentPlayer = PlayerType.Tiger;
needs to be public so you can do
TurnManager.instance,currentPlayer
from another script

dusk summit
#

if (_node.occupant != null && TurnManager.instance.currentPlayer.ToString()==_node.occupant.name)
is this the way in movement to write ?

tawdry sigil
#

yes

dusk summit
#

my enums are working and switching to but how to freeze the moves of respective turns objects

tawdry sigil
#

I dont understand, explain

dusk summit
#

so when we move them the enum is switching between goat and tiger respectively but the movement of them is not freezing meaning that i can move tiger by any amount of turn

tawdry sigil
#

I still do not understand what you mean

dusk summit
#

like tiger will play once then goat right ?

tawdry sigil
#

yes

dusk summit
#

so after playing for tiger the current situation is we can move the tiger once again

tawdry sigil
#

then you are not swityching the enum correctly

dusk summit
#

should i share the code

tawdry sigil
#

sure

dusk summit
#

placement one

#

movement one

tawdry sigil
#

In placement you have the switchturn in the wrong place. it should only happen if the move was successful

lethal burrow
tawdry sigil
#

and you have no switchturn in your movement code

dusk summit
tawdry sigil
lethal burrow
tawdry sigil
#

no. it is when node != null.
you are doing switchturn always

dusk summit
#

in movement i can write the switch tunrs anywhere

#

because on mouse up and down we cant write

#

check move is checking the movement and then we are calling it in update

tawdry sigil
dusk summit
#

so if i am writing in update it is working but sometimes it is returning the enum double time

dusk summit
#

it is behaving like a bug

tawdry sigil
#

show code

dusk summit
#

its working

#

sorry my mistake

tawdry sigil
#

so now you know how to implement turn based movement

  1. At the start of a move determine if the player can move the selected piece
  2. At the end of a successful move, switch turns.
    It's that simple
dusk summit
#

i have one more doubt that the goat should move after the all the 15 goat placed on a node

tawdry sigil
#

indeed

#

so you need an extra check in the mousedown of movement

dusk summit
#

so should i add the count for it

tawdry sigil
#

a bool in TurnManager will do
initially set to false
set to true by placement script when all goats are placed
checked by movement script when it is goats turn

lethal burrow
#

i didnt understand the last line

tawdry sigil
#

ok, you you have in movement

    private void OnMouseDown()
    {
        if (_node.occupant != null && TurnManager.instance.currentPlayer.ToString()==_node.occupant.name)
        {
            //this.GetComponent<SpriteRenderer>().color = Color.yellow;
            _isSelected = true;
            
        }
        
    }

which checks the selection against the turn
here you need to add an additional check that if it is Goats turn has he placed all of his goats yet?

dusk summit
#

trying

tawdry sigil
#

should than not be if NOT all Goats placed?

dusk summit
#

oo it should be true

#

but the condition is correct ?

tawdry sigil
#

it's round the wrong way

dusk summit
#

i didnt get it

tawdry sigil
#

you are setting to false if all goats have been placed

dusk summit
tawdry sigil
#

no

#

read your code

#

the false is correct. The condition is wrong

dusk summit
#

when all goat will be placed then the movement should be there
thats what i am writing right ?
like the bool will get true when max ==0 in placement and the toplace.name=="Goat" and in movement if the bool will be true and the player will be the goat then it should be false

#

is this wrong

tawdry sigil
#

no it is not what you are writing

dusk summit
#

i wrote this in placement added an else if

tawdry sigil
#

just look at the code for both if statements and tell me the value of _isSelected
if it is goats turn and all goats have not been placed

tawdry sigil
dusk summit
#

then how should i compare in the placement

tawdry sigil
#

There is already a very clear place in placement when you know if all goats have been placed

dusk summit
#

in the on mouseup where toplace is null ?

tawdry sigil
#

no.
Mouse up is correct but what do you think this
if (max == 0)
is telling you?

dusk summit
#

oooo i am sorry i didnt saw that

tawdry sigil
#

if you want to even think about being a programmer you need to learn to read and understand every line of code

dusk summit
#

yeah actually there were no brackets there so .......

#

but in movement how should i compare then whether it is goat or not if my condition is wrong

tawdry sigil
#

simply switch allgoatsplaced to not allgoatsplaced

dusk summit
#

means we dont need this condition TurnManager.instance.currentPlayer==TurnManager.PlayerType.Goat

tawdry sigil
#

as well

dusk summit
#

then its fine

tawdry sigil
#

no

#

copy/paste the if

#

actually better, copy paste the whole method

dusk summit
tawdry sigil
#

mousedown

dusk summit
#

from where ?

tawdry sigil
#

movement, that is the one we are working on

dusk summit
#

i should copy/paste the mousedown of movement to where ?

tawdry sigil
#

here

dusk summit
#

private void OnMouseDown()
{

if (_node.occupant != null && TurnManager.instance.currentPlayer.ToString()==_node.occupant.name)
{
    //this.GetComponent<SpriteRenderer>().color = Color.yellow;
    _isSelected = true;
    
}
if(!TurnManager.instance.allgoatplaced && TurnManager.instance.currentPlayer==TurnManager.PlayerType.Goat)
{
    _isSelected = false;
}

}

#

but now also the goat is moving it is not working actually

tawdry sigil
#
private void OnMouseDown()
{
    // Start of Players Turn
    // Is the node occupied ?
    // If so, is it occupied by the players piece ?
    if (_node.occupant != null && TurnManager.instance.currentPlayer.ToString()==_node.occupant.name)
    {
        // Yes, so can be selected
        //this.GetComponent<SpriteRenderer>().color = Color.yellow;
        _isSelected = true;

    }
    // But wait...
    // Goats can only move when all goats are placed
    // Is this true ?
    if(!TurnManager.instance.allgoatplaced && TurnManager.instance.currentPlayer==TurnManager.PlayerType.Goat)
    {
        // No it is not so it can not be selected
        _isSelected = false;
    }

It helps if you add comments to the code

dusk summit
#

but then also it is not working

tawdry sigil
#

but it is still not right because the second if needs to be inside of the first one doesn't it?

tawdry sigil
#

because the second if only needs to be evaluated if the first one is true

dusk summit
#

but after placing it inside it is still moving

#

i am placing it inside after the first amd setting the isselect=true

tawdry sigil
#

so where do you set allgoatplaced to true

dusk summit
tawdry sigil
#
private void OnMouseDown()
{
    // Start of Players Turn
    // Is the node occupied ?
    // If so, is it occupied by the players piece ?
    if (_node.occupant != null && TurnManager.instance.currentPlayer.ToString()==_node.occupant.name)
    {
        // Yes, so can be selected
        //this.GetComponent<SpriteRenderer>().color = Color.yellow;
        _isSelected = true;
        // But wait...
       // Goats can only move when all goats are placed
       // Is this true ?
       if(!TurnManager.instance.allgoatplaced && TurnManager.instance.currentPlayer==TurnManager.PlayerType.Goat)
       {
           // No it is not so it can not be selected
           _isSelected = false;
       }
    }
 }
dusk summit
tawdry sigil
#

so back to mouseup of placement

#

which should be

if (max == 0) {
   TurnManager.instance.allgoatplaced = true;
   gameObject.SetActive(false);
}
dusk summit
#

ya i did this earlier

tawdry sigil
#

ok. are you still using TigerHome for your preplaced Tigers?

dusk summit
#

should i place it in a node

#

occupied node

tawdry sigil
#

don't
remove them you do not need TigerHome at all

dusk summit
tawdry sigil
#

add 3 Tigers. place them in the correct positions and add them to the correct node.occupant

dusk summit
tawdry sigil
#

yes

dusk summit
#

here right ?

#

in the occupant

tawdry sigil
#

if you are placing a tiger on node 0, yes

dusk summit
#

so i have to change something else with it ?

#

i am just drag and dropping the tiger prefab

tawdry sigil
#

first you drag and drop a tiger prefab into the hierarchy
you move it to the node you want in the scene view
and then you can drag and drop the tiger gameobject onto the node in the inspector

dusk summit
#

i did this

#

but the tiger movement does not work

tawdry sigil
#

then you have not done it correctly

dusk summit
#

firstly i draged and droped the tiger prefab into hierarchy then i duplicated it into 3 tigers then i drag and droped it into the specified node occupant object

tawdry sigil
#

screenshot your unity window with one of the correct nodes selected

dusk summit
tawdry sigil
#

that is correct

dusk summit
#

yeah

lethal burrow
tawdry sigil
#

wait.
change this to
if(TurnManager.instance.currentPlayer==TurnManager.PlayerType.Goat && !TurnManager.instance.allgoatplaced)
check for Goatmove first

dusk summit
#

trying

tawdry sigil
#

show me all 3 Tigers in the hierarchy

dusk summit
tawdry sigil
#

Right Now think on this

TurnManager.instance.currentPlayer.ToString()==_node.occupant.name

and this

dusk summit
tawdry sigil
#

which is why the placement code contains this

toPlace.name = prefab.name;
tawdry sigil
#

yes

dusk summit
#

i gave them the same name

#

but the thing is for me the second one is just moving slighly and then it does not move the other 2 are working fine

tawdry sigil
#

then you need to see what is different. it's what we call debugging

dusk summit
#

ok

#

i will see

lethal burrow
#

even tho the tiger on node 0 is jumping to neigbhouring node, the sheep got deleted

#

is it happening for u too?

tawdry sigil
#

that is not a bug, it is correct. The tiger is going from node 0 to node 4 via node 5. it is a legal move

lethal burrow
#

No bro. U played it here? play as coop

#

it is not a legal move

dusk summit
#

technically it is a legal move but as for game it should not be a legal move actually

tawdry sigil
#

the rules state it is legal to jump a goat if the adjacent node is empty

lethal burrow
#

can i see where u got this rule?

#

cos in above game it wasnt working

tawdry sigil
#

Bagh-Chal (Nepali: bฤgh cฤl, Newar: เคงเฅเค เค•เคพเคธเคพ dhun kasa meaning "tiger game", Hindi: เคฌเคพเค˜ เคฌเค•เคฐเฅ€ bagh bakri) is a strategic, two-player board game that originated in Nepal. The game is asymmetric in that one player controls four tigers and the other player controls up to twenty goats. The tigers 'hunt' the goats while the goats attempt to block the...

dusk summit
#

i think so it is because the board is square ๐Ÿ˜…

tawdry sigil
#

But anyway, it is easy enough to add any rules you want into the MovementManager script.
For instance atm jumping will always take precedence over moving. The rules state that when both are possible jumping should be optional so that would need to be added

tawdry sigil
tawdry sigil
#

if you want to make moving take precedence over jumping in the MovementManger script just move the //Check direct connect loop above the //Check jump code in the script

#

this is why it is important to read and understand the code

#

I cannot stress this enough

tawdry sigil
# dusk summit okok

btw, with the code and prefabs you have you do realize that it is very trivial to setup any board shape and configuration you want

dusk summit
#

yeah i know

#

i am figuring out how can i play on many resolutions

tawdry sigil
#

I thought a full 3d pyramid board might be fun

#

gonna need lots of goats

lethal burrow
#

can u share where u got those tiger and goat models in your game?

#

i wanna place those

#

instead of cube or capsule

tawdry sigil
#

fully animated as well, much nicer

lethal burrow
#

ah. its a paid pack

tawdry sigil
#

yep, there is a free tiger on the store but no decent free goats

#

honestly, the animated animals are worth the money, they really bring the game to life, when you guys give me 10 minutes to get back to it I can finish off the animation sequences

lethal burrow
tawdry sigil
tawdry sigil
lethal burrow
#

haha. is okay

#

we are sticking to sprites

tawdry sigil
#

tbh, the game works with any characters, last night I played with Knights and Demons. works just as well

#

I also had a play with Cars and Motorcycles. It works

dusk summit
#

yeah

#

you have any idea how to put this reso in mobile devices

lethal burrow
tawdry sigil
#

Shouldn't be a problem. Building to Android?

lethal burrow
#

yup

tawdry sigil
#

let me try it rq

dusk summit
#

yeah

tawdry sigil
#

which Unity version you guys using?

lethal burrow
#

2022.3.10

tawdry sigil
#

not a good choice 2022 is still very buggy

lethal burrow
#

but its LTS

tawdry sigil
#

So what? Unity quality control even for LTS versions is totally crap

dusk summit
#

is it working for you ? @tawdry sigil

tawdry sigil
#

I'm using 2021.3.31

dusk summit
tawdry sigil
#

yes, very much so

lethal burrow
#

this is the res settings i used for android build

tawdry sigil
#

Huge window width and height, very much for high end phones

lethal burrow
#

That is my mobile res

tawdry sigil
#

switch the aspect ratio from Native to 16:9

lethal burrow
#

it is in 16:9 only

tawdry sigil
#

the setup I made was based on a 16:9 ratio

lethal burrow
tawdry sigil
#

The other thing you can do, this is using a perspective camera, change it to orthographic and adjust the node placement slightly

#

you will see I have a canvas in the hierarchy with your original game board image. So you can use that to help the placement of the nodes

dusk summit
#

ok

dusk summit
#

thanks for helping us @tawdry sigil though i have some doubts in the code that i will ask you tomorrow if its okay with you

tawdry sigil
#

Ok, built to android using 16:9 resolution. Works perfectly

dusk summit
#

i have to change the editor reso only right ?

tawdry sigil
#

no, the android build resolution
apologies for the bad photo

dusk summit
#

where is it ?

tawdry sigil
#

could be a 2022 thing, it's different in 2021

#

Also I noticed that the nodes are a little difficult to pick up on mobile so you might want to increase the size of them, they are scaled to 0.7 atm

#

mind you I have a crappy old phone because I only use it for, guess what, making phone calls

dusk summit
tawdry sigil
#

old school, very old school. I prefer phones that are attached by a cable and have a dial

dusk summit
#

@tawdry sigil want some help

tawdry sigil
dusk summit
#

I just wanted a explanation

tawdry sigil
#

OK, what do you want explaining?

dusk summit
#

what exactly we are doing here?

tawdry sigil
#

wait one whilst I bring up the code

dusk summit
#

yeah sure

tawdry sigil
#

To start with we have this

        // Fill lines with node numbers
        lines[0] = new List<int>() { 0, 2, 8, 14, 19 };
        lines[1] = new List<int>() { 0, 3, 9, 15, 20 };
        lines[2] = new List<int>() { 0, 4, 10, 16, 21 };
        lines[3] = new List<int>() { 0, 5, 11, 17, 22 };
        lines[4] = new List<int>() { 1, 2, 3, 4, 5, 6 };
        lines[5] = new List<int>() { 7, 8, 9, 10, 11, 12 };
        lines[6] = new List<int>() { 13, 14, 15, 16, 17, 18 };
        lines[7] = new List<int>() { 19, 20, 21, 22 };
        lines[8] = new List<int>() { 1, 7, 13 };
        lines[9] = new List<int>() { 6, 12, 18 };

which fills our lines array with all the node numbers on each line of the board
do you understand this?

dusk summit
#

yeah

tawdry sigil
#

ok next we have this

        // Calculate node connections
        foreach (Node node in nodes)
        {
            foreach (List<int> line in lines)
            {
                for (i=0;i<line.Count;i++)
                {
                    if (line[i] == node.ix)
                    {
                        if (i > 0) node.connects2.Add(line[i - 1]);
                        if (i < line.Count - 1) node.connects2.Add(line[i + 1]);
                        break;
                    }
                }
            }
        }

which takes those lines and for each node it tells it which nodes it is connected to
do you understand this?

dusk summit
#

yes

tawdry sigil
#

Ok, so then it is simple in this code

        // Check direct connect
        foreach (int ix in startNode.connects2)
        {
            if (ix == end)
            {
                return endNode;
            }
        }

we have a start and an end node
so because connects2 of a node tells us exactly which nodes this node is directly connected to
if the connnects2 List of the start node contains the end node we know they are directly connected and we already checked if the end node is empty so that start end combination is a valid move so we pass back a node to the caller as proof of this

dusk summit
#

Ok ok got it

tawdry sigil
#

this could also have been written as

if (startNode.connects2.Contains(end)) return endNode
dusk summit
#

i was just trying to write the win condition for goat so i just got confused that what was that code for ๐Ÿ˜…

tawdry sigil
#

win condition for goat is quite tricky however the stalemate condition is very tricky indeed

dusk summit
#

yeah

#

my approach was i should check whether the neighbour nodes and jump nodes are occupied or not

tawdry sigil
#

firstly, you only need to check the nodes which are occupied by a tiger

dusk summit
#

ok

tawdry sigil
#

so

  1. is the node occupied by a tiger
  2. if this node has an empty connecting node - can move
  3. if this node has a connecting node occupied by a goat and that node has an empty connecting node - can move
dusk summit
#

yeah

#

ok i will try this

#

thankyou

#

sorry to bother you

tawdry sigil
#

wait til you try to figure out the stalemate logic and implement that. I will be interested to see what you come up with, as I said, it's very tricky

dusk summit
#

yeah