Skip to content

Instantly share code, notes, and snippets.

@stravid stravid/client.rb
Created Nov 9, 2016

Embed
What would you like to do?
require "socket"
server_ip = "192.168.1.14"
client_ip = "192.168.1.5"
server_port = 22040
server_socket = UDPSocket.new
server_socket.connect(server_ip, server_port)
name = "Schorsch-Klaus-David-#{rand}"
client_deck = []
server_deck = []
money = 100
def deck_value(deck)
if deck.include?(11)
value_a = deck.inject(0, :+)
value_b = deck.inject(0, :+) - 10
if value_a <= 21
value_a
else
value_b
end
else
deck.inject(0, :+)
end
end
server_socket.send("JOIN;#{name}", 0)
while true do
message = server_socket.recvfrom(65536)
puts "received: #{message.inspect}"
parts = message[0].split(";")
if parts[0] == "ROUND STARTING"
client_deck = []
server_deck = []
end
if parts[0] == "SET"
if money < 50
amount_to_set = 5
else
amount_to_set = 17
end
server_socket.send("SET;#{amount_to_set};#{name}", 0)
end
if parts[0] == "MONEY"
money = parts[1].to_i
end
if parts[0] == "BANK"
server_deck << parts[1].to_i
end
if parts[0] == "CARD"
client_deck << parts[1].to_i
end
if parts[0] == "STAY_OR_CARD"
value_client_deck = deck_value(client_deck)
value_server_deck = server_deck.inject(0, :+)
if client_deck.include?(11) && value_client_deck < 17
server_socket.send("CARD;#{name}", 0)
elsif client_deck.include?(11) && value_server_deck > 10 && value_client_deck < 19
server_socket.send("CARD;#{name}", 0)
elsif value_server_deck == 11 && value_client_deck < 19
server_socket.send("CARD;#{name}", 0)
elsif value_server_deck == 10 && value_client_deck < 17
server_socket.send("CARD;#{name}", 0)
elsif [7, 8, 9].include?(value_server_deck) && value_client_deck < 13
server_socket.send("CARD;#{name}", 0)
elsif [5, 6].include?(value_server_deck) && value_client_deck < 15
server_socket.send("CARD;#{name}", 0)
elsif [3, 4].include?(value_server_deck) && value_client_deck < 13
server_socket.send("CARD;#{name}", 0)
elsif [2].include?(value_server_deck) && value_client_deck < 13
server_socket.send("CARD;#{name}", 0)
elsif value_client_deck < 15
server_socket.send("CARD;#{name}", 0)
else
server_socket.send("STAY;#{name}", 0)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.