Question

1. SUMMARY This project will simulate the card game Concentration for one player. In Concentration, all cards, or pictures in this case, are upside down. Two cards are chosen, and if they match they are taken out of the game. If they dont match in this version, one allowed fail is decremented. The player starts with a set number of allowed fails, such as 8. This project will involve the following: 2 Activities hooked together by an Intent. TableLayout Image Views. User OnClick event handling. 2. DETAILS a. Home Activity screen. i. Shows Wins, Losses, and highest number of Allowed Fails eft. ii. Shows a button to begin game. 1. Game Activity is started by expecting values back via its Intent. iii. Handles return from Game Activity upon GAME END. 1. If return is -1, then Losses number goes up 1. 2. If return is 0 or more, then Wins is incremented and the number returned from the game Activity replaces highest Allowed Fails left number if higher b. Game Activity. i. Displays 4x4 grid of pictures using the TableLayout type. ii. The pictures will be ImageView view types and each will have a unique ID. 1. There are 3 sets of pictures for each of the 16 ImageViews. a. The upside down pic, which can be a pic or something like that. b. The pic itself, revealed when the user clicks the upside down pic c. The completed pic, which can be a check mark, green light, etc. iii. The Activity java file will handle onClick events on the pictures 1. On first of two grid clicks: The picture is shown. 2. On second of two grid clicks: The second picture is shown for about 3 seconds or you can simply show a Continue button, then after the timer has ticked off 3 seconds or the user clicks the button (whichever way you designed it)...
media%2Fbe3%2Fbe35bd9c-eb95-4bf3-82f6-d6Please help me with this project by using Android Studio. I need to know the xml and the java activities in both activities. Also the manifest activity. I appreciate the effort and I'll rate you with 5 stars.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, This is what I could do in 2 hours .

MainActivity File :

package com.avi.samplememory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * Created by anushab on 3/19/2017.
 */

public class MainActivity extends Activity {
    Button startGame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startGame = (Button)findViewById(R.id.button);
        startGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Manager.class);
                startActivity(intent);
            }
        });
    }
}

Main activity xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:text="Start Game"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_weight="1" />
</LinearLayout>

Manager Activity :

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class Manager extends Activity {
    private static int ROW_COUNT = -1;
   private static int COL_COUNT = -1;
   private Context context;
   private Drawable backImage;
   private int [] [] cards;
   private List<Drawable> images;
   private Card firstCard;
   private Card seconedCard;
   private ButtonListener buttonListener;
   
   private static Object lock = new Object();
   
   int turns =0;
   private TableLayout mainTable;
   private UpdateCardsHandler handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
      
        
        handler = new UpdateCardsHandler();
        loadImages();
        setContentView(R.layout.main);
       

        
       backImage =  getResources().getDrawable(R.drawable.icon);
       

      
       buttonListener = new ButtonListener();
        
        mainTable = (TableLayout)findViewById(R.id.TableLayout03);
        
        
        context  = mainTable.getContext();
        
           newGame(4,4);
           



    }
    
    private void newGame(int c, int r) {
       ROW_COUNT = r;
       COL_COUNT = c;
       
       cards = new int [COL_COUNT] [ROW_COUNT];
       
       
       mainTable.removeView(findViewById(R.id.TableRow01));
       mainTable.removeView(findViewById(R.id.TableRow02));

       TableRow tr = ((TableRow)findViewById(R.id.TableRow03));
       tr.removeAllViews();
       
       mainTable = new TableLayout(context);
       tr.addView(mainTable);
       
        for (int y = 0; y < ROW_COUNT; y++) {
           mainTable.addView(createRow(y));
          }
        
        firstCard=null;
        loadCards();
        
        turns=0;
        ((TextView)findViewById(R.id.tv1)).setText("Tries: "+turns);
        
         
   }
    
    private void loadImages() {
       images = new ArrayList<Drawable>();
       
       images.add(getResources().getDrawable(R.drawable.card1));
       images.add(getResources().getDrawable(R.drawable.card2));
       images.add(getResources().getDrawable(R.drawable.card3));
       images.add(getResources().getDrawable(R.drawable.card4));
       images.add(getResources().getDrawable(R.drawable.card5));
       images.add(getResources().getDrawable(R.drawable.card6));
       images.add(getResources().getDrawable(R.drawable.card7));
       images.add(getResources().getDrawable(R.drawable.card8));
       images.add(getResources().getDrawable(R.drawable.card9));
       images.add(getResources().getDrawable(R.drawable.card10));
       images.add(getResources().getDrawable(R.drawable.card11));
       images.add(getResources().getDrawable(R.drawable.card12));
       images.add(getResources().getDrawable(R.drawable.card13));
       images.add(getResources().getDrawable(R.drawable.card14));
       images.add(getResources().getDrawable(R.drawable.card15));
       images.add(getResources().getDrawable(R.drawable.card16));
       images.add(getResources().getDrawable(R.drawable.card17));
       images.add(getResources().getDrawable(R.drawable.card18));
       images.add(getResources().getDrawable(R.drawable.card19));
       images.add(getResources().getDrawable(R.drawable.card20));
       images.add(getResources().getDrawable(R.drawable.card21));
      
   }

   private void loadCards(){
      try{
          int size = ROW_COUNT*COL_COUNT;
          
          Log.i("loadCards()","size=" + size);
          
          ArrayList<Integer> list = new ArrayList<Integer>();
          
          for(int i=0;i<size;i++){
             list.add(new Integer(i));
          }
          
          
          Random r = new Random();
       
          for(int i=size-1;i>=0;i--){
             int t=0;
             
             if(i>0){
                t = r.nextInt(i);
             }
             
             t=list.remove(t).intValue();
             cards[i%COL_COUNT][i/COL_COUNT]=t%(size/2);
             
             Log.i("loadCards()", "card["+(i%COL_COUNT)+
                   "]["+(i/COL_COUNT)+"]=" + cards[i%COL_COUNT][i/COL_COUNT]);
          }
       }
      catch (Exception e) {
         Log.e("loadCards()", e+"");
      }
      
    }
    
    private TableRow createRow(int y){
        TableRow row = new TableRow(context);
        row.setHorizontalGravity(Gravity.CENTER);
         
         for (int x = 0; x < COL_COUNT; x++) {
               row.addView(createImageButton(x,y));
         }
         return row;
    }
    
    private View createImageButton(int x, int y){
       Button button = new Button(context);
       button.setBackgroundDrawable(backImage);
       button.setId(100*x+y);
       button.setOnClickListener(buttonListener);
       return button;
    }
    
    class ButtonListener implements OnClickListener {

      @Override
      public void onClick(View v) {
         
         synchronized (lock) {
            if(firstCard!=null && seconedCard != null){
               return;
            }
            int id = v.getId();
            int x = id/100;
            int y = id%100;
            if(turns > 18){
               Toast.makeText(Manager.this,"Lost the game",Toast.LENGTH_SHORT).show();
               Intent intent = new Intent(Manager.this,MainActivity.class);
               startActivity(intent);
            }else{
               turnCard((Button)v,x,y);
            }

         }
         
      }

      private void turnCard(Button button,int x, int y) {
         button.setBackgroundDrawable(images.get(cards[x][y]));
         
         if(firstCard==null){
            firstCard = new Card(button,x,y);
         }
         else{ 
            
            if(firstCard.x == x && firstCard.y == y){
               return; //the user pressed the same card
            }
               
            seconedCard = new Card(button,x,y);
            
            turns++;
            ((TextView)findViewById(R.id.tv1)).setText("Tries: "+turns);
            
         
            TimerTask tt = new TimerTask() {
               
               @Override
               public void run() {
                  try{
                     synchronized (lock) {
                       handler.sendEmptyMessage(0);
                     }
                  }
                  catch (Exception e) {
                     Log.e("E1", e.getMessage());
                  }
               }
            };
            
              Timer t = new Timer(false);
                 t.schedule(tt, 1300);
         }
         
            
         }
         
      }
    
    class UpdateCardsHandler extends Handler{
       
       @Override
       public void handleMessage(Message msg) {
          synchronized (lock) {
             checkCards();
          }
       }
        public void checkCards(){
              if(cards[seconedCard.x][seconedCard.y] == cards[firstCard.x][firstCard.y]){
                firstCard.button.setVisibility(View.INVISIBLE);
                seconedCard.button.setVisibility(View.INVISIBLE);
             }
             else {
                seconedCard.button.setBackgroundDrawable(backImage);
                firstCard.button.setBackgroundDrawable(backImage);
             }
              
              firstCard=null;
             seconedCard=null;
           }
    }
    
   
    
    
}

Manager activity xml :

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout03"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <TableRow
        android:id="@+id/TableRow04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center">


    </TableRow>

    <TableRow
        android:id="@+id/TableRow05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="10sp">

        </TextView>
    </TableRow>

    <TableRow
        android:id="@+id/TableRow01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center">


    </TableRow>

    <TableRow
        android:id="@+id/TableRow02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center">


    </TableRow>

    <TableRow
        android:id="@+id/TableRow03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center">


    </TableRow>


</TableLayout>

Card Java file :

import android.widget.Button;


public class Card{

   public int x;
   public int y;
   public Button button;
   
   public Card(Button button, int x,int y) {
      this.x = x;
      this.y=y;
      this.button=button;
   }
   

}

Android Manifest file :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.aviy.samplememory"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="com.aviy.samplememory.MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.aviy.samplememory.Manager"></activity>

    </application>
    <uses-sdk android:minSdkVersion="2" />

</manifest> 
Add a comment
Know the answer?
Add Answer to:
Please help me with this project by using Android Studio. I need to know the xml...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project 03 (Chapter 04 Multiple Activities) Code an app in android studio that does the following:...

    Project 03 (Chapter 04 Multiple Activities) Code an app in android studio that does the following: There is one button in the middle of the screen. The button color is green and its text says 0. When the user clicks on the button GO, we go to a second activity that is red and has one button in the middle of the screen; its text says BACK. When the user clicks on that button, we go back to the first...

  • I need help developing this app using java in android studio Assignment: Tic-Tac-Toe app User interface...

    I need help developing this app using java in android studio Assignment: Tic-Tac-Toe app User interface Operation The app allows the user to play a game of Tic-Tac-Toe. The user can click the New Game button at any time to start a new game. The app displays other messages to the user as the game progresses such as (1) whose turn it is, (2) if a player wins, and (3) if the game ends in a tie. Specifications The app...

  • This is python3. Please help me out. Develop the following GUI interface to allow the user...

    This is python3. Please help me out. Develop the following GUI interface to allow the user to specify the number of rows and number of columns of cards to be shown to the user. Show default values 2 and 2 for number of rows and number of columns. The buttons "New Game" and "Turn Over" span two column-cells each. Fig 1. The GUI when the program first starts. When the user clicks the "New Game" button, the program will read...

  • If anyone here is a Java and Android Studio expert please I really need help fulfilling...

    If anyone here is a Java and Android Studio expert please I really need help fulfilling the requirements needed to make a Rock Paper Scissors game. I mainly need Java code to complete it I will post what I have so far I don't have much time left please if anyone can help me I would really appreciate it. Here's what the app is supposed to do... The player should be able to select either Rock, Paper, or Scissors.The app...

  • programming language c++ its a project if anyone can help me it's will be appreciated thnk...

    programming language c++ its a project if anyone can help me it's will be appreciated thnk u. u have to create a code based on the information im giving u you can do it in 1 or 2 day i can wait 1:26 #WW 2.53% Example: A user starts with 1000 points. Game #1 - user chooses to risk 200 points. User wins game (beats the dealer). User now has 1200 points. Game #2 - user chooses to risk 500...

  • Please I need your help I have the code below but I need to add one...

    Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...

  • please help me fix this. I'm getting it all mixed up in user defined functions. here's...

    please help me fix this. I'm getting it all mixed up in user defined functions. here's the question. This week we'll write a C program that lets the user play the game of Rock, Paper, Scissors against the computer. Your program will call a user-defined function named display_rules that displays the rules of the game. It then proceeds to play the game. To determine the winner, your program will call another user- defined function called determine_winner that takes two integer...

  • I am just curious about this question... please can you answer with applying indent and space...

    I am just curious about this question... please can you answer with applying indent and space clearly. Furthermore, can you make answer shortly as possible..? This is a python question Question 6.34 The two-player card game war is played with a standard deck of 52 cards. A shuffled deck is evenly split among the two players who keep their decks face-down. The game consists of battles until one of the players runs out of cards. In a battle, each player...

  • please help me with the project it's an embedded system project and i need a help...

    please help me with the project it's an embedded system project and i need a help with the code (for setting part especially) the code should be in C language not C++ and the mbed application board will be used. thanks in advance :) Note: mbed development board are used here with the mbed application board Design and implement an Air Conditioner Controller System to control automatically an air conditioner based on the ambient tempreture and surrounding light (Daytime/Nighttime). The...

  • I need help figuring out how to code this in C++ in Visual Studio. Problem Statement:...

    I need help figuring out how to code this in C++ in Visual Studio. Problem Statement: Open the rule document for the game LCR Rules Dice Game. Develop the code, use commenting to describe your code and practice debugging if you run into errors. Submit source code. Use the document to write rules for the user on how to play the game in a text file. Then, using the information from the resource articles, create a program that will read...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT