import random
mensaje='''JUEGO DE DADOS JUGADOR 1 Y JUGADOR 2 CON DOS DADOS PARA JUGAR SIMPLEMENTE EJECUTE EL PROGRAMA'''
print(mensaje)
jugador1=0
jugador2=0for i in range(2):
tiro=random.randint(1,6)
print(tiro)
jugador1=jugador1+tiro
print(f'El jugador 1 tiene {jugador1} de puntaje')
for i in range(2):
tiro=random.randint(1,6)
print(tiro)
jugador2=jugador2+tiro
print(f'El jugador 2 tiene {jugador2} de puntaje')
if jugador1>jugador2:
print('El ganador es jugador 1')
elif jugador1<jugador2:
print('El ganador es jugador 2')
else:
print('Ambos jugadores empataron')```
import random
defjugar(players):
sumas = []
for i in range(players):
d1,d2 = int(random.uniform(1,7)), int(random.uniform(1,7))
print(f"Turno {i+1}: {d1,d2}")
sumas.append(d1+d2)
maximos = [i+1for i in range(players) if sumas[i]==max(sumas)]
if len(maximos)==1:
return f"Ganó el jugador {maximos[0]}"return f"Ganaron los jugadores {','.join([str(i) for i in maximos])}"
print(jugar(3))
Combine el reto en el que no necesariamente necesitas turnos, todo está visual: https://juegodados.imfast.io/
Aquí el código:
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Juego Dados</title></head><style> * { margin: 0; padding: 0; background-color: rgb(255, 255, 255); } .contenedor { min-width: 750px; width: 100%; height: 100vh; min-width: 750px; display: grid; grid-template-columns: 33%34%33%; grid-template-rows: 20%20%20%20%20%; grid-template-areas: '. titulo .''jugador1 jugador2 jugador3''jugador1 jugador2 jugador3''jugador1 jugador2 jugador3''btn1 btn2 btn3' ; } #titulo { grid-area: titulo; display: flex; align-items: center; justify-content: center; } #jugador1 { grid-area: jugador1; } #jugador2 { grid-area: jugador2; } #jugador3 { grid-area: jugador3; } #btn1{ grid-area: btn1; } #btn2{ grid-area: btn2; } #btn3{ grid-area: btn3; } #reset{ display: none; grid-area: btn2; } section { display: flex; flex-direction: column; align-items: center; justify-content: space-around; } sectionh1 { text-align: center; color: chocolate; } .btn { margin: 8px; border-radius: 15px; outline: none; font-size: 24px; border: solid 1px black; color: #000504; text-align: center; padding: 16px; margin-bottom: 36px; } .btn:hover { cursor: pointer; background-color: rgba(0, 0, 0, 0.397); color: white; } </style><bodyid='body'><divclass="contenedor"id='contenedor'><h1id='titulo'>Juego de Dados</h1><sectionid="jugador1"><h1>Jugador 1</h1><imgid='dado1J1'src="./utils/lado1.png"alt="dado"width="30%"><imgid='dado2J1'src="./utils/lado1.png"alt="dado"width="30%"></section><sectionid="jugador2"><h1>Jugador 2</h1><imgid='dado1J2'src="./utils/lado1.png"alt="dado"width="30%"><imgid='dado2J2'src="./utils/lado1.png"alt="dado"width="30%"></section><sectionid="jugador3"><h1>Jugador 3</h1><imgid='dado1J3'src="./utils/lado1.png"alt="dado"width="30%"><imgid='dado2J3'src="./utils/lado1.png"alt="dado"width="30%"></section><buttononclick="lanzar1()"class='btn'id="btn1">Lanza...</button><buttononclick="lanzar2()"class='btn'id="btn2">Lanza...</button><buttononclick="lanzar3()"class='btn'id="btn3">Lanza...</button><buttononclick="reset()"class='btn'id="reset">Reset...</button></div><script>// const img1 = new Image(); img1.src = './utils/lado1.png'// const img2 = new Image(); img2.src = './utils/lado2.png'// const img3 = new Image(); img3.src = './utils/lado3.png'// const img4 = new Image(); img4.src = './utils/lado4.png'// const img5 = new Image(); img5.src = './utils/lado5.png'// const img6 = new Image(); img6.src = './utils/lado6.png'let flags = { j1: false, j2: false, j3: false } let dadosMemory = { j1: 0, j2: 0, j3: 0 } let ganadores = [] functionlanzar1(){ const dado1J1 = document.getElementById('dado1J1') const dado2J1 = document.getElementById('dado2J1') const btn1 = document.getElementById('btn1') let dados = generarDados() cara(dados[0], dado1J1) cara(dados[1], dado2J1) btn1.style = 'display: none;' flags.j1 = true dados.forEach( num => dadosMemory.j1 = dadosMemory.j1 + num) ganador() } functionlanzar2(){ const dado1J2 = document.getElementById('dado1J2') const dado2J2 = document.getElementById('dado2J2') const btn2 = document.getElementById('btn2') let dados = generarDados() cara(dados[0], dado1J2) cara(dados[1], dado2J2) btn2.style = 'display: none;' flags.j2 = true dados.forEach( num => dadosMemory.j2 = dadosMemory.j2 + num) ganador() } functionlanzar3(){ const dado1J3 = document.getElementById('dado1J3') const dado2J3 = document.getElementById('dado2J3') const btn3 = document.getElementById('btn3') let dados = generarDados() cara(dados[0], dado1J3) cara(dados[1], dado2J3) btn3.style = 'display: none;' flags.j3 = true dados.forEach( num => dadosMemory.j3 = dadosMemory.j3 + num) ganador() } functionganador(){ const reset = document.getElementById('reset') if (flags.j1 === true && flags.j2 === true && flags.j3 === true){ reset.style = 'display: inline-block;'let valores = Object.values(dadosMemory) valores.sort((a, b) => a - b) let ganador = valores.pop() if (ganador === dadosMemory.j1){ ganadores.push('Jugador 1') } if (ganador === dadosMemory.j2){ ganadores.push('Jugador 2') } if (ganador === dadosMemory.j3){ ganadores.push('Jugador 3') } setTimeout(() => { if (ganadores.length === 1){ alert(`El ganador es ${ganadores}`) } elseif (ganadores.length === 2) { alert(`Los ganadores son ${ganadores.join(' y ')}`) } else { alert(`Los ganadores son ${ganadores.join(', ')}`) } }, 100); } } functionreset() { const btn1 = document.getElementById('btn1') const btn2 = document.getElementById('btn2') const btn3 = document.getElementById('btn3') const reset = document.getElementById('reset') const contenedor = document.getElementById('contenedor') btn1.style = 'display: inline-block;' btn2.style = 'display: inline-block;' btn3.style = 'display: inline-block;' reset.style = 'display: none;' flags = { j1: false, j2: false, j3: false } dadosMemory = { j1: 0, j2: 0, j3: 0 } ganadores = [] contenedor.innerHTML = ` <h1 id='titulo'>Juego de Dados</h1> <section id="jugador1"> <h1>Jugador 1</h1> <img id='dado1J1' src="./utils/lado1.png" alt="dado" width="30%"> <img id='dado2J1' src="./utils/lado1.png" alt="dado" width="30%"> </section> <section id="jugador2"> <h1>Jugador 2</h1> <img id='dado1J2' src="./utils/lado1.png" alt="dado" width="30%"> <img id='dado2J2' src="./utils/lado1.png" alt="dado" width="30%"> </section> <section id="jugador3"> <h1>Jugador 3</h1> <img id='dado1J3' src="./utils/lado1.png" alt="dado" width="30%"> <img id='dado2J3' src="./utils/lado1.png" alt="dado" width="30%"> </section> <button onclick="lanzar1()" class='btn' id="btn1">Lanza...</button> <button onclick="lanzar2()" class='btn' id="btn2">Lanza...</button> <button onclick="lanzar3()" class='btn' id="btn3">Lanza...</button> <button onclick="reset()" class='btn' id="reset">Reset...</button> ` } functiongenerarDados() { let dados = [] for (let i = 0; i < 2; i++){ dados.push(Math.floor((Math.random()*6) + 1)) } return dados } functioncara(caraDado, jugador) { switch(caraDado){ case1: jugador.src = './utils/lado1.png'break; case2: jugador.src = './utils/lado2.png'break; case3: jugador.src = './utils/lado3.png'break; case4: jugador.src = './utils/lado4.png'break; case5: jugador.src = './utils/lado5.png'break; case6: jugador.src = './utils/lado6.png'break; } } </script></body></html>
En Python 😃
import random mensaje='''JUEGO DE DADOS JUGADOR 1 Y JUGADOR 2 CON DOS DADOS PARA JUGAR SIMPLEMENTE EJECUTE EL PROGRAMA''' print(mensaje) jugador1=0 jugador2=0for i in range(2): tiro=random.randint(1,6) print(tiro) jugador1=jugador1+tiro print(f'El jugador 1 tiene {jugador1} de puntaje') for i in range(2): tiro=random.randint(1,6) print(tiro) jugador2=jugador2+tiro print(f'El jugador 2 tiene {jugador2} de puntaje') if jugador1>jugador2: print('El ganador es jugador 1') elif jugador1<jugador2: print('El ganador es jugador 2') else: print('Ambos jugadores empataron')```
Python:
import random defjugar(players): sumas = [] for i in range(players): d1,d2 = int(random.uniform(1,7)), int(random.uniform(1,7)) print(f"Turno {i+1}: {d1,d2}") sumas.append(d1+d2) maximos = [i+1for i in range(players) if sumas[i]==max(sumas)] if len(maximos)==1: return f"Ganó el jugador {maximos[0]}"return f"Ganaron los jugadores {','.join([str(i) for i in maximos])}" print(jugar(3))
Turno 1: (5, 2)
Turno 2: (4, 6)
Turno 3: (6, 4)
Ganaron los jugadores 2,3
Mi repo con el código en JS y C. El parámetro rolls de la función es el número de veces que se lanzan los dados.
https://github.com/ceporro/PlatziCodingChallenge/tree/master/Day 38-39
Juego de dados con 🐍🐍
import random defgenerate_dices(number_of_dices): dices = [] for _ in range(number_of_dices): dices.append([1, 2, 3, 4, 5, 6]) return dices defthrow_dices(dices): throws = [] for dice in dices: throw = random.choice(dice) # print(throw) throws.append(throw) score = sum(throws) return (throws, score) defselect_winner(scores):if scores[0] > scores[1] and scores[0] > scores[2]: print('PLAYER 1 WON!!!') elif scores[1] > scores[0] and scores[1] > scores[2]: print('PLAYER 2 WON!!!') elif scores[2] > scores[0] and scores[2] > scores[1]: print('PLAYER 3 WON!!!') elif scores[0] == scores[1] and scores[0] == scores[2]: print('ALL PLAYERS ARE TIED.') elif scores[0] == scores[1]: print('Player 1 and Player 2 are TIED.') elif scores[1] == scores[2]: print('Player 2 and Player 3 are TIED.') elif scores[0] == scores[2]: print('Player 1 and Player 3 are TIED.') defrun(): print('3 players will throw 2 dices each. The one with the highest score wins!\n') players = [] for player in range(3): dices = generate_dices(2) results, score = throw_dices(dices) players.append(score) print(f'\nPLAYER {player+1}') print(f'Dices thrown: {results}.') print(f'Score: {score}.\n') winner = select_winner(players) if __name__ == "__main__": run()
Juego de Dados en JavaScript
// Juego de Dados en JavaScriptclassDicesGame{ constructor() { this.player1_score = this.throwDices() // Turno jugador 1this.player2_score = this.throwDices() // Turno jugador 2this.player3_score = this.throwDices() // Turno jugador 3 } throwDices() { let score = 0for (let d = 0; d < 2; d++) { score += Math.ceil(Math.random() * 6) } return score } getResults() { let gameResult = ''if (this.player1_score === this.player2_score) { if (this.player1_score === this.player3_score) { gameResult = 'Empate: Todos' } elseif (this.player1_score > this.player3_score) { gameResult = 'Empate: Jugadores 1 y 2' } elseif (this.player1_score < this.player3_score) { gameResult = 'Gana: Jugador 3' } } elseif (this.player1_score > this.player2_score) { if (this.player1_score === this.player3_score) { gameResult = 'Empate: Jugadores 1 y 3' } elseif (this.player1_score > this.player3_score) { gameResult = 'Gana: Jugador 1' } elseif (this.player1_score < this.player3_score) { gameResult = 'Gana: Jugador 3' } } elseif (this.player1_score < this.player2_score) { if (this.player2_score === this.player3_score) { gameResult = 'Empate: Jugadores 2 y 3' } elseif (this.player2_score > this.player3_score) { gameResult = 'Gana: Jugador 2' } elseif (this.player2_score < this.player3_score) { gameResult = 'Gana: Jugador 3' } } console.log(` Puntajes: Jugador 1: ${this.player1_score}, Jugador 2: ${this.player2_score}, Jugador 3: ${this.player3_score}, ${gameResult} `) } } const juego = new DicesGame() juego.getResults() // Puntajes:// Jugador 1: 11,// Jugador 2: 4,// Jugador 3: 4,// Gana: Jugador 1
Mi juego en JS:
functionletsPlay(players) { let scores = newArray(); let dices = newArray(); let maxScore = 0; let gameResult = ""; if (players.length != 3) { return"Players required: 3"; } for (let i = 0; i < 3; i++) { let dice1 = Math.floor(Math.random() * (6 - 1) + 1); let dice2 = Math.floor(Math.random() * (6 - 1) + 1); let score = dice1 + dice2; let scoreText = `${dice1}, ${dice2}`; dices.push(scoreText); scores.push(score); } for (let j = 0; j < scores.length; j++) { if (scores[j] === maxScore) { gameResult = "Draw!!"; } elseif (scores[j] > maxScore) { maxScore = scores[j]; gameResult = `${players[j]} WINS, score: ${maxScore}`; } console.log(`${players[j]} : ${dices[j]} = ${scores[j]}`); } console.log(gameResult); }