import SwiftUI
|
|
|
|
private var textoEquipo = "Equipo"
|
|
private var colorDeFondo = Color.gray
|
|
|
|
struct ContentView: View {
|
|
@State private var player1Score = 0
|
|
@State private var player2Score = 0
|
|
@State private var player1Advantage = false
|
|
@State private var player2Advantage = false
|
|
|
|
@State private var player1Games = [0, 0, 0]
|
|
@State private var player2Games = [0, 0, 0]
|
|
|
|
@State private var isPlayer1Serving = true
|
|
@State private var showSelectServer = true
|
|
@State private var showResetView = false
|
|
|
|
@State private var setEnJuego = 0
|
|
@State private var finDePartido = false
|
|
@State private var modoTieBreak = false
|
|
@State private var saqueDeTie = 0
|
|
|
|
let scoreValues = ["0", "15", "30", "40", "Ventaja"]
|
|
|
|
var body: some View {
|
|
ZStack { // ZStack 1
|
|
VStack { // VStack 1
|
|
if showSelectServer {
|
|
SelectServerView(isPlayer1Serving: $isPlayer1Serving, showSelectServer: $showSelectServer)
|
|
} else if showResetView {
|
|
ResetView(resetScores: resetScores)
|
|
.transition(.move(edge: .leading))
|
|
} else { // if else showSelectServer
|
|
if finDePartido {
|
|
FinalPartidoView(resetScores: resetScores, jugadorUno: $player1Games, jugadorDos: $player2Games)
|
|
} else { // if else fin de partido
|
|
GeometryReader { geometry in // GeomertyReader 1
|
|
VStack { // VStack 2
|
|
// Text("Puntuación de Pádel")
|
|
// .font(.headline)
|
|
// .padding(.top, -30)
|
|
|
|
HStack {
|
|
HStack {
|
|
Button(action: {
|
|
desHacer()
|
|
}) {
|
|
Text("<")
|
|
.font(.system(size: geometry.size.width * 0.05))
|
|
.padding(.top,-5)
|
|
}.controlSize(.mini)
|
|
}
|
|
|
|
HStack {
|
|
Button(action: {
|
|
reHacer()
|
|
}) {
|
|
Text(">")
|
|
.font(.system(size: geometry.size.width * 0.05))
|
|
.padding(.top,-3)
|
|
}.controlSize(.mini)
|
|
}
|
|
}.padding(.vertical,-5)
|
|
|
|
HStack { // HStack 1
|
|
VStack { // VStack 3 - Equipo 1
|
|
HStack { // HStack 2
|
|
Text("\(textoEquipo) 1")
|
|
.font(.system(size: geometry.size.width * 0.05))
|
|
.padding()
|
|
.background(Color.blue)
|
|
.foregroundColor(.white)
|
|
.cornerRadius(10)
|
|
|
|
if isPlayer1Serving {
|
|
Circle()
|
|
.fill(Color.yellow)
|
|
.frame(width: geometry.size.width * 0.03, height: geometry.size.width * 0.03)
|
|
} else {
|
|
Circle()
|
|
.fill(colorDeFondo)
|
|
.frame(width: geometry.size.width * 0.03, height: geometry.size.width * 0.03)
|
|
}
|
|
} // Fin HStack - Equipo 1
|
|
|
|
if modoTieBreak {
|
|
Text("\(player1Score)")
|
|
.font(.system(size: geometry.size.width * 0.15)).background(Color.green)
|
|
} else {
|
|
if player1Advantage && player1Score == 3 {
|
|
Text("AD")
|
|
.font(.system(size: geometry.size.width * 0.15))
|
|
} else {
|
|
Text("\(scoreValues[player1Score])")
|
|
.font(.system(size: geometry.size.width * 0.15))
|
|
}
|
|
}
|
|
|
|
|
|
Button(action: {
|
|
addPoint(to: &player1Score, opponentScore: &player2Score, playerAdvantage: &player1Advantage, opponentAdvantage: &player2Advantage, playerGames: &player1Games[setEnJuego], opponentGames: &player2Games[setEnJuego])
|
|
}) {
|
|
Text("+")
|
|
.font(.system(size: geometry.size.width * 0.25))
|
|
.padding(.top,-10)
|
|
}
|
|
}
|
|
.padding(.horizontal)
|
|
|
|
VStack { // VStack 3 - Equipo 2
|
|
HStack { // HStack - Equipo 2
|
|
Text("\(textoEquipo) 2")
|
|
.font(.system(size: geometry.size.width * 0.05))
|
|
.padding()
|
|
.background(Color.red)
|
|
.foregroundColor(.white)
|
|
.cornerRadius(10)
|
|
|
|
if !isPlayer1Serving {
|
|
Circle()
|
|
.fill(Color.yellow)
|
|
.frame(width: geometry.size.width * 0.03, height: geometry.size.width * 0.03)
|
|
} else {
|
|
Circle()
|
|
.fill(colorDeFondo)
|
|
.frame(width: geometry.size.width * 0.03, height: geometry.size.width * 0.03)
|
|
}
|
|
} // Fin HStack - Equipo 2
|
|
|
|
if modoTieBreak {
|
|
Text("\(player2Score)")
|
|
.font(.system(size: geometry.size.width * 0.15)).background(Color.green)
|
|
} else {
|
|
if player2Advantage && player2Score == 3 {
|
|
Text("AD")
|
|
.font(.system(size: geometry.size.width * 0.15))
|
|
} else {
|
|
Text("\(scoreValues[player2Score])")
|
|
.font(.system(size: geometry.size.width * 0.15))
|
|
}
|
|
} // Fin HStack - equipo 2
|
|
|
|
Button(action: {
|
|
addPoint(to: &player2Score, opponentScore: &player1Score, playerAdvantage: &player2Advantage, opponentAdvantage: &player1Advantage, playerGames: &player2Games[setEnJuego], opponentGames: &player1Games[setEnJuego])
|
|
}) {
|
|
Text("+")
|
|
.font(.system(size: geometry.size.width * 0.25))
|
|
.padding(.top,-10)
|
|
}
|
|
}
|
|
.padding(.horizontal)
|
|
}
|
|
|
|
HStack {
|
|
if setEnJuego == 0 {
|
|
Circle()
|
|
.fill(Color.green)
|
|
.frame(width: geometry.size.width * 0.02, height: geometry.size.width * 0.02)
|
|
} else {
|
|
Circle()
|
|
.fill(colorDeFondo)
|
|
.frame(width: geometry.size.width * 0.02, height: geometry.size.width * 0.02)
|
|
}
|
|
Text("\(player1Games[0])-\(player2Games[0])")
|
|
.font(.system(size: geometry.size.width * 0.1))
|
|
|
|
if setEnJuego == 1 {
|
|
Circle()
|
|
.fill(Color.green)
|
|
.frame(width: geometry.size.width * 0.02, height: geometry.size.width * 0.02)
|
|
} else {
|
|
Circle()
|
|
.fill(colorDeFondo)
|
|
.frame(width: geometry.size.width * 0.02, height: geometry.size.width * 0.02)
|
|
}
|
|
Text("\(player1Games[1])-\(player2Games[1])")
|
|
.font(.system(size: geometry.size.width * 0.1))
|
|
|
|
if setEnJuego == 2 {
|
|
Circle()
|
|
.fill(Color.green)
|
|
.frame(width: geometry.size.width * 0.02, height: geometry.size.width * 0.02)
|
|
} else {
|
|
Circle()
|
|
.fill(colorDeFondo)
|
|
.frame(width: geometry.size.width * 0.02, height: geometry.size.width * 0.02)
|
|
}
|
|
Text("\(player1Games[2])-\(player2Games[2])")
|
|
.font(.system(size: geometry.size.width * 0.1))
|
|
}
|
|
.padding(.top)
|
|
|
|
} // VStack 2
|
|
.padding(.vertical,-25)
|
|
.background(Color.gray)
|
|
.gesture(DragGesture()
|
|
.onChanged { value in
|
|
if value.translation.width < -50 {
|
|
withAnimation {
|
|
showResetView = true
|
|
}
|
|
}
|
|
}
|
|
)
|
|
} // GeomertyReader 1
|
|
} // if else fin de partido
|
|
} // if else showSelectServer
|
|
} // VStack 1
|
|
} // ZStack 1
|
|
.background(colorDeFondo)
|
|
}
|
|
|
|
private func desHacer() {
|
|
|
|
}
|
|
|
|
private func reHacer() {
|
|
|
|
}
|
|
|
|
private func addPoint(to playerScore: inout Int, opponentScore: inout Int, playerAdvantage: inout Bool, opponentAdvantage: inout Bool, playerGames: inout Int, opponentGames: inout Int) {
|
|
|
|
if modoTieBreak {
|
|
saqueDeTie += 1
|
|
|
|
if saqueDeTie % 2 == 0 { // si es saque par cambia servicio
|
|
isPlayer1Serving.toggle()
|
|
}
|
|
|
|
if playerScore < 6 {
|
|
playerScore += 1
|
|
} else {
|
|
if opponentScore <= playerScore - 2 {
|
|
// ha ganado el tie break
|
|
|
|
playerGames += 1
|
|
|
|
if haGanadoSet(juegosMiJugador: playerGames, juegosElOtro: opponentGames) {
|
|
finDePartido = true
|
|
}
|
|
|
|
playerScore = 0
|
|
opponentScore = 0
|
|
playerAdvantage = false
|
|
opponentAdvantage = false
|
|
modoTieBreak = false
|
|
switchServer()
|
|
} else {
|
|
playerScore += 1
|
|
}
|
|
}
|
|
} else {
|
|
if playerScore < 3 {
|
|
playerScore += 1
|
|
} else if playerScore == 3 {
|
|
if opponentScore == 3 {
|
|
if playerAdvantage {
|
|
// Gana el juego
|
|
playerGames += 1
|
|
|
|
if haGanadoSet(juegosMiJugador: playerGames, juegosElOtro: opponentGames) {
|
|
finDePartido = true
|
|
}
|
|
|
|
playerScore = 0
|
|
opponentScore = 0
|
|
playerAdvantage = false
|
|
opponentAdvantage = false
|
|
switchServer()
|
|
} else if opponentAdvantage {
|
|
// Oponente pierde la ventaja
|
|
opponentAdvantage = false
|
|
} else {
|
|
// Gana la ventaja
|
|
playerAdvantage = true
|
|
}
|
|
} else {
|
|
// Gana el juego sin deuce
|
|
playerGames += 1
|
|
|
|
if haGanadoSet(juegosMiJugador: playerGames, juegosElOtro: opponentGames) {
|
|
finDePartido = true
|
|
}
|
|
|
|
playerScore = 0
|
|
opponentScore = 0
|
|
playerAdvantage = false
|
|
opponentAdvantage = false
|
|
switchServer()
|
|
}
|
|
} else {
|
|
// Gana el juego con ventaja
|
|
playerGames += 1
|
|
|
|
if haGanadoSet(juegosMiJugador: playerGames, juegosElOtro: opponentGames) {
|
|
finDePartido = true
|
|
}
|
|
|
|
playerAdvantage = false
|
|
opponentAdvantage = false
|
|
switchServer()
|
|
}
|
|
}
|
|
}
|
|
|
|
private func resetGameScores() {
|
|
player1Score = 0
|
|
player2Score = 0
|
|
player1Advantage = false
|
|
player2Advantage = false
|
|
}
|
|
|
|
private func resetScores(solocancelar: Bool) {
|
|
// print("resetScores: \(solocancelar)")
|
|
if solocancelar {
|
|
showResetView = false
|
|
} else {
|
|
resetGameScores()
|
|
|
|
for i in 0...2 {
|
|
player1Games[i] = 0
|
|
player2Games[i] = 0
|
|
}
|
|
finDePartido = false
|
|
setEnJuego = 0
|
|
showResetView = false
|
|
showSelectServer = true
|
|
modoTieBreak = false
|
|
}
|
|
}
|
|
|
|
private func deshacerJugada() {
|
|
|
|
}
|
|
|
|
private func switchServer() {
|
|
isPlayer1Serving.toggle()
|
|
}
|
|
|
|
private func haGanadoSet(juegosMiJugador: Int, juegosElOtro: Int) -> Bool {
|
|
// print("Yo llevo \(juegosMiJugador) y el otro lleva \(juegosElOtro)")
|
|
if modoTieBreak {
|
|
if juegosMiJugador == 7 {
|
|
if setEnJuego < 2 {
|
|
setEnJuego += 1
|
|
} else {
|
|
return (true)
|
|
}
|
|
}
|
|
} else {
|
|
if juegosMiJugador > 5 && juegosElOtro <= juegosMiJugador - 2 {
|
|
if setEnJuego < 2 {
|
|
setEnJuego += 1
|
|
} else {
|
|
return (true)
|
|
}
|
|
} else {
|
|
if juegosElOtro == 6 && juegosMiJugador == 6 {
|
|
saqueDeTie = 1
|
|
modoTieBreak = true
|
|
}
|
|
}
|
|
}
|
|
|
|
return(false)
|
|
}
|
|
}
|
|
|
|
struct FinalPartidoView: View {
|
|
var resetScores: (Bool) -> Void
|
|
|
|
@Binding var jugadorUno: [Int]
|
|
@Binding var jugadorDos: [Int]
|
|
|
|
var body: some View {
|
|
GeometryReader { geometry in
|
|
|
|
VStack {
|
|
Text("Partido terminado").padding(.top).font(.headline)
|
|
Text("Equipo 1: \(jugadorUno[0]) - \(jugadorUno[1]) - \(jugadorUno[2])").padding(.top)
|
|
|
|
Text("Equipo 2: \(jugadorDos[0]) - \(jugadorDos[1]) - \(jugadorDos[2])").padding(.top)
|
|
|
|
Button(action: {
|
|
resetScores(false)
|
|
}) {
|
|
Text("Nuevo partido")
|
|
.font(.system(size: geometry.size.width * 0.1))
|
|
.padding()
|
|
.background(Color.red)
|
|
.foregroundColor(.white)
|
|
.cornerRadius(10)
|
|
}
|
|
.padding()
|
|
}
|
|
.padding().background(colorDeFondo)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct SelectServerView: View {
|
|
@Binding var isPlayer1Serving: Bool
|
|
@Binding var showSelectServer: Bool
|
|
|
|
var body: some View {
|
|
GeometryReader { geometry in
|
|
VStack {
|
|
Text("¿quién sirve primero?")
|
|
.font(.headline)
|
|
.padding(.bottom, 1)
|
|
.padding(.top, -20)
|
|
|
|
Button(action: {
|
|
isPlayer1Serving = true
|
|
showSelectServer = false
|
|
}) {
|
|
Text("\(textoEquipo) 1")
|
|
.font(.system(size: geometry.size.width * 0.1))
|
|
.padding()
|
|
.background(Color.blue)
|
|
.foregroundColor(.white)
|
|
.cornerRadius(10)
|
|
}
|
|
.padding(.bottom)
|
|
|
|
Button(action: {
|
|
isPlayer1Serving = false
|
|
showSelectServer = false
|
|
}) {
|
|
Text("\(textoEquipo) 2")
|
|
.font(.system(size: geometry.size.width * 0.1))
|
|
.padding()
|
|
.background(Color.red)
|
|
.foregroundColor(.white)
|
|
.cornerRadius(10)
|
|
}
|
|
}
|
|
.padding()
|
|
}
|
|
}
|
|
}
|
|
|
|
struct ResetView: View {
|
|
var resetScores: (Bool) -> Void
|
|
|
|
var body: some View {
|
|
GeometryReader { geometry in
|
|
VStack {
|
|
Text("¿Seguro que desea reiniciar?")
|
|
.font(.system(size: geometry.size.width * 0.08))
|
|
.padding(.top, 20)
|
|
|
|
Button(action: {
|
|
resetScores(false)
|
|
}) {
|
|
Text("Reiniciar")
|
|
.font(.system(size: geometry.size.width * 0.1))
|
|
.padding()
|
|
.background(Color.red)
|
|
.foregroundColor(.white)
|
|
.cornerRadius(10)
|
|
}
|
|
.padding()
|
|
|
|
Button(action: {
|
|
resetScores(true)
|
|
}) {
|
|
Text("Cancelar")
|
|
.font(.system(size: geometry.size.width * 0.1))
|
|
.padding()
|
|
.background(Color.green)
|
|
.foregroundColor(.white)
|
|
.cornerRadius(10)
|
|
}
|
|
.padding()
|
|
}
|
|
.background(Color.gray)
|
|
.edgesIgnoringSafeArea(.all)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct ContentView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
ContentView()
|
|
}
|
|
}
|