 Hey guys, it's MJ the student act tree and in this video what I want to do is I want to show you my little poker game Okay, and specifically what I can do my poker game is you draw five cards and then you can click which cards You want to discard you can see we've swapped these cards out. So oh look at that. We've got a flash We are very lucky. Okay, so what we're gonna be doing in this video is I'm gonna be showing you how do I Do this, you know when I click here it goes it changes it to swap and it says swap card over here Remember I have made other videos Before that show, you know, how do we check to see if we got a two pair? How do we generate these random cards? How do we you know do all that other stuff? But this video is about how do we swap the cards? Okay, so How it works or the logic that I've taken out is that when it comes to drawing the cards, okay? You would have remembered this from the previous videos I did where you draw the first five cards is I generate a random number for the suits and I'm generating a Random number for the cards value if the second card I'm gonna do this for all five cards, but if the two cards are the same it regenerates it But now in order to swap the cards, I'm not just gonna draw five cards I'm gonna go and I'm gonna draw Ten cards, so I'm gonna be drawing an extra five cards and you can see the logic here Does get a little bit messy, but basically what I'm just doing is I'm checking that each card is not the same as the other There's probably a more efficient way of doing that So if you can figure that out for yourself, that's great But I just have a little while loop that says while this card is not equal to any of the other cards You know generate another random number or while it is equal to one of the other cards generates random number until it is unique So now what we have is ten unique cards, but we're only displaying five of them In order to get the other cards by clicking swap here Then those other cards pop up So I'm generating the cards before the game actually starts But I'm keeping them hidden and when you swap then you can get them So what I need to do so this is the first thing is drawing the cards I have made a whole video on how I draw the card So if you want to know the logic behind or why the two random variables go check out that video Then what I do I try to make a swap card method But that that didn't really work too well Then what I want to do is each of my cards is a button So if we come here and click accept hand This is a button you can see I can click it and it's changing the pictures And it's also changing the state that my game is in it's going from accepting the hand Which will then take us back to the play method to the swap card method which will then activate the different things So we've got we've got some states that we're playing around with So yeah, you can see these are my five buttons. They all act very much the same I probably could have been more efficient with that as well But anyway, I'm not a master programmer more into statistics. So this is how I did it What I do is don't worry about the toggle that one there. That's just the general state What I want to look at here is this piece of code here So what I'm saying is I create these they basically are they toggle so I created them in the top here Here we go. Select one to select five. They're all set at zero If you click the button It makes select equal to one But if you click the button when it already has been selected so over here It's gonna go back to zero and it's gonna set it back to the old card Then what it do is if I click select here, it then puts this picture as the swap icon That's what that line of codes doing there or if I click it back It shows me back my old card face, but now it has the select one activated And this will happen for all the cards. So this will be select one select two select three select four select five Now when I click this you can see it changes the state. So now my swap card, which is basically my play button When I click that it's gonna display these different cards and let's go look at that logic now So what we do is we first say if all the selects are equal When we click play for the first time we want to just reset all the selects back to zero Then what we do is I have this code here so by clicking select you force yourself into toggle position one and What you're gonna do here is it says if select one equals one that means it was chosen We're gonna replace the first card with the sixth card and remember I'm generating the numbers and the suits separately. So I need to do both If card two was selected we're gonna replace card two if card three was selected We're gonna do that do that do that And then what I do is I change the toggle for the next date and I display the results Displaying results also recalculates whether it is one pair or not So if we click here, we've got the high card high cards terrible. We see we get another ace We didn't get another ace. Oh there. So you can see we got the two jacks Well, actually what I should have done is changed it And you know, that's the thing about poker Sometimes you just don't get what you want to get for demonstration purposes But what we do is we recalculate whether it was a full house or a flash or something like that We change our button to accept hand and we change a few things and we display our coins and our tokens And our winnings and all that type of stuff But generally that's how I had did it was in order to be able to swap the cards I made I originally draw 10 cards originally I make each of these a button by clicking the button it then tells that that all this buttons been clicked It then swaps the card saying all that button was chosen reap play that and that way I can simulate the feeling of drawing five cards Discarding however many you want and getting new cards to try and better your hand And what's also nice about generating the cards in the beginning is making sure that you don't discard the Queen and then you Pick up another card and oh my flip. It's the same Queen You know there could have been that probability if you just generate the cards later So that was my methodology behind it This app is available on the app store. So feel free to go and download it and check it out I might even put the code like put it on one of these code stores And then you can download the code and play with it for for yourself You can play the whole app, but that generally is how you swap cards in poker in Android studio Thanks guys so much for watching. Cheers