The Simple Lotto game uses custom methods to run the code rather than just the main method. This program could have been broken down futher into different classes and using more methods, but after the trials I faced with this program I decided to keep it simple for now. Simple Lotto also utilizes ArrayList objects instead of a typical array to hold the player’s numbers, computer’s numbers, and matching numbers. There are some different conditional statements checking for duplications, input, and case size.
I began by declaring three ArrayLists of Integers that would each be used to hold a set of numbers. One would hold the player’s input, a second to hold the computer’s randomly generated numbers, and a third to hold the numbers that were matching numbers.
I decided to just put everything inside of a method so I had to create an object of SimpleLotto to use the method to run the program.
Inside of the method the program displays a welcome message and asks the user for input. To fill the pNums ArrayList with the user’s input I had to use a for loop. This could have been done two ways, by using a final as a conditional inside the loop or by using the size() method of the ArrayList object. I decided to use some final variables since I already know the program would need 10 player numbers, 20 computer numbers, 0 for a minimum value and 80 for a maximum value.
Inside of the loop, the program uses a Scanner object to get the next input from the user. It then checks to make sure the input was greater than or equal to zero and less than or equal to eighty. If the conditions were not met, it outputs a message to the user displaying the value of the input and requesting a new number, then decrements the variable used to hold the number of iterations of the loop. If the conditions were met, the program moves on to another conditional statement to check if the user had already input the number entered. If the input passed both conditional tests (between zero and eighty and is a new number) the input was added to the ArrayList using the add() method.
Outside of the first for loop the program displays the input to the user and asks if they wish to continue on. Another conditional statement was added here to check if the user enters anything other than a “P” or a “Q” providing them with a message to enter their decision again.
Based on the users input the program branches off from here. If the player enters “Q” or “q”, the program calls the quit() method and displays a goodbye message to the user. If the user enters a “P” or “p” the program continues to generate random numbers.
**NOTE** This part had me stuck for a while. I had used a nested for loop with the initial program that used basic arrays but in the process of recreating that nested for loop I got stuck. I was able to generate twenty random numbers between 0 and 80 without a problem but when I tried to have the program check if that number had already been generated, I got stuck. I ended up creating a thread over at the JavaRanch and to have the members of the forum take a look at the portion of the code. They were helpful as always and pointed out what I was doing wrong and why it was acting the way it was (creating the duplicate number anyways).
To generate twenty different random numbers I used a while loop. The condition of the while loop was the size of the ArrayList and if it was under the value of the final CNUMS variable. While inside of the loop, the program creates a new random number and assigns it to a new Integer variable num. A second conditional statement was used to check the contents of the array for the number. If the cNums ArrayList did not have an element equal to the new num variable then that value held by num was added to the cNums ArrayList.
Outside of the while loop the program continues by displaying the contents of cNums to the user. After displaying the contents of the cNums ArrayList, I used a third loop to fill the last ArrayList with matching numbers. This loop cycled through the larger ArrayList, cNums, and compared each element with the pNums ArrayList. If the objects inside of each element were equal, that element was added to mNums.
Finally the program displays the matching numbers and the number of matches using the ArrayList’s size() method.
Simple Lotto didn’t end up being so simple. I began by creating a GUI using Swing and providing the user with a board containing 80 grids showing a number on each grid. The bottom of the screen provided the user with an area to enter their name and their numbers. The right portion of the screen was the Score Board, which was supposed to contain the user’s name, user’s numbers, computer generated numbers, matching numbers, and a score based on the number of matches. I threw in some standard text output to display using println and everything was working fine (printing out the correct information), but the ScoreBoard would not refresh itself showing the updated information. After doing some research and fussing around with valide() and repaint() I believe the issue is with Threads, or my lack of multithreading knowledge at this point (which is almost nonexistant).
I decided to start over again removing the Swing components and making it a simple console based program using standard arrays. I was able to get the project to work as intended by checking conditionals and providing accurate output. I wasn’t entirely happy with the difficulty increase from project one to project two so I decided to change it up a little bit. I recalled reading about ArrayLists and I was interested in learning more about them. This projected ended up being a perfect place to start learning about them and using them.
This project ended up being exactly what I was looking for, which is a good learning experience. Encountering the troubles with the GUI, I learned a bit about Threads, touched up on exception handling, and got more experience with Swing and AWT. The decision to recreate the program using ArrayLists was even more beneficial. I have learned to use ArrayList objects and its methods and know have an understanding of wrapper classes as well.