 So I made a game Let me change something. Um, yeah, I made a game with SDL to with C Oh, the get hub is in the description. You can compile it yourself anyways Yeah, it was pretty much. Oh, yeah, I'd like to I want to think to get soding or soding I don't know how to pronounce his name. He made it weird probably on purpose. Anyways He made he made a video where he made tic-tac-toe and see with SDL to So his project his code and his video. They were really helpful. They made this a lot Faster it allowed me to do it a lot faster. I basically copy the code structure and just how to get SDL working Okay With that said I'm just gonna get into it So I'm first I'm gonna start by showing you how what the game is how it works, and then I'll go over the code and How it achieves what I needed to So I'll start with the game. Yeah Basically three years ago in eighth grade There's nothing to do and we couldn't use our phones or anything so we had to Play tic-tac-toe and I got sick of it and I wanted to make some sort of game to replace it so it's a And I really like it. It's a two. It's playing on a four by four grid and You have you have two players the goal of the game is to get As much as your piece more or more of your piece than the other that your opponent's piece on the board I don't know if I explained myself Well, but let's say you have three X's by the end and the other player only has two the X wins so That the strap the strategic part is that you can only place you can only place Your next piece adjacent to the one you previously played so let's say X goes here and let's do all goes here So X, you know it can all it now can only place Right here and oh Now that it X's turn X thinks it's gonna be clever and it's gonna use the neutral piece Neutral piece is the strategic piece that both players can control and when they put it down when they use it They lose their turn so Now oh, let's say oh wants to use the piece to put it over here X goes down here. Oh goes here X goes here. Oh pretty much is stuck and Decides to put a square right here X realizes what always trying to do puts the square back here So this is the path of the square Okay, oh goes here X goes up here. Oh goes here X no oh no longer has any moves to make so it's always X's turn until the end so That's the path X took that's a path O took X is clearly the winner So now let's go to how I did it in my game Let me Set this back up Run it. Oh my god. I kept the weird thing Kept the weird resolution Let me change it back here. Oh, yeah, it's just a matter of Recompiling it. Okay, so instead of X. I made it Red and instead of oh, it's blue and the neutral piece is great So blue will go over here, then red will block it here blue will go over here or we'll go here. Oh Oh And red wins when you know you win a player wins It turns into the color the whole screen turns into the color of that player if oh If all wins if blue wins it turns blue and if it's a tie, it's great Okay So now let's go Over the code one interesting thing if any of you watching is really if anyone watching is really smart please try to solve the game Come up with some sort of like algorithm the best play I guess that's all me. Anyway, let's go to the code First let's go to main dot C and I'm going to explain Everything here So basically The code structure. Yeah, let's go to the code structure first Main dot C That initializes SDL and handles user input Then it sends that user input to logic dot C which alters the state of which is game.h game.h contains the game state Logic C alters it and rendering reads game the game state and renders the screen based on what the game state is pretty simple The way I probably would have done it would be have just one massive C file, so props TS Odin because I didn't exactly know how to use header files and stuff like that before so yeah one another thing that I learned Okay, let's head back here So again main dot C Initializes initializes SDL and handles user input, so let's go over what that looks like First you do SDL in it That's what you will always have to do when every time you start an SDL project you want to initialize SDL and you want to have this It does if it does not return zero because a lot of the when a program turns zero it means it's successful So if it doesn't it means there's some sort of error so you print out error and Exit failure Then we initialize the window This creates a window, but we call it block 8 block 8 This is where we put it the position we center it we put it out We center it here Here I'm gonna add some tabs Okay, okay, so it's named block aid again This is its exposition at When SDL when pause center That's just the center of the screen The same for for the Y Then the screen width and screen height they are defined in game dot H and I think this shows make sure the window is shown I'm not sure not too sure what this means Okay, then this again returns an error if it cannot create the window Okay, next it creates render render is like this It's this weird sort of object. It's this abstraction, which is basically what is being drawn on the screen So let's look at this at its flags. It's gonna be drawn to window Negative one it specifies that it's gonna it's just says Use the GPU drivers somewhere in this screen Automatically don't mess with don't mess with this if you are not like really if you don't know what you're doing Basically again black magic this I Don't know what render or accelerators. It means exactly, but I know this You know just activates v-sync If there is an error and render does not work destroy the window return sd return of failure. Okay, let's continue this Declare the game state at the beginning We'll go over that later now This while loop is the important part of this is the first these first parts. They're just set up So we get SDL event II And he is a variable where we're storing the game events, you know what we're What buttons we click will what things are we doing? So it's on next we go to a while loop which When game dot state does not equal quit state. It's just like if you haven't quit the window then Continue to the rest so Squid state is you know defining game dot H. Anyways so if you do not quit it Sends mouse button down To if it's mouse button down it does this function, which is a function in logic And it sends it the game state The Y position divided by cell height The X position divided by cell width and the type of button you send so whether it's like right click left click or you know Like the scroll click Okay, this sets the background to black It clears For double buffering. I sort of forgot this runs the render the The rendering dot see it runs that Sends sends it the render object and the game state To take it as a con yeah takes it as a constant and then I think this just presents it to the screen I'm gonna go get some water to drink my mouth is getting dry Okay Again, so let's go to game state now. I think I said yeah The background is black makes rendering visible and double buffering It sends sends this to since the game state and The object render render to rendering dot see and then presents it presents rendering to the screen Not entirely sure about that last part I'm just basing it on the name Okay, so let's get a game state What we're gonna need for the game this is these macros are just stuff If and for So and that's the number of rows and how high it is and stuff So if so right now we're just declaring it as for we can change it to five And we compile it And the game still works again like I I made it so that the gray limit is The same as the number of rows so now this time it's five As you can see we can't enter anything here. So Wins, okay works as normal. This is the screen resolution screen width screen height So we can make it something wonky like Six hundred by eight hundred like I did before Compile and now it's all wonky So I think it works best on a four by four grid three is too simple for five is Makes it too long for is perfect Especially when you're doing this on pen and paper which I Recommend is better. This is just a See if I can use sdl. Okay, so cell width That's just the screen width divided by the number of rows and stuff same goes for height If we're empty, okay now these this these set of macros This is what is going to be inside of the board the board is a two-dimensional array Made of n with the size of n on each side So this is what's going to be inside of the board these two also define the player turn So empty is empty red is red blue is blue gray is gray That's what's going to be inside the board This is the the gray limit, you know the amount of grays that you can put Again grays neutral piece. I don't know if I yeah, I did clarify that sorry This is the game state whether it's running whether red wins or the blue wins whether it's a tie or whether you have quit And quit is not used in rendering It's just used in the main to make sure to like if you quit the game or not, okay? So now this is the game structure, which is what's actually going to be affected by logic by the logic C so Again our board is a two-dimensional array Our player well, that's that's basically the turn we're in so yeah, it's either going to be red or it's going to be blue one or two Our previous previously played This is going to be used for the adjacency things so it's a two it's a It's a integer ray of size two The first one it's going to be its x-axis so index zero it's x-axis index one. It's y-axis Go same goes for blue and gray the red counter You have how many how many reds you have placed The blue counter how many blues you have placed and the same for great Okay, red moves the amount of red moves you have blue moves the amount of blue moves You have gray moves the amount of gray. I think gray might be redundant But I don't feel like refactoring it just to fix that small thing which is not really an issue and the game state that is You know going to be these five Let's go to Let's go to rendering Okay This function up here. That's the one that's called by main so So first we initialize our game over screen and if the state is Running then we do render running state, but if it's not this is when we're gonna When we're gonna show our game over screen if they if it's the case that red one Game over screen is red if it's blue game over screen is blue if it's a tie. It's great Okay, now for the real part the rendering running state so We first declare the size of the player piece then in the Then this checks inside of the board So it's gonna check the index of the array as a way to multiply as a as a as the position For where you're gonna put the rectangle or a square it's gonna Decide the color based on what's inside of the board So if inside the board is right and the color is gonna be red blue blue gray gray, and if it's empty It's gonna be black It doesn't it only needs this for restarting purposes So yeah Then it renders the grid on top of all of that the grid is Gonna be white so and it renders the line. This is the starting point. This is for the Vertical I think yeah, this is the vertical line. It starts from cell width We're deciding the X the X and Y the X is you know the cell width times the first part, you know It starts at I one So then zero Does the same here for this time the screen width You know so it goes across the whole screen or And this is for the horizontal does the same okay So pretty basic This is a constant takes it takes a game It takes the game structure as a constant cuz it should not because this should not change anything Okay, now for the real part for the logic. I imported STD Live so I can get the absolute value function Okay now one So this is a reset part Ah We go over the simple stuff first if we are not in the running state If it's blue if red blue if it's a red blue or Thai state Then it resets the board if it's a quit state It doesn't matter because the whole thing's gonna be shut down anyways, so This right here sets everything inside of the board array to zero this here Resets the player turn to red Set these three set the counters for all back to zero changes the moves back to one by the way The moves are either gonna be zero or one or two sometimes Because we just need them to be more than zero in order to make sure you know the turns continue so Yeah, and then it Sets the state the game state back to running For those who are not that familiar this arrow thing is basically dereferent or referencing the game structure Okay so If the game is running then it does player turn Again this one it sends the game the game state the row the column and the button as parameters So what it first does is it checks it runs do when no moves Do when no moves? It's this thing that gave me a lot of trouble Because You see this right here This actually should be on top But as you can see I'm gonna put it on top. It was at the bottom for debugging purposes It was a really stupid bug took too many hours of my life this Instead of being blue it was red and it was messing everything up Anyways, so it ran player turn so What that does first it checks where you if first it checks this which will you know automatically Change your turn based on Whether or not you have Whether blue or red have moves and it will but first it will check, you know, if anyone has one Okay So next it checks if you if the place you clicked is empty So if if it is not returns and you go back up here Next it checks what button you clicked it checks if you click if you right-click if you did right-click You do you do gray and it checks if the place It checks if available it also checks if the gray counter is Zero if it is or if it's not zero if it's less than zero, sorry, so If it's less than zero you can continue, but it also checks around if it's available This is a common function that you do for all of them So yeah, let's go to check if available what it does if the counter is zero Which means you have nothing on the board not none of that piece is on the board then you return true And it returns if the column minus the previous place of that piece Which is the parameters one of the parameters you're sending if it's greater than or equal to one Sorry And the same for the row if it's greater than or equal to one This is just if it's adjacent it returns true If not if none of these are true then it returns false which is which will not allow you to continue okay next it Set the previous and no it puts gray in that column in row on the 2d array Then it sets the previous plate gray to the column the previous played Row to the row and then it switches turns Here it checks if if it's not If it's not a right click, so let's it's a left click or a mouse button like a middle click Just isn't so long if it's not a right click then it will Turn to Sorry It'll check around the previous play Which is this we get that as a parameter then Again, it'll update update the board Change the colors change the column in row add to the counter and change to the turn Same goes for blue and now let's go to the do when no moves thing function so For anyone who saw do when no moves First runs this check moves Function so check moves that Provide some little counter measures for check around so for For this it checks for It checks if the counters not counters if the pieces you're checking are not zero Just you know if they are zero it'll stay it'll stay one because in the beginning it was one If it is not zero then it checks around for the gray piece It checks if it is not the limit if it is the limit then it's zero Like the counter sorry if the counter is the limit Okay next a dust check around check around function has this monstrous huge If statement and I can't even do it the whole screen, but I can't tell you what it does mean this first section is Checking if what you're doing is out of bounds Well, let's go to Okay, first let me describe what this does so let me Turn this off and go here so let's say you are here and You have previously and you previously placed X is right here This way no for making it better. Let's say it's right here. Okay What the check moves does is that in the array it will check around So it'll go here It'll check here here here here here and here the first part of The function is making sure you're not checking out of bounds so What I it first starts with a for loop it goes to this position where it is here and Then it goes here It goes here here It's an Esther for loop. Okay, and It checks if it's not out of bounds Then what it does is it makes sure where you are checking It is empty if it is empty and you're not checking out of bounds Then you return one if it is not empty or If none of this is true, and you never return one then it returns you Okay Then Once you're done with all of these our red moves becomes red moves plus gray moves same goes for blue moves Then do when no moves again checks Checks the moves the amount of red moves left and the amount of blue moves left or you know checks if it's above zero Based on that It adds the two that's it adds red moves and blue moves And if it's equal to zero then we know they have no moves left and you continue This automatically switches the turns as previously stated So Yeah, that's it. I want to end off with You know just some advice on how to learn programming You know based on my experience on on learning programming because right now At best I consider myself an intermediate at sea so Yeah, the best way to learn programming that I found is to do your own projects to find a cool little thing to do and And try to put the components together because it also teaches you how to problem solve so try to put the components together and figure out how and Learn as you go learn as you go to doing the concepts. I've learned the most about sea with my Wordle project I've learned a lot about reading files and Arrays I've learned from SDL. I've learned the most out of just analyzing a T. S. Odin's project Which is just basically like the most I've learned of that's the uh, but pretty basic stuff, right? Just uh You know just learning how to start and drawing drawing a squares and lines He did more complicated shape drawing stuff, but I it doesn't really matter what what shape I drew Anyways Yeah, so make your own projects. I will say Another general sea concept that I did learn with this project was two-dimensional rays. I learned more about two-dimensional rays and Pointing to structs I still don't know the accurate The accurate terms for referencing and de-referencing and what does what but I do know how how to use it and how it works that's a Hope you enjoyed and and most of all I hope you learned something