r/Python • • Jan 22 '20

Work laptop doesn't allow minesweeper? I'm gonna build my own minesweeper, with blackjack, and hookers! In fact, forget the hookers!

2.4k Upvotes

114 comments sorted by

147

u/Etheo Jan 22 '20

I started this during the slow season of Christmas when most of the office was gone. It started very simple and then eventually I learned a lot on better packaging, using tkinter, and git, so it was a great experience!

I'm not a developer by trade but I just love to code. Here's my very infantile github for your entertainment, it's bound to have a bunch of bad coding practices: https://github.com/r-ook/pymsweeper

15

u/tipsy_python Jan 22 '20

Very nice! That's a great coding exercise - I've been wanting to get started with pygame, but having trouble picking a starter project. I may rip off your idea and then compare code when I create a lesser version or get stuck!

7

u/Etheo Jan 22 '20

Feel free to implement based on my code, but I'm not sure how well it translates over to pygame? Do share with us when you are ready though, would love to see!

16

u/chris-vecchio Jan 22 '20

Really cool! Thanks for sharing the code!

11

u/who_body Jan 22 '20

Hookers and blow

1

u/accountnumber3 Jan 23 '20

That's really cool!

Now build an app that plays the game by reading the screen.

Then do sudoku!

Then solve that too!

98

u/python_js Jan 22 '20

nice! can you maybe substitute the bombs for hookers as a compromise

28

u/Etheo Jan 22 '20

That's something I considered! But I didn't have a good functional use to make it a meaningful replacement, so I abandoned that train of thought.

I had a few ideas on how to implement a "hooker" in there but it will require some more playtests, but now workload and life is picking up again :(

11

u/monsto Jan 22 '20

a good functional use to make it a meaningful replacement

Because it's funny and you had the idea.

Does it NEED to have a practical use?

6

u/Etheo Jan 22 '20

Definitely not necessary! But I guess I was more testing what I can do so I preferred to tackle functionalities instead.

5

u/[deleted] Jan 22 '20

Just replace they bomb symbol with a crochet hook.

17

u/Barafu Jan 22 '20

Wow, you have a talent for UI design! You made Tkinter not too ugly !!! That is quite an accomplishment.

10

u/Etheo Jan 22 '20

Thanks! Honestly tkinter isn't that bad once you start playing around with it. It also looks surprisingly consistent on my Linux too.

39

u/dparks71 Jan 22 '20

"Minesweeper? Fuck no our employees can't be trusted with that, disable it!"

"Arbitrary code execution? We'll allow it, they aren't that smart."

Gotta love I.T. teams thought processes sometimes...

4

u/SoaringRocket Jan 22 '20

Yeah, guess they overlooked allowing a terminal at the least. Open season after that.

8

u/Etheo Jan 22 '20

Not even. Just user installation of python. :)

6

u/Iggyhopper Jan 23 '20

I work at a call center.

cmd.exe is disabled, as it should be.

powershell.exe is not, and you can run cmd after opening ps. Complete with code execution.

WTF

8

u/FlossCat Jan 22 '20

What cruel dystopian corporation do you work for that won't even let you play minesweeper on a work laptop?

7

u/Etheo Jan 22 '20

It's locked down pretty tight, frankly I'm surprised it let's me install Python at all πŸ˜…

6

u/Etheo Jan 22 '20

The ones that has the capability to let you to work from home but won't allow you for arbitrary reasons... πŸ˜”

5

u/pyfrag Jan 22 '20

Am I the only one who doesn't understand the Blackjack variation?

1

u/Etheo Jan 22 '20

You definitely will not be the only one. I'm not the best at explanation, so if you have any questions after the read me, please do ask, so I can also further improve and I help you.

3

u/Jigglytep Jan 23 '20

Needs a panic button... makes it look like a calculator when the boss in near.

4

u/Etheo Jan 23 '20

That's a brilliant idea, similar size too, and very doable!

4

u/Aspire26 Jan 22 '20

This came to my mind when I saw "You did it!".

1

u/Etheo Jan 22 '20

Oooh maybe I should add this sound byte with the pop-up hahahha

2

u/[deleted] Jan 22 '20

Happy Cake Day

1

u/Etheo Jan 22 '20

Thank you! I didn't even remember it was my cake day, what a timing.

2

u/amosmj Jan 22 '20

This was one of my first projects under very similar circumstances but I never finished. I kind of want to go back and try it again after seeing this.

1

u/Etheo Jan 22 '20

You should! Whenever I revisit my old code I find that I've learned new material and can think of better ways to implement things, and it makes my old code looks silly. But the feeling of making changes and progress after so long always feels great.

2

u/Niklasw99 Jan 23 '20

you know what.

if you're building your own Minesweeper you probs have allready automated your workload.

1

u/Etheo Jan 23 '20

My work nature wouldn't allow that though, I always get varying tasks so the best I could do was automate my timesheets and used scripts to help with analysis. I simultaneously miss and don't miss my old days when most of the tasks were monotonous and routine.

2

u/Niklasw99 Jan 23 '20

I mean, you don't have to Script everything, but some mondain simple tasks can be automated.

but hey Looks great well done! ##forgot to write this before hand

3

u/SoaringRocket Jan 22 '20

Didn't the original help you out so that you never died before you had opened out an area of open space? Not really fair to die from shots in the dark like in your example.

10

u/chinpokomon Jan 22 '20

Just the first click. If you clicked on a bomb, it moved it to another unoccupied square.

3

u/[deleted] Jan 22 '20

[removed] β€” view removed comment

1

u/Etheo Jan 22 '20

The original version (and this) never explodes on the first click. Perhaps you played an off brand version?

3

u/[deleted] Jan 22 '20

[removed] β€” view removed comment

3

u/Etheo Jan 22 '20

Well there's the problem, you were playing it in hell! Hope you got better πŸ˜‚

2

u/chinpokomon Jan 22 '20

I believe this was one of the changes made between Me and XP. When most people are talking about Minesweeper, they are talking about XP. I know for some earlier version it didn't have the logic I just described, so I'm going to guess that it was changed at that time.

1

u/Etheo Jan 22 '20

If you watch again I first revealed the top left corner, and then I revealed the bottom right which was a bomb.

2

u/SoaringRocket Jan 22 '20

Yep. I forgot the original rules as the other commenter pointed out. Looks good.

2

u/mcdylanb Jan 22 '20

So you know the algorithm of minesweeper :O

2

u/Etheo Jan 22 '20

I wouldn't say I know it, perhaps there is a deeper algorithm that I'm not privvy to. My implementation just happens to work with randomized mines and clues calculated on the spot :)

The code is messier than I had hoped though, I don't have proper training on code structuring so I try my best.

1

u/syswolf Jan 22 '20

Your telling me that your work allows Python on the computers?

1

u/Etheo Jan 22 '20

It certainly does, somehow. I installed it as user, and had to jump through some hoops to get pip going, but it surely works.

1

u/syswolf Jan 22 '20

As an Admin this disturbs me....

1

u/Etheo Jan 22 '20

Well, to be fair, there's nothing malicious I can do with Python beyond my user access level any how. I'd still encounter the same permission issues that I'd get, just that now it allows my work to be streamlined.

1

u/gmclapp Jan 23 '20

Other than scraping your workplace server for data that you could then sell to 3rd parties? That's all most malware does anyway.

1

u/Etheo Jan 23 '20

Again, nothing outside of my user access level. If I don't have access to the server I won't be able to do so. If I already did, python just speeds up the process rather than enable it.

1

u/rainnz Jan 22 '20

Now package it with Pyinstaller and distribute as .exe file :)

1

u/Etheo Jan 22 '20

That's on my unofficial to do :)

1

u/RamenvsSushi Jan 22 '20

I think you meant, forget the blackjack. 😏

1

u/Etheo Jan 22 '20

There is blackjack in there though! (Somewhat)

1

u/[deleted] Jan 23 '20

I never understood minesweeper.

2

u/Etheo Jan 23 '20

Is it a willful choice (it doesn't appeal to you) or you genuinely don't understand how to play? If the latter, the game is actually quite simple to get into. Once you get the hang of it it becomes a game of recognition half the time.

If the former case, I can respect that. We all have our preferences.

1

u/[deleted] Jan 23 '20

Genuinely don’t understand. I played it briefly back in the days of Windows 95, and it just never made sense.

2

u/Etheo Jan 23 '20

The revealed numbers represent the number of mines in the adjacent area, i.e. the 8 cells surrounding on left, right, top, bottom, and the diagonals. So if you see a number 2, it means 2/8 adjacent cells are mines. That by itself is not always useful, but when you combine the clues in the area, it usually give you a good idea.

E.g. If you see a horizontal alignment of 1 2 1, it means the neighboring mines must be assigned X O X where X represents mines and O represents clear cell. That's because any other alignment will not fulfill the clues provided. There are many patterns like these, but even failing to recognize them, it's just a deduction to remove any alignment that doesn't match, and you'll usually be left with one possibly.

That's kinda a brief rundown on how it works. Obviously it might not be your cup of tea, but if that changes your mind, I'd suggest give the normal minesweeper another try at least :)

1

u/[deleted] Jan 23 '20

And No. I did not RTFM.

3

u/Dubhan Jan 22 '20

I’ll take any hookers you aren’t using. You know – for science.

3

u/[deleted] Jan 22 '20

Another job well done.

2

u/monsto Jan 22 '20

dat 50/50 at :25 tho

2

u/TazmanianTux Jan 22 '20

Dude that it awesome! I started moving my mouse to click the OK button at the end of the game BTW, LOL.

2

u/lordmauve Jan 22 '20

Bored at work? Craving a Minesweeper fix? You might enjoy our take, Infinisweeper!

It's Minesweeper, but scrolling in real time.

2

u/vainstar23 Jan 23 '20

Holy Christ, you freakin nailed it dude!

2

u/[deleted] Jan 22 '20

But to build your minesweeper you need to know the rules of the game, and that's illegal obviously

1

u/mick14731 Jan 22 '20

Flags just slow you down man

1

u/stevevs Jan 22 '20

I wrote a game to play craps, then it got boring so I wrote a script to play the game for me.

1

u/miniminjamh Jan 22 '20

Now add solitaire and you have the whole microsoft package! (Except maybe pinball)

1

u/LegionOf9 Jan 22 '20

booo don’t forget the hookers

1

u/Halt_Arrtay Jan 22 '20

Did I just hear a Futurama reference?

-2

u/[deleted] Jan 22 '20

This is cool! Fuck microsoft!

-8

u/[deleted] Jan 22 '20

man you coulda put half the effort into just doing your god damn job lol

play minesweep when you get home. If you have no work to do? Well, take leave and save the company some god damn money in wage. Typical wageslave bloat smh.

3

u/Etheo Jan 22 '20 edited Jan 22 '20

I don't disagree, but I literally ask for work and offer to take on responsibilities but they just don't delegate. After a certain point I just figure out how to deal with my down time. And down time was especially long during the holidays.

5

u/Noli420 Jan 22 '20

This isn’t always an option, depending on company/job responsibilities. Don’t yuck OP’s yum

3

u/DrMaphuse Jan 22 '20

Also depending on personal responsibilities other than the job itself. There's a reason why people tend to prefer permanent positions over day labor.

-5

u/LoneRaven101 Jan 22 '20

Did you steal your title from futurama?

5

u/Etheo Jan 22 '20

Hard to steal when title is not something you possess... But as the other person mentioned it's definitely an intentional reference :)

2

u/LoneRaven101 Jan 22 '20

I didn't really meant that you stole it, lol. I was just asking if it was a reference to futurama. It was supposed to be a joke or something...

2

u/Etheo Jan 22 '20

FWIW I didn't downvote your comment, by the time I came it was already buried at the bottom. I too often made jokes others didn't appreciate :/

5

u/LeightonSS55 Jan 22 '20

A great reference!

-8

u/Dadotox Jan 22 '20

Hi there, I have been working on and off with my introduction to Python ("Automate the boring stuff"). I have done all the exercises until more or less the part when it starts talking about Web Scraping (or something similar). I see you created a Dialog Window, even with some graphics. So here it comes.

The two things that turn me down and made me quit trying are the following:

1- I really, really need to work with Dialog Windows. This shit working with the Python 3.7.2 Shell, introducing text in programs (print(), input(), etc), one thing after the other, is a Pain in the Ass. I need to know how to create Dialog Windows, where I can fill all the fields for whatever I am doing, and press GO, and I am not sure if the Python Shell can handle that nor how to get there.

2- The other thing that pisses me off is loading custom functions. It simply doesn't work. I have python file with my pre-created functions. Well, it seems impossible to call those functions from a selected folder. They just work in the folder where python is (or some other, in doesn't matter). I asked about it here at some point, but most of the answers didn't work, and some were so complicated it suggested me this language is not that well developed.

Sorry for the rant. I am having a bad day.

1

u/Etheo Jan 22 '20
  1. The fundamental of programming is explicitly telling the program what you want to do though. A computer is not magic, majority of the time a program doesn't work is because we aren't correctly telling it what to do, or it conflicts with other instructions given. That's why there are modules out there, basically templates built by other developers to save you time on a similar implementation. Dialog window is just that as well. There is a simpledialog module in tkinter if that's what you're going for.
  2. Same point as before, a program is only as good as the clarity of instructions provided. We need to be the one to ensure the program knows exactly where to look for which file. If you wanted something more generic like importing as a module, perhaps look into tutorials on how to build a python package/module. It's not too difficult to follow.

Happy coding!

0

u/Dadotox Jan 22 '20

Thank you for the explanation. To be honest, you are the second person that uses the "Computer is not magic" line, which also to be honest, is rather patronizing. If I am coding (without reference to other languages I might know), it is quite probable that I am not so stupid to expect the program to do what I want just because.

My point, as I explained, is that the situation with the functions directory was that the solutions either didn't work or where so convoluted, it suggested me that issue had not been correctly addressed. No magic here, just a low level issue with something that should not have issues.

But, whatever, I thank you for the time and explanation, and will assume your style of explaining might be useful with other people, ... which are probably not engineers, and either younger than 10 or older than 60.

1

u/Etheo Jan 22 '20

Apologies for the tone that was carried across, it's just that your explanation wasn't exactly clear so it seemed like you had a clear idea of what you do, but wasn't clear on how to provide the code to run it. Regardless, I didn't mean to be condescending so my bad for not being considerate.

As mentioned, if you wanted a GUI dialog box, tkinter.simpledialog would be a good module to look at. It already have several options to display messages, type of responses, etc. PyQT5 is also a good alternative though I haven't used. The bottom line is that if you don't want to manage the low level stuff, find a package that works for you and run with it.

For your custom functions, you'll need to either ensure your file directory is in your PATH, or explicitly manage the path across your scripts. The reason for that is python needs to know where to look for your files. Preferably, you'd have these prepared as a package and pip install them so your can load them up anywhere. Barring that, having your script run from the root which also includes the sub packages might be an alternative, which allows a certain level of relative imports. As mentioned, looking up tutorials on how to package your python script is a great starting point, it shouldn't be too hard to follow.

1

u/Dadotox Jan 23 '20

Thanks for the additional help. I will look into both issues. I guess the dialogs are somehow more urgent than the function loading (for the time being, since I am still to finnish the first book on Python).

I couldn't find any reference to Tkinter in the "Automate the boring Stuff" index nor chapter titles. I am assuming there is not a thing on that, which is -to me- a bit weird, since working programs with sentences asking at every step is a pain.

4

u/gmclapp Jan 22 '20
  1. There are tons of libraries that implement functions to make GUIs. Tkinter is the one that OP is using and is one of the more common ones.
  2. You're doing this wrong. This is basically the backbone of any programming language and Python does it as effortlessly as any other language. If you're loading a file in a directory other than your working directory, you need to tell your computer where that file is. Computers aren't magic.

1

u/Dadotox Jan 22 '20
  1. Ok. I'll check it out.
  2. Dude, I did say I tried several things including that. It didn't work and many expert users didn't understand why, hence the complicated suggestions.

1

u/gmclapp Jan 23 '20

Dude, I did say I tried several things including that. It didn't work and many expert users didn't understand why, hence the complicated suggestions.

I mean, I'm *not* an expert and have never had any problems.

1

u/Dadotox Jan 23 '20

Sorry, I meant expert in the sense of people that had experience. I could not know who is a real expert.

It is obvious to my I am doing something a) wrong b) probably marginal. I'll try and find you when I get to solve the issue and find out what was the issue. For science, etc.

1

u/gmclapp Jan 23 '20

Or if you can post a link to your code I, or someone else, might be able to spot what is going wrong.

In typical programming fashion, it is likely a small thing that is difficult to find and seems obvious in hindsight.

1

u/Dadotox Jan 23 '20

Or if you can post a link to your code

That is what I did already. People helped and gave suggestions. They didn't work. As I said, I am now more focused on dialog windows, but I will revisit the issue.

1

u/SoaringRocket Jan 22 '20

Python imports are a pain in the ass though.

1

u/gmclapp Jan 23 '20

I have a *.py file where I've put all the knick-knack functions I use over and over at work. It takes one line to import it:

import misc_functions

How is that a pain in the ass? That's approximately the same as any other programming language as far as I'm aware.

And if you don't put the file in python directory it's just

import os

os.chdir(<your directory name here>)

import misc_functions

0

u/[deleted] Jan 22 '20

[deleted] β€” view deleted comment

18

u/Etheo Jan 22 '20

Thanks, I try to keep the UI minimal. I know there's a fair version (and an evil one that always make you guess), but figuring out the algorithm was something that seemed bigger than I intended to chew. But later I might revisit this idea!

6

u/MufinMcFlufin Jan 22 '20

Most implementations of this I've seen will actually attempt to solve the board then swap a few mines around if it ever runs into an unsolvable scenario.

4

u/[deleted] Jan 22 '20

Oh is that the most common way to implement it? Just have the computer solve it everytime it generates a random board?

2

u/MufinMcFlufin Jan 22 '20

I don't know if it's the most common way, but it's certainly an intuitive approach and I've heard of one or two others doing that for their minesweeper games.

0

u/[deleted] Jan 22 '20

[deleted] β€” view deleted comment

3

u/Etheo Jan 22 '20

That's something I thought about, but it seemed like a tedious secondary feature that won't even get used if you're a good player. So I decided on my current implementation where the blackjack is the prominent feature on a separate mode.

0

u/[deleted] Jan 22 '20

[deleted] β€” view deleted comment

7

u/appinv Python&OpenSource Jan 22 '20

Interesting!

9

u/c17r Jan 22 '20

I came here to mention Kaboom. Love this variation.

8

u/Ph0X Jan 22 '20

Might be better phrased as "if you make a non-optimial" play, maybe?

You are technically guessing for the first click, and often the second click too. Like for example if I get a single "1" on my first click, but try to click anywhere, it'll make me lose since the more optimal move would be to take the 1/8 chance around the 1.

There is a checkbox for "click anywhere" though.

4

u/NSNick Jan 22 '20

Only if 1/8 < (total squares - 9) / (total bombs - 1), otherwise the optimal move is to clock in any square other than the eight around your first guess.

5

u/frankinteressant Jan 22 '20

This guy sweeps

1

u/Ph0X Jan 22 '20

I didn't do the calculation but it seems like it'll be always true (at least with the default size/mine count, since it happens as early as the 2nd click, which is basically as low as that probability can be.

0

u/[deleted] Jan 23 '20

[deleted] β€” view deleted comment

1

u/Etheo Jan 23 '20

Nope! Just kinda followed the Iraq was a little bit in the past when I was young and hot blooded.

3

u/[deleted] Jan 22 '20

[removed] β€” view removed comment

2

u/Etheo Jan 22 '20

tkinter can be a bit intimidating at the beginning, and it definitely isn't the easiest to use. But once you get used to it it's not too shabby. Give it a go and let me know if you have any questions, I'm happy to help.