Zbornak
Zbornak
MModular
Created by Zbornak on 1/16/2025 in #questions
Problem with simple card shuffle program
Hello all, sorry if this is just me being dopey, but I have been playing around and made a simple card shuffle program, but I get nonsensical results like "2 of Jack" and "0 of Diamonds". I just wanted to check if it's just me? I can't see why a face would become a suit in this instance?
from random import random_ui64, seed
from time import monotonic

fn draw(suits: List[String], faces: List[String], numbereds: List[Int]) raises -> String:
seed(int(monotonic()))

var rand_suit_idx = int(random_ui64(0, len(suits)))
var rand_suit = suits[rand_suit_idx]

# decide whether to use a face card or a numbered card
if random_ui64(0, 2) == 0:
var rand_face_idx = int(random_ui64(0, len(faces)))
return faces[rand_face_idx] + " of " + rand_suit
else:
var rand_numbered_idx = int(random_ui64(0, len(numbereds)))
return str(numbereds[rand_numbered_idx]) + " of " + rand_suit

fn main() raises -> None:
var suits = List[String]("Clubs", "Spades", "Hearts", "Diamonds")
var faces = List[String]("Jack", "Queen", "King", "Ace")
var numbered = List[Int](2, 3, 4, 5,6 , 7, 8, 9, 10)

print(draw(suits, faces, numbered))
from random import random_ui64, seed
from time import monotonic

fn draw(suits: List[String], faces: List[String], numbereds: List[Int]) raises -> String:
seed(int(monotonic()))

var rand_suit_idx = int(random_ui64(0, len(suits)))
var rand_suit = suits[rand_suit_idx]

# decide whether to use a face card or a numbered card
if random_ui64(0, 2) == 0:
var rand_face_idx = int(random_ui64(0, len(faces)))
return faces[rand_face_idx] + " of " + rand_suit
else:
var rand_numbered_idx = int(random_ui64(0, len(numbereds)))
return str(numbereds[rand_numbered_idx]) + " of " + rand_suit

fn main() raises -> None:
var suits = List[String]("Clubs", "Spades", "Hearts", "Diamonds")
var faces = List[String]("Jack", "Queen", "King", "Ace")
var numbered = List[Int](2, 3, 4, 5,6 , 7, 8, 9, 10)

print(draw(suits, faces, numbered))
Many thanks!
5 replies
MModular
Created by Zbornak on 12/4/2024 in #community-showcase
Morabaraba: a traditional board game from South Africa written in Mojo
No description
1 replies
MModular
Created by Zbornak on 2/22/2024 in #questions
Py def vs Mojo fn
Hello there, forgive me if this is a stupid question but I am not very familiar with Python. I was reading up on the fact that Mojo accepts both 'dynamic' Python functions as well as safe Mojo functions. It also states that it is important to know both when using Mojo; I just wanted to ask for a use case where a def would be preferable to a fn? Many thanks!
9 replies