//thestartindexofeachside
intlen=cards.length();
intleft,right;
left=right=len/2;
StringBuffertemp=newStringBuffer("");
intnumOfL,numOfR;
while((left-0)/3>=3&&(len-1-right)/3>=3){
//theamountofcardstobeshuffledatrandomoneachside(1~3)
numOfL=(int)(Math.random()*3+1);
numOfR=(int)(Math.random()*3+1);
temp.append(cards.substring(left-numOfL*3,left));
temp.append(cards.substring(right,right+numOfR*3));
left-=numOfL*3;
right+=numOfR*3;
}
//lessthan3cardsineachside,putthemallinthenewstring
if(left!
=0)
temp.append(cards.substring(0,left));
if(right!
=len-1)
temp.append(cards.substring(right,len));
cards=temp;//renewtheoriginalcards
}//shufflefinished
}
voidshowCards(){
for(inti=0;i<52;i++){
if((i+13)%13==0)
System.out.println();
System.out.print(cards.substring(3*i,3*i+3)+"");
}
System.out.println();
}
}
=====part2======
packagefive;
publicclassShuffle{
publicstaticvoidmain(String[]args){
Pokepoke=newPoke();
poke.shuffle(4);//shufflefor4times
poke.showCards();
}
}
6.数独游戏
=====part1=====
packagesix;
importjava.util.Arrays;
/*
thisclassisdesignedtogenerateonesolutionofSudo,
tomaskseveralnumbersatrandominall81numberstoraiseaSudoproblem,
todefinetherulestocheckwhetherthenumbersinputtedbyuserarecorrect
*/
publicclassSudo{
privateint[][]solution;
//initializethesudokuaccordingto
//-theregularityofthesumofiandj
publicSudo(){
solution=newint[9][];
for(inti=0;i<9;i++){
solution[i]=newint[9];
for(intj=1;j<=9;++j)
this.solution[i][j-1]=(i+j>9)?
(i+j-9):
i+j;
}
martrix();
colSwap();
mask();
}
//returnaconcreteandrandomsolution
publicintgetDigit(inti,intj){
returnsolution[i][j];
}
//constructasituationobjecttothesudokurules
//byextractingtheline[147,258,369]fromthesolution
privatevoidmartrix(){
int[][]temp=newint[9][9];
for(inti=0,j=0;i<3;i++){
temp[j++]=Arrays.copyOf(solution[i],solution[i].length);
temp[j++]=Arrays.copyOf(solution[i+3],solution[i+3].length);
temp[j++]=Arrays.copyOf(solution[i+6],solution[i+6].length);
}
solution=temp;
}
//basedonthe[123,456,789]columngroup
//swapeachcolumnvalueamongeachgroup
privatevoidcolSwap(){
intcolForSwap=0;
int[]temp=newint[9];
for(inti=0;i<9;i++){
switch(i){//choosethecolumntoswap
case0:
case1:
case2:
colForSwap=(int)(Math.random()*3);break;//group1
case3:
case4:
case5:
colForSwap=(int)(Math.random()*3+3);break;//group2
case6:
case7:
case8:
colForSwap=(int)(Math.random()*3+6);//group3
}
if(colForSwap!
=i)
for(intj=0;j<9;j++){//jforline
temp[j]=solution[j][colForSwap];//thesamecolumn,diffentline
solution[j][colForSwap]=solution[j][i];
solution[j][i]=temp[j];
}//finishone-columnswap
}//finishall-columnsswap
}
privatevoidmask(){
//choosethevaluemaskedatrandom
for(inti=0;i<9;++i){
for(intj=0;j<9;j++){
intdifficulty=(int)(Math.random()*2);//0or1
if(difficulty==0)
solution[i][j]=0;
}
}
}
privatebooleanlineVlid(inti,intj,intvalue){
for(intm=0;m<9&&m!
=j;m++)
if(solution[i][m]==value)
returnfalse;
returntrue;
}
privatebooleancolValid(inti,intj,intvalue){
for(intn=0;n<9&&n!
=i;n++)
if(solution[n][j]==value)
returnfalse;
returntrue;
}
privatebooleanrecValid(inti,intj,intvalue){
for(intm=3*(i/3);m<=3*(i/3)+2;++m)
for(intn=3*(j/3);n<3*(j/3)+2;++n)
if(i!
=m&&j!
=n&&solution[m][n]==value)
returnfalse;
returntrue;
}
publicbooleanisValid(inti,intj,intvalue){
if(value>0&&value<10)
return(lineVlid(i,j,value)&&colValid(i,j,value)&&recValid(i,j,value));
else
returnfalse;
}
}
=====part2=====
packagesix;
importjavafx.geometry.Insets;
importjavafx.geometry.Pos;
importjavafx.scene.control.Button;
importjavafx.scene.control.TextField;
importjavafx.scene.layout.BorderPane;
importjavafx.scene.layout.GridPane;
importjavafx.scene.text.Font;
importjavafx.scene.text.FontWeight;
/*
thisclassisdesignedtoaddUIelementsoftheplainSudogame
definethesimplerulesoftheusers'operationonui
andthelayoutofalltheseelements