Thanks for watching! Code Review album drops next summer 😎 Don't forget that the first 1,000 people to use this link will get a 1 month free trial of Skillshare: skl.sh/thecherno11211 Couple things I missed in the video: - The king and queen positions are flipped! Not sure how I managed to not notice… - Castling _does_ actually work, but you have to drag the king onto the rook - I’m used to castling by dragging the king two spaces over, which doesn’t work in this game
Btw, I need that compressor in my life. Where can I get one?! If you don't know what the hell I'm talking about, just point me to your audio person. Thx in advance.
Hey, did you try clicking on the king and then on the rook? I think that might trigger castling, since when you clicked the king, it showed the rook square as a possible option.
@@AgentM124 I think moving two squares with the King is like the fundamental principle of castling and “moving to the rook” is more of a quality of life improvement. Which is probably why Cherno’s assumption that castling didn’t exist is a valid one.
@@AgentM124 the concept of castling is moving your King by two squares in one move provided the following conditions are met:…. The same as moving your pawn from the 2nd or 7th rank to the 4th or 5th which is two squares in a single move given the following conditions … respectively. The idea is that they are special moves that typically involve multiple moves but only take a single move if certain conditions are met. The king swapping positions with the rook somewhere in the middle is more of a derivative idea based on the previously mentioned underlying concept as a way to better visualize the move.
As a full time C++ dev i can only say excellent code review! You pointed out all of the major pinpoints with an easy to understand explanation on why and how to improve it without going into any super advanced C++ topics that would easily confuse & overwhelm a beginner programmer. Great Job! I wish i had someone like you when i was first getting into C++.
I'm about 8 minutes into the video and I just couldn't hold back. How did no one notice that the kings and queens starting positions are switched. I wouldn't have noticed until the Cherno started playing. Maybe you'll point that out later in the video but ahhhh.
Haha lol good point! I totally missed that! I guess I didn’t notice because it changes depending on which side you play, and I must not have paid too much attention to the colours since I was playing myself here
Actually the pieces are on the correct coordinate but the color of the squares are "wrong". If you imagine the coordinates, White's King is on e1 and Black's King is on e8 which is correct. Changing places would make kings start on the D-file which would be completely wrong. The rule "White's queen on white square and Black's queen on black square" can't overrule the coords, in order to make it right, black pieces should be arranged on top of that board and white pieces on the bottom of it.
@@ScorpionG4merBr I'm not quite sure I understand what you mean by the coordinates. Currently the kings are on the D file. The only problem with the layout is that the respective kings and queens need to be switched around.
if he played chess often he'd realize absolutely no chess clone does that, you have to drag the king two squares (3 squares if it's on queen side) close to the rook to trigger castling
Even if you have a constant (like 8 tiles per row), it's good to use a constant instead if you're gonna type if more than once so that you know exactly what that 8 is. Maybe there are other things that also have the value 8. It will help you understand your code.
That's true a lot of the times, but in a situation where you are writing something like: for (int row_number = 0; row_number < 8; ++row_number) {} I think it's obvious from context what that 8 means and if you accidentally type 9 instead, you will catch that mistake immediately from context. If you write a lot of words, instead of numbers, it can infact confuse you because you need to spend some mental energy deciphering what the words mean. If you see 8, you know the loop will iterate 8 times, if you see BOARD_SIZE, you first have to find out what BOARD_SIZE is - is it 8, is it the number of pixels in one dimension, .... My point is, you have to think more when you are using words instead of numbers. In cases where it may not be clear from context what the 8 means I agree with you fully, but considering this is a chess application and the person reading your code, be it you, or someone else, is most likely not stupid, it's a reasonanble thing to assume that they know what the 8 means.
@@stewartzayat7526Literal numbers are always bad: - Forbids you to ever change. Otherwise you'll have to go replacing the entire project. - May mistake their meaning. - Words can always describe better. If the number "is obvious", the word could has it: BOARD_SIZE_8. - ~100% chance of a word typo resulting in compile error. - C++ Core Guidelines says to always avoid them.
A video on building c++ code and about build systems will be great. The main problem for people is that they don't understand how compilation and linking works. And how to deal with libraries. Btw loved your voice 😋
I second this so much. I really wanna learn CMake as a linux user but it's just so complicated and there are pretty much no tutorials that go beyond the very basics
The build system in C++ is one of the reason I moved to Rust. Everybody does things differently and it's so hard to put the pieces together. Most of the people uses CMake, so it makes things just a little bit easier, but it's always terrible =/
Backend eng here, which uses python, cpp, dotnet, and rust. I love cpp, but not the build system lol. I don't know why no one in 20+ years streamlined this process with a standard. If anyone is interested I created some cmake templates for sdl2 and sfml which can make life easier for simple game dev projects. Is a work in progress. Would love feedback, so far every template works on windows and Linux. Need to write docs for running.
autotools is also fairly common. its amazing once it gets working (it can make porting things into an almost trivial task in some cases), but having to mess with it is an absolute pain.
Lets be fair, most of the projects on github (including big name ones) have many of the same issues - missing files or the expectation that the person building the code has just the right version of python / ninja / environment paths / ceiling cat set up. A huge amount of pain and wasted time would go away if we could solve this. My steps when I'm about to put anything out public is to take a totally fresh windows/linux/mac install as necessary (a vm is fine) and then see what it takes to build / run the thing I'm putting out. I document everything needed at that point and either "fix" stupid requirements and start again or ensure all steps are clearly documented.
@@sebastianmorris5394 It's really not though, I've had just as many issues trying to install docker versions of things as non docker versions. It just adds yet another layer of potentially broken stuff. The few times I have deployed something docker based that worked right away broke within a few weeks after an update was required and pushing that update either to docker or to the thing itself left it so broken I had to start back from scratch.
6:04 I'm pretty sure you can, it's just not standard, the rook is highlighted as a possible move 34:30 isMyPiece() or canMovePiece() 36:00 still doesn't notice you can castle :) 49:50 Or create once then copy construct the rest? (not sure what the non default copy constructor did, why it set texture to null)
One thing to add (months later), try not to hard code values everywhere. You could calculate the division value for example as a global. So if you change the resolution, the division values also change. You could still leave them const manual updates as well, but it would be better if they were all together in one place. Not spread out through the code. Edit: Nvm, just saw he mentions it later haha
I like how he never realised that he could castle the king because in the MoveType there was castle in the list, probably when he tried the game he couldn't castle because he had to drag the piece on top of the rook, but this is irrelevant, beautiful video!!!
It's fun to see that how you quickly tested checks, castling, checkmate and prise en-passant (and later promotion), but failed to notice that the king and queen's position were inverted in the initial position, which striked me the second I saw the board :P. It perfectly illustates why it's so important to have experienced alpha/beta testers (and sometimes a few team advisers familiar with the game genre during the design and early development) when developping games.
he probably just didn't wanna spend too long on it, cuz he didnt test promotion, playing an actual game of chess or a few would have definitely been enough to make one realise
I remember the first chess game I programed with my dad, well he did all the programing as I was like 8. Done in BASIC on an old IBM computer with no hard drive and 5 1/4" floppies! It was written such that you gave the coordinate position of the piece, then named the piece and then gave the coordinate position that you wanted to move too and the name of the piece. I think most of you can guess the bug. ... As long as the move was a legal move for the piece you were moving (at the start of the movie), you could rename the piece in the destination field and promote it to anything you wanted
3 move draw rule, 50 move without a capture rule, stalemate, insufficient material, draw offer, resigning, plenty of other test cases to deal with that are probably not implemented but more time consuming to test. It doesn't show the moves, who is to move or offer PGNs to clipboard which also is not great.
One thing you did not say but is something I heard as a point of graphics... If you need a texture at 80*80 and you can scale the textures... Always go larger than what you want and never smaller unless you are mapping and applying to a model. With 2d at least... Always scale down or match scale. It avoids the graphics blurring.
There is a cool wiki on chess programming of situations that could happen. Like en passant and ending up in check etc. There are a few cases that are difficult. Also some cool books on using bit fields to get the best speed.
6:00 the initial position of the pieces is wrong! For both Black and White, the positions of King and Queen should be interchanged. White queen in the initial position is only on white square, and black queen is initially only on black square. Another way to have things correct, would be to keep the chessboard as it is (not rotate it in any way) but put the white pieces lower, and the Black pieces upper.
I've seen some versions of online chess where if you click the king, you have to click the corresponding rook, and when you clicked the king, the corresponding rook highlighted, so that would likely have worked.
Instead of using std::pair for points he could have just used SDL_Point provided by SDL. This would then also be compatible with some other SDL functions as they only accept SDL_Points.
16:26, sure. It's less energy spent, less thing to write, read and think, since it has the same syntax as a pointer or class object evaluated against bool. Plus, if the boolean variable ever changes to a pointer by design, no refactoring will be needed here. 23:45, since this pointer is not meant to move, it'd be better as a reference: safer, less typing and a visual hint. 24:53, struct has better performance than tupple, because it doesn't get members by number. Generates much less bytecode too. 30:45, literal numbers are dangerous due to this kind of thing. Right thing to do would be to has it attached to the screen size, either at compile time, like a global constant, or at runtime (in case of user being allowed to change it) as information from a class, calculated together with the screen size. 36:25, bold security flaw here: this f() should not be public. If a piece goes to a wrong place, he'll has to look the entire project for calls to it. Fix: remove it from public domain, get a compile error, mark the f() who is calling it as 'friend', and only if it's actually 1 of the few that should be allowed to change piece location. 38:40, a good design choice is to separate the character logic from its multimedia resources, so that its logic may go to the CPU caches, becoming much faster.
I'm sure you will find this out later in the video, but castling seems to be possible by dragging the king to the rook rather than the square next to it. (:
@@Katt1n Well there, he couldn't castle because you can't castle trough a line of check. The bishop was up there. And it was implemented in the game I think, it even lighted that line of check in order to show why the castle is not possible there.
@@alexandrucalitescu5822 I understand the rules of chess, for the first few minutes of the video he implied that castling was not implemented which is not true.
One thing ive noticed is that the board is not actually reversed when the white is on the top side, it's always the white king should be on a black square.
Small tip: You shouldn't have 12 different images for each piece, instead put it all in one file and then crop that image accordingly. That's exactly how I made playing card games, imagine having to load 52 images separately lmao
52 images??? Bruh it's 4 suits, 13 cards (so 13 ranks, 3 face cards, that's 20 image assets. Like if you're concerned about space and loading, make the images from those 20 components. Cache the cards as needed, 52 generated assets using 20 image assets.
When I made a chess program, I didn't instruct the computer to find all the legal moves in a position, instead I instructed it to wait for the player to make a move and then check whether the move is legal or not. I instructed the computer to make 4 checks to see whether a move is legal or not: 1) Natural Movement of a piece (for instance you want to move a bishop, computer checks whether you are trying to move the bishop diagonally or not. If you try to move a rook, then computer checks whether you are trying to move the rook horizontally/ vertically or not, if you are trying to move the knight computer checks whether you are trying to move it in L shape or not etc.) 2) Whether you are capturing your own piece 3) Whether you are jumping over a piece 4) Whether you are in check after your move (in chess you must not be in check after your own move). If those 4 checks are done then the move is legal and computer changes the location of the chess piece. If the move is illegal, then computer does nothing.
The comment on "Initialize the field", I think it is a word-to-word translation from German. It would translate to "Intizialisiere das Feld" with "Feld" = "field" and means in this context the chessboard. So I guess it's just an unlucky translation on his part, I think he meant "Initialize the board" or something.
First of all, the positioning of king and queen is reversed. Next, 6:04 you can castle. You had to release the mouse button on rook instead. See the blue background behind the rook.
I am new to opengl and GLFW so the first project that I have done in C with openGL was a tic tac toe project, I put nothing on the heap there. OK, tic tac toe is a lot simpler than a chess app (I have made a chess app in Java), but I think that even if I were to make a chess app in C with opengl and GLFW I could just initialize all the objects for chess pieces in the function which contains the main loop (while (! glfwWindowShouldClose(window) loop) and I would put all the chess piece objects on the stack frame of this function which runs in its own thread (I would put it in a different thread). The stack frame of this function lasts as long as the window lasts. I don't think I need to put anything on the heap whatsoever. Even when dealing with promotion when a pawn becomes a queen/rook/knight/Bishop, I can still put that queen on the stack frame of the promotion function and then use memcpy to copy it into where the pawn used to be and that should work correctly as long as the size of the pawn object is bigger or equal to the size of the queen/rook/knight/Bishop object. My variables on the stack will last as long as the window of their chess board exists, their stack frame will exist as long as the main loop of their chess game exists, only after the main loop of that chess game is over, that function is over and leaveq CPU instruction is executed (leaveq is basically the same as add numberOfBytesInThisStackFrame, %rsp and then pop %rbp, I am talking about x86-64 architecture here) the stack frame of that function no longer exists.
Thanks for an interesting code review, and nice polling fix! From a readability standpoint (and to some degree safety) I would have expected more feedback on the code duplication -- I like what you did for the knight moves loop, but would have taken it further, e.g. all of the x + dx > 7 && ... that is, you should do that once (x_new = x + dx) (even better with a point class) and then (OnBoard(p) && Free(p)). I think a bit more of an investigation into how you want to handle the polymorphism would have been interesting too.
I noticed the queen is on the E file when playing as black. They didn't flip where the pieces are drawn to the screen based on if you are playing as white or black
Great review and great Code overall! I want to deepen my C/C++ knowledge and was wondering about the tuple thing. I really like to use tuples (C#) when readability is given since you can name your tuple values, kinda sad about that
Can we take a second to appreciate how good this code is for a beginner? Sure, there are some things to be improved, but this is quite remarkable. Well done!
using SDL properly the RAII way is slightly more difficult. I do it that way : std::unique_ptr window {nullptr, &SDL_DestroyWindow}; std::unique_ptr renderer {nullptr, &SDL_DestroyRenderer};
Isn't there a callback system from every event. instead of polling, coz at once there is going to only one event. but that could be slow depending upon how the library handles callback. besides one more optimizations that can be done is to calculate all possible moves once and make a set of them and later update them depending upon actual moves made. not a huge deal but it will save time when user is clicking all over the place.
Castle by clicking on the rook, not on the square next to it (in online chess usually both should work) Also, the king and the queen. For godness sake. Srsly? Nice project though
1:58 when a C++ developers realizes that *no one, literally no one finds C++ building intuitive and understandable,* but acts annoyed and states that is everyone else's fault. EDIT : 2:55 "I tried to add SDL2, messed up everything" -- _every SDL C++ project ever_ for anyone who hasn't been a C++ programmer for at least 2 years.
I remember 3 months ago watching this and not being able to understand a lot of things, now I watched this and I understood almost everything Cherno said in this video. I studied Java and C in the meantime, I guess that helped for C++ understanding as well. Still, it is not quite clear to me what tulp is and some usages of :: I don't understand. I even get the frustration of cherno that this guy didn't put more things on the stack, if I were making a chess game in C I would put everything on the stack, I don't think I would call malloc even once. As long as I put my chess pieces in the same stack frame where the game loop is, I will not get a seg fault, because that stack frame is alive as long as the game is alive.
@ ~2:10 (the line about making a vid on building cpp progs) More like a series. If you're wondering why nobody (new to cpp) understands it, take a look at the autotools documentation and you'll begin to understand.
Thanks for watching! Code Review album drops next summer 😎 Don't forget that the first 1,000 people to use this link will get a 1 month free trial of Skillshare: skl.sh/thecherno11211
Couple things I missed in the video:
- The king and queen positions are flipped! Not sure how I managed to not notice…
- Castling _does_ actually work, but you have to drag the king onto the rook - I’m used to castling by dragging the king two spaces over, which doesn’t work in this game
Ready enjoyed the song lol
Слушай, как насчёт рассказать как ты переехал в Австралию?
I believe I'm owed some royalties. Sunglasses McCoolguyface is _my_ thing!😡... 🤪 😎😎😎
Btw, I need that compressor in my life. Where can I get one?! If you don't know what the hell I'm talking about, just point me to your audio person. Thx in advance.
Oop is not the only way... It's just the only way that is _correct!_ again, 😂... No more, I promise.
Okay I didn't expect you to go this HARD on singing
It was so good as well!
@@Keregosh Did he use autotune?
@@emty5526 I have no idea xD
It was great! Except for the last bit
i sang along, it made my day!
Next video: How to build C++ projects and setup a git repo
(Best clip btw: th-cam.com/users/clipUgkxtL5J8amsZAOkbza7ucYwc6EDEAQEaEQ7 )
This would be so important!
would be cool
Yes
Create new repo, clone locally. Create cpp project in another directory. Then copy entire project to the repo dir. Done.
I want this
When my man played the bong cloud I knew he was a man of culture ♟️
I'm into chess and programming and now I like him even more.
Cue Hikaru bongcloud speedrun intro.
Hahaha
SVG (Vector) Images are perfect for these kind of simple textures, small filesize and scalable and supported in SDL
Hey, did you try clicking on the king and then on the rook? I think that might trigger castling, since when you clicked the king, it showed the rook square as a possible option.
We do see a MoveType CASTLE so I also guess it's there somewhere (haven't finished watching.. just noticed it now and ran to comments :P )
This annoyed me so much xD. Cherno bashing so hard on not being able to Castle. It's more an intuitive problem than it not being there. But ah well.
@@AgentM124 I think moving two squares with the King is like the fundamental principle of castling and “moving to the rook” is more of a quality of life improvement. Which is probably why Cherno’s assumption that castling didn’t exist is a valid one.
@@blablabla7796 the fundamentals of castling isn't to put your king behind the pawns in the corner and your rook into play?
@@AgentM124 the concept of castling is moving your King by two squares in one move provided the following conditions are met:…. The same as moving your pawn from the 2nd or 7th rank to the 4th or 5th which is two squares in a single move given the following conditions … respectively. The idea is that they are special moves that typically involve multiple moves but only take a single move if certain conditions are met. The king swapping positions with the rook somewhere in the middle is more of a derivative idea based on the previously mentioned underlying concept as a way to better visualize the move.
I like how he never realized that the queens and kings were placed incorrectly on the board.
Would really love to see Chess made in Hazel2D!
@@J3ekir Same lol
I too saw that.
Its not, its just turned around
@@kalilinuxuser972 king and queen need to swap or the colors need to flip
I literally thought you were trying to castle with the queen, because it was in that position 😂
As a full time C++ dev i can only say excellent code review! You pointed out all of the major pinpoints with an easy to understand explanation on why and how to improve it without going into any super advanced C++ topics that would easily confuse & overwhelm a beginner programmer. Great Job! I wish i had someone like you when i was first getting into C++.
"What are we thinking... like, Bongcloud opening, probably?"
Ah, I see you are a man of culture as well.
I'm about 8 minutes into the video and I just couldn't hold back. How did no one notice that the kings and queens starting positions are switched. I wouldn't have noticed until the Cherno started playing. Maybe you'll point that out later in the video but ahhhh.
Haha lol good point! I totally missed that! I guess I didn’t notice because it changes depending on which side you play, and I must not have paid too much attention to the colours since I was playing myself here
Actually the pieces are on the correct coordinate but the color of the squares are "wrong".
If you imagine the coordinates, White's King is on e1 and Black's King is on e8 which is correct. Changing places would make kings start on the D-file which would be completely wrong.
The rule "White's queen on white square and Black's queen on black square" can't overrule the coords, in order to make it right, black pieces should be arranged on top of that board and white pieces on the bottom of it.
@@ScorpionG4merBr I'm not quite sure I understand what you mean by the coordinates. Currently the kings are on the D file. The only problem with the layout is that the respective kings and queens need to be switched around.
I don't remember exactly my line of thought but you're right.
Sorry for the confusion.
Pretty sure he did castling but you need to drag the king to the rook..
Ya I saw the rook light up so it seemed like it was working.
I was so annoyed by this hahaha
Well, that would be misleading interface...
if he played chess often he'd realize absolutely no chess clone does that, you have to drag the king two squares (3 squares if it's on queen side) close to the rook to trigger castling
I love how he just instantly understands every function/class, tears it down and reduces the CPU usage by 99% in the first 5 mins of seeing the code 😂
I think one of the biggest design problems in the code that you missed is that they didn't decouple the rendering stuff from the actual chess engine
Exactly what I thought
Another vote for a video on build and distribution processes.
I also like how you separate your own opinions on code from actual issues in the code.
Even if you have a constant (like 8 tiles per row), it's good to use a constant instead if you're gonna type if more than once so that you know exactly what that 8 is. Maybe there are other things that also have the value 8. It will help you understand your code.
That's true a lot of the times, but in a situation where you are writing something like:
for (int row_number = 0; row_number < 8; ++row_number) {}
I think it's obvious from context what that 8 means and if you accidentally type 9 instead, you will catch that mistake immediately from context. If you write a lot of words, instead of numbers, it can infact confuse you because you need to spend some mental energy deciphering what the words mean. If you see 8, you know the loop will iterate 8 times, if you see BOARD_SIZE, you first have to find out what BOARD_SIZE is - is it 8, is it the number of pixels in one dimension, .... My point is, you have to think more when you are using words instead of numbers. In cases where it may not be clear from context what the 8 means I agree with you fully, but considering this is a chess application and the person reading your code, be it you, or someone else, is most likely not stupid, it's a reasonanble thing to assume that they know what the 8 means.
@@stewartzayat7526Literal numbers are always bad:
- Forbids you to ever change. Otherwise you'll have to go replacing the entire project.
- May mistake their meaning.
- Words can always describe better. If the number "is obvious", the word could has it: BOARD_SIZE_8.
- ~100% chance of a word typo resulting in compile error.
- C++ Core Guidelines says to always avoid them.
I just want to say, that seeing the way you go through the code, how quick you are and all looks so damn cool holy shit-
I love you videos
A video on building c++ code and about build systems will be great. The main problem for people is that they don't understand how compilation and linking works. And how to deal with libraries.
Btw loved your voice 😋
I second this so much. I really wanna learn CMake as a linux user but it's just so complicated and there are pretty much no tutorials that go beyond the very basics
YESYESYES
Yes would love to see that too!
Yes this would be very helpful
Build systems would be extremely helpful. I never understood it.
Very tough to see king and queen in the wrong place at the start of every game. Otherwise very cool project and great review!
The build system in C++ is one of the reason I moved to Rust. Everybody does things differently and it's so hard to put the pieces together. Most of the people uses CMake, so it makes things just a little bit easier, but it's always terrible =/
Backend eng here, which uses python, cpp, dotnet, and rust. I love cpp, but not the build system lol. I don't know why no one in 20+ years streamlined this process with a standard. If anyone is interested I created some cmake templates for sdl2 and sfml which can make life easier for simple game dev projects. Is a work in progress. Would love feedback, so far every template works on windows and Linux. Need to write docs for running.
autotools is also fairly common. its amazing once it gets working (it can make porting things into an almost trivial task in some cases), but having to mess with it is an absolute pain.
Lets be fair, most of the projects on github (including big name ones) have many of the same issues - missing files or the expectation that the person building the code has just the right version of python / ninja / environment paths / ceiling cat set up. A huge amount of pain and wasted time would go away if we could solve this. My steps when I'm about to put anything out public is to take a totally fresh windows/linux/mac install as necessary (a vm is fine) and then see what it takes to build / run the thing I'm putting out. I document everything needed at that point and either "fix" stupid requirements and start again or ensure all steps are clearly documented.
Docker is the way
@@sebastianmorris5394 It's really not though, I've had just as many issues trying to install docker versions of things as non docker versions. It just adds yet another layer of potentially broken stuff. The few times I have deployed something docker based that worked right away broke within a few weeks after an update was required and pushing that update either to docker or to the thing itself left it so broken I had to start back from scratch.
@@Ken-Creates you're probably not being specific enough with your yaml file container versions
@@sebastianmorris5394 Haha, I'll take that as agreement that it's not the way. 😆
@@Ken-Creates ok kiddo
when he clicked the king, the rook was highlighted, suggesting that he could've castled.
6:04 I'm pretty sure you can, it's just not standard, the rook is highlighted as a possible move
34:30 isMyPiece() or canMovePiece()
36:00 still doesn't notice you can castle :)
49:50 Or create once then copy construct the rest? (not sure what the non default copy constructor did, why it set texture to null)
18:15
Everyone chilling while cpu7 is dying
18:33
Cpu7: my suffering is finally over, i am out of event polling hell
😅
I just checked the github repo, and dude didn't even made that fix. wtf. Why send code for review and then not listen to the recommendations. 🤦♂️
Why is no one talking about the fact the King and Queen are literally in the wrong squares
2:50 why can bro sing so well
I think you can castle is you click on the rook after selecting the king.. the rook square is highlighted as a valid king move bro, you missed that
One thing to add (months later), try not to hard code values everywhere. You could calculate the division value for example as a global. So if you change the resolution, the division values also change. You could still leave them const manual updates as well, but it would be better if they were all together in one place. Not spread out through the code.
Edit: Nvm, just saw he mentions it later haha
that performance brought a tear to my eye. what a truly angelic voice. adele ain't shit.
I like how he never realised that he could castle the king because in the MoveType there was castle in the list, probably when he tried the game he couldn't castle because he had to drag the piece on top of the rook, but this is irrelevant, beautiful video!!!
It's fun to see that how you quickly tested checks, castling, checkmate and prise en-passant (and later promotion), but failed to notice that the king and queen's position were inverted in the initial position, which striked me the second I saw the board :P.
It perfectly illustates why it's so important to have experienced alpha/beta testers (and sometimes a few team advisers familiar with the game genre during the design and early development) when developping games.
he probably just didn't wanna spend too long on it, cuz he didnt test promotion, playing an actual game of chess or a few would have definitely been enough to make one realise
I remember the first chess game I programed with my dad, well he did all the programing as I was like 8. Done in BASIC on an old IBM computer with no hard drive and 5 1/4" floppies! It was written such that you gave the coordinate position of the piece, then named the piece and then gave the coordinate position that you wanted to move too and the name of the piece. I think most of you can guess the bug.
...
As long as the move was a legal move for the piece you were moving (at the start of the movie), you could rename the piece in the destination field and promote it to anything you wanted
3 move draw rule, 50 move without a capture rule, stalemate, insufficient material, draw offer, resigning, plenty of other test cases to deal with that are probably not implemented but more time consuming to test. It doesn't show the moves, who is to move or offer PGNs to clipboard which also is not great.
Did he test for discovered check?
came here for the code review and now I am waiting for your debut album.
This is so entertaining to watch as a programming student. I'm looking forward watching more code reviews 🧐
Best code review to date. Thanks!
One thing you did not say but is something I heard as a point of graphics... If you need a texture at 80*80 and you can scale the textures... Always go larger than what you want and never smaller unless you are mapping and applying to a model. With 2d at least... Always scale down or match scale. It avoids the graphics blurring.
There is a cool wiki on chess programming of situations that could happen. Like en passant and ending up in check etc. There are a few cases that are difficult. Also some cool books on using bit fields to get the best speed.
do you happen to have a link for the wiki (is it on wikipedia or something else?) im curious lol
he also needs to get rid of that class hierarchy
6:00 the initial position of the pieces is wrong! For both Black and White, the positions of King and Queen should be interchanged. White queen in the initial position is only on white square, and black queen is initially only on black square.
Another way to have things correct, would be to keep the chessboard as it is (not rotate it in any way) but put the white pieces lower, and the Black pieces upper.
This is what I'm looking for on TH-cam. Really learned a lot on writing clean codes. Thx!
His best album yet, the code review
Awesome glimpse into the mind of a professional. Thanks for the opportunity.
Please more singing, I love it 😂
Erstmal, Yan, cooles Video! I just keep learning from every video that you post. German game engine programmer here.
I've seen some versions of online chess where if you click the king, you have to click the corresponding rook, and when you clicked the king, the corresponding rook highlighted, so that would likely have worked.
Instead of using std::pair for points he could have just used SDL_Point provided by SDL. This would then also be compatible with some other SDL functions as they only accept SDL_Points.
16:26, sure. It's less energy spent, less thing to write, read and think, since it has the same syntax as a pointer or class object evaluated against bool. Plus, if the boolean variable ever changes to a pointer by design, no refactoring will be needed here.
23:45, since this pointer is not meant to move, it'd be better as a reference: safer, less typing and a visual hint.
24:53, struct has better performance than tupple, because it doesn't get members by number. Generates much less bytecode too.
30:45, literal numbers are dangerous due to this kind of thing. Right thing to do would be to has it attached to the screen size, either at compile time, like a global constant, or at runtime (in case of user being allowed to change it) as information from a class, calculated together with the screen size.
36:25, bold security flaw here: this f() should not be public. If a piece goes to a wrong place, he'll has to look the entire project for calls to it. Fix: remove it from public domain, get a compile error, mark the f() who is calling it as 'friend', and only if it's actually 1 of the few that should be allowed to change piece location.
38:40, a good design choice is to separate the character logic from its multimedia resources, so that its logic may go to the CPU caches, becoming much faster.
I'm sure you will find this out later in the video, but castling seems to be possible by dragging the king to the rook rather than the square next to it. (:
You reffer to the fact that he couldn't castle there in the first minutes of the video?
@@alexandrucalitescu5822 Yeah. I didn't have time to watch the whole thing, but i thought i would point it out anyways.
@@Katt1n Well there, he couldn't castle because you can't castle trough a line of check. The bishop was up there. And it was implemented in the game I think, it even lighted that line of check in order to show why the castle is not possible there.
@@alexandrucalitescu5822 I understand the rules of chess, for the first few minutes of the video he implied that castling was not implemented which is not true.
I need a full cover of hello right now Cherno!
Also the starting position is wrong. The Queens always go at their color. D1 and D8 respectively for white and black queens.
One thing ive noticed is that the board is not actually reversed when the white is on the top side, it's always the white king should be on a black square.
Small tip: You shouldn't have 12 different images for each piece, instead put it all in one file and then crop that image accordingly. That's exactly how I made playing card games, imagine having to load 52 images separately lmao
You load them only once per game so it shouldn't matter.
@@smrtfasizmu6161 I'm just saying it's good practice to do that. What if it was 100 images?
52 images??? Bruh it's 4 suits, 13 cards (so 13 ranks, 3 face cards, that's 20 image assets.
Like if you're concerned about space and loading, make the images from those 20 components.
Cache the cards as needed, 52 generated assets using 20 image assets.
I wish you would make a video on how to build and distribute it, Because I too am having problems with that part of it...
At 44 mins, i would even use std::array. Just use Point[] = {the squares}. Use array length macro which uses sizeof to iterate over them.
With the Hello Again song you literally had me crying LOOOOL
Cherno lowkey flexing his singing skills 😂
great singing
the singing was so on point i didn't realize it was you
I wouldn't call cleanup in the destructor. It could throw exceptions and you should never have a destructor that throws
that cpu core usage hack was cool !
I wouldn't put any magic numbers in my code. They all should be a constant so it's clear, even that 8. But good job! :)
When I made a chess program, I didn't instruct the computer to find all the legal moves in a position, instead I instructed it to wait for the player to make a move and then check whether the move is legal or not. I instructed the computer to make 4 checks to see whether a move is legal or not:
1) Natural Movement of a piece (for instance you want to move a bishop, computer checks whether you are trying to move the bishop diagonally or not. If you try to move a rook, then computer checks whether you are trying to move the rook horizontally/ vertically or not, if you are trying to move the knight computer checks whether you are trying to move it in L shape or not etc.)
2) Whether you are capturing your own piece
3) Whether you are jumping over a piece
4) Whether you are in check after your move (in chess you must not be in check after your own move).
If those 4 checks are done then the move is legal and computer changes the location of the chess piece. If the move is illegal, then computer does nothing.
If you want to creat visualisation's of possible moves as in this example you need to check before the move.
King is placed wrongly. Black king needs to be in a white square, e8, not d8. White king needs to be in black square, e1, not d1
Just an fyi, but you cannot castle out of check. That’s why you weren’t able to castle in the first game as black
That singing was pretty good actually
Honestly lack of castling isn't much of an issue, lack of en passant on the other hand would be absolutely barbaric
LOL that Hello rendition was bomb
The comment on "Initialize the field", I think it is a word-to-word translation from German. It would translate to "Intizialisiere das Feld" with "Feld" = "field" and means in this context the chessboard.
So I guess it's just an unlucky translation on his part, I think he meant "Initialize the board" or something.
31:19 didn't expect to see cyrillic here
He speaks Russian
First of all, the positioning of king and queen is reversed. Next, 6:04 you can castle. You had to release the mouse button on rook instead. See the blue background behind the rook.
I am new to opengl and GLFW so the first project that I have done in C with openGL was a tic tac toe project, I put nothing on the heap there. OK, tic tac toe is a lot simpler than a chess app (I have made a chess app in Java), but I think that even if I were to make a chess app in C with opengl and GLFW I could just initialize all the objects for chess pieces in the function which contains the main loop (while (! glfwWindowShouldClose(window) loop) and I would put all the chess piece objects on the stack frame of this function which runs in its own thread (I would put it in a different thread). The stack frame of this function lasts as long as the window lasts. I don't think I need to put anything on the heap whatsoever. Even when dealing with promotion when a pawn becomes a queen/rook/knight/Bishop, I can still put that queen on the stack frame of the promotion function and then use memcpy to copy it into where the pawn used to be and that should work correctly as long as the size of the pawn object is bigger or equal to the size of the queen/rook/knight/Bishop object.
My variables on the stack will last as long as the window of their chess board exists, their stack frame will exist as long as the main loop of their chess game exists, only after the main loop of that chess game is over, that function is over and leaveq CPU instruction is executed (leaveq is basically the same as add numberOfBytesInThisStackFrame, %rsp and then pop %rbp, I am talking about x86-64 architecture here)
the stack frame of that function no longer exists.
Thanks for an interesting code review, and nice polling fix!
From a readability standpoint (and to some degree safety) I would have expected more feedback on the code duplication -- I like what you did for the knight moves loop, but would have taken it further, e.g. all of the x + dx > 7 && ... that is, you should do that once (x_new = x + dx) (even better with a point class) and then (OnBoard(p) && Free(p)).
I think a bit more of an investigation into how you want to handle the polymorphism would have been interesting too.
5:41 WAIT!!
The board is set up incorrectly.
Kings and queens are on the wrong squares.
YES, please make the video on how to properly make a project on git.
2:08
YES YES YES PLZZZZZ SHOW US HOW TO BUILD PROJECTS AND DISTRIBUTE THEM PLZZZZZZZZ
I would be super interested in a video about distributing c++ code, getting it to build. This has been my biggest pain point with getting into c++.
A video on how to make a project would be really useful honestly
Thank you Cherno. I learned a lot.
I noticed the queen is on the E file when playing as black. They didn't flip where the pieces are drawn to the screen based on if you are playing as white or black
Lol the singing at 2:27 was so cool and funny
I would love to see a video on building C++ code :)
2:27 That was amazing!
When I'm writing code I like to use the principle of "least astonishment" when writing my comments.
Great review and great Code overall!
I want to deepen my C/C++ knowledge and was wondering about the tuple thing. I really like to use tuples (C#) when readability is given since you can name your tuple values, kinda sad about that
Can we take a second to appreciate how good this code is for a beginner? Sure, there are some things to be improved, but this is quite remarkable. Well done!
using SDL properly the RAII way is slightly more difficult. I do it that way :
std::unique_ptr window {nullptr, &SDL_DestroyWindow};
std::unique_ptr renderer {nullptr, &SDL_DestroyRenderer};
OMG, the singing!! 😂😂 Epic!
He did do castling, you just have to drag the king to the rook.
no chess game castles this way, so he has to fix it anyway
@@felipegomes6312 That one does.
"Quelle" also means source, which kind of makes sense for the water thing too since thats where the water comes from
Isn't there a callback system from every event. instead of polling, coz at once there is going to only one event. but that could be slow depending upon how the library handles callback. besides one more optimizations that can be done is to calculate all possible moves once and make a set of them and later update them depending upon actual moves made. not a huge deal but it will save time when user is clicking all over the place.
Shocking switch language to cyrilic 31:18
What a coincidence, I was looking for graphical libraries to make a chess game right as this video dropped.
bro were you in a band? If that wasn't autotuned your voice is incredible. would def want to hear you release a single or something
Subscribed after the singing🤣🤣🤣. I love this channel already
Hey Cherno, please make a video dedicated to a polymorphism in cpp.
Castle by clicking on the rook, not on the square next to it (in online chess usually both should work)
Also, the king and the queen. For godness sake. Srsly?
Nice project though
1:58 when a C++ developers realizes that *no one, literally no one finds C++ building intuitive and understandable,* but acts annoyed and states that is everyone else's fault.
EDIT : 2:55 "I tried to add SDL2, messed up everything" -- _every SDL C++ project ever_ for anyone who hasn't been a C++ programmer for at least 2 years.
Why do you have a Cyrillic keyboard? Where are you from? :)
I remember 3 months ago watching this and not being able to understand a lot of things, now I watched this and I understood almost everything Cherno said in this video. I studied Java and C in the meantime, I guess that helped for C++ understanding as well. Still, it is not quite clear to me what tulp is and some usages of :: I don't understand.
I even get the frustration of cherno that this guy didn't put more things on the stack, if I were making a chess game in C I would put everything on the stack, I don't think I would call malloc even once. As long as I put my chess pieces in the same stack frame where the game loop is, I will not get a seg fault, because that stack frame is alive as long as the game is alive.
@ ~2:10 (the line about making a vid on building cpp progs)
More like a series. If you're wondering why nobody (new to cpp) understands it, take a look at the autotools documentation and you'll begin to understand.
Quelle is also a source for the gospels according to some theories