The second project I made was a text based version of tic tac toe. This is a two player game, and in the future I would like to add a one player option where you can play against the computer however I would need to work out the logic for how the computer would determine how to choose moves - I would rather they don't just choose random squares as this would make no sense really. Here is a few segments of my code I was particularly pleased with
The segment related to name choice I first showed I really liked because I was happy I found the function that checks if the name is full of blank space (.isspace()) - this is a really cool function because you can add just tabs, \n for new line, or spaces, but it won't accept a name that is totally blank because I thought this would be confusing in game. It also doesn't allow player 1 and player 2 to have the same name as again this could be confusing. I was happy because originally I hadn't seen this function before and I could think of so many uses for something like this in other projects
Similarly the code segment I have to check win I found a really cool function to use called .issubset(). This basically allows me to have a list of all the winning conditions which in tic tac toe is only 8 combinations, and it checks each of these against the players current positions - if a winning combination is a subset of the players current position then it will return True showing they have won. This is so easy to read and understand when looking at the code because the function is so clear and well named - this is one of the reasons I really love python!
My character replacement code segment made me quite happy because I managed to come up with the idea and add it in without any problems or needing to look anything up - it works really well it basically takes the grid that has the numbers in each square, it will first replace any numbers in the players position lists with their corresponding symbol, and then any left over it will replace with a blank space which means the grid ends up printing correctly each turn. The reason it turns each one into a string first is because the list its reading from is a list of integers due to it being used elsewhere in the code as well, and the .replace() function will only work on a string. I thought this was the easiest way at this point to get around this with my code still being clear and easy to understand
Here is a quick shot showing what the code looks like in game. Some improvements I would like to make are there is some duplicate segments of code such as the screen clear() function which happens often, I would like to get rid of the duplicated lines to make it simple and easy but this requires some thinking as to how to reorder some areas of the code. Of course adding a one player version to player against the computer is another improvement I would like to make. I covered any exceptions or errors I could think of in my code (duplicate moves can't be made, you can't enter a letter, you can't enter a number outside of the available 1-9) but its possible I have missed some errors that could lead to a crash in the code. If you want to check out my full code, look at my github! I have tried to comment through my code so that its clear why I made the choices at each section.