---
# System prepended metadata

title: 'Android App 設計開發課程 #3'

---


## 剪刀石頭布遊戲精簡版

```kotlin=
enum class Choice {
    石頭, 布, 剪刀
}

fun main() {
    val playerChoice = Choice.布
    val computerChoice = Choice.values().random()
    val result = (playerChoice.ordinal - computerChoice.ordinal + 3) % 3
    println("玩家出拳：$playerChoice")
    println("電腦出拳：$computerChoice")
    println("結果：${when (result) { 0 -> "平手"; 1 -> "你贏了"; else -> "你輸了" }}")
}
```

## 課後練習：剪刀石頭布遊戲三戰兩勝版

# Jetpack Compose

* [Composable function naming rules](https://github.com/androidx/androidx/blob/androidx-main/compose/docs/compose-api-guidelines.md#naming-unit-composable-functions-as-entities)


## 名人語錄 App

```kotlin=
package com.example.myfirstcomposeapp

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.myfirstcomposeapp.ui.theme.MyFirstComposeAppTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyFirstComposeAppTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    QuoteText(message = "Life is like a box of chocolates.", from = "Forrest Gump")
                }
            }
        }
    }
}

@Composable
fun QuoteText(message: String, from: String, modifier: Modifier = Modifier) {
    Column (
        verticalArrangement = Arrangement.Center,
        modifier = modifier
    ) {
        Text(
            text = message,
            fontSize = 48.sp,
            lineHeight = 55.sp,
            textAlign = TextAlign.Center,
            modifier = modifier.padding(top = 16.dp)
        )
        Text(
            text = from,
            fontSize = 36.sp,
            modifier = modifier
                .padding(top = 16.dp)
                .padding(end = 16.dp)
                .align(alignment = Alignment.End)
        )
    }
}

@Preview(showBackground = true)
@Composable
fun QuotePreview() {
    MyFirstComposeAppTheme {
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colorScheme.background
        ) {
            QuoteText(message = "Life is like a box of chocolates.", from = "Forrest Gump")
        }
    }
}
```