{
PFont font;
String title;
color bg,word;
int posx,posy;
int butWidth,butHeight;
boolean pressed=false;
boolean mouseOver=false;
Button(String _title, color _bg, color _word)
{
setting();
title=_title;
word=_word;
bg=_bg;
}
Button(String _title, color _bg)
{
setting();
title=_title;
bg=_bg;
word=color(250,250,250);
}
Button(String _title)
{
setting();
title=_title;
bg=color(100,10,10);
word=color(250,250,250);
}
void setting()
{
butWidth=100;
butHeight=30;
font=createFont("Helvetica", 12, true);
textFont(font);
}
public void Position(int x, int y)
{
posx=x;
posy=y;
}
public void Display()
{
MouseLeaveWithoutReleasedChecking();
int fontsize;
if(!pressed)
{
fill(bg);
fontsize=20;
}
else
{
fill(50);
fontsize=15;
}
noStroke();
ellipse(posx,posy,butWidth,butHeight);
fill(word);
textAlign(CENTER);
textSize(fontsize);
text(title,posx,posy+butHeight/4);
}
public void Pressed()
{
pressed=true;
println(title+" button pressed");
}
public void Released()
{
pressed=false;
println(title+" button released");
}
public void MouseLeaveWithoutReleasedChecking()
{
if(pressed && !mouseOver) Released();
}
public void CheckArea(int x, int y)
{
if(y>posy-butHeight/2 && y<posy+butHeight/2 && x>posx-butWidth/2 && x<posx+butWidth/2)
{
mouseOver=true;
word=color(237,28,36);
}
else
{
mouseOver=false;
word=color(0,128,128);
}
}
public boolean CheckMouseOver()
{
return mouseOver;
}
}
class Card
{
String cradNum;
int cardValue;
int cardWidth,cardHeight;
int posx,posy;
color bg=color(255,255,255),fdbg=color(100,50,50);
color word,shadow=color(0,0,0,50);
PFont cardFont;
boolean faceddown=false;
Card(String _cradNum, int _cardValue)
{
cardWidth=30;
cardHeight=40;
cardFont=createFont("Helvetica", 12, true);
cardValue=_cardValue;
cradNum=_cradNum;
CheckWorkColor();
}
void CheckWorkColor()
{
if(cradNum.charAt(0)=='?' || cradNum.charAt(0)=='?') word=color(0);
else word=color(150,10,10);
}
public void Position(int x, int y)
{
posx=x;
posy=y;
}
public void Display()
{
fill(shadow);
rect(posx+2,posy+2,cardWidth,cardHeight);
if(!faceddown)
{
fill(bg);
noStroke();
rect(posx,posy,cardWidth,cardHeight);
textFont(cardFont);
fill(word);
textAlign(CENTER);
text(cradNum,posx+cardWidth/2,posy+cardHeight/2);
}
else
{
fill(fdbg);
noStroke();
rect(posx,posy,cardWidth,cardHeight);
}
}
public int GetCardValue()
{
return cardValue;
}
public void FaceUp()
{
faceddown=false;
}
public void Facedown()
{
faceddown=true;
}
}
class Deck
{
String[][] pokerDeck;
ArrayList theDeck;
ArrayList usedCards;
boolean deckEmpty;
int cardValue;
Deck()
{
StartSetting();
}
public void Test()
{
for(int i=0; i<theDeck.size(); i++)
{
println(theDeck.get(i));
}
}
public boolean CheckDeckEmpty()
{
return deckEmpty;
}
public String DrawACard()
{
if(!deckEmpty)
{
int card=(int)random(0,theDeck.size());
String temp;
temp=(String)theDeck.get(card);
theDeck.remove(card);
usedCards.add(temp);
StringToValue(temp);
DeckNumber();
return temp;
}
else return "Out of Cards!!";
}
void StringToValue(String temp)
{
char v=temp.charAt(1);
if(temp.length()==3) cardValue=10;
else if(v=='J' || v=='Q' || v=='K') cardValue=10;
else
{
switch(v)
{
case 'A':
cardValue=1;
break;
case '2':
cardValue=2;
break;
case '3':
cardValue=3;
break;
case '4':
cardValue=4;
break;
case '5':
cardValue=5;
break;
case '6':
cardValue=6;
break;
case '7':
cardValue=7;
break;
case '8':
cardValue=8;
break;
case '9':
cardValue=9;
break;
default:
println("cardValue in Deck="+v);
break;
}
}
}
public int GetValue()
{
return cardValue;
}
void StartSetting()
{
theDeck=new ArrayList();
usedCards=new ArrayList();
theDeck.clear();
usedCards.clear();
for(int i=0;i<4;i++)
{
for(int j=0;j<13;j++)
{
String temp;
if(j+1==1) temp="A";
else if(j+1==11) temp="J";
else if(j+1==12) temp="Q";
else if(j+1==13) temp="K";
else temp=""+(j+1);
if(i==0) theDeck.add(" "+temp);
else if(i==1) theDeck.add(" "+temp);
else if(i==2) theDeck.add(" "+temp);
else if(i==3) theDeck.add(" "+temp);
else
{
print("錯誤訊息");
}
}
}
deckEmpty=false;
}
public void DeckNumber()
{
println("Deck remains "+theDeck.size()+" cards");
}
}
class TextBox
{
PFont font;
String title;
color word=color(0,0,255);
int posx,posy;
TextBox(String _title, int _size)
{
font=createFont("Helvetica", _size, true);
title=_title;
}
TextBox(String _title)
{
font=createFont("Helvetica", 18, true);
title=_title;
}
TextBox()
{
font=createFont("Helvetica", 18, true);
title="";
}
public void ChangeContent(String txt)
{
title=txt;
}
public void Position(int x, int y)
{
posx=x;
posy=y;
}
public void Display()
{
textFont(font);
fill(word);
textAlign(CENTER);
text(title,posx,posy);
}
}
Deck myDeck;
Button hitBut, standBut,restartBut, testBut;
TextBox textbox,playerScore,dealerScore,verson;
ArrayList yourCards, dealerCards,buttons;
boolean freeze,lose,win,gameover;
int turns;
int playerx,playery,dealerx,dealery;
int playerSum,dealerSum;
Card testc;
void setup()
{
size(500, 400);
restartBut=new Button("再一次!!",color(255,255,128));
hitBut=new Button("再一張",color(255,255,128));
standBut=new Button("翻牌",color(255,255,128));
restartBut.Position(width/2,height-restartBut.butHeight);
hitBut.Position(width/2-hitBut.butWidth/2-3,height-hitBut.butHeight);
standBut.Position(width/2+standBut.butWidth/2+3,height-standBut.butHeight);
playerx=width/2-30;
playery=height*5/6;
dealerx=width/2-30;
dealery=height/8;
reset();
}
void draw()
{
DrawStage();
Gameset();
if(gameover)
{
GameOver();
}
verson.Display();
}
void Gameset()
{
if(turns==1)
{
PlayerAddCard(false);
DealerAddCard(true);
PlayerAddCard(false);
DealerAddCard(false);
turns++;
}
else if(turns==2)
{
}
else if(turns==3)
{
DealerTurn();
freeze=true;
}
if(lose)
{
textbox.ChangeContent("玩家失敗");
textbox.Display();
println("玩家失敗");
freeze=true;
gameover=true;
}
if(win)
{
textbox.ChangeContent("玩家勝利");
textbox.Display();
freeze=true;
gameover=true;
}
DisplayCards();
}
void reset()
{
gameover=false;
win=false;
lose=false;
turns=1;
freeze=false;
yourCards=new ArrayList();
dealerCards=new ArrayList();
yourCards.clear();
dealerCards.clear();
myDeck=new Deck();
dealerSum=0;
playerSum=0;
verson=new TextBox("",9);
textbox=new TextBox("",30);
textbox.Position(width/2,height/2-20);
verson.Position(width-20,height-10);
playerScore=new TextBox("",15);
dealerScore=new TextBox("玩家的分數:Unknow",15);
playerScore.Position(width/2,height*4/5-50);
dealerScore.Position(width/2,height*1/5+30);
}
void Test()
{
}
void mousePressed()
{
if(!freeze)
{
if(hitBut.CheckMouseOver()) hitBut.Pressed();
if(standBut.CheckMouseOver()) standBut.Pressed();
}
else
{
restartBut.Pressed();
}
}
void mouseReleased()
{
if(!freeze)
{
if(hitBut.CheckMouseOver())
{
hitBut.Released();
PlayerAddCard(false);
}
if(standBut.CheckMouseOver())
{
standBut.Released();
turns=3;
}
}
else
{
if(restartBut.CheckMouseOver())
{
restartBut.Released();
reset();
}
}
}
void DrawStage()
{
background(248, 148, 220);
fill(147,200,255);
rect(0,height*5/6,width,height/4);
if(!freeze)
{
hitBut.CheckArea(mouseX,mouseY);
standBut.CheckArea(mouseX,mouseY);
}
playerx=width/2-30;
playery=height*5/6-5;
dealerx=width/2-30;
dealery=height/8-3;
hitBut.Position(width/2-hitBut.butWidth/2-3,height-hitBut.butHeight);
standBut.Position(width/2+standBut.butWidth/2+3,height-standBut.butHeight);
playerScore.Display();
dealerScore.Display();
standBut.Display();
hitBut.Display();
}
void GameOver()
{
fill(147,200,255);
rect(0,height*5/6,width,height/4);
restartBut.Position(width/2,height-restartBut.butHeight);
restartBut.Display();
restartBut.CheckArea(mouseX,mouseY);
}
void PlayerAddCard(boolean facedown)
{
if(!myDeck.CheckDeckEmpty())
{
Card card=new Card(myDeck.DrawACard(),myDeck.GetValue());
if(facedown)
{
card.Facedown();
}
else
{
card.FaceUp();
}
yourCards.add(card);
AllocateCardPosition();
playerSum=Scoring(yourCards);
playerScore.ChangeContent("你的分數:"+playerSum);
YourStateChecking();
}
}
void DealerAddCard(boolean facedown)
{
if(!myDeck.CheckDeckEmpty())
{
Card card=new Card(myDeck.DrawACard(),myDeck.GetValue());
if(facedown)
{
card.Facedown();
}
else
{
card.FaceUp();
}
dealerCards.add(card);
AllocateCardPosition();
}
}
void AllocateCardPosition()
{
for(int i=0; i<yourCards.size();i++)
{
Card card=(Card)yourCards.get(i);
card.Position(playerx+(card.cardWidth+2)*i,playery-card.cardHeight);
}
for(int j=0; j<dealerCards.size();j++)
{
Card card=(Card)dealerCards.get(j);
card.Position(dealerx+(card.cardWidth+2)*j,dealery);
}
}
void DisplayCards()
{
for(int i=0; i<yourCards.size();i++)
{
Card card=(Card)yourCards.get(i);
card.Display();
}
for(int j=0; j<dealerCards.size();j++)
{
Card card=(Card)dealerCards.get(j);
card.Display();
}
}
void YourStateChecking()
{
if(playerSum<=21) lose=false;
else lose=true;
}
void DealerTurn()
{
dealerSum=Scoring(dealerCards);
dealerScore.ChangeContent("對手的分數:"+dealerSum);
if(dealerSum < 17 && dealerSum<playerSum)
{
DealerAddCard(false);
DealerTurn();
}
else
{
WinningCondition();
}
}
void WinningCondition()
{
println(" 你的分數是 "+playerSum);
println(" 對手的分數是 "+dealerSum);
if(dealerSum > 21 )
{
println("玩家勝利");
win=true;
}
else
{
if(dealerSum>=playerSum)
{
lose=true;
println("玩家失敗!");
}
else
{
println("玩家勝利");
win=true;
}
}
}
int Scoring(ArrayList alist)
{
int sum,ace;
sum=0;
ace=0;
for(int i=0; i<alist.size();i++)
{
int value;
Card card=(Card)alist.get(i);
card.FaceUp();
value=card.GetCardValue();
if(value == 1)
{
value=0;
ace+=1;
}
println("value="+value);
sum+=value;
}
if(ace!=0)
{
for(int j=0; j<ace; j++)
{
if(sum+11<=21) sum+=11;
else sum+=1;
}
}
return sum;
}
心得
拼拼湊湊完成 又加上其中考所以有點趕 所以在不會的時候直接把問題打在google找尋然後問身邊朋友以及哥哥 排版不是很漂亮 但是我們以顏色來吸引玩家 ㄎㄎ
沒有留言:
張貼留言