function blackjack
%
% ブラックジャックもどき
%
%
fprintf('\nComputer\n'); % コンピュータの初期カード決定
y=floor(rand(1)*10)+1; % 一枚目のカードの決定
fprintf('*\n'); % カードの数だけ*を書くという意味
y=y+floor(rand(1)*10)+1; % 二枚目のカードの決定と合計
fprintf('*\n');
for i=1:30 % コンピュータの思考ルーチン
if y >= 18 % 18以上なら勝負に出る
break;
elseif y == 17 % 17だったら、40%の確率で勝負
h=floor(rand(1)*10);
if h > 6
break;
else
y=y+floor(rand(1)*10)+1;
fprintf('*\n');
end
elseif y == 16 % 16だったら、50%の確率で勝負
h=floor(rand(1)*10);
if h > 5
break;
else
y=y+floor(rand(1)*10)+1;
fprintf('*\n');
end
else % 16未満だったらもう一枚カードを引く
y=y+floor(rand(1)*10)+1;
fprintf('*\n');
end
end
fprintf('\n\nyour turn もう一枚(enter)いらない(n)\n');
x1=floor(rand(1)*10)+1;
fprintf('%d\n',x1);
x2=floor(rand(1)*10)+1;
fprintf('%d',x2);
x=x1+x2;
for i=1:30 % もう一枚引くかどうか
command=input('','s'); % コマンド入力
if isempty(command) % isemptyは空行列であるかどうか判定する
xx=floor(rand(1)*10)+1;
fprintf('%d',xx);
x=x+xx;
elseif command=='n'
break;
end
end
fprintf('\n\ncomputer is %d\n',y); % ここから結果表示
fprintf('you are %d\n\n',x);
if x <= 21 & y <=21
if x > y
fprintf('あなたの勝ち!!');
elseif x == y
fprintf('引き分け!!');
else
fprintf('あなたの負け!!');
end
elseif x > 21 & y <= 21
fprintf('あなたの負け!!');
elseif x <=21 & y > 21
fprintf('あなたの勝ち!!');
else
fprintf('引き分け!!');
end
|