Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class OneDimensionalMemory {
- private Scanner cin=new Scanner(System.in);
- private int[] a={1, 1, 2, 2, 3, 3, 4, 4, 5, 5};
- private int[] b=new int[10];
- private boolean ok=false;
- private void shuffle(int[] a) {
- int temp;
- for (int i=0; i<100; i++) {
- int r1=(int) (Math.random()*a.length);
- int r2=(int) (Math.random()*a.length);
- temp=a[r1];
- a[r1]=a[r2];
- a[r2]=temp;
- }
- }
- private int input() {
- int num;
- while (true) {
- try {
- num=Integer.parseInt(cin.nextLine())-1;
- if (num>=0 && num<10) {
- break;
- } else {
- System.out.print("The number has to be from 1 to 10. Try again: ");
- }
- } catch (Exception e) {
- System.out.print("The value you entered is not valid. Try again: ");
- }
- }
- return num;
- }
- private void printUserChoice(int[] a, int x, int y) {
- int k=0;
- System.out.print("[ ");
- for (int i=0; i<a.length; i++) {
- if ((i==x || i==y)) {
- System.out.print(a[i] + " ");
- } else if (a[i]==b[i]) {
- System.out.print(a[i] + " ");
- } else {
- System.out.print(". ");
- }
- }
- System.out.println("]");
- if (a[x]==a[y]) {
- System.out.println("Lucky!");
- k++;
- b[x]=a[x];
- b[y]=a[y];
- } else {
- System.out.println("Too bad, try again!");
- }
- if (k==5) {
- ok=true;
- }
- }
- private void run() {
- int x, y;
- shuffle(a);
- while (!ok) {
- System.out.print("Enter index one (1-10): "); x=input();
- System.out.print("Enter index two (1-10): "); y=input();
- while (x==y) {
- System.out.print("The second index can't be the same with the first one. Try again: ");
- y=input();
- }
- printUserChoice(a, x, y);
- }
- System.out.println("You found all the pairs. Nice!");
- }
- public static void main(String[] args) {
- new OneDimensionalMemory().run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement