آخیش! پروژه تموم میشه آدم خستگی از تنش در میاد خدایا شکرت:)
فایل game.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:orientation="vertical" tools:context=".game"> <LinearLayout android:layout_width="match_parent" android:layout_height="360dp" android:id="@+id/goodlayout" android:background="@drawable/grid" android:backgroundTint="#FFEB3B" android:layout_centerInParent="true" android:orientation="vertical"> <LinearLayout android:paddingBottom="8dp" android:paddingTop="8dp" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="putbutton" android:tag="0" tools:srcCompat="@drawable/bl" /> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="putbutton" android:tag="1" tools:srcCompat="@drawable/bl" /> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="putbutton" android:tag="2" tools:srcCompat="@drawable/bl" /> </LinearLayout> <LinearLayout android:paddingBottom="8dp" android:paddingTop="8dp" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="putbutton" android:tag="3" tools:srcCompat="@drawable/bl" /> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="putbutton" android:tag="4" tools:srcCompat="@drawable/bl" /> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="putbutton" android:tag="5" tools:srcCompat="@drawable/bl" /> </LinearLayout> <LinearLayout android:paddingTop="8dp" android:paddingBottom="8dp" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="putbutton" android:tag="6" tools:srcCompat="@drawable/bl" /> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="putbutton" android:tag="7" tools:srcCompat="@drawable/bl" /> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="putbutton" android:tag="8" tools:srcCompat="@drawable/bl" /> </LinearLayout> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="160dp" android:id="@+id/news" android:layout_marginLeft="80dp" android:layout_marginRight="80dp" android:background="#FFC107" android:layout_centerInParent="true"> <TextView android:id="@+id/newstext" android:textAlignment="center" android:layout_marginTop="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:layout_centerHorizontal="true" android:text="Hey!!" /> <Button android:id="@+id/button" android:layout_centerHorizontal="true" android:layout_below="@+id/newstext" android:layout_width="wrap_content" android:layout_marginTop="10dp" android:layout_height="50dp" android:text="Play Again!" android:onClick="restart" /> </RelativeLayout> </RelativeLayout>
فایل game.java
package ir.ben.frog; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; public class game extends AppCompatActivity { public static final int empty=0,rng=1,blu=2; int turn=rng,sum=0; int [] fl={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; int[][] winpos={{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem resst = menu.add("New Game"); resst.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); resst.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { restart(null); return false; } }); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (item.getItemId() == android.R.id.home) finish(); return super.onOptionsItemSelected(item); } public void putbutton(View v) { ImageView img= (ImageView) v; int pos=Integer.parseInt((String) v.getTag()); if(fl[pos]!=empty) return; if(turn==rng) img.setImageResource(R.drawable.or2); else img.setImageResource(R.drawable.bl); fl[pos]=turn; checkwinner(); turn=rng+blu-turn; sum++; if(sum==9) { TextView xxx=findViewById(R.id.newstext); xxx.setText("DraW!"); findViewById(R.id.news).setVisibility(View.VISIBLE); } } private void checkwinner() { for(int j=0;j<winpos.length;j++) { if(fl[winpos[j][0]]!= empty && fl[winpos[j][0]]==fl[winpos[j][1]] && fl[winpos[j][1]]==fl[winpos[j][2]]) { String happy=(turn==rng)?"Orange":"Blue"; TextView khosh=findViewById(R.id.newstext); khosh.setText("Player "+happy+" Won!"); findViewById(R.id.news).setVisibility(View.VISIBLE); return; } } } public void restart(View view) { findViewById(R.id.news).setVisibility(View.GONE); for(int j=0;j<10;j++) fl[j]=0; sum=0; turn=rng; LinearLayout biglay=findViewById(R.id.goodlayout);//cast for(int i=0;i<biglay.getChildCount();i++) { LinearLayout lay= (biglay.getChildAt(i) instanceof LinearLayout)? (LinearLayout) biglay.getChildAt(i) :null; if(lay==null) return; ImageView heh; for(int j=0;j<lay.getChildCount();j++) { heh= (lay.getChildAt(j) instanceof ImageView)? (ImageView) lay.getChildAt(j) :null; if(heh==null)return; heh.setImageResource(0); } } } }
خدایا چقد اسکل شده بودم یه جا که به خاطر این که fl[pos] به جاش نوشته بودم pos هی کار نمی کرد تو winner
یه جا هم موقع مقدار دهی fl که میخواستم همه رو صفر بذارم فقط fl[0] صفر شده بود اینم اولین زخمی که از جاوا خوردم!