Can I Code Minesweeper in One Hour

The Development Process of a Game: A Coding Challenge Experience

Honestly, I'm pretty happy with this so far. We need a way just to reset when we win, and if I implement that, then I think like the core functionality of the game is pretty much finished, and we would just need to add some small touches. Um, yeah, that's and we need a timer too to keep track of how much time, so let's quickly, I think I have enough time to do a timer. So, let's say timer or start underscore time is equal to time.time and that's going to be as soon as you click. We'll say start time is equal to zero for now, and then as soon as we do our first click, so if clicks equals zero or field row i hate the way that I've done this here; it's not going to be clean but that's fine.

Start time equals time.time. Okay, we'll say current time is equal to time.time minus the start time, and then when we draw, we'll pass that as well. So, we'll pass current time and current time. Let's go to draw and let's take in current underscore time. Let's get a time font going quickly okay; time underscore font uh i guess we'll do this just like 50 or something. And then we're gonna go to draw. Okay, up here I guess we can just draw it down below; we will say time text is equal to and this will be the time font dot render, and this will be an f string of time elapsed colon round current time. Okay.

And that's good. Then we'll say win dot blitz and this will be time text and then we will put this we could just hard code a position for now something like it's going to be the height of the screen minus the time; the height of the screen minus the width of the screen because that's what's going to be the bottom over 2 and then we're going to subtract from that the time text dot get underscore height over 2 for the x position though this we can just put at like 10 or something okay. Let's try that and let's see if I mess that up okay; time is not defined, time text i need to import time.

Okay, i think i'm just about getting to my time limit too, i didn't start this exactly at an hour so it's a little bit messed up; time font.render let's just fix that quickly okay we can just go one and black. I would like some variables for these but that's fine; run one more time at least three arguments what did I what am I messing this up with? Oh, I passed that here okay come on let's get a good run in here while we end where's the time; it's not showing up uh come on uh windup lit time text the height minus the width over two minus maybe plus is what we're looking for there uh no okay let's just let's just hard code this so 10 and then this can just go to like 650.

What did we what did we make the height 700 so let's just do 700 and actually no come on this will work; height minus the time text dot get underscore height over two all right run time elapsed okay let's move that over height uh no minus just this okay there we go that's fine and that's giving me a massive number why does that give me such a big number? Okay, come on let's figure this out quickly we have current time ah it's the other yeah time to time minus start time oh it's because I haven't clicked yet ah okay that's that makes sense um damn we should only be doing that if we have a start time if start time is greater than zero else current time is equal to zero.

Okay, let's run and there we go; let's click ah okay i damn it i need to fix that okay uh come on run one more time there we go oh and it's showing bombs okay guys well I've clearly messed something up um yeah we'll I'll have to look at this in another video but that was my coding challenge I would say this is probably a fail in terms of completing the game but I was able to whip up what is it here uh almost 220 lines of code not fully functioning I was a little ambitious with how much I'd be able to get done but we could continue this in another coding challenge in the future if you guys would like to see that I wouldn't mind doing another video and kind of wrapping up this project please let me know in the comments down below also ditch it and work on something else if you guys would find that more interesting hopefully this was insightful hopefully this was a cool video if you guys enjoyed make sure to leave a like subscribe the channel I will see you in another one.

"WEBVTTKind: captionsLanguage: enhello everybody and welcome to a new style video on this channel where i'm going to be doing a live coding challenge i'm planning on doing a few more of these in the future so if you guys have some ideas for projects i can work on then leave them in the comments however for this video i'm going to be trying to create minesweeper in python using pygame as quickly as i can now i'm going to give myself a maximum time limit of 1 hour i think that's a good amount of time i should be able to come up with a functional game here and the point of this video is to kind of give you a taste of what programming actually looks like i do have some live coding on this channel but a lot of times you guys watch my tutorials uh and i already have the code pre-written in those tutorials right so everything's kind of perfect there's not too many mistakes however when i come up with that code it takes me a really long time i'm constantly messing up and that's the reality of programming so i'm hoping this video will be entertaining as well as pretty relatable for you guys uh because i will be messing up quite a bit and i'm sure there will be tons of errors and bugs and me banging my head against the desk anyways with that said that is what we're going to do in this video i do want to shout out web dev simplified for the uh kind of inspiration for this video idea i was looking at his channel the other day and saw he was doing some coding challenges related to kind of front-end development and i figured hey why don't i try that with python so kyle thank you very much for the idea hope you don't mind that i'm stealing it with that said let's head over to vs code here i'll give you a quick demo of what minesweeper is in case you're unfamiliar then we'll start the timer and i'll start trying to build this game out alright so i'm on the computer now and i'm just going to give you a quick demo of mine sweeper in case you haven't seen before just pulled one up on google here and the goal here is to sweep all of the mines and essentially clear all of the squares without hitting a bomb so what i can do here is press down it's going to clear a large portion of the screen and all of the numbers you see are telling me how many bombs are adjacent to this square so adjacent being either diagonal or to the left up right or down so when you see something like 1 that means there's one bomb in the neighborhood of this square three three bombs so on and so forth and if you want to place a flag that means you're kind of denoting that this square you think it's a bomb so i could do something like that and now if i continue to kind of clear out the rest of the screen this flag will kind of be protected right i won't accidentally hit this bomb because i have a flag here hopefully that makes a bit of sense but the point is to go through clear the entire screen don't hit any bombs if you hit a bomb then you lose and you have to restart a pretty famous game i'm sure most of you are familiar with it so let's try to build that out all right last thing i will mention here i do have a programming course if you guys are interested in getting better at programming specifically python and go check it out it's called programming expert i'll leave it in the description i spent countless hours working on it so if you want to get better at programming seriously it's a great resource highly recommend link in the description you can also use discount code tim alright so let's start the timer right now at this point i'm not going to cut the video so if i make any mistakes then you guys will see them live here so import pie game let's say width uh if we could spell that correctly comma height and let's go with a width of 700 and a height of 800. we'll make the height slightly larger so we can put a menu there we want a surface or a window so i'm going to say actually let's do it in lowercases when is equal to pygame.display.set underscore mode and let's set our width and our height uh let's go with a caption so pygame dot display dot set underscore caption and this will be mine sweeper like that okay now let's just code out the basic kind of starting script here so if name equals main we'll call a function called main we can define main i guess we could take a window in here as a variable so we'll take in win although i'm going to just treat these as global variables i think i'm not going to worry too much about having the best practice here because we're trying to do this quickly okay so inside of here we'll say run is equal to true we'll say while run say for event in pygame.event.get if event.type is equal to pygame.quit then we want to say run is equal to false break and then outside of here we can say hi game dot quit like that okay now we should also say hi game.init up here okay we have our width our height our window and now we want to draw some stuff out so i'm going to say define draw we'll take in a window and again i don't necessarily need to take that but let's do that for now let's have our background color let's make the background for now just equal to white i guess so we'll go with white we'll say win dot fill bg color and then pie game dot display dot update like that and then here we need to draw so let's say draw and let's pass our window like that and i just called it win okay so now if i run this we should get a screen showing up let's see and there we go okay and then if i hit x we quit perfect that's what i wanted for now now let's do the grid so let's say define create underscore grid let's go rows calls like that okay pass for the grid we want to have the underlying grid and the grid on top of it i guess let's call this create mine field because then creating the other grid will be really easy so we need to know the number of rows and calls so i'll say rows comma calls i don't know how many you usually have in mind sweeper but we can start with like 30 by 30 or something like that and then the number of mines i guess we'll go with something like 15 for now so we want rows calls and mines and then inside of here we need to randomly put a bunch of mines inside of a 2d array and then we need to go through the 2d array and figure out what number we're going to have for every single square or if the number is going to be 0 and we're going to have an empty tile okay so let's do that so let's create a 2d array here we can say our field is equal to and this will be a list comprehension and we'll say zero underscore or zero for underscore in range and then this will be calls for underscore in range rows okay so that will give us a 2d array that's the size of the field uh then we want to randomly pick positions for our minds so say mine positions is equal to we'll make this a set we'll randomly select number of mines positions make sure that they're unique uh and then we will set them in here and then go through the fields so let's do that we're gonna say four underscore in range and this is going to be mines we're going to say random dot rand range and we need to select a random row and column so row is equal to this and this will be 0 and then the number of rows and then we'll say call is equal to random rand range okay position will then be row call as a tuple and we'll say if pause in mind positions let's actually change this to a while loop here so we're going to say wow minds created is less than mines okay and then we'll have minds created is equal to zero so if the position is in the mind's position that we need to do this again because we already selected that position otherwise we'll add it so we'll say minds underscore positions dot add pause and i realize we don't need this we could just use the line of the set so let's say len of minds and then positions and i realize uh do we want to call this mind position so that probably reads a bit better so let's go with that mind positions all right i don't know how good my pace is here because how long has been five minutes uh okay that's not bad so far so my positions dot add pause okay once we add that position uh we can go ahead and just set it in the field so now that we've done that let's say field at and then i guess we already have row call so we can say field at row call is equal to and then for a mine i guess we'll go with the string x for now although maybe we'll do something like negative one and we'll just use numbers so negative one will be a mine zero will be nothing and then all the positive integers will represent the correct value okay now that we have all of that uh this may not be the most efficient way to do this but i'm just gonna loop through check the neighbors of every single position and essentially set those although now that i'm here i'm thinking that when i add a mine i can probably increment all the neighbors in the field and do it all in this one while loop so let's do that but i need a function here called get underscore neighbors is that how you spell neighbors or is it ors there you go okay so let's take in a row a call rows and calls and let's generate all of the eight valid neighbors for a specific square so we're going to say if row is greater than zero yeah if rho is greater than zero let's make a list here neighbors equal to a list if rho is greater than zero then neighbors dot appends and this will be row minus one call okay we'll say if row is less than the length of rows minus one i think that should be fine then we will add the row that is below so row plus one call okay so this is going to be up just so i don't confuse myself here this is down let's do left and right and then let's do diagonals so let's say if the call is greater than 0 then this is going to to the left then neighbors dot append and this will be row call minus one okay autocomplete give it to me here perfect then neighbors dot append row call plus one okay continuing let's do the diagonals so if the row is greater than zero and the call is greater than zero i believe this is going to be the top left neighbor and then we're going to say neighbors dot append and this will be row minus one call minus one okay if the row is less than the line of rows minus one and yeah i guess we can just do top right neighbor i think that's the one here neighbors dot append and then this will be row plus one call plus one okay and then we'll say if the row is less than that len of rows minus one and the call is greater than zero then neighbors dot append row and this will be plus one call minus one okay and then if the row is greater than zero and call is less than the line of calls minus one then we will add our final neighbor uh row minus one call plus one okay let's look at this we have row minus one call minus one row plus one call plus one row plus one column minus one row minus one call plus one okay nice return the neighbors and that works for me okay so now that we have all the valid neighbors whenever we do this here we add a bomb for every single one of the neighbors let's increment their value in the field if it's not a bomb to b plus one and that actually tells me here that i'm not going to be able to do it this way because if i do have two bombs that are beside each other i think that's valid to happen in mind sweeper not super familiar but if there's two beside each other that's not going to work so we're going to have to do it this way we're going to say for mine in mind positions we're going to say neighbors is equal to get neighbors and then we'll pass an asterix mine to decompose that as well as our rows and calls and we'll say for neighbor in neighbors and we can say for rc because we want to get the row and the column and yeah i think that'll be fine then we will say field at r c plus equals one okay then we will return the field and i think that should be good for the field i'm going to zoom out a bit even though i know it's going to be harder for you guys to read sorry about that okay so now that we have that let's just test the get create minefield function so let's start here and have field is equal to create minefield this is going to be rows calls and minds i'm just putting these in all capitals at the top so it's really easy for me to quickly change them by the way rather than just defining them here and maybe not knowing what they are and let's run this but i want to print out the field i just have a look at this before i start trying to draw it okay so int has no length len oops i keep doing that with len of rows just needs to be calls uh okay kind of a silly error there but fix that quickly okay i think that's good let's rerun and let's have a look here okay one one one one negative one i think i might need to either add more mines or make this a bit smaller so that's kind of hard to read so let's just import something here import uh from p print import pretty printer okay printer is equal to pretty printer and let's go here printer.p print okay run that one more time that should be that didn't actually make it any easier uh okay i think i might not have enough minds because it doesn't look like it's correct although i can try drawing it which is what we're going to have to do next anyways but for now let's add a few more mines because oh 30 by 30 is kind of massive right like let's go maybe 15 by 15 and then we have i guess 15 times more squares than mine so let's run this now and let's see if we're getting any numbers that are two okay nice we have some twos i just wanted to make sure we're getting some larger numbers here before we continued okay so now we want to draw that out so let's go to draw here and let's take in the field and let's take in i guess the cover field because now that we have the field i also want to have a two dimensional right here that's the exact same but that's representing which ones i've clicked on so we'll just say cover field is equal to that and zero i guess will represent we haven't clicked on or something okay but we have field and cover field for now let's figure out how we just draw the field then we can draw on top i guess so let's do the following okay so we need to figure out how many squares we're going to have on our screen and how large those are going to be that's going to be based on the rows and the calls uh okay so let's take let's say size is equal to and this is going to be the width integer divided by and then we'll go with the number of rows and that'll be how large each square is then we want to draw each square we want to draw a number in the middle we're going to need a font to do that so let's just go with num font and this will be equal to pi game dot font dot sys font uh not arial you guys already know we're going with comic sans here and that can be 20 let's say num colors is equal to and then let's just set what color we want each one to be so one can be black two can be green three can be red four we can have up to eight so i kind of have to pick a lot of different colors here okay let's go six is equal to purple seven is equal to what else can i even do black green red orange yellow uh do we have a blue okay let's go with blue and i guess i could go with like a pink or something so eight pink these are just the colors that where i'm gonna draw the text in by the way okay so now we have num colors num font uh because i'm gonna use those in one second so let's say 4i comma row in enumerate field let's say for j comma value in enumerate and this is going to be row then we want to get the value and we're going to draw that so we're going to say uh we need to render some text so text is going to be equal to and this will be what i call it the num underscore font dot render we're going to render the string version of the value we're going to do this anti-aliasing 1 and then the color will be my num underscore colors at the index of the value okay then we're going to say when dot blit and we're going to draw this text value at some position calculated by i j which i'll do in one second now the top left is where i'm going to start here so this should be relatively easy to do but let's say here that the y position is going to be equal to size multiplied by i and the x position is going to be equal to size multiplied by j okay and then we should be able to just split this at x y i'll have to do some more calculations though based on the size of the square i also want to draw a rectangle so let's draw the rectangle first actually and we'll go pygame.draw.rect we'll draw this on the window we need the x y and then the rex we'll say x y actually sorry we need the color the color of this let's make a variable wrecked color is equal to for now i suppose we can go with white okay and let's actually let's go with a gray i always forget if it's g-r-a-y or e-y but i think actually let's just go with rgb 128 128 128 okay so this is now going to be what did i call that variable rect color okay erect color x y and then this is going to be size size because we're drawing a square and then i also want to have an outline for this so i'm going to draw another rectangle except the rect color here is just going to be black and then we're going to make the outline thickness of two so we'll draw actually i guess we'll draw that on top of this uh that way we're not getting a square that's too large okay so now that should be good we just need to fix this so now we're going in the center of where we currently are so we're going to say x plus and then this is going to be text dot get underscore width over 2 and actually this is going to be x plus size over 2 minus text i'll get under square width over 2 just trying to center vertically and horizontally we're gonna do the same thing with y y plus size over two minus text dot get underscore height and over two in case you're unfamiliar that's how you center the text at least that's how i'm going to do it and i think we should now be drawing that so when we go draw let's pass the field let's also pass the cover field and let's see what this looks like right now rose is not defined okay that's down here uh yes we need to have these in all capitals so rows and calls okay rerun uh key error zero forgot about that zero is going to be no color and we don't actually want to draw text for zero so i should have figured that out but let's say if if value does not equal zero great okay let's try this now key error negative one ah that's a bomb so if value is greater than zero there you go because again i don't want to draw the bombs okay oh nice that was i'm shocked that i actually got that first try okay so we're a little bit off here i think that's a rounding error so i'll try to fix that but that looks good right now now we need to cover the tiles change the colors and stuff like that i think i'm about 20 minutes in yeah i mean i have a lot of the hard stuff done but there's still a lot to do one third of the way i think i have a decent chance here to finish this but we'll see okay so window blit text x2 um what was the issue oh my size was a little bit off can we do one division sign and see if that gives it to us a little bit better there we go i guess we can draw with with decimals that's fine okay there is a little bit of a white gap here the way i'm drawing this but for now i think that's fine and now we want to do something where we are going to not show those numbers if they're covered i also don't want those to be so gray let's make this closer to a white so like 200 200 200 and then we'll have clicked wrecked color and that can be a darker gray of like 140 140 140. let's try that quickly nice okay a little bit of a lighter gray i think that's nice so now that we have that we are only going to actually display the number and we're going to have this wrecked color as opposed to the cover direct color if we're not covered right so what we need to do here is check if the current thing that we're looping on is covered and we haven't clicked on it yet so we can do that and we can say is covered is equal to and then this will be cover field and then we'll have row column or yeah row no i j and if that's equal to 1 or if that's equal to 0 then it will be covered so we're going to say if is covered then we will simply draw actually i guess we can leave the outline then we'll draw this but we'll do this in actually yeah that'll be wrecked colored and then this will be the clicked wrecked color okay um just trying to think here how i want to do this let's put in else let's put this we want to do this no matter what i actually think it's going to be cleaner to do this okay that looks good to me now when we run this it shouldn't show us any numbers okay good now i want to make it so i can click and have numbers okay we're getting somewhere here but this is still far from being finished so let's now implement a click so we're going to say if event.type equals equals pi game dot not key down but mouse button down it's pass for now let's write a function that can determine which square we clicked on so let's say define and then this is going to be get grid pause so from our mouse position we need to know where we are in the grid so that we know what to uncover uh so we're going to take in i guess mouse pause okay we'll say mx and y is equal to mouse position and just decompose that and now that we have that we also need to know actually we have these as global so i'll just treat them as global variables again not best practice here i'm trying to go fast don't roast me too badly here let's figure this out so row is going to be equal to and then this will be mx and i think we can just integer divide this by the size but i need to recalculate size i really should have that as a global probably so let's actually do that and yeah i can put that here size and then let's look for size okay and i guess let's do this and replace that with size okay replace that everywhere we don't need this anymore uh now we have size we don't need to recalculate because we already have it so we can divide by size call is equal to m y and this needs to be the other way around oversize and then return row call um yeah the issue is we need to we could click outside of the grid as well so i guess we'll just do a check down here and make sure we are in the grid before we continue there but i think that should actually give it to us so let's say grid underscore pause is equal to get grid pause and then we want the mouse position so this would be pygame mouse.get underscore pause and we can go row call we should always be in the correct column but we might not be in the correct row so we'll say if row is greater than or equal to rows and i guess while we're at it or calls or call is greater than or equal to calls then we will simply continue because we didn't click in the square otherwise we did click in the square so let's uncover it so to do that we can just say cover field row call is equal to 1 and that can tell us that we uncovered it and then we can kind of trigger an event and uncover the rest of the stuff that we need to see so let's try this now and let's click and the list indices must be interested slice is not float okay integer divided that should have given me let's just round these and that should fix it okay let's try and listen what why is it giving me color field row call equals one row call recall uh okay let's do int the integer division i should be giving me an integer but i think it's because my mouse position is float that i'm getting that so this should hopefully work now let's see and when i click okay nice uh unfortunately okay there we go and then i find a number now of course this is not how the clicking is supposed to work in minesweeper you're supposed to cover all the blank tiles until you hit the numbers so we have to implement that but for now this is looking pretty good okay so once i click cover field equals one then i need to say uncover from position let's pass row column and let's say oops so now i actually need to write a graphing algorithm that's going to essentially find where i clicked if it's my first click at least because that's how this one works i think and if it's a blank tile then it's going to essentially find all of the other blank tiles until it gets to completely surrounded by numbers and that's what it will uncover so i need to write that which will be a breadth-first search um yeah breadth first search i think not depth first search anyways uncover from position row call uh cover field uh we also need not just the cover field but we need the mine field as well okay and did i call it minefield no i just called it field okay so let's take in exactly that here i was writing a function was i not define uncover from position and let's implement a depth first search here so we can use a cue so let's import that here we also no longer need this am i still printing out the thing i don't think i am so that looks good no i am so let's get rid of that okay we need a queue though so let's say from collections import queue you could also use a list but this should be better for rq let's say q is equal to q and actually i believe that it's q dot q uh we'll see if that works though okay q.q q dot add or is it q dot put i think it's q output we're gonna put row call uh we're gonna say wow q well not q dot empty yep that's what we want uh then we will say i think it's q dot pop we'll say current is equal to q pop okay uh and what we're doing here is we're from every element in our queue just expanding looking at the neighbors if there's an empty tile we're going to uncover it if there's a number we're going to uncover it as soon as we get to a number we're no longer going to add the neighbors that's what we're going to do here so we're going to say neighbors we can spell that is equal to get neighbors and this is going to be asterisk current which will be a position we'll say 4 neighbor in neighbors and we need to get the value in the uncovered field so we'll say field at and instead of neighbor we can say rc so we'll say field rc value is equal to field rc we'll say if value uh is less if value is equal to zero actually so if it's an empty tile then what we will do is uncover it otherwise actually one second let's think about this for a second q dot put and i just called this q so let's put that there we'll say q output rc okay if the value is 0 because we're going to continue looking if the value is a number however we don't want to do that but we do want to uncover so we'll say the cover field at rc is equal to and then this will be one and then we'll have to do a check somewhere else to make sure we didn't click on a bomb but i think that's actually fine for now let me try that uh unable to import q that's because i need to do this okay let's try that uh can i import q what is this called let's do a quick search here q python um from oh is it literally is that actually what it is okay i thought it was from collections uh from no way that's right yeah lowercase that's what i thought okay from q import q and then i guess yeah let's try this now okay press q is not defined okay let's go back here because i realize i need to do this all right how are we looking on time looks like i'm about half an hour in uh cue object hasn't let you pop q dot get yeah okay let's try this now missing two rows and calls okay get neighbors uh rose calls let's try this okay and now nothing's happening when i click oh ah my um i wrote an infinite loop accidentally uh let's how did i mess that up q put rc oh it's because i'm revisiting elements uh visited is equal to set okay and we need to add our c to set and skip if in set so we will say we'll do this at the end i guess uh set dot add rc we'll say if rc in visited continue okay that should hopefully fix that now when i click add for set object it's not applied to tuple object uh can i not add a tuple to the set was that not allowed yeah i can do that right set o visited dot add okay i guess i'm making these mistakes if i want to finish this wow okay um is that what should have happened i actually yeah i think that's correct uh any of you that play minesweeper i guess i mean i'm not live right now but uh you can tell me in the comments if this looks right i think this is actually correct uh now if i click somewhere else yeah okay it fills in for the bombs i want to draw something okay that looks good though for uncovering currently yeah i think that's fine uh okay obviously more stuff that needs to be added there but for now that looks good let's add some more bombs because i have a feeling that these are not uh and really i should call these bombs so let's rename that um bombs no not bombs okay there we go let's try this and there we go that's better i feel like that's more like what we should get here right when you have when you click on the first try okay uh and then we need to have something for left clicking now so you can add a flag so let's do that we're gonna say flag underscore positions is equal to and i guess that can just be a set that's fine uh yes that looks fine to me so now when we're checking why am i doing that in here sorry it's to go down here in main okay flag positions uh for the flag positions we need to differentiate between if you're clicking the left mouse button or the right mouse button uh there is a few ways to do this but i'll say mouse pressed is equal to pygame dot mouse dot get underscore pressed and we'll say if mouse pressed zero that means left click then do all of that l if mouse underscore pressed 2 then we want to add a flag so same thing though we want to get the row call that we press so we can i guess actually take this and put this up here because if we didn't click a valid square it's going to be the same process okay let's move that back how we doing on time okay it looks like i have about 25 minutes left 20ish minutes uh should be a fair amount of time but we probably won't get any nice animations or anything done okay now if you press the right mouse button we want to add a flag so we will say flag underscore positions dot add row column okay we want to then set something in our cover field for a flag i really should have been more specific with what i'm putting in the field so i have negative one which is a bomb zero which is nothing regular numbers and i guess we'll go with negative two as a flag just because i don't have anything else to put there so we'll say cover field and then this will not be row call in fact i don't actually need to do that we can leave that for now we'll say cover field row call is equal to negative 2 indicating a flag and then we need to draw something on there to make it a flag so let's go back to our draw and okay is covered okay draw a rectangle value we need another lf in here checking if it's a flag essentially so if it's covered we actually need something for this to check if it's a flag not the cleanest way to do this but should be fine so if value is equal to negative two we'll continue after we simply draw a rectangle that's a different color so rather than rect color let's go with flag color and i guess we can just color the whole rectangle like red or something like that indicating that it's a flag so let's just say flag underscore wrecked underscore color is equal to we can go with red and where are we here flag underscore wrecked color okay uh you do also have a limited number of flags so let's say flags is equal to bombs and when you set a flag that needs to decrease so flags minus equals one although if you left-click on a position that's already a flag then we need to change that and have it not be a flag so we'll implement all of that so we'll say if yeah we'll say if cover field row call is equal to negative 2 then we'll actually do something different we'll make it equal to oh god this is a bit more difficult now because you could make something a flag that's a number so we need to now essentially oh well this is in the cover field though so that's fine it's just the cover field so we can just make it zero and just make it still be covered uh okay so we've cover field like that that's fine we need to remove you now so flag positions dot remove row call and now that i'm thinking of it i probably don't actually need this set here uh because we're just going to represent in the cover field so let's remove that this will be flags plus equals one if you remove a flag we'll say otherwise we will do this okay let's try this out now and see if we can add a flag okay so it looks like my right click actually isn't working if mouse pressed 0 if mouse pressed 2 i'm right clicking but it doesn't seem like it's doing this cover field row call equals negative two where are we here that where's my draw if value equals negative two ah it's because we don't want to check value we want to check cover field is equal to zero so actually let's just do this is covered is flag is equal to cover field i j equal to negative 2. so now rather than if valley you're going to say if is flag and then we'll have to do something with the bombs yeah i'm weary about the time here okay so there we go so those are my flags when i press we need to we need to do something with the flags i can't not actually implement them obviously so let's do something with the flag so that when we hit a flag in in the cover field we don't add that as a neighbor something along the lines of that again i'm not super familiar with minesweeper let's actually go back to minesweeper here and let me just add a flag like here here here here and if let's do like this and if i like click in there oh well of course i clicked on a bomb that was great timing uh okay can we change back here easy let's go flag flag flag flag flag flag uh maybe it's because of my it was my first turn let's try this though okay so it doesn't expand beyond the flags but if i click like here it just does a single square at the time at a time eh okay so maybe on my first click i'll expand everywhere it's if you're clicking on blank square i'm not sure i'm super familiar with how the clicking works but we'll try to continue okay so continuing here we have our flags those need to be accounted for as we expand from a square uh so create mine field where even do i have this function uncover from position yeah let's do something here hmm i'm really i'm just confused here with how the flags work in terms of uncovering again i'm not a huge mine sweeper player and i keep calling it minefield 2 i think i'm pretty sure you only do this if you hit a blank square or if it's your first click so we can i guess say clicks equals zero we'll say if clicks is equal to 0 then we'll do this or if and then this will be field at row call is equal to zero yes so i think if you hit a blank tile we expand all the way yeah that makes sense to me if you just hit an individual number you just uncover that one number now let's go to uncover from position and we need to now handle our flags and checking if something is a flag in the cover field so if value is equal to zero and the cover field at rc is not a flag does not equal how did i how did i put these flags did i put them in the cover field or did i put them in the field now i'm really confusing myself here okay cover field negative two all right that's fine so actually in the cover field yeah i guess we can still keep it as negative two but i'm using the cover field not the regular field for my flags okay so let's go where were we here cover field rcu so we can't set that yet it's gonna have to go here okay so uncover uh if the field does not equal negative two then put this okay and then so if the value is zero put it in the queue but no matter what on this we're uncovering it we're adding it to visit it that makes sense if this is a flag though we probably we don't want to uncover it if it's a flag um if value equals zero and cover field does not equal negative two and then i guess i have to do something like this okay let's try this i'm again a little confused on on what's going on here so let's put a few flags like this uh okay i think that worked so it just uncovered all the neighbors of the square i clicked on because it was my first click um all right let's click again and see that shouldn't have happened because this was a value so since it was a value i shouldn't have done that so let's go back here and if cover fields so wait where does this press it's right here uh and field row call equals zero i'm not incrementing clicks okay plus equals one okay let's try this now so when i click first time okay that's good click again we're only getting the individual number uh and then let's have something so we draw the bombs so we can see where those are so let's go to draw and we need something for bombs we have is covered okay otherwise draw this but we need to check if we are bomb so we'll say is underscore bomb is equal to value equal to negative one if is flag okay and then we'll say if is underscore bomb uh in flag i'm thinking we should probably change that to be green and then the bomb will be red so green and then bomb is equal to red and i guess we can draw a red circle inside of a rectangle for the bomb so we'll say if is bomb then we will draw yeah we want to draw this clicked cover rectangle if is covered okay we'll say otherwise here we'll do this we'll draw this but then on top if it's a bomb and it's uncovered then we'll draw that so we'll say if bomb then pygame dot draw dot rectangle uh we want to draw this on window we want this to be the bomb underscore color did i call it that uh bomb underscore color okay all right bomb color where are we going here i've completely lost myself okay there we go my game direct and then we need to pass drawing a circle need to pass the x y and the radius so for the x this is going to be i think i have x y here yeah so x y but this is going to be x plus size over 2 because we need the middle and y plus size over 2 but this needs to be all capitals okay so let's replace that for the radius this will be the size minus two just so it doesn't fill actually we'll go size minus i don't know like three or four just so doesn't fill the entire thing and that's fine for now okay let's try this and let's click okay so it uncovers a bunch of stuff here click again okay so since it was empty space it uncovered all the empty space if i add a flag okay and i can click so two click here i want to see a bomb oh there you go okay that's a massive bomb uh the radius needs to be way less than i thought okay so let's go for the size of the radius i guess this needs to be size over 2 minus some constant factor because yeah the size is the entire size of the square so that makes sense let's rerun okay let's click click click click okay give me a bomb man come on where's the bomb i guess there's got to be one close okay and then there we go we'll find a bomb so when we click on a bomb yes we got to end the game we can just reset i guess uh but we'll continue i think i mean from what i can tell here this this looks good um that's interesting okay so i can remove that by by clicking on it like that and i can uncover by clicking once and actually i shouldn't let myself click on a flag that's kind of the reason why they're there so let's implement that too we're mouse pressing here to uncover so if the clicks equals zero or it's that but we're going to say if mouse pressed actually we'll go here and this will be the cover field at r row call does not equal negative two for flag so don't let me click on a flag essentially i can unset the flag in that way uh clicks plus one okay just so we know how many clicks we're at looking better let's influence something for clicking on a bomb so uncover from field okay now let's handle clicking on the bomb down here i guess so we can continue to do all of this um actually we shouldn't uncover from position if we clicked on a bomb i guess we'll check this at the top then so we will cover field but then we'll say if field row call is equal to um negative one which is a bomb then bomb clicked exclamation point uh we need to reset and we want to probably just draw something on the screen uh let's implement a function you say define draw lost okay let's just take in some text and let's take in our window too and let's just draw some text on the screen and just display it saying you lost and then i guess we'll reset everything so for drawing lost we will say text is equal to this will be the lost font which i will implement up here so let's go lost underscore font is equal to pie comic game.system.font.com you guys already know best font ever okay lost font go with 100 let's spread these out a bit okay we have our lost font which is here dot render text one and color uh i'll just hard code this in as black for now and then we're gonna say win dot blitz and wanna blitz the text and the position is going to be the width over 2 minus the text i get with over 2 and there we go we can do that to put it in the center of the screen then we'll say hi game.display.update and time actually we'll delay down here so let's go draw lost win and then you lost try again dot dot dot i know you're supposed to uncover all the bombs i'm kind of on a time crunch here though so i want to get something functioning and then we can maybe work on this in another video but let's draw lost and then let's go pygame dot time dot delay uh let's delay five seconds and then we need to reset so to reset we really can just do that um i don't like writing this all in here this is getting to be very messy code but we can do that just fine and that that should reset all the variables we need usually i put this in some type of object or some type of class uh but for now that's fine okay so that handles if we lost uh to handle if we won we essentially check if every single thing is uncovered other than the bombs that's more complicated but let's try this for now and see if we can get losing to work okay so three so there should be a bomb somewhere around here okay there must be one there okay you lost try again uh we want to refresh and show the bomb though first before we lose yeah see that's no good okay so let's so we draw a lost and we delay however before we draw that let's draw and we can pass the win the field and the cover uh and then it's delaying so when i click it's we could just do something like this lost is equal to true i think this is going to be better here i'm going to say lost is equal to false and then down here we'll say if lost we can put all that in here that's a little cleaner for us i just want to make sure that our yeah this delay is kind of throwing me off here maybe we just won't let you do anything when it's lost and then after five seconds it will reset and there's some options here but let's try this for now okay so click uh there must be a bomb here right uh no okay one here nope one here so i might be tripping but it says three so that would mean that there would be a bomb here here and here but when i click on it i'm not getting one so i must something must be messed up here unless i've incremented these incorrectly but that should be a bomb right uh cover is not defined where did i where do i even have that cover underscore field um i want to draw all of the bombs let's just remove something here so if his flag if is covered we do this where's my bomb so if it's not covered if it's bomb all right guys i'm getting a bit lost here because i've i feel like i have a bug here so this is five right that's not it's not possible for this to be five so i think for the way i'm incrementing this must be incorrect in some way um yeah surely it's got to be incorrect based on the the bomb positions because there's got to be a bomb somewhere here and okay there's a bomb there but like if i'm looking at that i don't the numbers here are just they look to be incorrect to me so i think i've messed something up you lost try again we need to stop doing that i need to set lost equal to false so let's get out of that um did that even quit the game okay let's go where was this here and say lost is equal to false because we're resetting that would be better but i got to check my my bomb positions here something's wrong with what i'm doing with the bombs and the uh the numbers uh so for mine in mind positions neighbors equals get neighbors mine row call for c field rc plus equals one ah i can only do this if it's not a bomb that's probably the issue is that i'm overriding bombs that currently exists so if um field r c does not equal negative one then we can do that i think that will fix it but i think i was overriding bombs by adding one to them and that's probably why i was losing them so let's see if we have neighbors loop through all the neighbors uh if the neighbor is not a bomb then it's fine to increment one we'll do that yeah okay i think that's good let's try this now that's probably going to fix a whole whack of problems for me okay so there must be a bomb there right yeah okay good so now that looks like that's working okay you lost try again all right now let's add a flag i guess we're going to cover the bombs with flags so let's actually try to do this so 2 2 1 1 1 so there must be one here so we can cover that 2 so it's either here so there's one there so it's either here or here so we can't do anything there that's one so that must be a bomb so that means oh because there's two there okay apparently i'm not very good at this game uh you guys get the point now there's also a thing where when you click on the number it's supposed to expand outwards i think that's going to be an addition because i'm probably almost at an hour here but let's try to see if we can implement a way to win first and that's probably going to be easier if i set this down a bit and make this like 10 by 10. so let's try this okay um yeah this is gonna be much easier okay so two one one so there must be a bomb here and then there's i guess one here and one here would that make sense one if this says one oh it's because it's on the diagonal so that could be it too okay so we don't i don't actually know which ones all of these would be although i'm sure there's a way to figure that out right um okay so one one one one so there must be a bomb here one one one one two surely one of these isn't a bomb right that would be one there you go and then that one okay good nice uh let's continue here so one on one so this would be a bomb um up here there's only one that's good okay one that's fine so three if it's three is only three squares so that's a bomb that's a bomb that's a bomb three that must be a bomb two that's three this is one that's two that must be a bomb um this is a lot harder than it looks i never really play this game i'm gonna spend so much time just trying to complete it let's just just just go crazy and click a few random ones and see if we get okay and we lost uh i think you know honestly i'm pretty happy with this so far we need a way just to reset when we win so if i implement that then i think like the core functionality of the game is is pretty much finished and we would just need to add some small touches tell you how many flags you have left stuff like that um yeah that's and we need a timer too to keep track of how much time so let's quickly i think i have enough time to do a timer so let's say timer or start underscore time is equal to time.time and that's going to be as soon as you click so we'll say start time is equal to zero for now and then as soon as we do our first click so if clicks equals zero or field row i hate the way that i've i've done this here if clicks equals zero or field row okay you know what just for time's sake here it's not going to be clean but that's fine start time equals time.time okay we'll say current time is equal to time.time minus the start time and then when we draw we'll pass that as well so we'll pass current time and current time let's go to draw and let's take in current underscore time uh let's get a time font going quickly okay time underscore font uh i guess we'll do this just like 50 or something and then we're gonna go to draw okay and up here i guess we can just draw it down below uh we will say time text is equal to and this will be the time font dot render and this will be an f string of time elapsed colon round current time okay and that is good and then we'll say win dot blitz and this will be time text and then we will put this we could just hard code a position for now something like it's going to be the height of the screen minus the time the height of the screen minus the width of the screen because that's what's going to be the bottom over 2 and then we're going to subtract from that the time text dot get underscore height over 2 for the x position though this we can just put at like 10 or something okay let's try that and let's see if i mess that up okay time is not defined time text i need to import time okay i think i'm just about getting to my time limit too i didn't start this exactly at an hour so it's a little bit messed up time font.render let's just fix that quickly okay we can just go one and black i would like some variables for these but that's fine run one more time at least three arguments what did i what am i messing this up with oh i passed that here okay come on let's get a good run in here while we end where's the time it's not showing up uh come on uh windup lit time text the height minus the width over two minus maybe plus is what we're looking for there uh no okay let's just let's just hard code this so 10 and then this can just go to like 650. what did we what did we make the height 700 so let's just do 700 and actually no come on this will work height minus the time text dot get underscore height over two all right run time elapsed okay let's move that over height uh no minus just this okay there we go that's fine and that's giving me a massive number why does that give me such a big number okay come on let's figure this out quickly we have current time ah it's the other yeah time to time minus start time oh it's because i haven't clicked yet ah okay that's that makes sense um damn we should only be doing that if we have a start time if start time is greater than zero else current time is equal to zero okay let's run and there we go let's click ah okay i damn it i need to fix that okay uh come on run one more time there we go oh and it's showing bombs okay guys well i've clearly messed something up um yeah we'll i'll have to look at this in another video but that was my coding challenge i would say this is probably a fail in terms of completing the game but i was able to whip up what is it here uh almost 220 lines of code not fully functioning i was a little ambitious with how much i'd be able to get done but we could continue this in another coding challenge in the future if you guys would like to see that i wouldn't mind doing another video and kind of wrapping up this project please let me know in the comments down below also down to ditch it and work on something else if you guys would find that more interesting hopefully this was insightful hopefully this was a cool video if you guys enjoyed make sure to leave a like subscribe the channel i will see you in another one you\n"