我正在为一个班级制作黑杰克程序,我有一系列“if循环”我的问题是,当我说是的时候,用另一张卡打我,它会按照我的意愿继续下一步,但是当我说“不,我在抱着”时,它仍然问我是否想再次击中。我怎么能这样做,所以当我说不,我然后完成函数完成?程序在下面。
players=input('Welcome to Matjack! Aces count as 11, and face cards count as 10. Please enter the number of players:')
if players==1;
card_1=randi(11,1);
card_2=randi(11,1);
fprintf ('Face up card is %d! Face down is unknown!',card_1)
hit=input(' Hit? Y/N:','s')
if hit=='Y';
card_3=randi(11,1);
cards=card_1+card_3;
player_1=card_1+card_2+card_3;
if player_1<21;
fprintf('Value of face up cards is %d. Face down is still unknown!',cards);
hit=input(' Hit again? Y/N:','s')
if hit=='Y';
card_4=randi(11,1);
cards=card_1+card_3+card_4;
player_1=card_1+card_2+card_3+card_4;
end
if player_1>21 ;
fprintf ('Player one broke! Total card value was %d',player_1)
end
if hit=='N';
player_1=card_1+card_2+card_3;
fprintf ('Player one holds! Total card value was %d',player_1)
end
end
if player_1<21
fprintf('Value of face up cards is %d. Face down is still unknown!',cards);
hit=input(' Hit again? Y/N:','s')
if hit=='Y';
card_5=randi(11,1);
cards=card_1+card_3+card_4+card_5;
player_1=card_1+card_2+card_3+card_4+card_5;
end
if player_1>21 ;
fprintf ('Player one broke! Total card value was %d',player_1)
end
if hit=='N';
player_1=card_1+card_2+card_3+card_4;
fprintf ('Player one holds! Total card value was %d',player_1)
end
end
if player_1<21
fprintf('Value of face up cards is %d. Face down is still unknown!',cards);
hit=input(' Hit again? Y/N:','s')
if hit=='Y';
card_6=randi(11,1);
cards=card_1+card_3+card_4+card_5+card_6;
player_1=card_1+card_2+card_3+card_4+card_5+card_6;
end
if player_1>21 ;
fprintf ('Player one broke! Total card value was %d',player_1)
end
if hit=='N';
player_1=card_1+card_2+card_3+card_4+card_5;
fprintf ('Player one holds! Total card value was %d',player_1)
end
end
if player_1<21
fprintf('Value of face up cards is %d. Face down is still unknown!',cards);
hit=input(' Hit again? Y/N:','s')
if hit=='Y';
card_7=randi(11,1);
cards=card_1+card_3+card_4+card_5+card_6+card_7;
player_1=card_1+card_2+card_3+card_4+card_5+card_6+card_7;
end
if player_1>21 ;
fprintf ('Player one broke! Total card value was %d',player_1)
end
if hit=='N';
player_1=card_1+card_2+card_3+card_4+card_5+card_6;
fprintf ('Player one holds! Total card value was %d',player_1)
end
end
end
if hit=='N';
player_1=card_1+card_2;
fprintf ('Player one holds! Total card value was %d',player_1)
end
end
答案 0 :(得分:0)
我做了一点工作。您可能想要添加的一些功能是拆分,穿上套装,并添加持久性玩家名称。我试图输入描述性变量和函数名称。
function Matjack
global AllCards decks
AllCards = [];
clc
DealerHitOnSoft17 = 1;
disp('Welcome to Matjack! Aces count as 11, and face cards count as 10.')
if DealerHitOnSoft17
disp('Dealer hits on soft 17')
else
disp('Dealer holds on soft 17')
end
NumberPlayers = input('Please enter the number of players:');
decks = input('How many decks of cards do you want to play with? ');
playerName = cellfun(@(n) strjoin({'Player ', num2str(n)}, ' '), num2cell((1:NumberPlayers)'), 'uni', 0);
dealerCards = DealCards(2, []);
if Blackjack('Dealer', dealerCards)
printHand('Dealer''s', dealerCards)
disp('You lose')
return
end
printHand('Dealer''s', dealerCards(1))
playerHands = cellfun(@(n) DealCards(n, []), num2cell(2 * ones( NumberPlayers, 1)), 'uni', 0);
isPlaying = ones(size(playerHands));
for i = 1:NumberPlayers
isPlaying(i) = ~Blackjack(['Player ', num2str(i)], playerHands{i});
end
while any(isPlaying)
for i = 1:NumberPlayers
if isPlaying(i)
printHand(playerName{i}, playerHands{i})
if strcmpi(input([playerName{i}, ' has ', num2str(sumHand(playerHands{i}, 0)),'. Hit? Y/N:'],'s'), 'y')
playerHands{i} = DealCards(1, playerHands{i});
printHand(playerName{i}, playerHands{i})
disp([playerName{i}, ' has ', num2str(sumHand(playerHands{i}, 0)),'.'])
if sumHand(playerHands{i}, 0) > 21
disp([playerName{i}, ' busts!'])
isPlaying(i) = 0;
end
else
disp([playerName{i}, ' holds at ', num2str(sumHand(playerHands{i}, 0)),'.'])
isPlaying(i) = 0;
end
end
end
end
printHand('Dealer''s', dealerCards)
while sumHand(dealerCards, DealerHitOnSoft17) < 17
dealerCards = DealCards(1, dealerCards);
printHand('Dealer', dealerCards)
if sumHand(dealerCards, DealerHitOnSoft17) > 21
disp('Dealer busts!')
elseif sumHand(dealerCards, DealerHitOnSoft17) > 17
disp(['Dealer holds at ', num2str(sumHand(dealerCards, DealerHitOnSoft17)),'.'])
else
disp(['Dealer has ', num2str(sumHand(dealerCards, DealerHitOnSoft17)),'.'])
end
end
cellfun(@(name, h) WinDrawLose(name, sumHand(h, 0), sumHand(dealerCards, DealerHitOnSoft17)), playerName, playerHands);
end
function hand = DealCards(N, hand)
global AllCards decks
possibleCards = randi(52 * decks, N, 1);
[C, ia, ~] = unique([AllCards; possibleCards], 'stable');
ia = ia - length(AllCards);
ia(ia < 1) = [];
n = length(AllCards) + N - length(C);
AllCards = C;
hand = [hand; possibleCards(ia)];
if n > 0
hand = DealCards(n, hand);
end
end
function printHand(player, hand)
ord = horzcat('Ace', cellfun(@num2str, num2cell(2:10), 'uni', 0), 'Jack', 'Queen', 'King');
hand = mod(hand - 1, 13) + 1;
if length(hand) == 1
disp([player, ' face up card is ', strjoin(ord(hand), ', '), '. Face down is unknown!'])
elseif length(hand) > 1
disp([player, ' cards are ', strjoin(ord(hand), ', '), '.'])
end
end
function S = sumHand(hand, dealerSoft)
hand = mod(hand - 1, 13) + 1;
hand(hand >= 10) = 10;
hand(hand == 1 ) = 11;
S = sum(hand);
while ((S > 21) && any(hand == 11))
if ~(dealerSoft && ((17 <= S) && (S <= 21)))
n = find(hand == 11, 1, 'first');
hand(n) = 1;
S = sum(hand);
end
end
end
function bj = Blackjack(player, hand)
if (length(hand) == 2) && (sumHand(hand, 0) == 21)
bj = 1;
disp([player, ' hit blackjack!'])
else
bj = 0;
end
end
function WinDrawLose(name, player, dealer)
if ((player < dealer) && (dealer <= 21)) || (player > 21)
disp([name, ' loses.'])
elseif (player == dealer)
disp([name, ' draws.'])
else
disp([name, ' wins.'])
end
end