我正在创建一个扑克游戏,我试图弄清楚如何在玩家手中拿着某些牌,同时替换玩家选择替换的牌。
tl;博士:我如何允许玩家挑选哪些牌以及用新牌取代哪些牌?
我的代码到目前为止:
交易类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PROG120_FINALPROJECT
{
class Deal
{
int CardHNum = 1;//Player's membership card number
private DeckofCards Deck = new DeckofCards();
private Card[] PlayerHand;
private Card[] ComputerHand;
private Card[] SortPHand; //sorted player hand
private Card[] SortCHand; // sorted computer hand
public Deal()
{
var selected = this.Deck.GetDeck.Take(10).Select((x, n) => new { x, n }).GroupBy(x => x.n % 2, x => x.x).ToArray();
this.PlayerHand = selected[0].ToArray();
this.ComputerHand = selected[1].ToArray();
}
public void DealSetup()
{
sortCards();
evaluateHands();
}
public void sortCards()
{
this.SortPHand = this.PlayerHand.OrderBy(x => x.CardValue).ToArray();
this.SortCHand = this.ComputerHand.OrderBy(x => x.CardValue).ToArray();
}
public void evaluateHands()
{
//create player's computer's evaluation objects (passing Sorted hand to constructor)
HandEvaluator playerHandEvaluator = new HandEvaluator(SortPHand);
HandEvaluator computerHandEvaluator = new HandEvaluator(SortCHand);
//get the player's and computer's handj
Hand playerHand = playerHandEvaluator.EvaluateHand();
Hand computerHand = computerHandEvaluator.EvaluateHand();
//display each hand
Console.WriteLine("\n\n\n\n\nPlayer's Hand: " + playerHand);
foreach (var card in PlayerHand)
{
Console.Write(card.CardValue.ToString());
Console.Write(" of ");
Console.Write(card.CardSuit.ToString());
Console.Write("\n");
}
///Where I tried to remove/add card to hand
Console.WriteLine();
Console.WriteLine("\nEnter the cards you would like to remove as such: 1,2");
string HandHolder = Console.ReadLine();
string[] splitHandHolder = HandHolder.Split(',');
int[] splitHandHolderint = Array.ConvertAll(splitHandHolder, int.Parse);
{
///remove at only works for Lists- find another way
playerHand.RemoveAt(i - 1);
int next = r.Next(0, deck.Count);
Card c = deck[next];
playerHand.Insert(i - 1, c);
}
Console.WriteLine();
Console.WriteLine("\nThis is your new hand."+ playerHand);
CardHNum = 1;
foreach (var card in PlayerHand)
{
Console.Write(card.CardValue.ToString());
Console.Write(" of ");
Console.Write(card.CardSuit.ToString());
Console.Write("\n");
}
Console.WriteLine("\n\n\n\n\nComputer's Hand: " + computerHand);
foreach (var card in ComputerHand)
{
Console.Write(card.CardValue.ToString());
Console.Write(" of ");
Console.Write(card.CardSuit.ToString());
Console.Write("\n");
}
//evaluate hands
if (playerHand > computerHand)
{
Console.WriteLine("Player WINS!");
}
else if (playerHand < computerHand)
{
Console.WriteLine("Computer WINS!");
}
else //if the hands are the same, evaluate the values
{
//first evaluate who has higher value of hand
if (playerHandEvaluator.HVs.Total > computerHandEvaluator.HVs.Total)
Console.WriteLine("Player WINS!");
else if (playerHandEvaluator.HVs.Total < computerHandEvaluator.HVs.Total)
Console.WriteLine("Computer WINS!");
//if both hanve the same poker hand
// player with the next higher card wins
else if (playerHandEvaluator.HVs.HighCard > computerHandEvaluator.HVs.HighCard)
Console.WriteLine("Player WINS!");
else if (playerHandEvaluator.HVs.HighCard < computerHandEvaluator.HVs.HighCard)
Console.WriteLine("Computer WINS!");
else
Console.WriteLine("Draw, no one wins!");
}
}
}
}
编辑:删除不需要/混淆的信息。
答案 0 :(得分:2)
你需要这样的东西:
Console.WriteLine("\nEnter the cards you would like to remove as such: 1,2");
string HandHolder = Console.ReadLine();
string[] splitHandHolder = HandHolder.Split(',');
int[] splitHandHolderint = Array.ConvertAll(splitHandHolder, int.Parse);
var updated = playerHand.Where((x, n) => !splitHandHolderint.Contains(n + 1)).ToArray();
playerHand = updated.Concat(deck.Take(5 - updated.Length)).ToArray();
这样可以保留splitHandHolderint
中不存在的卡片,然后“#34”充值&#34;来自deck
的{{1}}手。
使用deck.Take(5 - updated.Length)
向手牌添加卡片时遇到的问题是您可能会获得已经使用过的卡片。您确实需要使用r.Next(0, deck.Count)
类型而不是Stack<Card>
来为Card[]
建模。
我的上述解决方案假设Deck
已经随机排序,即已经洗牌,已经并且不包含当前使用过的任何卡片。