Uno (Card Game)/Julia: Difference between revisions

m
Fixed syntax highlighting.
m (move logging area to side)
m (Fixed syntax highlighting.)
 
(4 intermediate revisions by one other user not shown)
Line 2:
 
Gtk based graphical version.
<langsyntaxhighlight lang="julia">using Random, Colors, Gtk, Cairo
 
#=========== Channel and flag (IPC) section ===================#
 
""" channel, communicates player's mouse choice of card or color to game logic """
const channel = Channel{Any}(100)
 
""" flush the channel from mouse choice to game logic """
Line 18:
 
""" The Uno card type. The first field is color, second field is number or command. """
const UnoCard = Pair{String, String}
color(c::UnoCard) = first(c)
type(c::UnoCard) = last(c)
Line 97:
hands = [deck[i:i+6] for i in 1:7:27]
game = UnoCardGameState(drawpile, discardpile, [UnoCardGamePlayer(playernames[i], 0,
startswith(playernames[i], "Bot") ? true : false, hands[i])
for i in 1:length(playernames)], 1, "Wild", "Wild", true, true)
dealer = rand(1:length(playernames))
Line 152:
# bot will challenge half the time, player must challenge in 5 seconds.
if game.players[game.pnow].isabot && rand() < 0.5 ||
(!game.players[game.pnow].isabot && challenge[begin] == true)
challenge[begin] = false
logline("$(game.players[game.pnow].name) challenged Draw Four!")
Line 158:
nextplayer!(game); nextplayer!(game); nextplayer!(game); # prior player
game.colornow = game.lastcolor
indices = playableindices(game)
hand = game.players[game.pnow].hand
if any(ic -> color(hand[i]c) !== "Wild"game.colornow, playableindices(game)hand)
logline("Challenge sustained! Challenged Draw Four player draws 4.")
drawcardsfromdeck!(game, 2); drawcardsfromdeck!(game, 2)
Line 335 ⟶ 334:
"Green" => colorant"green", "Blue" => colorant"blue", "Wild" => colorant"black")
 
""" CSS style button for bold colored text """
function colorbutton(txt::String, clr::String)
button = GtkButton(txt)
Line 465:
hand = first(game.players).hand
isempty(hand) && return
nrow = (length(hand) + 15) ÷ 1615
for row in 1:nrow
cards = hand[(row - 1) * 1615 + 1 : min(length(hand), row * 1615 - 1)]
startx, starty = 40 + (1615 - length(cards)) * 20, 500 + 85 * (row - 1)
for (i, card) in enumerate(cards)
idx, x0 = (row - 1) * 1615 + i, startx + 50 * (i - 1)
cardpositions[idx] = [x0, starty, x0 + 40, starty + 80]
cairocard(ctx, card, x0, starty, 40, 80)
Line 536:
end
 
UnoCardGameApp()</syntaxhighlight>
</lang>
9,476

edits