Results 1 to 1 of 1
- 03-14-2011, 01:28 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Latest CS Project. Switching values in a Array
Program 3: Square-Up
This is my assignment, I am currently on part E. I need to find out how arrange the values.
0 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
Lets say I choose row 1, my goal is for the values in the array of row 1, (0-6) to be assigned into 9,16,3,10,17,4,11; respectively.
I'm not sure how to do this while also shuffling the board. Any help is appreciated.
Here is a copy of my class Board:
Here is the class Square:Java Code:import java.util.Random; // allows getting a random number import java.util.Scanner; // allows user input from the keyboard class Board { // create the array of 52 cards public static void main(String[] args) { Square[] cardDeck= new Square[52]; // setting up the deck for (int x = 0; x <= 12; x++){ int l=x+1; String y = "" + l; cardDeck[x] = new Square( 0,0, 40, "red", false, y); // red } for (int x = 13; x <= 25; x++){ int l=x-12; String y = "" + l; cardDeck[x] = new Square( 0,0, 40, "green", false, y); // green } for (int x = 26; x <= 38; x++){ int l=x-25; String y = "" + l; cardDeck[x] = new Square( 0,0, 40, "blue", false, y); // blue } for (int x = 39; x <= 51; x++){ int l=x-38; String y = "" + l; cardDeck[x] = new Square( 0,0, 40, "yellow", false, y); // yellow } // output of original deck System.out.println("The deck is:"); for(int x=0; x<=51; x++){ char theColor = cardDeck[x].getColor().charAt( 0); String theNumber = cardDeck[x].getLabel(); if (x<12){ System.out.print(theColor+theNumber+" "); } if (x==12){ System.out.print(theColor+theNumber+"\n"); } if (x>12 && x<25){ System.out.print(theColor+theNumber+" "); } if (x==25){ System.out.print(theColor+theNumber+"\n"); } if (x>25 && x<38){ System.out.print(theColor+theNumber+" "); } if (x==38){ System.out.print(theColor+theNumber+"\n"); } if (x>38 && x<51){ System.out.print(theColor+theNumber+" "); } if (x==51){ System.out.print(theColor+theNumber+"\n"); } } System.out.println("Enter any letter to continue:"); Scanner keyboard = new Scanner( System.in); keyboard.next(); //Shuffling the deck.. System.out.println("\nThe shuffled deck is:"); Random rnG = new Random(); //--- Shuffle by exchanging each element randomly for (int c=0; c<cardDeck.length; c++) { int index1 = rnG.nextInt(cardDeck.length); Square temp = cardDeck[c]; cardDeck[c] = cardDeck[index1]; cardDeck[index1] = temp; } for(int x=0; x<=51; x++){ char theColor = cardDeck[x].getColor().charAt( 0); String theNumber = cardDeck[x].getLabel(); if (x<12){ System.out.print(theColor+theNumber+" "); } if (x==12){ System.out.print(theColor+theNumber+"\n"); } if (x>12 && x<25){ System.out.print(theColor+theNumber+" "); } if (x==25){ System.out.print(theColor+theNumber+"\n"); } if (x>25 && x<38){ System.out.print(theColor+theNumber+" "); } if (x==38){ System.out.print(theColor+theNumber+"\n"); } if (x>38 && x<51){ System.out.print(theColor+theNumber+" "); } if (x==51){ System.out.print(theColor+theNumber+"\n"); } } //Making a new array with first 21 cards. Square[] selectedCards = new Square[ 21]; for( int i=0; i<21; i++) { selectedCards[ i]=cardDeck[ i]; // copy the address from the card deck } System.out.println("Enter any letter to continue:"); keyboard.next(); System.out.println("\nThe first 21 cards is:"); for(int x=0; x<=20; x++){ char theColor = selectedCards[x].getColor().charAt( 0); String theNumber = selectedCards[x].getLabel(); if (x<6){ System.out.print(theColor+theNumber+" "); } if (x==6){ System.out.print(theColor+theNumber+"\n"); } if (x>6 && x<13){ System.out.print(theColor+theNumber+" "); } if (x==13){ System.out.print(theColor+theNumber+"\n"); } if (x>13 && x<20){ System.out.print(theColor+theNumber+" "); } if (x==20){ System.out.print(theColor+theNumber+"\n"); } } System.out.println("Think of one of the cards and remember it. Enter any letter when you're ready:"); keyboard.next(); //Ask for userInput System.out.println("To make it harder for me I'm going to reshuffle the cards, but first\n" +"I need a little help, since my mind-reading powers are limited.\n" +"In the table above, which row (1, 2, or 3) is your card in?"); int userInput = keyboard.nextInt(); //Rearrange the arrays //Reshuffle the deck if (userInput == 1){ for (int c=0; c<8; c++) { int index1 = rnG.nextInt(cardDeck.length); Square temp = selectedCards[c]; selectedCards[c] = selectedCards[index1]; selectedCards[index1] = temp; } } } }//end Board
Java Code:public class Square { private int size; private int xPosition; private int yPosition; private String color; private boolean isVisible; private String label; private int id; // used to identify squares without using their label /** * Create a new Square at default position with default color. */ public Square() { size = 30; xPosition = 60; yPosition = 50; color = "red"; isVisible = false; label = ""; id = 0; } /** * Create a new Square with supplied position, size, color, and visibility */ public Square( int x, int y, int theSize, String theColor, boolean visibility) { xPosition = x; yPosition = y; size = theSize; color = theColor; isVisible = visibility; label = ""; id = 0; } /** * Create a new Square with supplied position, size, color, and visibility */ public Square( int x, int y, int theSize, String theColor, boolean visibility, String theLabel) { xPosition = x; yPosition = y; size = theSize; color = theColor; isVisible = visibility; label = theLabel; id = 0; } // Square copy constructor, making a new Square based on an existing Square public Square( Square otherSquare) { xPosition = otherSquare.xPosition; yPosition = otherSquare.yPosition; size = otherSquare.size; color = otherSquare.color; isVisible = otherSquare.isVisible; label = otherSquare.label; id = otherSquare.id; } /** * Get the color for this square */ public String getColor() { return color; } /** * Get the xPosition for this square */ public int getX() { return xPosition; } /** * Get the yPosition for this square */ public int getY() { return yPosition; } /** * Get the size for this square */ public int getSize() { return size; } /** * Get the label for this square */ public String getLabel() { return label; } /** * Get the id for this square */ public int getID() { return id; } /** * Get the visibility for this square */ public boolean getVisibility() { return isVisible; } /** * Make this Square visible. If it was already visible, do nothing. */ public void makeVisible() { isVisible = true; } /** * Make this Square invisible. If it was already invisible, do nothing. */ public void makeInvisible() { isVisible = false; } /** * Slowly move the Square horizontally by 'distance' pixels. */ public void slowMoveHorizontal(int distance) { int delta; if(distance < 0) { delta = -1; distance = -distance; } else { delta = 1; } for(int i = 0; i < distance; i++) { xPosition += delta; } } /** * Slowly move the Square vertically by 'distance' pixels. */ public void slowMoveVertical(int distance) { int delta; if(distance < 0) { delta = -1; distance = -distance; } else { delta = 1; } for(int i = 0; i < distance; i++) { yPosition += delta; } } /** * Change the size to the new size (in pixels). Size must be >= 0. */ public void changeSize(int newSize) { size = newSize; } /** * Change the position of the square on the canvas. */ public void setPosition(int newX, int newY) { xPosition = newX; yPosition = newY; } /** * Change the color. Valid colors are "red", "yellow", "blue", "green", * "magenta", "black", "cyan", "darkGray", "gray", "lightGray", "orange", * "pink", "white" */ public void setColor(String newColor) { color = newColor; } /* * Set the label */ public void setLabel( String theLabel) { label = theLabel; } /* * Set the id */ public void setID( int theID) { id = theID; } /* * Set the size */ public void setSize( int theSize) { size = theSize; } }
Similar Threads
-
help inputting values into an array
By pds8475 in forum New To JavaReplies: 3Last Post: 01-22-2011, 05:45 PM -
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
Pass Values into 2D Array?
By noble in forum New To JavaReplies: 7Last Post: 11-09-2010, 07:30 AM -
An Array of different integer values
By lithium002 in forum New To JavaReplies: 7Last Post: 12-04-2009, 05:25 AM -
Same values in an array
By hawaiifiver in forum New To JavaReplies: 3Last Post: 02-24-2009, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks