Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Improving my coding abilities

  1. #1
    Junior Member
    Join Date
    May 2020
    Location
    Netherlands
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Improving my coding abilities

    Hello everyone,

    I've just joined the forum. I've started to learn some coding since last week as a hobby. I have no real experience with programming apart from some real simple HTML and Java is the first language I'm now seriously taking on, because my end goal for now is to create a complex game that I have in mind with a GUI and everything. However, before I tumble head first into that project, I need to get the basics down first, otherwise I'm going to get lost.

    I decided I'd write something from scratch and only use the forums to look up specific syntax or parts that I didn't know how to do, so that way I would mostly follow my own sense of logic to work through the program and thus most of the mistakes should be in my own line of thinking. That's where I hope your help comes in, because although the code seems to work as intended, I'm sure there's inefficiencies, mistakes and/or violations of certain principles I'm not aware of yet.

    My question is if you guys have any suggestions to make this better and if you have the time, a little explanation as to why what I'm doing is not optimal or plain wrong. Your help has already proven to be invaluable as I've used the forum to find most of the answers to my initial questions and any suggestions will be greatly appreciated.

    So here's the code:

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
     
    public class BingoMain {
     
    	public static void main(String[] args) {
     
    		boolean[] bingoCheck = new boolean[3];
    		boolean playerWin = false;
    		boolean computerWin = false;
    		int playerHit = 0;
    		int computerHit = 0;
    		int bingoNumber = 0;
    		int rowColumn = 5;
    		int fields = rowColumn * rowColumn;
    		int listSize = fields * 4;
     
    		ArrayList<Integer> playerNumbers = new ArrayList<Integer>(listSize);
    		ArrayList<Integer> computerNumbers = new ArrayList<Integer>(listSize);
    		ArrayList<Integer> picker = new ArrayList<Integer>(listSize);
     
    		// Generate 3 shuffled lists with integers from 1 to listSize
    		// Refers to method 1: generateArray
    		playerNumbers = generateArray(listSize);
    		computerNumbers = generateArray(listSize);
    		picker = generateArray(listSize);
     
    		// truncate the bingo cards to the size of the bingo card (listSize)
    		playerNumbers.subList(fields, listSize).clear();
    		computerNumbers.subList(fields, listSize).clear();
     
    		// Format playerNumbers and computerNumbers in a 2 dimensional array
    		// Refers to method 2: printCard
    		System.out.println("This is your Bingo card: ");
    		System.out.println();
    		printCard(playerNumbers, rowColumn);
    		System.out.println();
    		System.out.println("This is the computer's Bingo card: ");
    		System.out.println();
    		printCard(computerNumbers, rowColumn);
    		System.out.println();
     
    		// Go through the picker list and replace numbers with 0 in playerNumbers and computerNumbers if they match
    		for (int i = 0; i < listSize; i++) {
    			bingoNumber = picker.get(i);
    			if (playerNumbers.contains(bingoNumber)) {
    				playerHit = playerNumbers.indexOf(bingoNumber);
    				playerNumbers.set(playerHit, 0);
    			}
     
    			if (computerNumbers.contains(bingoNumber)) {
    				computerHit = computerNumbers.indexOf(bingoNumber);
    				computerNumbers.set(computerHit, 0);
    			}
     
    			// Check if row, column, diagonal 1 and/or diagonal 2 is full for player and computer
    			// Refers to method 3: checkArray
    			if (Collections.frequency(playerNumbers, 0) >= rowColumn) {
    				bingoCheck = checkArray(playerNumbers, rowColumn);
    				if ((bingoCheck[0] == true) | (bingoCheck[1] == true) | (bingoCheck[2] == true)
    						| (bingoCheck[3] == true)) {
    					playerWin = true;
    				}
    			}
     
    			if (Collections.frequency(computerNumbers, 0) >= rowColumn) {
    				bingoCheck = checkArray(computerNumbers, rowColumn);
    				if ((bingoCheck[0] == true) | (bingoCheck[1] == true) | (bingoCheck[2] == true) | (bingoCheck[3] == true)) {
    					computerWin = true;
    				}
    			}
     
    			// Determine who won or if there was a draw, then print the cards
    			if ((playerWin == true) && (computerWin && true)) {
    				System.out.println("It's a draw!");
    				System.out.println();
    				System.out.println("Your card: ");
    				printCard(playerNumbers, rowColumn);
    				System.out.println();
    				System.out.println("The computer card: ");
    				printCard(computerNumbers, rowColumn);
    				break;
    			} else if (playerWin == true) {
    				System.out.println("Congratulations, you won!");
    				System.out.println();
    				System.out.println("Your card: ");
    				printCard(playerNumbers, rowColumn);
    				System.out.println();
    				System.out.println("The computer card: ");
    				printCard(computerNumbers, rowColumn);
    				break;
    			} else if (computerWin == true) {
    				System.out.println("Too bad... You lost!");
    				System.out.println();
    				System.out.println("Your card: ");
    				printCard(playerNumbers, rowColumn);
    				System.out.println();
    				System.out.println("The computer card: ");
    				printCard(computerNumbers, rowColumn);
    				break;
    			}
    		}
    	}
     
    	// Method 1: generateArray
    	// Generate populated and shuffled arrays of the appropriate list size
    	public static ArrayList<Integer> generateArray(int listSize) {
    		ArrayList<Integer> generatedArray = new ArrayList<Integer>(listSize);
     
    		for (int i = 1; i <= listSize; i++) {
    			generatedArray.add(i);
    		}
    		Collections.shuffle(generatedArray);
    		return generatedArray;
    	}
     
    	// Method 2: printCard
    	// method to print the arrays in rows and columns
    	public static void printCard(ArrayList<Integer> array, int rowColumn) {
    		int i = 0;
    		Object[][] bingoCard = new Object[rowColumn][rowColumn];
     
    		for (int j = 0; j < rowColumn; j++) {
    			for (int k = 0; k < rowColumn; k++) {
    				bingoCard[j][k] = array.get(i);
    				i++;
    				if (i > rowColumn * rowColumn) {
    					break;
    				}
    			}
    		}
    		System.out.println(Arrays.deepToString(bingoCard).replace("[[", "[").replace("]]", "]").replace("], ", "]\n"));
    	}
     
    	// Method 3: checkArray
    	// Method to return boolean array if there's a bingo, where row is index 0, column on 1, diagonal 1 on 2 and diagonal 2 on 3
    	public static boolean[] checkArray(ArrayList<Integer> array, int rowColumn) {
    		boolean row = false;
    		boolean column = false;
    		boolean diagonal1 = false;
    		boolean diagonal2 = false;
    		boolean[] bingoReturn = new boolean[4];
    		ArrayList<Integer> dummy = new ArrayList<Integer>(rowColumn);
    		ArrayList<Integer> placeHolder = new ArrayList<Integer>(rowColumn);
     
    		// Populate dummy list with 0's to compare with
    		for (int i = 0; i < rowColumn; i++) {
    			dummy.add(0);
    		}
     
    		// Check rows for bingo
    		for (int i = 0; i <= rowColumn * rowColumn-1; i += rowColumn) {
    			if ((array.get(i) == 0) && (row == false)) {
    				if (array.subList(i, i + rowColumn).equals(dummy)) {
    					row = true;
    					bingoReturn[0] = row;
    					System.out.println("Bingo! Row " + ((i / rowColumn) + 1));
    					break;
    				}
    			}
    		}
     
    		// Check columns for bingo
    		for (int i = 0; i < rowColumn; i++) {
    			if (array.get(i) == 0) {
    				for (int j = 0; j < rowColumn * rowColumn - 1; j += rowColumn) {
    					if ((array.get(i + j) != 0) | (row == true)) {
    						placeHolder.clear();
    						break;
    					} else {
    						placeHolder.add(array.get(i + j));
    					}
    					if (placeHolder.equals(dummy)) {
    						column = true;
    						bingoReturn[1] = column;
    						System.out.println("Bingo! Column " + (i + 1));
    						break;
    					}
    				}
    				placeHolder.clear();
    			}
    		}
     
    		// Check diagonal 1 for bingo
    		for (int i = 0; i <= rowColumn * rowColumn - 1; i += rowColumn + 1) {
    			if (array.get(i) == 0) {
    				placeHolder.add(0);
    			} else {
    				placeHolder.clear();
    				break;
    			}
    			if (placeHolder.equals(dummy)) {
    				diagonal1 = true;
    				bingoReturn[2] = diagonal1;
    				System.out.println("Bingo! Diagonal 1");
    				break;
    			}
    		}
     
    		placeHolder.clear();
     
    		// Check diagonal 2 for bingo
    		for (int i = rowColumn - 1; i <= rowColumn * rowColumn - 1; i += rowColumn - 1) {
    			if (array.get(i) == 0) {
    				placeHolder.add(0);
    			} else {
    				placeHolder.clear();
    				break;
    			}
    			if (placeHolder.equals(dummy)) {
    				diagonal2 = true;
    				bingoReturn[3] = diagonal2;
    				System.out.println("Bingo! Diagonal 2");
    				break;
    			}
    		}
    		placeHolder.clear();
    		dummy.clear();
    		return bingoReturn;
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Improving my coding abilities

    First suggestions:
    Move the code out of the main method into methods in the class.

    Don't test boolean variables with ==
    if (bingoCheck[0] == true)
    The boolean variable itself is all that is needed
    if ((bingoCheck[0])

    If all the cases are supposed to have been tested in a chain of if/else if statements, add an ending else to catch the case when you have missed one. Have it print a message saying what ever.

    Nice work with the comments. Very few students use them. I like to see them myself.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Rookie_Programmer (May 13th, 2020)

  4. #3
    Junior Member
    Join Date
    May 2020
    Location
    Netherlands
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Improving my coding abilities

    Thanks for the suggestions!

    A question. With moving the code outside the main method, do you mean like I did with the generator and printing the cards and such? I don't fully understand the use of classes and methods yet, except that I saw the value of putting a calculation in a method if it is performed multiple times. What are the other advantages of placing stuff outside the main method or alternatively, what are the disadvantages of keeping stuff in it?

    Ah yes, the if statement already expects a boolean, right? That == was completely redundant.

    I figured that the if statements should catch everything, but it makes sense to double check with else statements. Good suggestion!

    I initially wrote down into comments what I thought the steps should be and I then used that as a guideline, changing it as I got further and learned more . I'm not very good at reading the code quickly yet, so this helps me find everything quicker.

    Thanks again!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Improving my coding abilities

    I'm not that good with programming theory.
    Try this site for more academic helpers: http://www.coderanch.com/forums
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Improving the SQL statement
    By aussiemcgr in forum Other Programming Languages
    Replies: 9
    Last Post: December 25th, 2013, 12:22 PM
  2. [SOLVED] I need help with my coding
    By knockturnal22 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 16th, 2012, 04:19 PM
  3. Replies: 2
    Last Post: February 19th, 2012, 07:36 AM
  4. Improving the code to produce the same program
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 13
    Last Post: April 8th, 2011, 09:41 PM