lundi 21 mars 2022

I am making a memory game. How do I randomize the images in my gridview?

I am trying to randomize the images shown in my 4x4 grid for a memory card game. But I do not know how. Could someone help me.

I am very lost, the game itself works but does not randomise the images like i want it to. I have tried various methods but my knowledge of java is not deep enough. This is for a small project and we have yet to be taught randomising and

My game activity

public class Game extends AppCompatActivity { 
//Variables 
ImageView curView = null; 
private int countPair = 0; 
//Score 
private TextView scoreLabel; 
private int score = 0; 
//Tries 
private int tries = 0; 
private TextView tryLabel; 
//Attempts 
private int attempt = 10; 
private TextView attemptlabel; 
//FinalScore 
private TextView finalscore;

//The cards images initialised
final int[] drawable = new int[] {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three,
        R.drawable.four,
        R.drawable.five,
        R.drawable.six,
        R.drawable.seven,
        R.drawable.eight,
        R.drawable.nine,
        R.drawable.ten,
        R.drawable.eleven,
        R.drawable.jack,
        R.drawable.king,
        R.drawable.trump};
//Position of the cards / variables
int[] pos = {0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7};
int currentPos = -1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    //Hides action bar
    getSupportActionBar().hide();


    //Changes score text
    scoreLabel = (TextView) findViewById(R.id.scoreText);
    scoreLabel.setText("Score : 0");

    //Changes try text
    tryLabel = (TextView) findViewById(R.id.tries);
    tryLabel.setText("Tries : 0");

    //Changes attempt text
    attemptlabel = (TextView) findViewById(R.id.attemptext);
    attemptlabel.setText("Attempts left : 10");

    //Change text for final score
    finalscore = (TextView) findViewById(R.id.textView15);

    //Initialises as a MediaPlayer
    final MediaPlayer yay = MediaPlayer.create(this, R.raw.yay);
    final MediaPlayer fail= MediaPlayer.create(this, R.raw.fail);
    final MediaPlayer music = MediaPlayer.create(this, R.raw.battle);

    //Button OnClick to enable and loop music
    final Button button1 = (Button)findViewById(R.id.song);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            music.setLooping(true);
            music.start();
        }
    });
    //Button OnClick to pause music
    final Button button2 = (Button)findViewById(R.id.stop);
    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            music.pause();
        }
    });






    //Initiates GridView alongside the ImageAdapter I created
    GridView gridView = (GridView) findViewById(R.id.gridView);
    //Collections.shuffle(list);
    ImageAdapter imageAdapter = new ImageAdapter(this);
    gridView.setAdapter(imageAdapter);





    
    //What happens when you click the card
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        //This section of code enables the position of the code
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            if (currentPos < 0)
            {
                currentPos = position;
                curView = (ImageView)view;
                ((ImageView)view).setImageResource(drawable[pos[position]]);
                //Whenever an attempt is made give 1 point for tries and remove from attempts left
                tries +=1;
                attempt -=1;


            }

            else
            {
                //If the card is pressed and it is wrong, then say to the user it is incorrect
                if (currentPos == position)
                {
                    ((ImageView)view).setImageResource(R.drawable.back);
                }
                else if (pos[currentPos]!=pos[position])
                {
                    curView.setImageResource(R.drawable.back);
                    Toast.makeText(getApplicationContext(),"No match, try again!", Toast.LENGTH_SHORT).show();
                    //When correct plays the wav fail file below
                    fail.start();
                }
                else
                //If the card is pressed and it is right, then say to the user it is correct
                {
                    ((ImageView)view).setImageResource(drawable[pos[position]]);
                    countPair++;
                    //Add 1 points everytime the user is correct
                    score +=1;
                    //When correct plays the wav yay file below
                    yay.start();


                    Toast.makeText(getApplicationContext(),"That was correct!",Toast.LENGTH_SHORT).show();


                    //When all the cards are correct
                    if(countPair==0)
                    {
                        Toast.makeText(getApplicationContext(),"You win!",Toast.LENGTH_SHORT).show();
                    }
                }
                //Current position is back

                currentPos = -1;
                //Change text to x + score/tries/attempt
                scoreLabel.setText("Score : " + score);
                tryLabel.setText("Tries : " + tries);
                attemptlabel.setText("Attempts left : " + attempt);

                //If attempts reach 0 then a toast message shows your final score
                if(attempt==0)
                {
                    Toast.makeText(getApplicationContext(),"Game Over!, your final score was : "+ score,Toast.LENGTH_SHORT).show();
                    //Disables the ability to continue playing until reset
                    gridView.setOnItemClickListener(null);
                    //Changes Match them all to You lose after 0 attempts left
                    finalscore.setText("YOU LOST!");

                }
                //Allows the button to be pressed
                final Button button = (Button)findViewById(R.id.finish);
                button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        //Makes a toast message saying games over, alongside how many attempts were successful
                        Toast.makeText(getApplicationContext(),"Game Ended! Successful attempts = "+ score,Toast.LENGTH_SHORT).show();
                        //Makes a toast message showing how many tried were attempted
                        Toast.makeText(getApplicationContext(),"Tries Attempted = "+ tries,Toast.LENGTH_SHORT).show();
                        //Disables onClick on the gridView to stop the game
                        gridView.setOnItemClickListener(null);
                        //Changes the title to Game Ended
                        finalscore.setText("Game Ended");
                        //Accuracy of attempts (How many were correct in a percentage))
                        float endScore = tries / score;
                        float magical = endScore*100;
                        Toast.makeText(getApplicationContext(),"Total accuracy= "+ magical,Toast.LENGTH_SHORT).show();


                    }
                });

            }
        }
    });


}

}

Then my base adapter

public class ImageAdapter extends BaseAdapter

{ private Context context;
public ImageAdapter(Context context)
{
    this.context = context;
}

@Override
public int getCount() {
    return 16;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null)  //If it is not recycled, initialise some attributes
    {
        //The parameters of the card block.
        imageView = new ImageView(this.context);
        imageView.setLayoutParams(new GridView.LayoutParams(350,350));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setPadding(8,8,8,8);

    }
    //Changes the block to "back" which is the cards behind.

    else imageView = (ImageView) convertView;


        imageView.setImageResource(R.drawable.back);


    //If the card is correct then show the image.
    return imageView;


}



Aucun commentaire:

Enregistrer un commentaire