Topic: 发桥牌

  Print this page

1.发桥牌 Copy to clipboard
Posted by: cainiao025
Posted on: 2006-06-08 10:18

设计一个由计算机发桥牌给4位玩家,并输出他们分得的花色和点数。梅花为C,方块为D,黑桃为S,红心为H。牌点为A,2,3,4,5,6,7,8,9,T,J,Q,K。

不能用数组。请大虾们写出对应的算法。

2.Re:发桥牌 [Re: cainiao025] Copy to clipboard
Posted by: lisliefor
Posted on: 2006-06-08 14:51

这个题目比较有意思,小做一下,仅供参考:
桥牌类:
package bridge;

public class Bridge {
  String name;
//花色与点数
  int Colors = -1;
  int Count = -1;
  
  public Bridge(String name){
    this.name = name;
  }
  
}
发牌类:
package bridge;

public class BridgeDeal {
  
  static Bridge player1,player2,player3,player4;
  
  //发牌
  public void Deal(){
    player1.Colors = (int)(Math.random()*4);
    player1.Count = (int)(Math.random()*13);
    player2.Colors = (int)(Math.random()*4);
    player2.Count = (int)(Math.random()*13);
    player3.Colors = (int)(Math.random()*4);
    player3.Count = (int)(Math.random()*13);
    player4.Colors = (int)(Math.random()*4);
    player4.Count = (int)(Math.random()*13);
    
    //第二个玩家的牌不能和第一个玩家牌一样
    while(!Check(player1,player2)){
      player2.Colors = (int)(Math.random()*4);
      player2.Count = (int)(Math.random()*13);
    }
    
    //同理
    while(!Check(player1,player3) && !Check(player2,player3)){
      player2.Count = (int)(Math.random()*13);
      player3.Colors = (int)(Math.random()*4);
    }
    
    while(!Check(player1,player4) &&!Check(player2,player4) && !Check(player3,player4)){
      player4.Colors = (int)(Math.random()*4);
      player4.Count = (int)(Math.random()*13);
    }
  }
  
  //检查花色、点数是否一致
  public boolean Check(Bridge b,Bridge c){
    boolean Flag = false;
    if(b.Colors!=c.Colors && b.Count!=c.Count)
      Flag = true;
    return Flag;
  }
  
  public void ShowBridge(Bridge b){
    String cor = "",cot = "";
    
    switch(b.Colors){
    case 0:
      cor = "梅花(C)";
      break;
    case 1:
      cor = "方块(D)";
      break;
    case 2:
      cor = "黑桃(S)";
      break;
    case 3:
      cor = "红心(H)";
      break;
      default:
        break;
    }
    
    switch(b.Count){
    case 0:
      cot = "A";
      break;
    case 1:
      cot = "2";
      break;
    case 2:
      cot = "3";
      break;
    case 3:
      cot = "4";
      break;
    case 4:
      cot = "5";
      break;
    case 5:
      cot = "6";
      break;
    case 6:
      cot = "7";
      break;
    case 7:
      cot = "8";
      break;
    case 8:
      cot = "9";
      break;
    case 9:
      cot = "T";
      break;
    case 10:
      cot = "J";
      break;
    case 11:
      cot = "Q";
      break;
    case 12:
      cot = "K";
      break;
      default:
        break;
    }
    System.out.println("玩家"+b.name+"得到的牌为:"+cor+cot);
  }
  
  public static void main(String args[]){
    player1 = new Bridge("player1");
    player2 = new Bridge("player2");
    player3 = new Bridge("player3");
    player4 = new Bridge("player4");
    
    BridgeDeal bd = new BridgeDeal();
    bd.Deal();
    bd.ShowBridge(player1);
    bd.ShowBridge(player2);
    bd.ShowBridge(player3);
    bd.ShowBridge(player4);
  }
}

3.Re:发桥牌 [Re: cainiao025] Copy to clipboard
Posted by: why
Posted on: 2006-06-08 18:43

> 不能用数组
Then can you use ArrayList? Wink Just kidding, ArrayList is using an array internally.

You may use java.util.List for the Deck of Cards (with unique suit (color) and rank (high-low)).
java.util.Collections has a shuffle method.

4.Re:发桥牌 [Re: cainiao025] Copy to clipboard
Posted by: cainiao025
Posted on: 2006-06-08 23:10

多谢lisliefor 的源代码。

5.Re:发桥牌 [Re: cainiao025] Copy to clipboard
Posted by: why
Posted on: 2006-06-08 23:50

cainiao025 wrote in a PM :
你是元老,但你不能用大学生才懂的方法去教小学生做题。一本好的初级编程书让你去评价,你会认为它写的没有深度,不值得一看,但是它对于我们初学者却是很受用的。
对于我们这些初学者最难的,就是拿到一个题目不知道如何着手,怎样把它变成计算机语言,如何用程序去实现它。
而你作为元老,必是过来人,应该指导我们这些初学者怎样跨出这个瓶颈,而不是用几句英文指出api文档里有什么,初学者能看懂吗?


Sorry that I can't type in Chinese now and most likely during the weekend.

FYI, when I first tried to write a program 20 years or so ago, I learnt to read the API doc or reference book from day 2 -- day 1 was for "Hello World" type exercises.

I think that the most time-consuming part would be to research for the proper API/tools to use.

If one can't read the docs (could have problems in understanding, then raise specific questions), then I would suggest he/she not to spend more effort on programming (or software engineering).

BTW, the hardest part, to me, would be design (including system architecture and data modelling) and then testing.

Back to this question.
I believe you could find a large number of code examples regarding Card Dealing using Google. Most of them are using arrays (or ArrayList) for the deck of cards. But now your requirement is 不能用数组, so you would have to use some other data structure to hold the deck of cards as well as the four hands of cards.
Basically I was suggesting you to implement those as java.util.Lists of Card objects (each Card has "suit" and "rank")
Another suggestion: java.util.Collection.shuffle() would be helpful for implementing the shuffling the the deck of cards.

If you don't find these suggestions useful, please just ignore them.
I am not going to spend time in detailing the design of the Card object, Hand object, etc., since these should be well illustrated from the various examples that you could find with Google.

Good luck and goodbye.

6.Re:发桥牌 [Re: cainiao025] Copy to clipboard
Posted by: lisliefor
Posted on: 2006-06-09 11:00

why说的很有道理,就像以前我老师教我那样,他总是给我很多忠告,而我总是当耳边风,而且因为他老叫家长过来,我很反感,很讨厌他!
不过,当你到某个年龄段的时候就能理解了,他当年教的确实是真理。我现在很愧疚,漠视别人的劳动成果,是很.....的事情。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923