Monday, June 2, 2014

Wow! This program ROCKS! Writes your PAPERS! And Functions as SCISSORS! (The Last Two Statements are False)

The next class I created is to actually play rock paper scissors. It uses the "Q" "W" and "E" keys in order to get an input from the user. Heres the code here:
First off it reads the LastLogin file and sets the User string to the information in that text file. That information is used before the computer input is decided and is printed to the console with a ":" after it so that when the user roll is chosen it reads "[User]: [Rock/Paper/Scissors]." The program uses a KeyListener which checks for if the "Q" "W" or "E" keys are pressed. Once it determines which key is pressed it sets Rock Paper or Scissors accordingly. Then a random number generator is started and picks a number between 1 and 3, and sets that to the roll variable. Then based on what the roll variable is either rock paper or scissors is chosen for the computer and based on what the conclusion of the game is it prints "Win!", "Loss!" or "Tie!" to the console. Overall the actual rock paper scissors program was fairly simple to make, the only difficult parts were the launcher and attempting to get the leaderboard to work. The Launcher was detailed in the last post, and the Leaderboard never actually came to be. The reason for this was that I couldn't get variables to be pulled out of if loops, which seems like it should be very easy but actually isn't. Because I couldn't get those pulled out of the loops I couldn't get the wins and losses to accumulate so I could never get that part to work. Overall this program was a good experience in java, and got me to learn a lot. It exercised all the aspects of Java I had learned in the past, and got me to learn many more. I had some difficult moments, and some aspects of the program that were done in seconds, but overall it was a sufficient challenge and I hope to make more programs like it in the future.

Update! That Launcher got Laun-short, but the Code Got Long-cher!

            The launcher for my game was once a huge window with lots of text and boxes. I decided to modify it so that it was smaller and easier to handle when first seen. Here is the new launcher:
            I decided to remove the remember boxes, and have less text so it was significantly less confusing. Now instead of remember boxes the username from the last play session is automatically put in the username box to save time for the user. Once again to create this I used the Netbeans GUI builder to create this beautiful piece of work. As far as functionality goes, once the information is typed into the two boxes and "Sign Up!" is pressed this code runs:
            Once the action is done it sets the string variable "Username" equal to the information in the first text field, and the string variable "Password" to the information in the other text field. Then it starts up a BufferedWriter which is used for writing to files. It first creates a temporary file called the Username variable and prints the file path for easy access. Then it writes the Username and Password strings with an enter after each one and closes the writer. If I put Steve as my username, and John as my password the text file would look like this:
             This text file is then used when the other button, the "Play!" button is pressed, here is the code that is used when the other button is pressed:
            Basically what happens here is when the action happens the text from the two fields is captured as the Username and Password variables. Then a file scanner is opened which will scan for file titled the Username Variable aka whatever is typed into the first text field. Once this file is found the first and second lines are set to the string variables Fusername and Fpassword respectively. After those variables are set the information from the text fields is saved to the "LastLogin" text file which we will see in use in a later post. Then Fusername and Username as well as Fpassword and Password are checked to see fi they match, and if they do "Match Successful!" is printed into the console. Because the way the program searches for files the user must have the correct username in order to log in, because of that its possible to have "Incorrect password" Messages if the Username is matched but the Password is not, and also makes it impossible to have messages for correct password and incorrect username, not that you would need that anyways. If the password match is false, then it writes "No User Signed In" to the "LastLogin" file and prints to the console "Incorrect Password!" 
             The last function of the leaderboard, as mentioned before, is making the LastLogin information appear within the username text field when the launcher is opened. Here is the code for that:
            Basically when the launcher is loading it reads the LastLogin file and sets the string "Luser" to the information in the first line of the file and sets the information in the text field to that string. Thats a fairly simple operation once everything else is set up. 
            That is the last function of this launcher, which is actually more of a login window than anything, as it doesn't actually launch the game. I found it very difficult to make a java program open another java program within the same class, but it is possible. The main purpose of the launcher was to have a leaderboard, but I ran into trouble with "if" loops in the actual game itself as we will see in the next post. 

Friday, May 9, 2014

That GUI isn't GOOEY, its clean, sleek and dependable!


Wow! Look at that launcher!


           For my final project I am creating an amped up, more extreme version of rock paper scissors. The game will have multiple users be able to play against the computer to attempt to get their high score on the leaderboard. In order to have separate users they will be able to sign up in the launcher shown above, which once it is operational will save their login info to a text document when they hit sign up. Then when they try to log in later it will check the text file to make sure their username matches their password, and then the game will open. The remember check box will save the information to a separate text document that will make it stay when the launcher is reopened. Thats all the progress as of now, I'll be back later with new progress!

Monday, January 6, 2014

Dice! Dice! READ All About It!

Summary:
This program was designed to analyze the data from the Dice Saving program earlier. What the program does is it opens the text file read mode, Takes the amount of lines in the program and divides that by four. It does that because there are four lines for each roll. It then prints the amount of rolls that it just determined, and closes the file. Then it sets the amounts of sevens and double sixes in the document to 0 so it is ready to search for how many there are. Then while the amount of Lines discovered earlier is greater than the amount of lines scanned it searches for totals of 7 and roll1's and roll2's of 6. Finally it prints out the information it searched for and found.
Issues:
The main issue in this program was getting it to search for the correct thing. The tricky part is that when we wrote to text file, we put a new line after every number entered. So when reading the file we had to search for the number with the new line (\n) included. This confused a lot of people. 
Profit:
Just like in the writing program this reading program is useful for learning how to save games. Also it gave us a nice little reminder that we have to be very careful because computers search for exactly what they are told to do and wont assume anything for you. 

Put that Roll in a To-Go Box, We Want to Save it for Later!

Summary:
The point of this program was to use the dice rolling program to write to a text file in order to be analyzed later. I did this by opening the file, setting 3 variables to the variables from the Dice Roll program to save some time later on, and asking how many times the player wants to roll. Then, while the amount of rolls done, is less than the amount of rolls wanted it rolls the dice, then it writes that to a file with f.write command and then a blank line after. All this is repeated again until the amount of requested rolls has been achieved. 
Issues:
It was difficult to find the command to write information to a file as it had not been covered in our codeacademy lessons. Also figuring out how to make the printed information go into another line was difficult, but we figured out that it was \n.
Profit:
This is important so that we can learn how to save games in the future. But also to get a more complete knowledge of python.

Aww Craps!

Summary:
This assignment was to make a program that simulates the game craps. First of all, it imports the Dice rolling program from earlier, and takes the various Dice rolls from that. Then based on the sum of those rolls it determines the result of what would happen in a game of craps. If the roll is not one that would initiate point mode, then the player instantly wins. But if it is not an instant win roll, point mode is activated. When point mode is activated the player tries to roll the number that initiated point mode, before rolling an 7 or 11. Then the player is asked if they want to roll again, and if they do the program restarts.
Issues:
Originally I went for the brute force method, with the intent of saving mental effort at the exchange of a bit of time. All that time amounted to nothing, and I wound up restarting and going with the point mode method that I have now. Another issue was getting the program to roll more than once, but I managed to get it working by reloading the dice program after any roll was completed.
Profit:
This project was great for putting all of our skills together, as well as learning how to import our own previous programs as modules. Importing our own programs as modules comes back often in future programs.

Dice, Dice, Baby!

Summary:
This program is extremely simple, and it basically is just reusing past knowledge. It imports a random module, sets the first dice to random numbers one through 6, then adds them together for the Dicesum variable. Finally it sets the Diceprint variable to a string combining the first number, then a space, the second number, then a space, and then "Total of", then another space, and then the total of the two dice. The purpose of the Diceprint variable is so that all of the data can be easily printed in future programs. Also, this is my first program that I used a comment in, the comment is completely irrelevant because it was mainly just a test. 
Issues:
There were no issues in this program worth noting. 
Profit:
I learned how to use comments from this program, also the program reenforced past knowledge so it was not forgotten.