Lesson_9 作业_1 ---- 21点

一.作业描述

    编写一个21点的扑克游戏——程序显示玩家和庄家(计算机)手中的第一张牌。21点思想是:尽可能的使手中的牌的总点数尽可能的接近21点,但不要超过21点。花牌(J、Q、K)为10点,其他牌的点数与其基数相同。 A的数可以是1点也可以是11点,由玩家来确定。庄家(计算机)手中的牌总点数不能少于17点,如果少就必须要一张牌。如果庄家总点数超过21点则爆庄,玩家胜利,反之庄家(计算机)胜利

二.代码

 

  1 /************************************************************
  2 *                    Lesson_9 作业_1 -- 21点
  3 *                         2013-01-20
  4 *                        by CocoonFan
  5 *
  6 *************************************************************
  7 *************************作业描述*****************************
  8 *
  9 *    编写一个21点的扑克游戏——程序显示玩家和庄家(计算机)手中
 10 * 的第一张牌.21点思想是:尽可能的使手中的牌的总点数尽可能的接
 11 * 近21点,但不要超过21点。花牌(J、Q、K)为10点,其他牌的点数
 12 * 与其基数相同。 A的数可以是1点也可以是11点,由玩家来确定。
 13 * 庄家(计算机)手中的牌总点数不能少于17点,如果少就必须要一
 14 * 张牌。如果庄家总点数超过21点则爆庄,玩家胜利,反之庄家(计
 15 * 算机)胜利.
 16 *************************************************************/
 17 public class TestPoker21 {
 18     public static void main(String[] args){
 19         Poker poker = new Poker();
 20         poker.initPoker();
 21         System.out.println("牌的初始状态为:");
 22         poker.showPokerNumber();
 23         
 24         Person person = new Person("CocoonFan");
 25         Computer computer = new Computer("可恶的计算机");
 26         
 27         //人拿第一张牌
 28         person.getPoker(poker);
 29         person.showGetPoker();
 30         person.totalScore();
 31         
 32         //计算机拿第一张牌
 33         computer.getPoker(poker);
 34         computer.showGetPoker();
 35         computer.totalScore();
 36         
 37         //人拿第二张牌
 38         person.getPoker(poker);
 39         person.showGetPoker();
 40         person.totalScore();
 41         
 42         //计算机拿第二张牌
 43         computer.getPoker(poker);
 44         computer.showGetPoker();
 45         computer.totalScore();
 46         
 47         while(computer.isGetNextPoker() && computer.getScore()>0){//计算机是否继续拿牌
 48             computer.getPoker(poker);
 49             computer.showGetPoker();
 50             computer.totalScore();
 51         }
 52         
 53         System.out.println("牌的最终状态为:");
 54         poker.showPokerNumber();
 55         
 56         person.showTotalScore();
 57         if(computer.getScore() < 0){
 58             System.out.println(computer.getPlayerName() + ",自爆了!!哈哈~~");
 59         }else {
 60             computer.showTotalScore();
 61         }
 62         
 63         if(person.getScore() > computer.getScore()){
 64             System.out.println("\n\n最后的裁决:" + person.getPlayerName() + "胜利!!!!");
 65         }else {
 66             System.out.println("\n\n最后的裁决:" + computer.getPlayerName() + "胜利!!!!");
 67         }
 68         
 69     }
 70 }
 71 
 72 class Poker{
 73     private static final String[] pokerColorString = {"黑桃", "红桃", "方块", "梅花"};
 74     private static final String [] pokerNumberString = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
 75     private static int[][] pokerNumber = new int[4][14];
 76     
 77     //get和set
 78     public static int[][] getPokerNumber() {
 79         return pokerNumber;
 80     }
 81     public static void setPokerNumber(int[][] pokerNumber) {
 82         Poker.pokerNumber = pokerNumber;
 83     }
 84     public static String[] getPokercolorstring() {
 85         return pokerColorString;
 86     }
 87     public static String[] getPokernumberstring() {
 88         return pokerNumberString;
 89     }
 90 
 91     /**
 92      * 初始化扑克牌
 93      */
 94     public void initPoker(){
 95         for (int i = 0; i < pokerNumber.length; i++) {
 96             for (int j = 0; j < pokerNumber[i].length; j++) {
 97                 if (j < 10) {
 98                     pokerNumber[i][j] = j;
 99                 }else {
100                     pokerNumber[i][j] = 10;
101                 }
102             }
103         }
104     }
105     
106     //显示所有扑克牌
107     public void showPokerNumber(){
108         for (int i = 0; i < pokerNumber.length; i++) {
109             for (int j = 0; j < pokerNumber[i].length; j++) {
110 //                System.out.print(pokerNumber[i][j] + " ");
111                 showThisPoker(i, j);
112             }
113             System.out.println();
114         }
115     }
116     
117     // 显示当前扑克牌,用于显示去掉拿掉张数的扑克牌
118     public static void showThisPoker(int i, int j){
119 //        String space = "已拿走";
120         if(Poker.pokerNumber[i][j] != 0){
121             System.out.printf("%s%s\t",pokerColorString[i],pokerNumberString[j]);
122         }else{
123             System.out.printf("%s\t","  ");
124         }
125     }
126     
127     //用于显示拿到的当前的扑克牌
128     public static void showThisPoker_Get(int i, int j){
129         System.out.print(pokerColorString[i]);
130         System.out.println(pokerNumberString[j]);
131     }
132     
133     //更新扑克牌
134     public static void refresh(int i, int j){
135         pokerNumber[i][j] = 0;
136     }
137 }
138 
139 class Player{
140     private String playerName;
141     private int  win = 0;
142     private int lose = 0;
143     private int i = 0;
144     private int j = 0;
145     private int pokerCount = 0;
146     
147     //set和get
148     public int getPokerCount() {
149         return pokerCount;
150     }
151     public void setPokerCount(int pokerCount) {
152         this.pokerCount = pokerCount;
153     }
154     public Player(String playerName) {
155         this.playerName = playerName;
156     }
157     public String getPlayerName() {
158         return playerName;
159     }
160     public void setPlayerName(String playerName) {
161         this.playerName = playerName;
162     }
163     public int getWin() {
164         return win;
165     }
166     public void setWin(int win) {
167         this.win = win;
168     }
169     public int getLose() {
170         return lose;
171     }
172     public void setLose(int lose) {
173         this.lose = lose;
174     }
175     public int getI() {
176         return i;
177     }
178     public void setI(int i) {
179         this.i = i;
180     }
181     public int getJ() {
182         return j;
183     }
184     public void setJ(int j) {
185         this.j = j;
186     }
187     //随机产生扑克牌花色
188     public int getPokerClolor(){
189         int i = (int)(Math.random()*10)%3;
190 //        System.out.println("随机数是:" + i);
191         return i;
192     }
193     
194     //随机产生扑克牌数字
195     public int getPokerNumber(){
196         int i = 0;
197         while(i == 0){
198             i = (int)((Math.random()*100)%13);
199 //            System.out.println("随机数是:" + i);
200         }
201         return i;
202     }
203     
204     //拿一张扑克牌
205     public void getPoker(Poker poker) {
206         i = j = 0;
207         while(Poker.getPokerNumber()[i][j] == 0){//防止拿到重复的牌
208             i = getPokerClolor();
209             j = getPokerNumber();
210         }
211 //        System.out.println("i = " + i+ ", j = " + j);
212         Poker.refresh(i, j);
213 //        Poker.showThisPoker(i, j);
214 //        poker.showPokerNumber();
215         pokerCount++;
216     }
217     
218     //显示拿到的扑克牌
219     public void showGetPoker(){
220         System.out.print(this.playerName + "  拿到的第  "+ this.pokerCount + " 张牌是:");
221         Poker.showThisPoker_Get(i, j);
222 //        System.out.println();
223     }
224     
225 }
226 
227 /**
228  * 玩家类
229  * @author CocoonFan
230  *
231  */
232 class Person extends Player{
233     private int score = 0;
234 
235     //构造方法
236     public Person(String PlayerName){
237         super(PlayerName);
238     }
239     
240     //get和set
241     public int getScore() {
242         return score;
243     }
244     public void setScore(int score) {
245         this.score = score;
246     }
247     
248     //计算总分
249     public int totalScore(){
250         if(1 == super.getJ()){//如果抽到A
251             if((score+11)%21 > score + 1){
252                 score += 11;
253             }else {
254                 score++;
255             }
256         }else if(super.getJ() >= 10){
257             score += 10;
258         }else {
259             score = score + super.getJ();
260         }
261         
262         score %=21;
263         
264         System.out.println(super.getPlayerName() + " 当前得分:" + score + "\n");
265         return score;
266     }
267     
268     //显示总分得分
269     public void showTotalScore(){
270         System.out.println(super.getPlayerName() + " 的总分为:" + score);
271     }
272 }
273 
274 /**
275  * 计算机类
276  * @author CocoonFan
277  *
278  */
279 class Computer extends Player{
280     private int score = 0;
281     
282     //构造方法
283     public Computer(String PlayerName){
284         super(PlayerName);
285     }
286     
287     //set和get
288     public int getScore() {
289         return score;
290     }
291     public void setScore(int score) {
292         this.score = score;
293     }
294     
295     //计算总分
296     public int totalScore(){
297         if(1 == super.getJ()){//如果抽到A
298             if((score+11)%21 > score + 1){
299                 score += 11;
300             }else {
301                 score++;
302             }
303         }else if(super.getJ() >= 10){
304             score += 10;
305         }else {
306             score = score + super.getJ();
307         }
308         
309         if (score > 21) {
310             System.out.println(super.getPlayerName() + ",你自爆了!");
311             score = -1;
312         }else {
313             System.out.println(super.getPlayerName() + " 当前得分:" + score + "\n");
314         }
315         return score;
316     }
317     
318     public boolean isGetNextPoker(){
319         if (score <= 17) {
320             return true;
321         }else {
322             return false;
323         }
324     }
325     
326     //显示总分得分
327         public void showTotalScore(){
328             System.out.println(super.getPlayerName() + " 的总分为:" + score);
329         }
330 }

 

三.运行结果

 

posted @ 2013-01-21 02:43  Cocoon  阅读(371)  评论(0编辑  收藏  举报