1. 程式人生 > >上課案列-翻牌遊戲(重置、歷史記錄)

上課案列-翻牌遊戲(重置、歷史記錄)

settag tin car size public ext round utf-8 constrain

布局文件:

activity_main.xml:
1.<?xml?version="1.0"?encoding="utf-8"?>??
2.<android.support.constraint.ConstraintLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
3.????xmlns:app="http://schemas.android.com/apk/res-auto"??
4.????xmlns:tools="http://schemas.android.com/tools"??
5.????android:layout_width="match_parent"??
6.????android:layout_height="match_parent"??
7.????tools:context=".MainActivity">??
8.??
9.????<android.support.v7.widget.RecyclerView??
10.????????android:id="@+id/recyclerView"??
11.????????android:layout_width="0dp"??
12.????????android:layout_height="0dp"??
13.????????android:layout_marginStart="8dp"??
14.????????android:layout_marginLeft="8dp"??
15.????????android:layout_marginTop="8dp"??
16.????????android:layout_marginEnd="8dp"??
17.????????android:layout_marginRight="8dp"??
18.????????android:layout_marginBottom="8dp"??
19.????????app:layout_constraintBottom_toTopOf="@+id/linearLayout"??
20.????????app:layout_constraintEnd_toEndOf="parent"??
21.????????app:layout_constraintStart_toStartOf="parent"??
22.????????app:layout_constraintTop_toTopOf="parent"?/>??
23.??
24.????<LinearLayout??
25.????????android:id="@+id/linearLayout"??
26.????????android:layout_width="match_parent"??
27.????????android:layout_height="60dp"??
28.????????android:layout_marginStart="8dp"??
29.????????android:layout_marginLeft="8dp"??
30.????????android:layout_marginEnd="8dp"??
31.????????android:layout_marginRight="8dp"??
32.????????android:layout_marginBottom="8dp"??
33.????????android:orientation="horizontal"??
34.????????app:layout_constraintBottom_toBottomOf="parent"??
35.????????app:layout_constraintEnd_toEndOf="parent"??
36.????????app:layout_constraintStart_toStartOf="parent"??
37.????????app:layout_constraintTop_toBottomOf="@+id/recyclerView">??
38.??
39.????????<TextView??
40.????????????android:id="@+id/textView"??
41.????????????android:layout_width="wrap_content"??
42.????????????android:layout_height="wrap_content"??
43.????????????android:layout_weight="1"??
44.????????????android:text="score:"?/>??
45.??
46.????????<Button??
47.????????????android:id="@+id/button"??
48.????????????android:layout_width="wrap_content"??
49.????????????android:layout_height="wrap_content"??
50.????????????android:onClick="reset"??
51.????????????android:text="reset"?/>??
52.??
53.????????<Button??
54.????????????android:id="@+id/button2"??
55.????????????android:layout_width="wrap_content"??
56.????????????android:layout_height="wrap_content"??
57.????????????android:onClick="showHistory"??
58.????????????android:text="history"?/>??
59.????</LinearLayout>??
60.</android.support.constraint.ConstraintLayout>??
activity_history.xml
1.<?xml?version="1.0"?encoding="utf-8"?>??
2.<android.support.constraint.ConstraintLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
3.????xmlns:app="http://schemas.android.com/apk/res-auto"??
4.????xmlns:tools="http://schemas.android.com/tools"??
5.????android:layout_width="match_parent"??
6.????android:layout_height="match_parent"??
7.????tools:context=".HistoryActivity">??
8.??
9.????<ListView??
10.????????android:id="@+id/listView"??
11.????????android:layout_width="0dp"??
12.????????android:layout_height="0dp"??
13.????????android:layout_marginStart="8dp"??
14.????????android:layout_marginLeft="8dp"??
15.????????android:layout_marginTop="8dp"??
16.????????android:layout_marginEnd="8dp"??
17.????????android:layout_marginRight="8dp"??
18.????????android:layout_marginBottom="8dp"??
19.????????app:layout_constraintBottom_toBottomOf="parent"??
20.????????app:layout_constraintEnd_toEndOf="parent"??
21.????????app:layout_constraintStart_toStartOf="parent"??
22.????????app:layout_constraintTop_toTopOf="parent"?/>??
23.</android.support.constraint.ConstraintLayout>??

Java文件

MainActivity.java
1.package?com.example.recyclercardgame2;??
2.??
3.import?android.support.annotation.NonNull;??
4.import?android.support.v7.widget.RecyclerView;??
5.import?android.view.View;??
6.import?android.view.ViewGroup;??
7.import?android.widget.Button;??
8.??
9.import?com.example.recyclercardgame2.model.Card;??
10.import?com.example.recyclercardgame2.model.CardMatchingGame;??
11.??
12.public?class?CardGameAdapter?extends?RecyclerView.Adapter<CardGameAdapter.ViewHolder>?{??
13.??
14.????private?CardMatchingGame?game;??
15.????private?View.OnClickListener?monClickListener;??
16.??
17.????public?void?setonClickListener(View.OnClickListener?monClickListener)?{??
18.????????this.monClickListener?=?monClickListener;??
19.????}??
20.??
21.??
22.????public?CardGameAdapter(CardMatchingGame?game)?{??
23.????????this.game?=?game;??
24.????}??
25.??
[email protected]??
[email protected]??
28.????public?ViewHolder?onCreateViewHolder(@NonNull?ViewGroup?viewGroup,?int?i)?{??
29.????????View?view?=?new?Button(viewGroup.getContext());??
30.????????view.setLayoutParams(new?ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,360));??
31.????????ViewHolder?viewHolder?=?new?ViewHolder(view);??
32.????????return?viewHolder;??
33.????}??
34.??
[email protected]??
36.????public?void?onBindViewHolder(@NonNull?ViewHolder?viewHolder,?final?int?i)?{??
37.????????Card?card?=?game.cardAtIndex(i);??
38.??
39.????????viewHolder.cardButton.setAlpha(card.isMatched()???(float)0.5:?1);??
40.????????if(card.isChosen()){??
41.????????????viewHolder.cardButton.setBackgroundResource(R.drawable.blankcard);??
42.????????????viewHolder.cardButton.setText(card.getContents());??
43.????????}?else?{??
44.????????????viewHolder.cardButton.setBackgroundResource(R.drawable.stanfordtree);??
45.????????????viewHolder.cardButton.setText("");??
46.????????}??
47.??
48.????????viewHolder.cardButton.setOnClickListener(new?View.OnClickListener()?{??
[email protected]??
50.????????????public?void?onClick(View?v)?{??
51.????????????????if?(monClickListener?!=?null?)?{??
52.????????????????????v.setTag(i);??
53.????????????????????monClickListener.onClick(v);??
54.????????????????}??
55.????????????}??
56.????????});??
57.????}??
58.??
[email protected]??
60.????public?int?getItemCount()?{??
61.????????return?game.count();??
62.????}??
63.??
64.????public?class?ViewHolder?extends?RecyclerView.ViewHolder?{??
65.????????Button?cardButton;??
66.????????public?ViewHolder(@NonNull?View?itemView)?{??
67.????????????super(itemView);??
68.????????????cardButton?=?(Button)?itemView;??
69.????????}??
70.????}??
71.}??
HistoryActivity?.java
1.package?com.example.recyclercardgame2;??
2.??
3.import?android.content.Intent;??
4.import?android.support.v7.app.AppCompatActivity;??
5.import?android.os.Bundle;??
6.import?android.widget.ArrayAdapter;??
7.import?android.widget.ListView;??
8.??
9.import?java.util.ArrayList;??
10.??
11.public?class?HistoryActivity?extends?AppCompatActivity?{??
12.??
[email protected]??
14.????protected?void?onCreate(Bundle?savedInstanceState)?{??
15.????????super.onCreate(savedInstanceState);??
16.????????setContentView(R.layout.activity_history);??
17.??
18.????????Intent?intent?=?getIntent();??
19.????????ArrayList<String>?playHistory?=?intent.getStringArrayListExtra("history");??
20.??
21.????????ListView?listView?=?findViewById(R.id.listView);??
22.??
23.????????ArrayAdapter<String>?adapter?=?new?ArrayAdapter<>(this,android.R.layout.simple_list_item_1,playHistory);??
24.????????listView.setAdapter(adapter);??
25.??
26.????}??
27.??
[email protected]??
29.????public?void?finish()?{??
30.????????super.finish();??
31.????????overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);??
32.????}??
33.}??

CardGameAdapter.java

1.package?com.example.recyclercardgame2;??
2.??
3.import?android.support.annotation.NonNull;??
4.import?android.support.v7.widget.RecyclerView;??
5.import?android.view.View;??
6.import?android.view.ViewGroup;??
7.import?android.widget.Button;??
8.??
9.import?com.example.recyclercardgame2.model.Card;??
10.import?com.example.recyclercardgame2.model.CardMatchingGame;??
11.??
12.public?class?CardGameAdapter?extends?RecyclerView.Adapter<CardGameAdapter.ViewHolder>?{??
13.??
14.????private?CardMatchingGame?game;??
15.????private?View.OnClickListener?monClickListener;??
16.??
17.????public?void?setonClickListener(View.OnClickListener?monClickListener)?{??
18.????????this.monClickListener?=?monClickListener;??
19.????}??
20.??
21.??
22.????public?CardGameAdapter(CardMatchingGame?game)?{??
23.????????this.game?=?game;??
24.????}??
25.??
[email protected]??
[email protected]??
28.????public?ViewHolder?onCreateViewHolder(@NonNull?ViewGroup?viewGroup,?int?i)?{??
29.????????View?view?=?new?Button(viewGroup.getContext());??
30.????????view.setLayoutParams(new?ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,360));??
31.????????ViewHolder?viewHolder?=?new?ViewHolder(view);??
32.????????return?viewHolder;??
33.????}??
34.??
[email protected]??
36.????public?void?onBindViewHolder(@NonNull?ViewHolder?viewHolder,?final?int?i)?{??
37.????????Card?card?=?game.cardAtIndex(i);??
38.??
39.????????viewHolder.cardButton.setAlpha(card.isMatched()???(float)0.5:?1);??
40.????????if(card.isChosen()){??
41.????????????viewHolder.cardButton.setBackgroundResource(R.drawable.blankcard);??
42.????????????viewHolder.cardButton.setText(card.getContents());??
43.????????}?else?{??
44.????????????viewHolder.cardButton.setBackgroundResource(R.drawable.stanfordtree);??
45.????????????viewHolder.cardButton.setText("");??
46.????????}??
47.??
48.????????viewHolder.cardButton.setOnClickListener(new?View.OnClickListener()?{??
[email protected]??
50.????????????public?void?onClick(View?v)?{??
51.????????????????if?(monClickListener?!=?null?)?{??
52.????????????????????v.setTag(i);??
53.????????????????????monClickListener.onClick(v);??
54.????????????????}??
55.????????????}??
56.????????});??
57.????}??
58.??
[email protected]??
60.????public?int?getItemCount()?{??
61.????????return?game.count();??
62.????}??
63.??
64.????public?class?ViewHolder?extends?RecyclerView.ViewHolder?{??
65.????????Button?cardButton;??
66.????????public?ViewHolder(@NonNull?View?itemView)?{??
67.????????????super(itemView);??
68.????????????cardButton?=?(Button)?itemView;??
69.????????}??
70.????}??
71.}??

Card.java文件

1.package?com.example.recyclercardgame2.model;??
2.??
3.import?java.io.Serializable;??
4.??
5./**?
6.?*?Created?by?lys?on?19/3/20.?
7.?*/??
8.public?abstract?class?Card?implements?Serializable?{??
9.????private?boolean?_chosen;??
10.????private??boolean?_matched;??
11.????public?abstract?String?getContents();??
12.????public?boolean?isChosen(){??
13.????????return?_chosen;??
14.????}??
15.????public?void?setChosen(boolean?chosen){??
16.????????_chosen?=?chosen;??
17.????}??
18.????public?boolean?isMatched(){??
19.????????return?_matched;??
20.????}??
21.????public?void?setMatched(boolean?matched){??
22.????????_matched?=?matched;??
23.????}??
24.????public?int?match(Card[]?otherCards){??
25.????????int?score?=?0;??
26.????????for?(Card?card?:?otherCards)?{??
27.???????????if?(card.getContents().equals(this)){??
28.???????????????score?=?1;??
29.???????????}??
30.????????}??
31.????????return?score;??
32.????}??
33.}??

Deck.java文件

1.package?com.example.recyclercardgame2.model;??
2.??
3.import?java.util.ArrayList;??
4.import?java.util.Random;??
5.??
6./**?
7.?*?Created?by?liguiyang?on?16/3/7.?
8.?*/??
9.public?class?Deck?{??
10.????private?ArrayList<Card>?_cards?=?new?ArrayList<>();??
11.????private?Random?r?=?new?Random();??
12.??
13.????public?void?addCard(Card?card,boolean?atTop){??
14.????????if?(atTop)?{??
15.????????????_cards.add(0,card);??
16.????????}else{??
17.????????????_cards.add(card);??
18.????????}??
19.????}??
20.??
21.????public?void?addCard(Card?card){??
22.????????addCard(card,false);??
23.????}??
24.??
25.????public?Card?drawRandomCard(){??
26.????????Card?randomCard?=?null;??
27.????????if?(_cards.size()>0){??
28.????????????randomCard?=?_cards.remove(r.nextInt(_cards.size()));??
29.????????}??
30.????????return?randomCard;??
31.????}??
32.}??

PlayingCard.java文件

1.package?com.example.recyclercardgame2.model;??
2.??
3.import?java.util.Arrays;??
4.??
5.public?class?PlayingCard?extends?Card{??
6.????private?String?_suit;??
7.????private?int?_rank;??
8.??
9.????public?String?getSuit(){??
10.????????return?_suit;??
11.????}??
12.????public?void?setSuit(String?suit){??
13.????????if(Arrays.asList(PlayingCard.validSuits()).contains(suit)){??
14.????????????_suit?=??suit;??
15.????????}??
16.????}??
17.????public?int?getRank(){??
18.????????return?_rank;??
19.????}??
20.????public?void?setRank(int?rank){??
21.????????if(rank?<=?PlayingCard.maxRank())?{??
22.????????????_rank?=?rank;??
23.????????}??
24.????}??
25.????public?static?String[]?validSuits(){??
26.????????return?new?String[]{"?","?","?","?"};??
27.????}??
28.????private?static?String[]?rankStrings(){??
29.????????return?new?String[]{"?","A","2","3","4","5","6","7","8","9","10","J","Q","K"};??
30.????}??
31.
32.????public?static?int?maxRank(){??
33.????????return?PlayingCard.rankStrings().length?-?1;??
34.????}??
35.??
[email protected]??
37.????public?int?match(Card[]?otherCards)?{??
38.????????int?score?=?0;??
39.????????if(otherCards.length?==?1){??
40.????????????PlayingCard?otherCard?=?(PlayingCard)?otherCards[0];??
41.????????????if(otherCard.getRank()==this.getRank()){??
42.????????????????score?=??4;??
43.????????????}else?if?(otherCard.getSuit().equals(this.getSuit())){??
44.????????????????score?=?1;??
45.????????????}??
46.????????}??
47.??
48.????????return?score;??
49.????}??
50.??
[email protected]??
52.????public?String?getContents()?{??
53.????????return?PlayingCard.rankStrings()[_rank]?+?_suit;??
54.????}??
55.}??

PlayingDeck.java文件

1.package?com.example.recyclercardgame2.model;??
2.??
3.??
4.public?class?PlayingDeck?extends?Deck{??
5.????public?PlayingDeck(){??
6.????????super();??
7.????????for?(String?suit?:??
8.????????????????PlayingCard.validSuits())?{??
9.????????????for?(int?rank?=?1??;?rank?<=?PlayingCard.maxRank()?;?rank++)?{??
10.????????????????PlayingCard?card?=?new?PlayingCard();??
11.????????????????card.setRank(rank);??
12.????????????????card.setSuit(suit);??
13.????????????????addCard(card);??
14.????????????}??
15.????????}??
16.????}??
17.}??

運行結果:
技術分享圖片
技術分享圖片
技術分享圖片

技術分享圖片

上課案列-翻牌遊戲(重置、歷史記錄)