המשך מיני הפרויקט שלנו–השיעור של 22.9.2020

קישור לסרטון

 

החלון הראשי (main_activity)

clip_image001

העיצוב (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:background
="#567DA64D"
   
tools:context=".MainActivity"
>

    <TextView
       
android:id
="@+id/mainTitle"
       
android:layout_width
="match_parent"
       
android:layout_height
="wrap_content"
       
android:text="
אפליקצית המשחקים שלי"
       
android:layout_centerHorizontal="true"
       
android:gravity
="center"
       
android:textSize
="24sp"
       
android:layout_margin
="10dp"
   
>

    </TextView>

    <LinearLayout
       
android:id
="@+id/box2"
       
android:layout_below
="@+id/mainTitle"
       
android:layout_width
="match_parent"
       
android:layout_height
="wrap_content"
       
android:weightSum
="3"
       
android:layout_margin
="5dp"
       
>
        <
Button
           
android:id
="@+id/ticBTN"
           
android:layout_weight
="1"
           
android:text="
איקס עיגול"
           
android:textColor="#FFFFFF"
           
android:textSize
="20sp"

            android:layout_width="wrap_content"
           
android:layout_height
="wrap_content"
           
android:background
="@drawable/buttonshape"
           
android:shadowColor
="#A8A8A8"
           
android:shadowDx
="0"
           
android:shadowDy
="0"
           
android:shadowRadius
="0"
           
android:onClick
="TicTacToe"
           
>
        </
Button
>
        <
Button
           
android:layout_weight
="1"
           
android:layout_width
="wrap_content"
           
android:layout_height
="wrap_content"
           
android:text="
ארבע בשורה"
           
android:textColor="#FFFFFF"
           
android:textSize
="20sp"

            android:background="@drawable/buttonshape"
           
android:shadowColor
="#A8A8A8"
           
android:shadowDx
="0"
           
android:shadowDy
="0"
           
android:shadowRadius
="0"
           
android:onClick
="fourInARow"
           
>

        </Button>
        <
Button
           
android:layout_weight
="1"
           
android:layout_width
="wrap_content"
           
android:layout_height
="wrap_content"
           
android:text="
לא ידוע"
           
android:textColor="#FFFFFF"
           
android:textSize
="20sp"

            android:background="@drawable/buttonshape"
           
android:shadowColor
="#A8A8A8"
           
android:shadowDx
="0"
           
android:shadowDy
="0"
           
android:shadowRadius
="0"
           
>

        </Button>
    </
LinearLayout
>

    <RelativeLayout
       
android:layout_width
="match_parent"
       
android:layout_height
="match_parent"
       
android:layout_below
="@+id/box2"
       
android:layout_above
="@+id/footer"
       
android:layout_centerInParent
="true"
       
android:layout_margin
="20dp"
       
android:gravity="center"
>

        <ImageView
           
android:id
="@+id/pic1"
           
android:layout_width
="150dp"
           
android:layout_height
="150dp"
           
android:src
="@drawable/pic4"

            >

        </ImageView>

        <ImageView
           
android:id
="@+id/pic2"
           
android:layout_width
="150dp"
           
android:layout_height
="150dp"
           
android:layout_toRightOf
="@+id/pic1"
           
android:src
="@drawable/pic3"

            >

        </ImageView>

        <ImageView
           
android:id
="@+id/pic3"
           
android:layout_width
="150dp"
           
android:layout_height
="150dp"
           
android:layout_below
="@+id/pic1"
           
android:src="@drawable/pic1"
>

        </ImageView>

        <ImageView
           
android:id
="@+id/pic4"
           
android:layout_width
="150dp"
           
android:layout_height
="150dp"
           
android:layout_below
="@+id/pic2"
           
android:layout_toRightOf
="@+id/pic3"
           
android:src="@drawable/pic2"
>

        </ImageView>

    </RelativeLayout>

    <TextView
       
android:id
="@+id/footer"
       
android:layout_alignParentBottom
="true"
       
android:layout_width
="match_parent"
       
android:layout_height
="wrap_content"
       
android:text="
כל הזכויות שמורות (c)"
       
android:textSize="18dp"
        
android:gravity
="center"
       
>

    </TextView>

</RelativeLayout>

 

ההרצה (JAVA)

 

package com.contineo.mygames;

import
androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends
AppCompatActivity {

    Button
b1,b2,b3
;
   
@Override
   
protected void
onCreate(Bundle savedInstanceState) {
       
super
.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main
);
       
b1 = findViewById(R.id.ticBTN
);
    }



   
public void
fourInARow(View view) {

    }

   
public void
TicTacToe(View view) {
            Intent intent =
new Intent(this,tictactoe.class
);
            startActivity(intent);

    }
}

 

החלון של איקס עיגול (tictactoe)

clip_image002

קובץ העיצוב (XML)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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
=".tictactoe"
   
android:background
="#567DA64D"
   
android:orientation="vertical"
>
    <
TextView
       
android:id
="@+id/xtitle"
       
android:layout_width
="match_parent"
       
android:layout_height
="wrap_content"
       
android:text="
איקס עיגול"
       
android:gravity="center"
       
android:textSize
="25dp"
       
>

    </
TextView
>

    <
LinearLayout
       
android:id
="@+id/secBox"
       
android:layout_width
="match_parent"
       
android:layout_height
="wrap_content"
       
android:weightSum
="2"
       
android:layout_margin
="10dp"
       
>

        <
Button
           
android:layout_weight
="1"
           
android:layout_width
="wrap_content"
           
android:layout_height
="wrap_content"
           
android:text="
התחל"
           
android:onClick="startGame"
           
android:textColor
="#FFFFFF"
           
android:textSize
="20sp"
           
android:background
="@drawable/buttonshape"
           
android:shadowColor
="#A8A8A8"
           
android:shadowDx
="0"
           
android:shadowDy
="0"
           
android:shadowRadius
="0"
           
>


        </
Button
>
        <
Button
           
android:layout_weight
="1"
           
android:layout_width
="wrap_content"
           
android:layout_height
="wrap_content"
           
android:text="
איפוס"
           
android:textColor="#FFFFFF"
           
android:textSize
="20sp"


           
android:background
="@drawable/buttonshape"
           
android:shadowColor
="#A8A8A8"
           
android:shadowDx
="0"
           
android:shadowDy
="0"
           
android:shadowRadius
="0"
           
>

        </
Button
>
    </
LinearLayout
>

    <
RelativeLayout

       
android:layout_width
="match_parent"
       
android:layout_height="match_parent"
>

        <
ImageView
           
android:id
="@+id/b5"
           
android:layout_width
="120dp"
           
android:layout_height
="120dp"
           
android:src
="@drawable/sq"
           
android:onClick
="doClick"
           
android:layout_centerInParent
="true"
           
>
        </
ImageView
>
        <
ImageView
           
android:id
="@+id/b2"
           
android:layout_width
="120dp"
           
android:layout_height
="120dp"
           
android:src
="@drawable/sq"
           
android:layout_above
="@id/b5"
           
android:onClick
="doClick"
           
android:layout_centerInParent
="true"
           
>
        </
ImageView
>
        <
ImageView
           
android:id
="@+id/b8"
           
android:layout_width
="120dp"
           
android:layout_height
="120dp"
           
android:src
="@drawable/sq"
           
android:layout_below
="@id/b5"
           
android:onClick
="doClick"
           
android:layout_centerInParent
="true"
           
>
        </
ImageView
>
        <
ImageView
           
android:id
="@+id/b4"
           
android:layout_width
="120dp"
           
android:layout_height
="120dp"
           
android:src
="@drawable/sq"
           
android:layout_toLeftOf
="@id/b5"
           
android:onClick
="doClick"
           
android:layout_centerInParent
="true"
           
>
        </
ImageView
>

        <
ImageView
           
android:id
="@+id/b6"
           
android:layout_width
="120dp"
           
android:layout_height
="120dp"
           
android:src
="@drawable/sq"
           
android:onClick
="doClick"
           
android:layout_toRightOf
="@id/b5"
           
android:layout_centerInParent
="true"
           
>
        </
ImageView
>
        <
ImageView
           
android:id
="@+id/b1"
           
android:layout_width
="120dp"
           
android:layout_height
="120dp"
           
android:src
="@drawable/sq"
           
android:layout_above
="@id/b4"
           
android:layout_toLeftOf
="@id/b2"
           
android:onClick
="doClick"
           
android:layout_centerInParent
="true"
           
>
        </
ImageView
>
        <
ImageView
           
android:id
="@+id/b3"
           
android:layout_width
="120dp"
           
android:layout_height
="120dp"
           
android:src
="@drawable/sq"
           
android:layout_above
="@id/b6"
           
android:layout_toRightOf
="@id/b2"
           
android:onClick
="doClick"
           
android:layout_centerInParent
="true"
           
>
        </
ImageView
>
        <
ImageView
           
android:id
="@+id/b7"
           
android:layout_width
="120dp"
           
android:layout_height
="120dp"
           
android:src
="@drawable/sq"
           
android:layout_below
="@id/b4"
           
android:layout_toLeftOf
="@id/b8"
           
android:onClick
="doClick"
           
android:layout_centerInParent
="true"
           
>
        </
ImageView
>
        <
ImageView
           
android:id
="@+id/b9"
           
android:layout_width
="120dp"
           
android:layout_height
="120dp"
           
android:src
="@drawable/sq"
           
android:layout_toRightOf
="@id/b8"
           
android:layout_below
="@id/b4"
           
android:onClick
="doClick"
           
android:layout_centerInParent
="true"
           
>
        </
ImageView
>
    </
RelativeLayout
>

</
LinearLayout
>

 

 

ההרצה (JAVA)

package com.contineo.mygames;

import
androidx.appcompat.app.AppCompatActivity;

import
android.os.Bundle;
import
android.view.View;
import
android.widget.ImageView;
import
android.widget.Toast;

public class tictactoe extends
AppCompatActivity {

   
int [][] arr
;
    ImageView [][]
imgArr
;
   
int x
;
   
@Override
   
protected void
onCreate(Bundle savedInstanceState) {
       
super
.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_tictactoe
);

       
imgArr = new ImageView[3][3
];
       
arr = new int[3][3
];
       
imgArr[0][0] = findViewById(R.id.b1
);
       
imgArr[0][1] = findViewById(R.id.b2
);
       
imgArr[0][2] = findViewById(R.id.b3
);
       
imgArr[1][0] = findViewById(R.id.b4
);
       
imgArr[1][1] = findViewById(R.id.b5
);
       
imgArr[1][2] = findViewById(R.id.b6
);
       
imgArr[2][0] = findViewById(R.id.b7
);
       
imgArr[2][1] = findViewById(R.id.b8
);
       
imgArr[2][2] = findViewById(R.id.b9
);
       
x = 0
;
        clear();
        updateBoard();

    }

   
private ImageView getImage(int
id){
       
for (int row=0;row<arr.length
;row++)
           
for (int col=0;col<arr[0].length
;col++){
               
if (imgArr
[row][col].getId() == id)
                   
return imgArr
[row][col];
            }
       
return null
;
    }
   
private  int getRow(int
id){
       
for (int row=0;row<arr.length
;row++)
           
for (int col=0;col<arr[0].length
;col++){
               
if (imgArr
[row][col].getId() == id)
                   
return
row;
            }
       
return -1
;
    }
   
private  int getCol(int
id){
       
for (int row=0;row<arr.length
;row++)
           
for (int col=0;col<arr[0].length
;col++){
               
if (imgArr
[row][col].getId() == id)
                   
return
col;
            }
       
return -1
;
    }
   
private void
clear(){
       
for (int i=0;i<arr.length
;i++)
           
for (int j=0;j<arr[0].length
;j++)
               
arr[i][j] = 0
;
    }

   
public void
doClick(View view) {
       
int
id = view.getId();
       
int
row = getRow(id);
       
int
col = getCol(id);
       
if (arr[row][col] == 0
)
           
arr[row][col] = 1
;
       
else if (arr[row][col] == 1
)
           
arr[row][col] = 2
;
       
else
           
arr[row][col] = 0
;
        updateBoard();

    }
   
private void
updateBoard(){
       
for (int row=0;row<arr.length
;row++)
           
for (int col=0;col<arr[0].length
;col++){
               
if (arr[row][col] == 0
){
                   
imgArr[row][col].setImageDrawable(getDrawable(R.drawable.sq
));
                }
               
if (arr[row][col] == 1
){
                   
imgArr[row][col].setImageDrawable(getDrawable(R.drawable.xpic
));
                }
               
if (arr[row][col] == 2
){
                   
imgArr[row][col].setImageDrawable(getDrawable(R.drawable.opic
));
                }
            }
    }

   
public void
startGame(View view) {
        clear();
        updateBoard();
    }
}