December 19, 2014

Week of 12/14/14 - Python Part 15: Mid-Term

    For my midterm, I decided what I would do is make a game of tic-tac-toe on codingground. First, I didn't know exactly what I was doing, so I imported the random library, because I had used that before in the battleship post. Then I made the function randint(a,b) and made it to reference random.randint(a,b) because I forgot I could import specific functions from a library and make them native functions, but hey it worked at the time. I went on to make a function to print out the game board, because I knew I'd need one. It went through several iterations of what it looked like, and I got enough whitespace and padding into the board that it looked... not bad. Here's how it looks:
               
    1   2   3  
 1    |   |    
   ----------- 
 2    |   |    
   ----------- 
 3    |   |    
               
     The numbers are there to serve as a reference to the player because I don't know how to do click-based input. Later on, I decided I would need to highlight the numbers on each axis depending on which axis I was prompting the player for, so I made it so that they were in brackets. It took a while for me to be able to get the spacing right and all the parts of the board lined up, but I got it.

     Later, I decided that I would have to clear the board (because looking at the past stuff entered into the terminal would look sloppy and unprofessional, which I didn't know that I could do when I was working on the battleship game). So, after searching on stackexchange, I found what I was looking for; a function that would invoke the Unix terminal's "clear" command, blanking the screen.

     Next I decided that all the functions that I had made made my code look all messy. So after searching some forums, I found out how to put all my functions into a separate .py file, and imported everything from it. Then, I wrote the code to make the back end of the board, using several lists. Next was to make the loops that would allow the game to be played over and over until you decide to stop. I had to use while loops using a single variable that I wouldn't change, because I didn't know how to just make thing loop. I used a global variable for this, because I would use it in other loops as well. I went on to ask the player whether he wanted to play against a friend or computer, and made it so that if they chose computer, the console would print that that feature was not implemented yet, and when they picked a friend, it would clear the screen and print out the board. Getting the choosing thing to work was hard, and took up a lot of time, but it worked by the end. I had considered creating a checking function to check if the game was won or not, but it was too difficult. Here are the files if you want to upload them to codingground and try it yourself.

No comments: