Skip to content

Commit

Permalink
tictactoe and weather app added
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-i committed Oct 30, 2023
1 parent 299adbd commit 7ba9890
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 57 deletions.
130 changes: 73 additions & 57 deletions video/ai-extensions/video-4-tictactoe-game/AiExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
package com.glowbom.custom

import androidx.compose.foundation.layout.*
import androidx.compose.material3.Card
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

class AiExtensions {
companion object {
// Flag to enable or disable the GlowbyScreen
var enabled = true

// Title for the GlowbyScreen
var title = "Weather App"
var title = "App"

/**
* Composable function that displays the GlowbyScreen when enabled is set to true.
* The GlowbyScreen consists of the Current Weather Screen and the Weekly Forecast Screen.
* The GlowbyScreen consists of a Tic-tac-toe game when enabled is true.
*
* @param modifier Modifier to be applied to the Box layout
*/
Expand All @@ -30,59 +39,66 @@ class AiExtensions {
modifier: Modifier = Modifier
) {
if (enabled) {
Column(
modifier = modifier
.fillMaxSize()
.padding(16.dp)
) {
CurrentWeatherScreen()
Divider(modifier = Modifier.padding(vertical = 16.dp))
WeeklyForecastScreen()
}
}
}

@Composable
fun CurrentWeatherScreen() {
Card(
modifier = Modifier.fillMaxWidth()
) {
Column(
modifier = Modifier.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "San Francisco", style = MaterialTheme.typography.headlineMedium)
Text(text = "🌤", style = MaterialTheme.typography.displayLarge, textAlign = TextAlign.Center)
Text(text = "72°", style = MaterialTheme.typography.displayMedium)
Text(text = "Sunny", style = MaterialTheme.typography.bodyMedium)
}
}
}
// Tic-tac-toe game implementation...
val boardSize = 3
val board = remember { mutableStateOf(Array(boardSize) { Array(boardSize) { "" } }) }
val currentPlayer = remember { mutableStateOf("X") }
val winner = remember { mutableStateOf("") }

@Composable
fun WeeklyForecastScreen() {
Column {
for (i in 0..6) {
ForecastCard()
Spacer(modifier = Modifier.height(8.dp))
// Check for a win condition
fun checkWinner() {
// Check rows and columns
for (i in 0 until boardSize) {
if (board.value[i][0] != "" && board.value[i][0] == board.value[i][1] && board.value[i][0] == board.value[i][2]) {
winner.value = board.value[i][0]
}
if (board.value[0][i] != "" && board.value[0][i] == board.value[1][i] && board.value[0][i] == board.value[2][i]) {
winner.value = board.value[0][i]
}
}
// Check diagonals
if (board.value[0][0] != "" && board.value[0][0] == board.value[1][1] && board.value[0][0] == board.value[2][2]) {
winner.value = board.value[0][0]
}
if (board.value[2][0] != "" && board.value[2][0] == board.value[1][1] && board.value[2][0] == board.value[0][2]) {
winner.value = board.value[2][0]
}
}
}
}

@Composable
fun ForecastCard() {
Card(
modifier = Modifier.fillMaxWidth()
) {
Row(
modifier = Modifier.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = "MON", style = MaterialTheme.typography.labelMedium)
Spacer(modifier = Modifier.weight(1f))
Text(text = "🌤", style = MaterialTheme.typography.displayMedium, textAlign = TextAlign.Center)
Spacer(modifier = Modifier.weight(1f))
Text(text = "68°/52°", style = MaterialTheme.typography.bodyMedium)
Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Column {
for (i in 0 until boardSize) {
Row {
for (j in 0 until boardSize) {
Box(
Modifier
.clickable {
if (board.value[i][j] == "" && winner.value == "") {
val newBoard = board.value.map { it.copyOf() }.toTypedArray() // Create a new copy of the board
newBoard[i][j] = currentPlayer.value // Update the new copy with the new value
board.value = newBoard // Assign the new copy to the board state
checkWinner()
currentPlayer.value = if (currentPlayer.value == "X") "O" else "X"
}
}
.size(100.dp) // Increase the size of the box
) {
Surface(
color = if (board.value[i][j] == "X") Color.Red else if (board.value[i][j] == "O") Color.Green else Color.Gray,
) {
Text(
text = board.value[i][j],
color = Color.White,
modifier = Modifier.padding(16.dp),
style = TextStyle(fontSize = 24.sp, fontWeight = FontWeight.Bold) // Increase the size of the text and make it bold
)
}
}
}
}
}
Text(text = "Winner: ${winner.value}")
}
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions video/ai-extensions/video-5-weather-app/AiExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import SwiftUI

struct GlowbyScreen: View {
// This is a designated area where the OpenAI model can add or modify code.
// To enable the screen, set this value to true
// If it is false, the screen won't be visible in the app
static let enabled = true

// Change the title according to the assigned task
// This will be the name of the screen in the app
static let title = "Weather App"

// Replace this with the generated SwiftUI view
// Ensure the generated content shows up in the center of the screen
// within a frame with a maximum width of 360.0.
var body: some View {
VStack {
CurrentWeatherView()
Divider()
WeeklyForecastView()
}
.frame(maxWidth: 360)
}
}

struct CurrentWeatherView: View {
var body: some View {
VStack {
Text("San Francisco")
.font(.title)
.fontWeight(.bold)
Text("🌤")
.font(.system(size: 96))
Text("72°")
.font(.system(size: 64))
.fontWeight(.medium)
Text("Sunny")
.font(.title3)
.fontWeight(.light)
}
.padding()
}
}

struct WeeklyForecastView: View {
var body: some View {
ForEach(0..<7) { _ in
VStack {
HStack {
Text("MON")
.font(.headline)
Spacer()
Text("🌤")
.font(.title2)
Spacer()
Text("68°/52°")
.font(.body)
}
.padding(.horizontal)
Divider()
}
}
}
}

0 comments on commit 7ba9890

Please sign in to comment.