ผลต่างระหว่างรุ่นของ "Sw-spec/2048"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
 
(ไม่แสดง 7 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน)
แถว 261: แถว 261:
  
 
     fun reset() {
 
     fun reset() {
 +
    }
 +
 +
    fun isFull(): Boolean {
 +
        return false
 
     }
 
     }
  
แถว 305: แถว 309:
 
         }
 
         }
 
     }
 
     }
 +
</pre>
 +
== API for high score ==
 +
Base url is at http://theory.cpe.ku.ac.th:5000/
 +
 +
You should pick a unique bucket name (e.g., your name).  Later on refer to as 'BUCKETNAME'
 +
 +
There are 4 end points (access with GET requests):
 +
 +
* <nowiki>http://theory.cpe.ku.ac.th:5000/scores/[BUCKETNAME]</nowiki> -- lists all scores; server would returns json list of score, for example: <tt>[{"score": 20000, "title": "warit"}, {"score": 1500, "title": "james"}, {"score": 100, "title": "warit"}]</tt>
 +
* <nowiki>http://theory.cpe.ku.ac.th:5000/save/[BUCKETNAME]/[NAME]/[SCORE]</nowiki> -- saves score of [SCORE] for [NAME]; server would returns '''OK'''
 +
 +
The other 2 are for debugging:
 +
 +
* <nowiki>http://theory.cpe.ku.ac.th:5000/reset/[BUCKETNAME]</nowiki> -- clear bucket [BUCKETNAME]; server would returns '''OK'''
 +
* <nowiki>http://theory.cpe.ku.ac.th:5000/</nowiki> -- list all buckets
 +
 +
=== How to make a GET request ===
 +
<pre>
 +
    // create a url object
 +
    val url = URL("http://theory.cpe.ku.ac.th:5000/")
 +
    val text = url.readText()
 +
</pre>
 +
 +
== Tests ==
 +
=== GameBoardTest ===
 +
<pre>
 +
package com.example.jittat.twfty1
 +
 +
import junit.framework.Assert.assertEquals
 +
import org.junit.Test
 +
 +
/**
 +
* Created by jittat on 16/3/2561.
 +
*/
 +
class GameBoardTest {
 +
    val board = GameBoard()
 +
 +
    @Test
 +
    fun testClearBoard() {
 +
        // when
 +
        board.clearCells()
 +
 +
        // we expect
 +
        assertEquals(16,countInBoard(0))
 +
    }
 +
 +
    @Test
 +
    fun testResetBoard() {
 +
        // when
 +
        board.reset()
 +
        // we expect
 +
        assertEquals(14,countInBoard(0))
 +
        assertEquals(2,countInBoard(2))
 +
    }
 +
 +
    @Test
 +
    fun testSlideLeft() {
 +
        initCells("0 0 2 2;" +
 +
                  "0 0 0 0;" +
 +
                  "0 0 0 0;" +
 +
                  "0 0 0 0")
 +
        board.slide(GameBoard.DIR_LEFT)
 +
        assertEquals("4 0 0 0;" +
 +
                    "0 0 0 0;" + "" +
 +
                    "0 0 0 0;" +
 +
                    "0 0 0 0",
 +
                extractCells())
 +
    }
 +
 +
    fun initCells(str: String) {
 +
        var r = 0
 +
        for(row in str.split(";")) {
 +
            var c = 0
 +
            for(col in row.split(" ")) {
 +
                board.values[r][c] = col.toInt()
 +
                c++
 +
            }
 +
            r++
 +
        }
 +
    }
 +
 +
    fun extractCells(): String {
 +
        val builder = StringBuilder()
 +
        for(r in 0..3) {
 +
            for(c in 0..3) {
 +
                builder.append(board.values[r][c].toString())
 +
                if(c != 3) { builder.append(" ") }
 +
            }
 +
            if(r != 3) { builder.append(";") }
 +
        }
 +
        return builder.toString()
 +
    }
 +
 +
    fun countInBoard(target: Int): Int {
 +
        var count = 0
 +
        for(r in 0..3) {
 +
            count += board.values[r].count { it == target }
 +
        }
 +
        return count
 +
    }
 +
}
 
</pre>
 
</pre>

รุ่นแก้ไขปัจจุบันเมื่อ 05:01, 16 มีนาคม 2561

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 isFull(): Boolean {
        return false
    }

    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]}"
                }
            }
        }
    }

API for high score

Base url is at http://theory.cpe.ku.ac.th:5000/

You should pick a unique bucket name (e.g., your name). Later on refer to as 'BUCKETNAME'

There are 4 end points (access with GET requests):

  • http://theory.cpe.ku.ac.th:5000/scores/[BUCKETNAME] -- lists all scores; server would returns json list of score, for example: [{"score": 20000, "title": "warit"}, {"score": 1500, "title": "james"}, {"score": 100, "title": "warit"}]
  • http://theory.cpe.ku.ac.th:5000/save/[BUCKETNAME]/[NAME]/[SCORE] -- saves score of [SCORE] for [NAME]; server would returns OK

The other 2 are for debugging:

  • http://theory.cpe.ku.ac.th:5000/reset/[BUCKETNAME] -- clear bucket [BUCKETNAME]; server would returns OK
  • http://theory.cpe.ku.ac.th:5000/ -- list all buckets

How to make a GET request

    // create a url object
    val url = URL("http://theory.cpe.ku.ac.th:5000/")
    val text = url.readText()

Tests

GameBoardTest

package com.example.jittat.twfty1

import junit.framework.Assert.assertEquals
import org.junit.Test

/**
 * Created by jittat on 16/3/2561.
 */
class GameBoardTest {
    val board = GameBoard()

    @Test
    fun testClearBoard() {
        // when
        board.clearCells()

        // we expect
        assertEquals(16,countInBoard(0))
    }

    @Test
    fun testResetBoard() {
        // when
        board.reset()
        // we expect
        assertEquals(14,countInBoard(0))
        assertEquals(2,countInBoard(2))
    }

    @Test
    fun testSlideLeft() {
        initCells("0 0 2 2;" +
                  "0 0 0 0;" +
                  "0 0 0 0;" +
                  "0 0 0 0")
        board.slide(GameBoard.DIR_LEFT)
        assertEquals("4 0 0 0;" +
                     "0 0 0 0;" + "" +
                     "0 0 0 0;" +
                     "0 0 0 0",
                extractCells())
    }

    fun initCells(str: String) {
        var r = 0
        for(row in str.split(";")) {
            var c = 0
            for(col in row.split(" ")) {
                board.values[r][c] = col.toInt()
                c++
            }
            r++
        }
    }

    fun extractCells(): String {
        val builder = StringBuilder()
        for(r in 0..3) {
            for(c in 0..3) {
                builder.append(board.values[r][c].toString())
                if(c != 3) { builder.append(" ") }
            }
            if(r != 3) { builder.append(";") }
        }
        return builder.toString()
    }

    fun countInBoard(target: Int): Int {
        var count = 0
        for(r in 0..3) {
            count += board.values[r].count { it == target }
        }
        return count
    }
}