查看贴子 返回上一页


贴子作者:王志成 发贴日期:2003-3-3 21:41
阅读次数:639 回复条数:0
所属版块:计算机与网络
 
标题:扑克牌魔术。
内容:  
  这是一个扑克牌魔术:先由观众在魔术师手中的扑克牌中选择一张并默记在心中,然后魔术师洗了一下牌并把它分成三堆,再请观众找出刚才选择的扑克牌在哪一堆中,并告诉魔术师选择的扑克牌在哪一堆。然后再洗牌,并再分成三堆,再请观众指出选择的牌在哪一堆中。经过三次洗牌之后。魔术师就已经知道你选择的牌了。

  以下程序是这个魔术的模拟。事实上这个魔术的关键在洗牌上,原来我也打算在按照这个魔术的原理来编写的,不过后来一想,用排除法编程更为简单,所以这个魔术也就失去了神秘感。

注:为了方便程序输出,分别用W,X,Y,Z代表四种花色,A234567890JQK代表点数。
编译后在网页中插入<applet code="DeckofCards.class" width=370 height=160></applet>即可运行。
软件环境IE+JRE1.4.1
或建议安装JDK后用appletviewer运行会得到更佳的显示效果。


//Start of DeckofCards.java
//Deck magic
//date2003.3.2  DeckofCards.java

//import class
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.*;

public class DeckofCards extends JApplet{

final int CHOOSE_NUMBER = 27;  //

private Card cards[] = new Card[52];
private Card choosedCard[] = new Card[CHOOSE_NUMBER];
private Card selectedCard[] = new Card[CHOOSE_NUMBER/3];
private int round;
private JTextArea outputArea;
private JLabel outputLabel;
private JButton firstRow,secondRow,thirdRow,startButton;

public void init(){

Container c = getContentPane();
c.setLayout( new FlowLayout() );

outputArea = new JTextArea( 2,20 );
outputArea.setEditable( false );
outputArea.setFont( new Font( "Courier",Font.BOLD,12 ) );
c.add( outputArea );

outputLabel = new JLabel( "Please choose a card & press Start button." );
c.add( outputLabel );

firstRow = new JButton( "The first row" );
firstRow.setEnabled( false );
firstRow.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e ){
dealCard( 0,1,2 );
}
}
);
c.add( firstRow );

secondRow = new JButton( "The second row" );
secondRow.setEnabled( false );
secondRow.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e ){
dealCard( 1,0,2 );
}
}
);
c.add( secondRow );

thirdRow = new JButton( "The third row" );
thirdRow.setEnabled( false );
thirdRow.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e ){
dealCard( 2,0,1 );
}
}
);
c.add( thirdRow );

startButton= new JButton( "Start" );
startButton.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e ){
outputArea.setFont( new Font( "Courier",Font.BOLD,12 ) );
firstRow.setEnabled( true );
secondRow.setEnabled( true );
thirdRow.setEnabled( true );
startButton.setEnabled( false );
outputLabel.setText( "Please show me your choice is in which row?" );
shuffle();
buildOutput();
}
}
);
c.add( startButton );


for ( int i=0 ; i<52 ; i++ ){

cards[ i ] = new Card( i );
}

choose();
buildOutput();
}

public void dealCard( int first , int second , int third ){
int n = choosedCard.length;
if ( round == 1 ){
for ( int i=0 ; i<n/9 ; i++ ){
swap( choosedCard,first*(n/3)+n/9+i,second*(n/3)+i );
}
for ( int i=0 ; i<n/9 ; i++ ){
swap( choosedCard,first*(n/3)+(n/9)*2+i,third*(n/3)+i );
}
round++;
buildOutput();
}
else if ( round == 2 ){
swap( choosedCard,first*(n/3)+1,second*(n/3) );
swap( choosedCard,first*(n/3)+2,third*(n/3) );
round++;
buildOutput();
}
else if ( round == 3 ){
outputArea.setText( "Your choice is:" + choosedCard[first*(n/3)] );
outputArea.setFont( new Font( "Courier",Font.BOLD,18 ) );
outputLabel.setText( "The game is over.Press the Start button to restart." );
firstRow.setEnabled( false );
secondRow.setEnabled( false );
thirdRow.setEnabled( false );
startButton.setEnabled( true );
round = 1;
}
}

public void choose(){
round = 1;
for ( int i=0 ; i<choosedCard.length ; i++ ){

int rdm;
rdm = (int)( Math.random() * ( 52 - i ) );
choosedCard[ i ] = cards[ rdm ];
swap( cards,rdm,51-i );

}
}

public void shuffle(){

int temp;
for ( int i=0 ; i<choosedCard.length ; i++ ){
temp = (int)( Math.random() * choosedCard.length );
swap( choosedCard,i,temp );
}
}

public void buildOutput(){

int temp[] = new int[ choosedCard.length/3 ];
int rdm,m;
String output = "";
for ( int i=0 ; i<3 ; i++ ){
for ( int l=0 ; l<temp.length ; l++ ){
temp[l]=l;
}
output +=" ";
for ( int l=0 ; l<temp.length ; l++ ){
rdm = (int)( Math.random() * ( temp.length-l ) );
output += choosedCard[ i*temp.length + temp[rdm] ] + " ";
m=temp[rdm]; temp[rdm]=temp[temp.length-l-1]; temp[temp.length-l-1]=m;
}
if ( i != 2 ) output += "\n";
}
outputArea.setText( output );
repaint();
}

public void swap( Card cd[], int a , int b ){

Card t;
t = cd[a];
cd[a] = cd[b];
cd[b] = t;
}
}

class Card{

private int cardNumber;   //unique identify the card
private String face[] = { "W","X","Y","Z" };   //four kinds of faces
private char count[] = { 'A','2','3','4','5','6','7','8','9','0','J','Q','K' };
public Card( int n ){

cardNumber = ( n>=0 && n<52 ) ? n : 0;
}

public String toString(){

return ( count[ cardNumber % 13 ] + face[ cardNumber / 13 ] );
}
}


您尚未登陆网站,不能回复贴子!



(C) Copyright 2000-2003 Shengze Middle School Class 4 Grade 3 of the Year 1999