和视频功能有点区别,我改为了2个玩家,每个玩家抽取N张牌, 然后每次取一张牌比大小,比N次,输出信息。
添加牌:A 黑桃
添加牌:2 黑桃
添加牌:3 黑桃
添加牌:4 黑桃
。。。
... 太多了就不显示了
。。。
添加牌:j 方块
添加牌:q 方块
添加牌:k 方块
输入1号名字
jack
输入2号名字
lucy
jack get card :10 红心 cards:51
lucy get card :2 黑桃 cards:50
jack get card :6 黑桃 cards:49
lucy get card :q 方块 cards:48
jack get card :k 红心 cards:47
lucy get card :q 梅花 cards:46
jack get card :8 方块 cards:45
lucy get card :8 梅花 cards:44
jack get card :q 黑桃 cards:43
lucy get card :7 黑桃 cards:42
jack胜 jack:8 方块|| lucy:8 梅花
jack胜 jack:10 红心|| lucy:2 黑桃
lucy胜 jack:6 黑桃|| lucy:q 方块
jack胜 jack:k 红心|| lucy:q 梅花
jack胜 jack:8 方块|| lucy:8 梅花
-------------------RESULT---------------------
jackwin4times
lucywin1times
1建立牌的对象
package com.pock;
//牌 类 存储牌信息
public class pock implements Comparable<pock>{
public String num;//牌大小 字符串
public String color;//牌花色
public int nnum;//牌大小 整形
public int ncolor;//牌花色
static public String[] color_list={"黑桃","梅花","红心","方块"}; //牌花色查询表
static public String[] num_list={"A","2","3","4","5","6","7","8","9","10","j","q","k"};//牌大小查询表
public pock(int newnum,int newcolor){//牌初始化
this.num =num_list[newnum];
this.color =color_list[newcolor];
this.nnum=newnum;
this.ncolor=newcolor;
}
public pock(){
}
public int compareTo(pock c){//牌大小比较
if(this.nnum==c.nnum){
return this.ncolor-c.ncolor;
}
return this.nnum-c.nnum;
}
}
2 建立牌池对象
package com.pock;
import java.util.*;
//桌面牌集合
public class pock_set {
public List<pock> cardlist;//桌面牌池 初始13*4张
pock_set(){
cardlist=new ArrayList<pock>();
}
}
3 建立玩家对象
package com.pock;
import java.util.*;
//玩家类- 保存玩家信息,增加了赢得次数
public class player {
public String name; //名字
public pock_set player_cards;//玩家牌池
public int win_num;//胜利次数
player(String newname){
this.name=newname;
player_cards=new pock_set();
win_num=0;
}
player( ){
player_cards=new pock_set();
win_num=0;
}
}
游戏程序:
package com.pock;
import java.util.Random;
import java.util.Scanner;
//game
public class Init {
public static void main(String[] args) {
// TODO Auto-generated method stub
pock_set allcards=new pock_set();//建立桌面牌池
player p1=new player();//建立玩家
player p2=new player();
//填充桌面牌池 共52张牌
for(int i=0;i<4;i++)
{
for(int j=0;j<13;j++)
{
pock newpock=new pock(j,i);
allcards.cardlist.add(newpock);
System.out.println("添加牌:"+newpock.num+"\t"+newpock.color);
}
}
//玩家加入 防止重名
System.out.println("输入1号名字");
Scanner s1=new Scanner(System.in);
p1.name=s1.next();
System.out.println("输入2号名字 ");
Scanner s2=new Scanner(System.in);
String name=s2.next();
while(p1.name.equals(name)){
System.out.println("输入2号名字 不要重名");
name=s2.next();
}
p2.name=name;
//发牌 通过调用random函数获取随机index指向的牌池的牌,获取后删除这个牌,加入玩家的牌池。random最大值为牌池牌数
for(int i=0;i<5;i++)//发牌4次
{
pock newpock=new pock();
Random newrandom=new Random();
int x=newrandom.nextInt(allcards.cardlist.size()-1);
newpock=(pock)allcards.cardlist.get(x);
p1.player_cards.cardlist.add(newpock);
allcards.cardlist.remove(x);
System.out.println(p1.name+" get card :"+newpock.num+"\t"+newpock.color+"\t cards:"+allcards.cardlist.size());
x=newrandom.nextInt(allcards.cardlist.size()-1);
newpock=(pock)allcards.cardlist.get(x);
p2.player_cards.cardlist.add(newpock);
allcards.cardlist.remove(x);
System.out.println(p2.name +" get card :"+newpock.num+"\t"+newpock.color+"\t cards:"+allcards.cardlist.size());
}
//每个玩家抽取一张比较,可以用一个random 也可以用2个random分别抽玩家不同的牌,效果差不多 玩家牌池内有几张牌就比较几次
//这里忘记加入 比较一次后删除本次比较的牌了,会出现重复比较,这个后面再修改吧
for(int i=0;i<p1.player_cards.cardlist.size();i++){
Random newrandom=new Random();
int x=newrandom.nextInt(p1.player_cards.cardlist.size()-1);
int result=p1.player_cards.cardlist.get(x).compareTo (p2.player_cards.cardlist.get(x));
if(result==0){
System.out.print("平局");
}
else if(result>0)
{
System.out.print(p1.name+"胜");
p1.win_num++;
}
else
{
System.out.print(p2.name+"胜");
p2.win_num++;
}
System.out.print("\t"+p1.name+":"+p1.player_cards.cardlist.get(x).num+"\t"+p1.player_cards.cardlist.get(x).color+"||");
System.out.println("\t"+p2.name+":"+p2.player_cards.cardlist.get(x).num+"\t"+p2.player_cards.cardlist.get(x).color);
}//输出最后结果,可以加个判断谁赢得多,懒得写了~~
System.out.println("-------------------RESULT---------------------");
System.out.println(p1.name+"win"+p1.win_num+"times");
System.out.println(p2.name+"win"+p2.win_num+"times");
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦