我如何洗牌和交易卡一次一个玩家? [英] How do I shuffle and deal cards one at a time to players?

查看:105
本文介绍了我如何洗牌和交易卡一次一个玩家?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止,发现自己陷入困境。= /

This is what I got so far and find myself stuck.=/

Private Sub Dealbtn_Click(sender As Object, e As EventArgs) Handles Dealbtn.Click  
     Dim Suits() As String  = {"S", "D", "C", "H"} 
     Dim Faces() As String = {"2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"}  
     Dim rand As New Random  
     Dim rand1 As Integer = rand.Next(12)  
     Dim rand2 As Integer = rand.Next(3)  
     Label2.Text() = Faces(rand1) + Suits(rand2)  
End Sub


推荐答案

p>您需要先生成整个牌面(52张牌),将其存储在列表/堆栈/队列中,如果需要,可向玩家处理一张。

You need to generate the whole deck upfront (52 cards), store it in a List/Stack/Queue, and when required, deal one to the player.

双循环应该足够好以顺序生成卡,然后按随机数排序:

A double loop should be good enough to generate cards sequentially, then sort by random number:

Dim Suits() As String = {"S", "D", "C", "H"}
Dim Faces() As String = {"2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"}

Dim cards As New List(Of String)
For Each s As String In Suits
  For Each f As String In Faces
    cards.Add(s & f)
  Next
Next

Dim r As New Random
Dim cardsShuffled = cards.OrderBy(Function() r.Next)

编辑:这里是如何填充标签(只是一种方法) p>

Here is how you can populate your labels (just one way of doing it):

Dim deck As New Stack(Of String)(cardsShuffled)
For Each lbl As Label in {Label1, Label2, Label3, ...} 'you need to write all
  Try
    lbl.Text = deck.Pop()
  Catch ex As InvalidOperationException
    MessageBox.Show("No more cards.")
  End Try      
Next

参考:

  • Stack(Of T).Pop @ MSDN.

一个合适的解决方案是动态创建标签,但首先确保你可以得到这个工作。重构通常在之后之后完成。您有一个有效的产品。

A proper solution would be to create labels dynamically, but first make sure you can get this to work. Refactoring is usually done after you have a working product.

这篇关于我如何洗牌和交易卡一次一个玩家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆