 Hello programmers, today we're gonna look at a chess app to make it more dry and improve its class design If you want to prove your code ask me live on twitch at twitch.tv slash dragger. Here we go. Cool chess We got a gooey and stuff. I imagine this is a decent size project. So where should I focus? Where do you want feedback on the most and what kind of feedback do you want game handler? There's no game handler game model chessboard handler. You mean feel like this isn't dry There you go. Wait, what is this doing? Is that updating potential move spots? Wait, what is what is this? What why are these empty? What's going on here? So Pawn is like interesting, but What about Rook for example or? Bishop let's see Wait, what why would it matter whether it's? Oh Okay, yeah, gotcha gotcha. It's definitely don't look dry to me. This is all right This looks like the problem. So I'm curious. Why do you have this movement class? Maybe this movement class is causing problems for you Like it mental problems like preventing you from thinking about better design because I saw like you have this these empty Functions, I don't know where you use this stuff, but this looks concerning not because it's empty But because who's gonna call it? I suspect you don't have anything calling your movement methods Polymorphically so as far as my drawing of this up what I would do is look at the differences put them side by side Rook model. Okay, so we got this and we got This Side by side. I mean highlight the differences You can't see it But there's like the white piece and there's black piece and then here there's white piece black piece So that looks like it's the only difference. What you could do is say You can make a function. That's is enemy piece. So you have is black piece. You could say is enemy piece Yeah, you could say is enemy piece and then you give it the current player And then you give it a position and it tells you whether there's a match. Does that make sense? That's just an idea. There might be a better idea another idea is Get piece color. Is this equal to? Selected piece color. So give me whether this is black and white. Actually, this would be not equals Obviously wouldn't have both of these in the same thing But I'm liking this is enemy piece if it's an enemy then we can step on it But we can't go even any further so we have to stop looping So that would cut Rook model in half Which is what I think you're looking for is that dry And I think you could use the same technique for Bishop as well because it looks like it has the same problem So if we ignore the color aspects, you have the diagonal left up left up right down left. All right You spelled diagonal like three different ways See the the main difference is that One the bounds and to the direction that you're going See here you're going negative negative here you're going negative positive here you're going positive negative and you're going positive positive So one insight you could have is that this It's the same as plus equals one Plus equals one and then negative case is minus equals one minus equals one in other words plus equals negative one see that that this is a Down right and this is up left Would be plus equals negative one plus equals negative one so now there's there's more similarity I'm making these these two very similar. That is what I'm trying to do. You also have the starting position So plus negative one Plus negative one and for both we can check both of these conditions. I think Hopefully I've done like simple enough transformations. They can see it preserves the behavior exactly But now it's very you see very similar things The only differences are these these ones are either positive or negative so what you could do is put This loop into a function and parameterize the minus one minus one plus one plus one I like to call it DX as in like Delta X and Delta Y But you're using I and J or row and tile. I'll call it Delta I and Delta J. You pick better names for this particular case They're both negative one so Delta I Delta J Delta J Delta I you would put this into a function like, you know private void whatever whatever check one diagonal Stuff and in still to I J and Then you know, you got all this stuff and then you would call check one diagonal Diagonal Now you got me confused how to spell it Passing the stuff and then you have Delta I which would be plus one plus one Minus minus It would look like that or you could have a loop around something I think function calls is probably the best, you know, I took some shortcuts But hopefully see the pattern you could do the same thing not with just Bishop But with Rook because Rook has to go one way, you know, it has to Wait hold on doesn't Rook also go four directions. You're only going two directions. Oh You split it up into a horizontal movement and vertical movement So I don't know why you split it up that way But you could do the same technique with Rook vertical, you know Because you have to check the four directions except for for Rook instead of having something like this. It'll be You'll have plus one and zero And you'll have minus one and zero and you'll have Zero and minus one and zero and plus one. It'll sound like this for Rook Bishop like you wouldn't use the same function here because Well, maybe you could use the same function actually You might be able to use the exact same function for both Rook and for Bishop and I guess queen queen You could use Bishop and Rook combine them two and you get a queen So these are reusable functions or this rather this is a reusable function Even nicer. How's that dry for you king? I don't think can use the same exact logic At least you won't be able to use this function with the king another thing related to dry Is you have this a lot where you make a pair and then you add it one thing you can do to Remove some repetition is to just do that Another thing you could do to produce repetition is instead of using a rayless directly I recommend making domain types so you can have a move list type or something And then what with move lists you can say just this You add a you have a function add that takes in the file on the rank So you don't have to keep saying new pair so that reduces Also, that would reduce the duplication here. You would say It's not a moves list necessarily because the order of the moves don't matter I don't think so it's just a move set so you can say that if you make custom types Then you can make nice syntax for using those custom collection types But at the very least you can just merge these two lines into one line because you repeat in a bunch of spots like all over In fact, what you could do is say like for pawn to make a function, which does all this work private void add check pawn move Row in style and the other stuff you need And you can throw that in there give her these plus ones And just like earlier you have to do both bounds checks So you have to check greater than zero, but now you can say check pawn a move row plus one tile plus one And then row plus one tile minus one and all that stuff Actually, it looks like it depends on the black and the whiteness. Hold on. Hold on. Hold on. Maybe this won't work I don't know. I'd say just play around with things Look at into using look at patterns for things because you probably copy pasted a lot of this Look at the pattern you used for copy pasting and see if you can do some basic math or just put some things into variables And then just find the patterns common ways to factor out those patterns are functions classes and loops A function might be appropriate here. You might have multiple functions. You might have something like add if enemy Or empty like if you made that custom type, you could say add if enemy or empty You can do bounds checking in this function So you don't need to do bounds checking outside and we're already checking enemy and we already know we're a pawn So you could just say this Well, actually uh Because your pawns because black can only move in one direction white can only move in another direction So what you might do is say row plus Black is plus How do you do the plus two? Do you not do the plus two? How do you do two steps forward? Oh, this is capturing. Oh, okay. Okay. So You could say something like this Oh, you have a comment here handling a potential attack. So you could do this And then if you're a white pawn So, yeah, if you make a class for your collection, you could do fancy stuff like add if enemy as nice and short very not verbose with all these checks. Uh, well, you might need like A selected piece in here. I don't know. I don't have all the details. I'm not running the program or anything I'm just pseudocoding it. Just uh, it was brainstorming, you know Yeah, the custom collection class probably only has a few add methods and maybe has a function to return Their array list contained within it. You don't have to implement a full collection You don't have to implement the java collections Probably you don't want to implement the java collections thing because you're not implementing a general purpose collection You're implementing a very specific purpose collection So you don't want to use it like a normal java collection Most collections that you make yourself are not used like the standard collections So don't deal with their hierarchy of interfaces and stuff. Don't bother with that This this whole separation of vertical movement diagonal movement and and horizontal doesn't make sense You're not even following the rules because you're allowing the pawn's diagonal movement, uh, like the attacking You're handling that in a vertical movement function. It really should be long in the diagonal movement I don't think you should have three functions. I think you should just have one. Give me all the movement options What do you do with night unique movement? Oh, you even have a fourth thing And why why is there another interface? I don't understand your interfaces I bet you one Satoshi that you should just delete all your interfaces and you're not going to see any Impacts on the rest of your code aside from moving all these override things, which will be fine I don't think you need those interfaces. You're not using anything. Yeah the movement and unique movement interfaces. Get rid of them Fresmo, I'd like to see how your code turns out when you go through and totally change everything I like to see how it goes because uh, there's other aspects I'd like to discuss like the thing you linked the repetition here. We could take a look at that We'll see how the other stuff goes first and never implemented chess actually I probably should do that Want to improve your coding skills coming out at twitch tv slash dragger? I give free code reviews and lessons every weekday