Uno (Card Game)/Julia: Difference between revisions

m
Fixed syntax highlighting.
m (link)
m (Fixed syntax highlighting.)
 
(7 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 """
flushchannel() = while !isempty(channel) take!(channel); end
 
""" challenge flag: true if challenge taken by player """
const challenge = [false]
 
#============ Game play section ==================#
 
""" 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)
 
""" Each Uno player has a name, a score, may be a bot, and has a hand of UnoCards. """
mutable struct UnoCardGamePlayer
name::String
score::Int
isabot::Bool
hand::Vector{UnoCard}
Line 28 ⟶ 32:
"""
mutable struct UnoCardGameState
 
Encapsulates a board state of the gane, including players, cards, color being played,
order of play, current player, and whether the card showing has had its command used
Line 59 ⟶ 62:
const originaldeck = [vec([UnoCard(c, v) for v in ttypes, c in colors]);
fill(UnoCard("Wild", "Wild"), 4); fill(UnoCard("Wild", "Draw Four"), 4)]
 
""" challenge flag """
const challenge = [false]
 
""" Set the next player to play to game.pnow (clockwise or counterclockwise) """
Line 71:
"""
nextsaiduno(game)
 
Returns true if the next player to play has said Uno, which means they have only one card left.
If so, it is best for the current player if they play to make them draw or lose a turn.
Line 80 ⟶ 79:
return length(game.players[nextp].hand) == 1
end
 
cardscore(c) = (t = type(c); t == "Draw Four" ? 50 : t in cmdtypes ? 20 : parse(Int, t))
handscore(hand) = isempty(hand) ? 0 : sum(cardscore, hand)
 
"""
UnoCardGameState(playernames = ["Player", "Bot1", "Bot2", "Bot3"])
 
Construct and initialize Uno game. Includes dealing hands and picking who is to start.
"""
Line 95 ⟶ 96:
end
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 117 ⟶ 118:
end
return game
end
 
function nextgame(game::UnoCardGameState)
newgame = UnoCardGameState()
for i in eachindex(newgame.players)
newgame.players[i].score = game.players[i].score
end
return newgame
end
 
Line 128 ⟶ 137:
"""
playableindices(game)
 
Return a vector of indices of cards in hand that are legal to discard
"""
Line 140 ⟶ 148:
""" Current player to draw n cards from the draw pile. """
function drawcardsfromdeck!(game, n=1)
n in [2, 4] && (game.commandsvalid = false)
if n == 4 # draw four
# 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 149 ⟶ 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 playerrDraw Four player draws 4.")
drawcardsfromdeck!(game, 42); drawcardsfromdeck!(game, 2)
game.pnow, game.colornow = challenger, savecolor
return
Line 162 ⟶ 170:
game.pnow, game.colornow = challenger, savecolor
end
challenge[begin] = false
end
logline("Player $(game.players[game.pnow].name) draws $n card$(n == 1 ? "" : "s").")
for _ in 1:n
push!(game.players[game.pnow].hand, pop!(game.drawpile))
if isempty(game.drawpile) # out of draw pile
game.drawpile = shuffle(game.discardpile[begin:end-1])
game.discardpile = [game.discardpile[end]]
end
end
end
Line 171 ⟶ 184:
"""
discard!(game, idx = -1)
 
Current player to discard card at index idx in hand (last card in hand as default).
Handle wild card discard by having current player choose the new game.colornow.
Line 195 ⟶ 207:
"""
turn!(game)
 
Execute a single turn of the game. Command cards are followed only the first turn after played.
"""
Line 220 ⟶ 231:
else # num card, or command card is already used
if isempty(indices)
drawcardsfromdeck!(game, 1) # draw, then discard if drawn card is a match
indices = playableindices(game)
if !isempty(indices) && discard!(game, first(indices))
logline("Drawn card was discardable.")
discard!(game, first(indices))
end
elseif !startswith(name, "Bot") # not bot, so player moves
logline("Click on a card to play.")
Line 248 ⟶ 262:
"""
choosecolor!(game)
 
Choose a new game.colornow, automatically if a bot, via player choice if not a bot.
"""
Line 274 ⟶ 287:
const unodocshtml = """
Official Rules For Uno Card Game
 
The aim of the game is to be the first player to score 500 points, achieved (usually over several rounds of play) by being the first to play all of one's own cards and scoring points for the cards still held by the other players.
 
 
The deck consists of 108 cards: four each of "Wild" and "Wild Draw Four", and 25 each of four colors (red, yellow, green, blue). Each color consists of one zero, two each of 1 through 9, and two each of "Skip", "Draw Two", and "Reverse". These last three types are known as "action cards".
 
 
To start a hand, seven cards are dealt to each player, and the top card of the remaining deck is flipped over and set aside to begin the discard pile. The player to the dealer's left plays first unless the first card on the discard pile is an action or Wild card (see below). On a player's turn, they must do one of the following:
* play one card matching the discard in color, number, or symbol
* play a Wild card, or a playable Wild Draw Four card (see restriction below)
* draw the top card from the deck, then play it if possible
 
Cards are played by laying them face-up on top of the discard pile. Play proceeds clockwise around the table.
 
Action or Wild cards have the following effects:
 
===============================================================================================================================================================
Card Effect when played from hand Effect as first discard
Line 298 ⟶ 303:
Wild Player declares the next color to be matched ; current color may be chosen Player to dealer's left declares the first color to be matched and plays a card in it
Wild Draw Four Player declares the next color to be matched; next player in sequence draws four Return card to the deck, shuffle, flip top card to start discard pile
 
A player who draws from the deck must either play or keep that card and may play no other card from their hand on that turn.
 
A player may play a Wild card at any time, even if that player has other playable cards.
 
A player may play a Wild Draw Four card only if that player has no cards matching the current color. The player may have cards of a different color matching the current number or symbol or a Wild card and still play the Wild Draw Four card.[5] A player who plays a Wild Draw Four may be challenged by the next player in sequence (see Penalties) to prove that their hand meets this condition.
 
If the entire deck is used during play, the top discard is set aside and the rest of the pile is shuffled to create a new deck. Play then proceeds normally.
 
It is illegal to trade cards of any sort with another player.
 
A player who plays their next-to-last-card must call "uno" as a warning to the other players.[6]
 
The first player to get rid of their last card ("going out") wins the hand and scores points for the cards held by the other players. Number cards count their face value, all action cards count 20, and Wild and Wild Draw Four cards count 50. If a Draw Two or Wild Draw Four card is played to go out, the next player in the sequence must draw the appropriate number of cards before the score is tallied.
 
The first player to score 500 points wins the game.
 
Penalties
=========
If a player does not call "uno" after laying down their next-to-last card and is caught before the next player in sequence takes a turn (i.e., plays a card from their hand, draws from the deck, or touches the discard pile), they must draw two cards as a penalty. If the player is not caught in time (subject to interpretation) or remembers to call "uno" before being caught, they suffer no penalty.
 
If a player plays a Wild Draw Four card, the following player can challenge its use. The player who used the Wild Draw Four must privately show their hand to the challenging player, in order to demonstrate that they had no matching colored cards. If the challenge is correct, then the challenged player draws four cards instead. If the challenge is wrong, then the challenger must draw six cards; the four cards they were already required to draw plus two more cards.
 
"""
 
Line 339 ⟶ 333:
const cairocolor = Dict("Red" => colorant"red", "Yellow" => colorant"gold",
"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)
sc = Gtk.GAccessor.style_context(button)
pr = Gtk.CssProviderLeaf(data="button {color:$(clr); font-weight: bolder}")
push!(sc, Gtk.StyleProvider(pr), 600)
return button
end
 
""" Draw a UnoCard as a rectangle with rounded corners. """
Line 406 ⟶ 409:
"""
UnoCardGameApp(w = 800, hcan = 600, hlog = 100)
 
Uno card game Gtk app. Draws game on a canvas, logs play on box below canvas.
"""
function UnoCardGameApp(w = 8641120, hcanwcan = 700810, hlogh = 100700)
win = GtkWindow("Uno Card Game", w, hcan + hlogh) |> (GtkFrame() |> (vboxhbox = GtkBox(:vh)))
swinvbox = GtkScrolledWindowGtkBox(:v)
can = GtkCanvas(wwcan, hcanh)
set_gtk_propertypush!(canhbox, :expand, truecan)
push!(swin, can)
push!(vbox, swin)
push!(vbox, logwindow) # from log section
set_gtk_property!(logwindow, :expand, true)
b, g = colorbutton("Blue", "blue"), colorbutton("Green", "green")
r, y = colorbutton("Red", "red"), colorbutton("Yellow", "gold")
chal = GtkButton("Challenge")
signal_connect(w -> push!(channel, "Blue"), b, "clicked")
signal_connect(w -> push!(channel, "Green"), g, "clicked")
signal_connect(w -> push!(channel, "Red"), r, "clicked")
signal_connect(w -> push!(channel, "Yellow"), y, "clicked")
signal_connect(w -> (challenge[begin] = true), chal, "clicked")
push!(vbox, b, g, r, y, chal)
push!(hbox, vbox)
fontpointsize = w / 50
cardpositions = Dict{Int, Vector{Int}}()
 
colorpositions = Dict("Red" => [280, 435, 320, 475], "Yellow" => [340, 435, 380, 475],
# announce the rules and penalties per task description
"Green" => [400, 435, 440, 475], "Blue" => [460, 435, 500, 475])
info_dialog(unodocshtml, win)
challengeposition = [300, 492, 470, 482]
 
# create a game instance to start play
game = UnoCardGameState()
 
Line 436 ⟶ 449:
color = colorant"navy"
set_source(ctx, color)
move_to(ctx, 360, 400420)
show_text(ctx, game.players[1].name)
stroke(ctx)
Line 450 ⟶ 463:
cairocard(ctx, last(game.discardpile), 350, 240, 40, 80)
cairodrawfacedowncard(ctx, 410, 240, 40, 80)
for (i, p) in enumerate(colorpositions)
set_source(ctx, cairocolor[first(p)])
x0, y0, x1, y1 = last(p)
rectangle(ctx, x0, y0, 40, 40)
fill(ctx)
end
set_source(ctx, colorant"black")
move_to(ctx, challengeposition[1], challengeposition[2])
show_text(ctx, "Challenge Draw Four")
stroke(ctx)
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 474 ⟶ 477:
end
 
""" Gtk mouse callback: translates vaildvalid mouse clicks to a channel item """
signal_connect(can, "button-press-event") do b, evt
if challengeposition[1] < evt.x < challengeposition[3] &&
challengeposition[4] < evt.y < challengeposition[2]
challenge[begin] = true
return
end
challenge[begin] = false
for p in colorpositions
x0, y0, x1, y1 = last(p)
if x0 < evt.x < x1 && y0 < evt.y < y1
push!(channel, first(p))
return
end
end
for p in cardpositions
x0, y0, x1, y1 = last(p)
Line 498 ⟶ 488:
end
 
# do the turns of play in the game, keeping score totals from the rounds
info_dialog(unodocshtml, win)
draw(can)for n in 1:1000
Gtk.showall draw(wincan)
Gtk.showall(win)
while !any(i -> isempty(game.players[i].hand), 1:4)
turnwhile !any(i -> isempty(game.players[i].hand), 1:4)
if type(game.discardpile[end]) == "Draw Four" && turn!(game.pnow == 1)
if startswith(game.players[game.pnow].name, "Play") &&
type(game.discardpile[end]) == "Draw Four" && game.commandsvalid
draw(can)
Gtk.showall(win)
info_dialog("Click Challenge within 5 seconds to challenge Draw Four")
sleep(3)
end
sleep(2)
draw(can)
showGtk.showall(canwin)
end
info_dialog("Choose Challenge to challenge a Draw Four")
winner = findfirst(i -> isempty(game.players[i].hand), 1:length(game.players))
sleep(5)
if type(game.discardpile[end]) == "Draw Two"
nextplayer!(game) # next player might have to draw before scoring done
drawcardsfromdeck!(game, 2)
elseif type(game.discardpile[end]) == "Draw Four"
nextplayer!(game)
drawcardsfromdeck!(game, 2)
drawcardsfromdeck!(game, 2) # D2 twice because not to be contested as a D4
end
roundpoints = sum(x -> handscore(x.hand), game.players)
game.players[winner].score += roundpoints
 
logline("Player $(game.players[winner].name) wins round $(n)!")
info_dialog("The winner of round $n is $(game.players[winner].name).\n" *
"Winner gains $roundpoints points.", win)
logline("Scores: $([x.score for x in game.players])")
if any(x -> x.score >= 500, game.players)
s = "Game over. Scores:\n\n"
for p in game.players
s *= " $(p.name): $(p.score) $(p.score >= 500 ? "WINNER!" : "")\n"
end
info_dialog(s)
break
end
logline("-------------------------\nNew round!\n-----------------------")
sleep(2)
game = nextgame(game)
draw(can)
showGtk.showall(canwin)
end
winner = findfirst(i -> isempty(game.players[i].hand), 1:4)
logline("Player number $winner wins!")
info_dialog(winner == nothing ? "No winner found." :
"The WINNER is $(game.players[winner].name)!", win)
end
 
UnoCardGameApp()</syntaxhighlight>
</lang>
9,476

edits