# AN25 課題No.06
###### tags: `課題`
```xml=
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300sp"
android:layout_height="300sp"
android:padding="20sp"
android:background="@drawable/masu">
<ImageButton
android:id="@+id/left_top"
android:layout_height="75sp"
android:layout_width="75sp"
android:background="@drawable/none"
android:layout_above="@+id/center"
android:layout_toLeftOf="@+id/center"
android:onClick="buttonMethod"
/>
<ImageButton
android:id="@+id/top"
android:layout_height="75sp"
android:layout_width="75sp"
android:background="@drawable/none"
android:layout_above="@+id/center"
android:layout_alignLeft="@+id/center"
android:onClick="buttonMethod"
/>
<ImageButton
android:id="@+id/right_top"
android:layout_height="75sp"
android:layout_width="75sp"
android:background="@drawable/none"
android:layout_above="@+id/center"
android:layout_toRightOf="@+id/center"
android:onClick="buttonMethod"
/>
<ImageButton
android:id="@+id/left"
android:layout_height="75sp"
android:layout_width="75sp"
android:background="@drawable/none"
android:layout_toLeftOf="@+id/center"
android:layout_alignTop="@+id/center"
android:onClick="buttonMethod"
/>
<ImageButton
android:id="@+id/center"
android:layout_height="75sp"
android:layout_width="75sp"
android:layout_margin="20sp"
android:background="@drawable/none"
android:layout_centerInParent="true"
android:onClick="buttonMethod"
/>
<ImageButton
android:id="@+id/right"
android:layout_height="75sp"
android:layout_width="75sp"
android:background="@drawable/none"
android:layout_toRightOf="@+id/center"
android:layout_alignTop="@+id/center"
android:onClick="buttonMethod"
/>
<ImageButton
android:id="@+id/left_bottom"
android:layout_height="75sp"
android:layout_width="75sp"
android:background="@drawable/none"
android:layout_below="@+id/center"
android:layout_toLeftOf="@+id/center"
android:onClick="buttonMethod"
/>
<ImageButton
android:id="@+id/bottom"
android:layout_height="75sp"
android:layout_width="75sp"
android:background="@drawable/none"
android:layout_below="@+id/center"
android:layout_alignLeft="@+id/center"
android:onClick="buttonMethod"
/>
<ImageButton
android:id="@+id/right_bottom"
android:layout_height="75sp"
android:layout_width="75sp"
android:background="@drawable/none"
android:layout_below="@+id/center"
android:layout_toRightOf="@+id/center"
android:onClick="buttonMethod"
/>
</RelativeLayout>
```
```java=
//mainactivity.java
package nhs00402.hal.ac.oxgame;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageButton[] rectButtons = new ImageButton[9];
private boolean senkou = true;
private int[] button_flag = {9,9,9,9,9,9,9,9,9};
int i =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rectButtons[0] = (ImageButton)findViewById(R.id.left_top);
rectButtons[1] = (ImageButton)findViewById(R.id.top);
rectButtons[2] = (ImageButton)findViewById(R.id.right_top);
rectButtons[3] = (ImageButton)findViewById(R.id.left);
rectButtons[4] = (ImageButton)findViewById(R.id.center);
rectButtons[5] = (ImageButton)findViewById(R.id.right);
rectButtons[6] = (ImageButton)findViewById(R.id.left_bottom);
rectButtons[7] = (ImageButton)findViewById(R.id.bottom);
rectButtons[8] = (ImageButton)findViewById(R.id.right_bottom);
}
private void resetGame(){
for (int j =0; j<9; j++){
rectButtons[j].setBackgroundResource(R.drawable.none);
button_flag[j] = 9;
}
i = 0;
senkou = true;
}
private void judgeGame(){
if (button_flag[0] == button_flag[1] && button_flag[1]== button_flag[2]){
if (button_flag[0] == 0){
toast0();
}
else if (button_flag[0] == 1){
toast1();
}
}
if (button_flag[0] == button_flag[3] && button_flag[3]== button_flag[6]){
if (button_flag[0] == 0){
toast0();
}
else if (button_flag[0] == 1){
toast1();
}
}
if (button_flag[0] == button_flag[4] && button_flag[4]== button_flag[8]){
if (button_flag[0] == 0){
toast0();
}
else if (button_flag[0] == 1){
toast1();
}
}
if (button_flag[3] == button_flag[4] && button_flag[4]== button_flag[5]){
if (button_flag[3] == 0){
toast0();
}
else if (button_flag[3] == 1){
toast1();
}
}
if (button_flag[6] == button_flag[7] && button_flag[7]== button_flag[8]){
if (button_flag[6] == 0){
toast0();
}
else if (button_flag[6] == 1){
toast1();
}
}
if (button_flag[1] == button_flag[4] && button_flag[4]== button_flag[7]){
if (button_flag[1] == 0){
toast0();
}
else if (button_flag[1] == 1){
toast1();
}
}
if (button_flag[2] == button_flag[5] && button_flag[5]== button_flag[8]){
if (button_flag[2] == 0){
toast0();
}
else if (button_flag[2] == 1){
toast1();
}
}
if (button_flag[2] == button_flag[4] && button_flag[4]== button_flag[6]){
if (button_flag[2] == 0){
toast0();
}
else if (button_flag[2] == 1){
toast1();
}
}
}
private void Toast_(){
if (i == 9){
Toast drawToast = Toast.makeText(this,
"引き分けです",Toast.LENGTH_LONG);
drawToast.show();
}
}
private void toast0(){
Toast Toast0 = Toast.makeText(this,
"〇の勝利です",Toast.LENGTH_LONG);
Toast0.show();
i = 0;
for (int j =0; j<9; j++){
button_flag[j] = 6;
}
}
private void toast1(){
Toast Toast1 = Toast.makeText(this,
"×の勝利です",Toast.LENGTH_LONG);
Toast1.show();
i = 0;
for (int j =0; j<9; j++){
button_flag[j] = 6;
}
}
public void buttonMethod(View OXButton){
ImageButton aImageButton =(ImageButton)OXButton;
switch (OXButton.getId()) {
case R.id.left_top:
if (button_flag[0] == 9){
i++;
if (senkou){
aImageButton.setBackgroundResource(R.drawable.maru);
senkou = false;
button_flag[0] = 0;
}
else{
aImageButton.setBackgroundResource(R.drawable.batu);
senkou = true;
button_flag[0] = 1;
}
}
break;
case R.id.top:
if (button_flag[1] == 9){
i++;
if (senkou){
aImageButton.setBackgroundResource(R.drawable.maru);
senkou = false;
button_flag[1] = 0;
}
else{
aImageButton.setBackgroundResource(R.drawable.batu);
senkou = true;
button_flag[1] = 1;
}
}
break;
case R.id.right_top:
if (button_flag[2] == 9){
i++;
if (senkou){
aImageButton.setBackgroundResource(R.drawable.maru);
senkou = false;
button_flag[2] = 0;
}
else{
aImageButton.setBackgroundResource(R.drawable.batu);
senkou = true;
button_flag[2] = 1;
}
}
break;
case R.id.left:
if (button_flag[3] == 9){
i++;
if (senkou){
aImageButton.setBackgroundResource(R.drawable.maru);
senkou = false;
button_flag[3] = 0;
}
else{
aImageButton.setBackgroundResource(R.drawable.batu);
senkou = true;
button_flag[3] = 1;
}
}
break;
case R.id.center:
if (button_flag[4] == 9){
i++;
if (senkou){
aImageButton.setBackgroundResource(R.drawable.maru);
senkou = false;
button_flag[4] = 0;
}
else{
aImageButton.setBackgroundResource(R.drawable.batu);
senkou = true;
button_flag[4] = 1;
}
}
break;
case R.id.right:
if (button_flag[5] == 9){
i++;
if (senkou){
aImageButton.setBackgroundResource(R.drawable.maru);
senkou = false;
button_flag[5] = 0;
}
else{
aImageButton.setBackgroundResource(R.drawable.batu);
senkou = true;
button_flag[5] = 1;
}
}
break;
case R.id.left_bottom:
if (button_flag[6] == 9){
i++;
if (senkou){
aImageButton.setBackgroundResource(R.drawable.maru);
senkou = false;
button_flag[6] = 0;
}
else{
aImageButton.setBackgroundResource(R.drawable.batu);
senkou = true;
button_flag[6] = 1;
}
}
break;
case R.id.bottom:
if (button_flag[7] == 9){
i++;
if (senkou){
aImageButton.setBackgroundResource(R.drawable.maru);
senkou = false;
button_flag[7] = 0;
}
else{
aImageButton.setBackgroundResource(R.drawable.batu);
senkou = true;
button_flag[7] = 1;
}
}
break;
case R.id.right_bottom:
if (button_flag[8] == 9){
i++;
if (senkou){
aImageButton.setBackgroundResource(R.drawable.maru);
senkou = false;
button_flag[8] = 0;
}
else{
aImageButton.setBackgroundResource(R.drawable.batu);
senkou = true;
button_flag[8] = 1;
}
}
break;
}
judgeGame();
Toast_();
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0,0,0,"リトライ");
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected(item);
resetGame();
return true;
}
}
```
## 雑談 (0→やまぴ 1→みや 2→やすい 3→りょうくん 4→アミカ 5→やの 6→山田)
---
<span style="color: #ff3333">aaa</span>
<span style="text-decoration: underline">aaa</span>