Sw-spec/2048
Layout
res/drawable/cell_back.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/white" />
<stroke android:width="1dip" android:color="#707070"/>
</shape>
activity_main.xml
<TextView
android:id="@+id/cell00"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell01"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell02"
app:layout_constraintLeft_toRightOf="@id/cell00"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell02"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell03"
app:layout_constraintLeft_toRightOf="@id/cell01"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell03"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintLeft_toRightOf="@id/cell02"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell10"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell11"
app:layout_constraintTop_toBottomOf="@id/cell00"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell11"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell12"
app:layout_constraintLeft_toRightOf="@id/cell10"
app:layout_constraintTop_toBottomOf="@id/cell00"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell12"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell13"
app:layout_constraintLeft_toRightOf="@id/cell11"
app:layout_constraintTop_toBottomOf="@id/cell00"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell13"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintLeft_toRightOf="@id/cell12"
app:layout_constraintTop_toBottomOf="@id/cell00"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell20"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell21"
app:layout_constraintTop_toBottomOf="@id/cell10"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell21"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell22"
app:layout_constraintLeft_toRightOf="@id/cell20"
app:layout_constraintTop_toBottomOf="@id/cell10"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell22"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell23"
app:layout_constraintLeft_toRightOf="@id/cell21"
app:layout_constraintTop_toBottomOf="@id/cell10"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell23"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintLeft_toRightOf="@id/cell22"
app:layout_constraintTop_toBottomOf="@id/cell10"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell30"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell31"
app:layout_constraintTop_toBottomOf="@id/cell20"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell31"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell32"
app:layout_constraintLeft_toRightOf="@id/cell30"
app:layout_constraintTop_toBottomOf="@id/cell20"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell32"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintRight_toLeftOf="@id/cell33"
app:layout_constraintLeft_toRightOf="@id/cell31"
app:layout_constraintTop_toBottomOf="@id/cell20"
android:gravity="center"
android:text="2048"/>
<TextView
android:id="@+id/cell33"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:minWidth="70dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintDimensionRatio="1:1"
android:background="@drawable/cell_back"
app:layout_constraintLeft_toRightOf="@id/cell32"
app:layout_constraintTop_toBottomOf="@id/cell20"
android:gravity="center"
android:text="2048"/>
<Button
android:id="@+id/restartButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/cell33"
android:text="Restart"/>
<Button
android:id="@+id/leftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/restartButton"
android:text="Left"/>
<Button
android:id="@+id/upButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/restartButton"
app:layout_constraintLeft_toRightOf="@id/leftButton"
android:text="Up"/>
<Button
android:id="@+id/downButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/restartButton"
app:layout_constraintLeft_toRightOf="@id/upButton"
android:text="Down"/>
<Button
android:id="@+id/rightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/restartButton"
app:layout_constraintLeft_toRightOf="@id/downButton"
android:text="Right"/>
GameBoard
GameBoard.kt
class GameBoard { var values = Array(4,{IntArray(4)}) companion object { val DIR_UP = 1 val DIR_RIGHT = 2 val DIR_DOWN = 3 val DIR_LEFT = 4 } fun reset() { } fun randomAdd() { } fun slide(dir: Int) { } }
GameBoardViewHolder
Put this inside MainActivity.kt
class GameBoardViewHolder(activity: Activity) { val views: Array<Array<TextView?>> = Array(4, { arrayOf<TextView?>(null,null,null,null) }) lateinit var gameBoard: GameBoard init { for(r in 0..3) { for(c in 0..3) { val cellId = "cell${r}${c}" val resId = activity.resources.getIdentifier(cellId, "id", activity.packageName) views[r][c] = activity.findViewById(resId) } } } fun bind(board: GameBoard) { gameBoard = board updateView() } fun updateView() { for(r in 0..3) { for (c in 0..3) { views[r][c]?.text = "${gameBoard.values[r][c]}" } } } }