program.views.py
Slotmachine/My thoughts on this code
Press here to run the game.This is the first application that I ever got ”running” in Django. I wen't about it in probably the worst way possible. I took existing code that i had written following a guide on YouTube. The code was built to run in a console and my thought was ”how hard can it be to convert this into code that runs in a browser..?”. My understanding of webdevelopment was very limited and the result of this approach is needlessly complicated code that runs pretty poorly. The original program arranged the spin results with a nested list so that it looked like this in a console:
[[A][C][C]]
[[B][C][B]]
[[A][A][B]]
I did not think to remove that part of the function as I thought I could just write it out the same way for the user on the page, that did not work. The result is that I sent the data out as written above and tried to parse it and then rearrange the parsed data in the frontend part of the application, that resulted in the symbols coming out in a different order than they where supposed to. Which kind of destroyed the purpose of the game. You win without seeing the same symbols in a line, and seeing the symbols arranged in a line is what's supposed to be satisfying about the game.
There are more things that are bad about the code but I'd rather explain how I would go about it if i tried today. For starters I would think about how an actual slotmachine works, since the code on this page completely randomises the symbols there's no chance of any animation other then some blurry wheel that then just shows the results. I'd like to see the spinnings wheels with arranged symbols that stops at a random time, I think that with an animated wheel (could just be a gif where a timestamp on the gif represents and shows 3 symbols, i think that makes it way more exciting. To do this I would for starters make three seperate lists to represent the symbols on each wheel in a slotmachine A_SYMBOLS, B_SYMBOLS and C_SYMBOLS. The randomness would come from the number of itterations you make through these lists. To get the 3 values out of each list I would just go A_SYMBOLS -1 and +1 in the list. It would be shown like this in the frontend:
A_SYMBOLS -1, B_SYMBOLS -1, C_SYMBOLS -1
A_SYMBOLS, B_SYMBOLS, C_SYMBOLS
A_SYMBOLS +1, B_SYMBOLS +1, C_SYMBOLS +1
And then check winnings would be as easy as A_SYMBOLS == B_SYMBOLS == C_SYMBOLS = win, with the same for the +1 line and the -1 line. Alongside functions for balance, winnings and betting, and also the list of values for each symbols the application would be a lot simpler and more fun to use with frontend animation. This could probably also be optimized a lot but I'm not great at optimizing without the written code in front of me and right now I don't really see the value in actually working on this project. The point of me trying this from the start was just to learn and i feel like i already have.