r/learnpython • • Jun 18 '20

Python projects for beginners

Hi! I just learned basic python syntax (watched a 4 hour long python tutorial playlist while also doing it myself), what would be some good beginner projects/ programs that i’d be able to make, and second of all, what can i learn next to become better at python?

449 Upvotes

108 comments sorted by

View all comments

16

u/dogs_like_me Jun 18 '20

Start with tic tac toe. Work towards minesweeper.

1

u/OnlySeesLastSentence Jun 18 '20 edited Jun 20 '20

I feel like minesweeper should be relatively easy to program.

Get a location. BombCount=8 If bomb, you dead. If not bomb, for i in range 0,8 if bomb in adjacent[i]: BombCount-=1

Displace BombCount in location. Mark location as visited. If BombCount == 0, then visit each of the adjacent locations and repeat.

Edit: bombs should start at 0 and increase their count. I thought of that backwards.

Project is done now: https://www.reddit.com/r/learnpython/comments/hbbzoa/python_projects_for_beginners/fvevwof

13

u/dogs_like_me Jun 18 '20

You seem to be describing an algorithm for playing minesweeper. I'm talking about programming minesweeper. The game. This entails:

  • building a lattice of 'cell' objects, which will probably need attributes like n_neighboring_bombs, is_visible, is_flagged, is_bomb....

  • Visiting an empty cell should trigger a cascade which reveals all adjacent empty neighbors and the boundary non-empty neighbors (where 'non-empty' means it is bordering at least one bomb, so contains a numeric value). Probably wanna implement this as a graph traversal.

  • "double-clicking" a revealed, unflagged cell, should reveal it's neighbors, potentially triggering a cascade as above.

  • appropriately testing win/lose conditions.

  • possibly maintaining a database of high scores

  • possibly maintaining a database of save states

  • possibly presenting the user with a running timer

  • presenting a visual representation of the board

  • possibly representing the game with a proper GUI (native? webapp?)

  • ...

There's really a lot you can do here. I like to use minesweeper as an exercise to get a tour of language features when I'm learning new tooling. I've coded it up in at least three languages now, two or three different ways just in python.

-1

u/OnlySeesLastSentence Jun 18 '20

I was describing programing it. I didn't list literally everything, but I mean it's pretty easy to make a grid and place a random number of bombs.

The steps I showed were the logic behind what's supposed to happen when someone clicks a spot. You make a matrix and using the logic from from tic tac toe, you search surrounding titles for bombs and place the number as the player plays.

2

u/Poddster Jun 19 '20

The steps I showed were the logic behind what's supposed to happen when someone clicks a spot. You make a matrix and using the logic from from tic tac toe, you search surrounding titles for bombs and place the number as the player plays.

In which case your logic is incorrect.

 . . .
 . x B
 . . .

If you click the "x" square your code would show 7 but the answer is 1.

If Minesweeper is so easy how could you get that simple thing wrong?

1

u/OnlySeesLastSentence Jun 19 '20

Fair enough. I'd have caught that error (as well as noting that corners and walls are supposed to have fewer bombs) immediately upon actually trying because I'd have done a print(location.bombCount) immediately.

2

u/Poddster Jun 19 '20

So what you're saying is that making minesweeper would have been a good way to practice your python skills and make you a better python developer? ;)

Seems like it was a good choice after all!

1

u/OnlySeesLastSentence Jun 20 '20 edited Jun 20 '20

Anyway, it took me about six hours of off and on work while watching TV (which I feel is a bit slow honestly), but here you go:

GitHub.com/MOABdali/pysweeper

I'm going to add a GUI when I feel like it (I'm not too invested in the project at the moment), but feel free to try it out in console mode. I know the formatting isn't pretty, but my main purpose was getting the logic to work.

I didn't use any outside sources other than googling how to clear screen (I always forget this) and looking up how to use deep copy's syntax (I had a similar problem in the past with python copying lists by reference instead of duplicating by value when I was making my connect 4 game).

Feel free to let me know if there's any logic errors. I even added a "mark potential mines" feature just for you.

Just wanted to make sure I put this out today to show minesweeper isn't challenging enough as a big project considering an entry level graduate like me was able to handle it lol. The end goal should be something that takes a week or so to complete, not just a few hours.

Edit: before anyone mentions it - I know my code can be broken up into multiple files. I was trying to just get the game running so I wasn't focusing on making it pretty yet. The final product will be nicely commented and broken into functions where needed. I also realized about an hour in that I would not need to keep track of how many bombs are in each structure's vicinity, so I'll be deleting the scary squares[0,0,0,0,0,0,0,0,0,0,0,0] thing eventually and reducing it to a simpler squares [0,0,0,0]. Yes, I know a class can replace the 4 item list, but there's no need to add complexity when my simple list can handle it.

2

u/dogs_like_me Jun 20 '20 edited Jun 20 '20

I think you're missing my point. Minesweeper can be simple or complex, depending on how "fully featured" you want it to be. It's simple enough that a novice can successfully make a version that they can pat themselves on the back, but also interesting enough that if you wanted to you could use it to practice OOP, GUI, MVC, multiprocessing, graph algorithms, recursion, functional programming, TDD, database design, ORM, ...

You didn't use objects because you didn't feel like it. Fine. But if you were someone who wanted to learn or practice OOP, you can see how that would fit in here.

Minesweeper is a good exercise because the bar for success is basically as low or high as you want it to be. There's just enough there to allow you to exercise basically every language feature you might care to play with, but it's simple enough you can cobble together a version quickly if you're bored.

PS: is you're repo private? Not seeing it. Anyway, seeing your github, I think it's a bit weird that you're bragging about how easy this was for you given that you're advanced enough to be learning sockets, but it still took you several hours and I was recommending this to someone who's only watched 4 hours of intro videos.

1

u/OnlySeesLastSentence Jun 20 '20

I did indeed forget to click on public. I uploaded it and spammed Next and saw the "keep Private" thing checked and told myself I'd make it public later.