2017-06-17 1 views
0

ここにコードを入力してください.2つのグリッドビューを使用しています.1つのグリッドビューには9X9セルと1つ、3つは3x3セルです。 imageviewで9x9グリッドを設定しました(合計81個の画像ビューがあります)と3x3には9個の画像ビューがあります。アンドロイドの2つのグリッドビューを使用

9x9グリッドをクリックすると、それぞれの場所に画像が表示されます。 9x9グリッドで条件が満たされたときに、3x3グリッドに画像を表示する方法を理解できません。私は「それは今9つのゲームは2人の間で一度に再生されているチックタックつま先のゲームがプレイされている基本的だと1セットが勝っているとき、私はゲーム

what i want

以上、そのセットの勝者を表示したいです私が欲しいものを添付しました。 3x3グリッドビューは9x9グリッドビューとオーバーラップしています。

私のコードは以下の通りです

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.auk01.tictactoegrand.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

<include layout="@layout/content_main" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    app:srcCompat="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="16dp" 
android:paddingLeft="16dp" 
android:paddingRight="16dp" 
android:paddingTop="16dp" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.example.auk01.tictactoegrand.MainActivity" 
tools:showIn="@layout/activity_main"> 

<GridLayout 
android:id="@+id/GrandGridLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:columnCount="3" 
android:rowCount="3" 
tools:layout_editor_absoluteX="8dp" 
tools:layout_editor_absoluteY="8dp"> 

<ImageView 
    android:id="@+id/imageViewSet0" 
    android:layout_width="100dp" 
    android:layout_height="140dp" 
    android:layout_margin="5dp" /> 

<ImageView 
    android:id="@+id/imageViewSet1" 
    android:layout_width="100dp" 
    android:layout_height="140dp" 
    android:layout_margin="5dp" /> 

<ImageView 
    android:id="@+id/imageViewSet2" 
    android:layout_width="100dp" 
    android:layout_height="140dp" 
    android:layout_margin="5dp" /> 

<ImageView 
    android:id="@+id/imageViewSet3" 
    android:layout_width="100dp" 
    android:layout_height="140dp" 
    android:layout_margin="5dp" /> 

<ImageView 
    android:id="@+id/imageViewSet4" 
    android:layout_width="100dp" 
    android:layout_height="140dp" 
    android:layout_margin="5dp" /> 

<ImageView 
    android:id="@+id/imageViewSet5" 
    android:layout_width="116dp" 
    android:layout_height="140dp" 
    android:layout_margin="5dp" /> 

<ImageView 
    android:id="@+id/imageViewSet6" 
    android:layout_width="100dp" 
    android:layout_height="140dp" 
    android:layout_margin="5dp" /> 

<ImageView 
    android:id="@+id/imageViewSet7" 
    android:layout_width="100dp" 
    android:layout_height="140dp" 
    android:layout_margin="5dp" /> 

<ImageView 
    android:id="@+id/imageViewSet8" 
    android:layout_width="100dp" 
    android:layout_height="140dp" 
    android:layout_margin="5dp" /> 

</GridLayout> 

<GridLayout 
android:id="@+id/gridLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 

android:background="@drawable/board9" 
android:columnCount="9" 
android:rowCount="9" 
tools:layout_editor_absoluteX="8dp" 
tools:layout_editor_absoluteY="8dp"> 


<ImageView 
    android:id="@+id/imageView0" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="2dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="0" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="1" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView2" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="2" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView3" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="3" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView4" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="4" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView5" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="5" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView6" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="6" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView7" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="7" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView8" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="8" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView9" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="2dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="9" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView10" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="10" 
    app:srcCompat="@drawable/invisable" /> 

//i have removed id/imageView11 to imageView75 just to explain my code in shoter 
<ImageView 
    android:id="@+id/imageView76" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="76" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView77" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="77" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView78" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="78" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView79" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="79" 
    app:srcCompat="@drawable/invisable" /> 

<ImageView 
    android:id="@+id/imageView80" 
    android:layout_width="35dp" 
    android:layout_height="46dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="6dp" 
    android:layout_marginTop="2dp" 
    android:onClick="gamelogic" 
    android:tag="80" 
    app:srcCompat="@drawable/invisable" /> 

</GridLayout> 

<LinearLayout 
android:id="@+id/WinnerLayout" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true" 
android:layout_centerVertical="true" 
android:background="@color/colorPrimaryDark" 
android:orientation="vertical" 
android:padding="35dp"> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:textColor="@android:color/white" /> 

<Button 
    android:id="@+id/button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:onClick="PlayAgain" 
    android:text="Play Again" /> 
</LinearLayout> 

</RelativeLayout> 

mainactivity.java

package com.example.auk01.tictactoegrand; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.GridLayout; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

int ActivePlayer = 0; // 0 for red 
int[] gameState = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};//2 means unplayed. 
int[][] WinningLocation0 = {{0,1,2}, {9,10,11}, {18,19,20}, {0,9,18}, {1,10,19}, {2,11,20}, {0,10,20}, {2,10,18}}; 
int[][] WinningLocation1 = {{3,4,5}, {12,13,14}, {21,22,23}, {3,12,21}, {4,13,22}, {5,14,23}, {3,13,23}, {5,13,21}}; 
int[][] WinningLocation2 = {{6,7,8}, {15,16,17}, {24,25,26}, {6,15,24}, {7,16,25}, {8,17,26}, {6,16,26}, {8,16,24}}; 
int[][] WinningLocation3 = {{27,28,29}, {36,37,38}, {45,46,47}, {27,36,45}, {28,37,46}, {29,38,47}, {27,37,47}, {29,37,45}}; 
int[][] WinningLocation4 = {{30,31,32}, {39,40,41}, {48,49,50}, {30,39,48}, {31,40,49}, {32,41,50}, {30,40,50}, {32,40,48}}; 
int[][] WinningLocation5 = {{33,34,35}, {42,43,44}, {51,52,53}, {33,42,51}, {34,43,52}, {35,44,53}, {33,43,53}, {35,43,51}}; 
int[][] WinningLocation6 = {{54,55,56}, {63,64,65}, {72,73,74}, {54,63,72}, {55,64,73}, {56,65,74}, {54,64,74}, {56,64,72}}; 
int[][] WinningLocation7 = {{57,58,59}, {66,67,68}, {75,76,77}, {57,66,75}, {58,67,76}, {59,68,77}, {57,67,77}, {59,67,75}}; 
int[][] WinningLocation8 = {{60,61,62}, {69,70,71}, {78,79,80}, {60,69,78}, {61,70,79}, {62,71,80}, {60,70,80}, {62,70,78}}; 
boolean GameOver = false; 

public void gamelogic(View view) { 
    ImageView tappedview = (ImageView) view; 

    int tappedLocation = Integer.parseInt(view.getTag().toString()); 

    if(gameState[tappedLocation] == 2 && !GameOver) { 
     gameState[tappedLocation] = ActivePlayer; 
     tappedview.setTranslationY(-3000f); 
     if (ActivePlayer == 0) { 
      tappedview.setImageResource(R.drawable.red); 
      ActivePlayer = 1; 
     } else if (ActivePlayer == 1) { 
      tappedview.setImageResource(R.drawable.yellow); 
      ActivePlayer = 0; 
     } 
     tappedview.animate().translationYBy(3000f).setDuration(10); 


    } 
    String msg = ""; 
    for (int[]WinningPosition : WinningLocation0){ 
     if(gameState[WinningPosition[0]] == gameState[WinningPosition[1]] 
       && gameState[WinningPosition[1]] == gameState[WinningPosition[2]] 
       && gameState[WinningPosition[0]] !=2){ 

      if(ActivePlayer == 0) 
       Toast.makeText(getApplicationContext(), "0 IS THE WINNER Of Set Zero", Toast.LENGTH_LONG).show(); 
       GridLayout GrandGridLayout = (GridLayout) findViewById(R.id.GrandGridLayout); 
       msg = "0 IS THE WINNER"; 
      if(ActivePlayer == 1) 
       Toast.makeText(getApplicationContext(), "X IS THE WINNER Of Set Zero", Toast.LENGTH_LONG).show(); 
       msg = "X IS THE WINNER"; 
     } 
    } 

    for (int[]WinningPosition : WinningLocation1){ 
     if(gameState[WinningPosition[0]] == gameState[WinningPosition[1]] 
       && gameState[WinningPosition[1]] == gameState[WinningPosition[2]] 
       && gameState[WinningPosition[0]] !=2){ 

      if(ActivePlayer == 0) 
       msg = "0 IS THE WINNER"; 
      if(ActivePlayer == 1) 
       msg = "X IS THE WINNER"; 

      LinearLayout WinnerLayout = (LinearLayout) findViewById(R.id.WinnerLayout); 
      WinnerLayout.setVisibility(View.VISIBLE); 

      TextView WinnerMsg = (TextView) findViewById(R.id.textView); 
      WinnerMsg.setText(msg); 
      GameOver = true; 
     } 
    } 

} 

public void PlayAgain(View view){ 
    LinearLayout WinnerLayout = (LinearLayout) findViewById(R.id.WinnerLayout); 
    WinnerLayout.setVisibility(View.INVISIBLE); 
    GameOver = false; 
    ActivePlayer = 0; 

    for(int i = 0; i < gameState.length; i++) 
     gameState[i] = 2; 
    GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout); 
    for(int i = 0; i < gridLayout.getChildCount(); i++) 
     ((ImageView)gridLayout.getChildAt(i)).setImageResource(0); 
} 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    LinearLayout WinnerLayout = (LinearLayout) findViewById(R.id.WinnerLayout) ; 
    WinnerLayout.setVisibility(View.INVISIBLE); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

これは、今私はカントすべてのコードですフィギュアouどのように小さなgridviewのdrawable \ redを得るか私の場合id.grandgridlayout(3x3) id.gridlayoutは9x9

答えて

0

私たちは、プログラム自体のロジックや使用する "正しい"パターンについては論じません。 私はちょうどいくつかのデータがプリロードされているグリッドビューで1回クリックすると、左上隅の3つのXがチェックされる場合に備えて、単純なアプリケーションを作成しました。クリックすると大きなグリッドの下の小さなグリッドに新しいXが表示されます。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#C0C0C0" 
tools:context="com.ad3luc.problemsolving.AMain"> 

    <GridView 
     android:id="@+id/gridView_big" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:numColumns="9" 
     android:stretchMode="columnWidth" /> 

    <ImageView 
     android:id="@+id/separator" 
     android:layout_below="@id/gridView_big" 
     android:layout_width="fill_parent" 
     android:layout_height="10dp" 
     android:background="@color/colorPrimaryDark"/> 

    <GridView 
     android:id="@+id/gridView_small" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:numColumns="3" 
     android:stretchMode="columnWidth" 
     android:layout_below="@+id/separator" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/> 
</RelativeLayout> 

AMain.java

public class AMain extends AppCompatActivity { 

    private GridView gGridViewBig; 
    private GridView gGridViewSmall; 

    static final String[] ticTacToeBig = new String[] { 
      "X", "O", "", "", "", "", "", "", "", 
      "X", "", "O", "", "", "", "", "", "", 
      "X", "O", "", "", "", "", "", "", "", 
      "", "", "", "", "", "X", "", "", "", 
      "", "", "", "", "", "", "O", "", "", 
      "", "", "X", "", "", "", "", "", "", 
      "", "", "O", "", "", "", "O", "X", "X", 
      "", "", "", "", "", "", "", "O", "", 
      "", "", "", "", "", "", "X", "", "O" 
    }; 

    static final String[] ticTacToeSmall = new String[] { 
      "", "", "", 
      "", "", "", 
      "", "", "", 
    }; 

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

     gGridViewBig = (GridView) findViewById(R.id.gridView_big); 
     gGridViewSmall = (GridView) findViewById(R.id.gridView_small); 

     ArrayAdapter<String> adapterBig = new ArrayAdapter<>(this, 
       android.R.layout.simple_list_item_1, ticTacToeBig); 

     final ArrayAdapter<String> adapterSmall = new ArrayAdapter<>(this, 
       android.R.layout.simple_list_item_1, ticTacToeSmall); 

     gGridViewBig.setAdapter(adapterBig); 
     gGridViewSmall.setAdapter(adapterSmall); 

     gGridViewBig.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 

       if(ticTacToeBig[0].equals("X") && ticTacToeBig[9].equals("X") && ticTacToeBig[18].equals("X")) { 

        ticTacToeSmall[0] = "X"; 
        gGridViewSmall.setAdapter(adapterSmall); 
       } 
      } 
     }); 
    } 
} 

私は、実装するためのソリューションは、リスト、マップの大きなグリッドとの一致に作られた現在のクリックを読み取ることだと思います。あなたはすべてのクリックを保存しています。条件が一致すると、リスト、マップなどを更新する必要があります。小さなマップに渡すアダプタに渡します。

関連する問題