蜘蛛纸牌java注释_自己摸索的纸牌游戏代码,感觉还有很多知识不懂,任重道远啊!...
package com.poker;
/*
* 定義一個紙牌的實體類
*/
public class Poker implements Comparable{
String color ;//定義花色
String card ;//定義牌
Double num ;//定義用來比較的數字
public Poker() {
}
public Poker(String color,String card,double num) {//含參構造器
this.color = color ;
this.card = card ;
this.num = num ;
}
@Override
public String toString() {//重寫下,打印輸出的時候屏蔽掉數字
return color+card;
}
@Override
public int compareTo(Poker o) {//定義比較規則
// TODO Auto-generated method stub
return this.num.compareTo(o.num);
}
}
package com.poker;
import java.util.ArrayList;
import java.util.List;
/*
* 定義一個玩家實體類
*/
public class Player {
String name ;
Integer id ;
List playingcards;
public Player() {
}
public Player(String name , Integer id ,List playingcards) {//含參構造器
playingcards = new ArrayList();//終于知道在構造器中初始化對象的用處了,寫在上面某些情況下會出現空指針異常
this.name = name ;
this.id = id ;
this.playingcards = playingcards ;
}
@Override
public String toString() {//重寫,方便打印
return "昵稱:"+name+" ?id:"+id;
}
}
package com.poker;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class Game {
List poker = new ArrayList();//創建撲克牌集合的對象
Player pr1 ; //玩家1 對象
Player pr2 ; //玩家2 對象
public static void main(String[] args) {
// TODO Auto-generated method stub
Game game = new Game();
game.pokerAdd();
game.playerAdd();
game.licensing();
}
public void pokerAdd() {
//創建撲克牌數組
System.out.println("-------------創建撲克牌-----------------------------");
//建立卡牌數組
String [] card = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
//建立花色數組
String [] color = {"黑桃","紅心","梅花","方片"};
//建立比較值數組
int [] num = {2,3,4,5,6,7,8,9,10,11,12,13,14};
//建立花色比較值數組
double [] colorNum = {0.4,0.3,0.2,0.1};
for(int i = 0 ; i < card.length ; i++) {
for(int j = 0 ; j< color.length; j++) {
Poker pk = new Poker(color[j],card[i],num[i]+colorNum[j]);
poker.add(pk);
}
}
System.out.println("------------撲克牌創建成功-----------------------");
System.out.println("為:"+poker);
System.out.println("---------------開始洗牌--------------------------");
Collections.shuffle(poker);//把數組元素順序打亂
System.out.println("---------------洗牌結束--------------------------");
//System.out.println("為:"+poker);//測試用
}
public void playerAdd() {
for(int i = 0 ; i < 2 ; i++ ) {
System.out.println("-------請輸入第"+(i+1)+"位玩家的ID和昵稱------------");
System.out.println("輸入ID");
try {
Scanner sca = new Scanner(System.in);//初始化輸入對象,這步必須寫在這里,否則會進入死循環
Integer id = sca.nextInt();
System.out.println("請輸入昵稱");
String name = sca.next();
if(i==0) {
pr1 = new Player(name,id,null);
System.out.println("成功添加玩家"+pr1);
}else if(i==1) {
pr2 = new Player(name,id,null);
System.out.println("成功添加玩家"+pr2);
}
}catch(InputMismatchException e) {
System.out.println("請輸入整數類型的ID");
i--;
continue;
}
}
System.out.println("玩家添加完畢,分別為:"+pr1+","+pr2+" ?!");
}
public void licensing() {
for(int i = 0 ; i < 4 ; i ++) {
if(i%2==0) {//方便大家看,pr1是玩家1對象,palyingcards 是玩家對象中的ArrayList集合,用來放牌
pr1.playingcards.add(poker.get(i));
System.out.println("玩家"+pr1.name+"拿牌");
}else {
pr2.playingcards.add(poker.get(i));
System.out.println("玩家"+pr2.name+"拿牌");
}
}
//pr1.playingcards.add(poker.get(0)); ?//測試用
//System.out.println("玩家"+pr1.name+"拿牌");
//poker.remove(0);
//pr2.playingcards.add(poker.get(0));
//System.out.println("玩家"+pr2.name+"拿牌");
//poker.remove(0);
System.out.println("--------------發牌結束------------------------------");
System.out.println("--------------游戲開始------------------------------");
Collections.sort(pr1.playingcards);
Collections.sort(pr2.playingcards);
//方便大家看,pr1是玩家1對象,palyingcards 是玩家對象中的ArrayList集合,用來放牌
System.out.println(pr1.name+"最大的手牌為"+pr1.playingcards.get(1).color+
pr1.playingcards.get(1).card);
System.out.println(pr2.name+"最大的手牌為"+pr2.playingcards.get(1).color+
pr2.playingcards.get(1).card);
if(pr1.playingcards.get(1).num>pr2.playingcards.get(1).num){
System.out.println("------------玩家"+pr1.name+"獲勝-------------------------");
}else {
System.out.println("------------玩家"+pr2.name+"獲勝-------------------------");
}
System.out.println("玩家各自的手牌為:");
System.out.println(pr1.name+":"+pr1.playingcards);
System.out.println(pr2.name+":"+pr2.playingcards);
}
}
總結
以上是生活随笔為你收集整理的蜘蛛纸牌java注释_自己摸索的纸牌游戏代码,感觉还有很多知识不懂,任重道远啊!...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: private访问权限java_Java
- 下一篇: python写excel文件出错_【求教