(coding) How to make user Interactive functions in a row?
Posted: Fri Jul 07, 2017 2:44 pm
I am hopefully asking for coding tip from someone.
What I have many times encounted when programming, upon which i still dont have good programming solution, is following:
I might have
So far so good and simple, except, problem is that each of these functions reguire some interactivity from user, which means they cant be executed this simply, since by default they be executed in a row, but actually program needs to go to wait for users interaction (for example to push "yes" or "no" button) which makes the real execution much harder.
As close to real example, I am taking a board game which is based upon cards. Depending upon cards depends what happens next in game.
So lets suppose there are two different cards:
1. Card which makes player be able to do three different things: 1. put cube on board, 2. Draw card from other players hand, 3. Discard three cards from your hand.
2. Card which lets player do two thing: 1. Draw two new cards, 2. Discard one card from hand.
Cards like this would theoretically be extremely easy to add to the program, and even make more, for all I would need to do is following:
And then in game I would just look which card is drawn, and tell it to either execute function "card1()" or "card2()"
However, in practice this doesnt work, since already in card1 case in first case I already need to "halt" the program to wait for player to choose where on board he wishes to put that cube. Then only after he have done that, it should return back to that "Card1()" function and continue to next line "DrawCardFromAnotherPlayer()" in which program would once again need to halt until player decides from which player he is going to steal that card from.
I do have one solution to this, which I personally dont think is very good one, but roughly it goes like this:
There are multiple problems with this one. First of all, i have to have some way for it to keep looping back to this "card1()" function when it is still not done. Then I also need to take care to change the state of this at end of for example when placing the cube, which again can be a problem since several cards can be using the same function (in this example both card1() and card2() have DiscardCards() function), and on top of that, these same functions might be used even by some who dont use this state system at all.
Or, even if i do general fix of putting after everyaction simply "state = state + 1", then i still get the problem that what if one of the functions goes on to execute another function. As example, lets say when player puts his cube to board, maybe that placing of cube activates another DiscardCard() function. In this case "state = state + 1" would happen twice before getting back to card1() function, and "DrawCardFromAnotherPLayer()" might get skipped.
Hence. Does anyone have any better solution to this? I am myself thinking if maybe some sort of Observer Pattern would be the solution? (I have just recently read about Observer Pattern, but havent tried it ever)
What I have many times encounted when programming, upon which i still dont have good programming solution, is following:
I might have
Code: Select all
function Myfunction()
func1()
func2()
func3()
endfunctionAs close to real example, I am taking a board game which is based upon cards. Depending upon cards depends what happens next in game.
So lets suppose there are two different cards:
1. Card which makes player be able to do three different things: 1. put cube on board, 2. Draw card from other players hand, 3. Discard three cards from your hand.
2. Card which lets player do two thing: 1. Draw two new cards, 2. Discard one card from hand.
Cards like this would theoretically be extremely easy to add to the program, and even make more, for all I would need to do is following:
Code: Select all
function card1()
PutCubeOnBoard()
DrawCardFromAnotherPlayer()
DiscardCards(3)
endfunction
function card2()
DrawCard(1)
DiscardCards(1)
endfunctionHowever, in practice this doesnt work, since already in card1 case in first case I already need to "halt" the program to wait for player to choose where on board he wishes to put that cube. Then only after he have done that, it should return back to that "Card1()" function and continue to next line "DrawCardFromAnotherPlayer()" in which program would once again need to halt until player decides from which player he is going to steal that card from.
I do have one solution to this, which I personally dont think is very good one, but roughly it goes like this:
Code: Select all
Function card1()
If state = 1
PutCubeOnBoard()
elseif state = 2
DrawCardFromAnotherPlayer()
elseif state = 3
DiscardCards(3)
endif
endfunctionThere are multiple problems with this one. First of all, i have to have some way for it to keep looping back to this "card1()" function when it is still not done. Then I also need to take care to change the state of this at end of for example when placing the cube, which again can be a problem since several cards can be using the same function (in this example both card1() and card2() have DiscardCards() function), and on top of that, these same functions might be used even by some who dont use this state system at all.
Or, even if i do general fix of putting after everyaction simply "state = state + 1", then i still get the problem that what if one of the functions goes on to execute another function. As example, lets say when player puts his cube to board, maybe that placing of cube activates another DiscardCard() function. In this case "state = state + 1" would happen twice before getting back to card1() function, and "DrawCardFromAnotherPLayer()" might get skipped.
Hence. Does anyone have any better solution to this? I am myself thinking if maybe some sort of Observer Pattern would be the solution? (I have just recently read about Observer Pattern, but havent tried it ever)
