用java写游戏--“21点”

我们应该都知道有一个游戏叫做21点,最近用Java写了这个游戏,希望读者能够提出宝贵意见。
这个游戏我写了4个Java文件。
游戏说明:
开始游戏
可以做一些选择,输入cm表示要查看自己的可见牌和不可见牌,输入co表示查看其它玩家的不可见牌,输入cp表示查看其它玩家的passed情况,输入no或者输入其它字符表示表示什么都不做。

开始发牌
输入rv or rV or RV or Rv(不区分大小写)表示需要(require)可见牌(visible),所以可以选择输入这四个字符中的一个。
输入rh ot rH or or RH or Rh表示需要(require)不可见牌(hidden),所以可以选择输入这四个字符中的一个。

下面就是轮流给没有passed的玩家进行游戏,我用了一个自定义的KEY类作为函数的参数,这样key的值的改变就不是值传递了.

最后就是游戏输赢的判定了.

package Game;

import java.util.Scanner;

public class Player {
	String name;
	Game game;
	
	private int hiddenCard;
	public int sumOfVisibleCards;
	public boolean passed;
	
	public Player(String name,Game game)//给定的一个游戏构造一个玩家
	{
		this.name=name;
		this.game=game;
	}
	void takeCard(int card, boolean hidden)//得到一张牌
	{
		System.out.println("The card you acquire is:"+card);
		if(hidden==true)
			hiddenCard+=card;
		else
			sumOfVisibleCards+=card;
	}
	int getScore()//获取总点数
	{
		return this.hiddenCard+this.sumOfVisibleCards;
	}
	public void initialHiddenCard(int point)//初始化隐藏牌
	{
		this.hiddenCard=point;
	}
	void play()//决定要牌或者pass,检查自己的隐藏牌和可见牌,那些玩家通过
	{
		System.out.println("Player name:"+this.name);
		System.out.println("Before round you cards.visible:"+this.sumOfVisibleCards+",hidden:"+this.hiddenCard);
		Scanner in =new Scanner(System.in);
		
		char c1='p',c2='v';
		String tstr;
		String str;
		
		System.out.println("Please decide 'cm','co','cp' or 'no'.");
		tstr=in.nextLine();
		switch(tstr)//决定要牌之前的操作
		{
			case "cm":
				System.out.println("name:"+this.name);
				System.out.println("visible:"+this.sumOfVisibleCards+",hidden:"+this.hiddenCard);
				break;
			case "co":
				if(this.game.player1==this)
				{
					System.out.println("name:"+this.game.player2.name+",visible"+this.game.player2.sumOfVisibleCards);
					System.out.println("name:"+this.game.player3.name+",visible"+this.game.player3.sumOfVisibleCards);
				}
				else if(this.game.player2==this)
				{
					System.out.println("name:"+this.game.player1.name+",visible"+this.game.player1.sumOfVisibleCards);
					System.out.println("name:"+this.game.player3.name+",visible"+this.game.player3.sumOfVisibleCards);
				}
				else
				{
					System.out.println("name:"+this.game.player1.name+",visible"+this.game.player1.sumOfVisibleCards);
					System.out.println("name:"+this.game.player2.name+",visible"+this.game.player2.sumOfVisibleCards);
				}
				break;
			case "cp":
				System.out.println("name:"+this.game.player1.name+",passed or not?"+this.game.player1.passed);
				System.out.println("name:"+this.game.player2.name+",passed or not?"+this.game.player2.passed);
				System.out.println("name:"+this.game.player3.name+",passed or not?"+this.game.player3.passed);
				break;
			case "no":break;
			default:break;
		}
		
		System.out.println("Please decide whether pass and hidden! 'r/Rh/H','r/Rv/V' or 'p/P'.");
		System.out.print("please enter your choice:");
		str=in.nextLine();
		c1=str.charAt(0);
		
		if(str.length()>=2)
		c2=str.charAt(1);
		
		//输出错误默认玩家选择过
		if(c1=='R' || c1=='r')
		{
			if(c2=='H' || c2=='h')
				this.takeCard(this.game.dealer.nextCard(),true);
			else if(c2=='V' || c2=='v')
				this.takeCard(this.game.dealer.nextCard(),false);
			else
			{
				System.out.println("Enter Error! default passed");
				this.passed=true;
			}
		}
		else if(c1=='P' || c1=='p')
			this.passed=true;
		else
		{
			System.out.println("Enter Error! default passed");
			this.passed=true;
		}
		System.out.println("After round you cards.visible:"+this.sumOfVisibleCards+",hidden:"+this.hiddenCard);
	}
}
`package Game;
import java.util.Random;

/**
 * Represents a player in this simplified version of the
 * @see 21 card game.
 * @author LWD
 * @version 2021-3-30
 */
public class Dealer {
	String name;
	Player player1;
	Player player2;
	Player player3;
	
	Random random = new Random();
	 public Dealer(String name, Player player1, Player player2,Player player3) 
	 {
		 this.name=name;
		 this.player1=player1;
		 this.player2=player2;
		 this.player3=player3;
	 }
	 void runGame()
	 {
		 this.deal();//初始化每个玩家的两张牌
		 //在都没有passed的情况下
		 Key k=new Key();
		 while(this.player1.passed==false || this.player2.passed==false || this.player3.passed==false)
		 {
			 this.allowPlay(k);
			 k.key++;
		 }
	 }
	 void allowPlay(Key k)//轮流给没有passed的玩家机会要牌 
	 {
		 if(k.key%3==0)
		 {
			 if(this.player1.passed==false)
				 this.player1.play();
			 else
			 {
				 k.key++;
				 allowPlay(k);
			 }
		 }
		 else if(k.key%3==1)
		 {
			 if(this.player2.passed==false)
				 this.player2.play();
			 else
			 {
				 k.key++;
				 allowPlay(k);
			 }
		 }
		 else
		 {
			 if(this.player3.passed==false)
				 this.player3.play();
			 else
			 {
				 k.key++;
				 allowPlay(k);
			 }
		 }
	 }
	 int nextCard()//返回下一张牌的大小,范围是1~10.
	 {
		 return 1+random.nextInt(10);
	 }
	 void deal()//处理两张初始的牌
	 {
		 //第一张没有隐藏的牌
		 this.player1.sumOfVisibleCards=nextCard();
		 this.player2.sumOfVisibleCards=nextCard();
		 this.player3.sumOfVisibleCards=nextCard();
		 //第二张隐藏的牌
		 this.player1.initialHiddenCard(nextCard());
		 this.player2.initialHiddenCard(nextCard());
		 this.player2.initialHiddenCard(nextCard());
	 }
	 private boolean playerWins(int score1, int score2, int score3)
	 {
		 //三个都超过了21点,没有胜利者
		 if(this.player1.getScore()>21 && this.player2.getScore()>21 && this.player3.getScore()>21)
			 return false;
		 //两个超过21点肯定有胜利者
		 
		 //1个超过21点,其它两个相等,没有胜利者
		 else if(this.player1.getScore()>21 && this.player2.getScore()==this.player3.getScore())
			 return false;
		 else if(this.player2.getScore()>21 && this.player1.getScore()==this.player3.getScore())
			 return false;
		 else if(this.player3.getScore()>21 && this.player1.getScore()==this.player2.getScore())
			 return false;
		 //三个都不超过21点
		 //三个相等 或者 是其中两个相等并且大于第三个
		 //都不相等肯定有胜利者
		 if(this.player1.getScore()==this.player2.getScore() && this.player1.getScore()==this.player3.getScore())
			 return false;
		 else if(this.player1.getScore()==this.player2.getScore() && this.player1.getScore()>this.player3.getScore())
			 return false;
		 else if(this.player1.getScore()==this.player3.getScore() && this.player1.getScore()>this.player2.getScore())
			 return false;
		 else if(this.player2.getScore()==this.player3.getScore() && this.player2.getScore()>this.player1.getScore())
			 return false;
		 return true;
	 }
	 void scoreGame()//决定获胜者并且打印
	 {
		System.out.println("player1:"+this.player1.name+",total score:"+this.player1.getScore());
		System.out.println("player2:"+this.player2.name+",total score:"+this.player2.getScore());
		System.out.println("player1:"+this.player3.name+",total score:"+this.player3.getScore());
		
		if( this.playerWins(this.player1.getScore(),this.player2.getScore(),this.player3.getScore())==false)
			System.out.println("The Current game have no Winner!");
		//两个大于21点
		else if(this.player1.getScore()>21 && this.player2.getScore()>21)
			System.out.println("Congratulation!!!The "+this.player3.name+" is the Winner!");
		else if(this.player1.getScore()>21 && this.player3.getScore()>21)
			System.out.println("Congratulation!!!The "+this.player2.name+" is the Winner!");
		else if(this.player2.getScore()>21 && this.player3.getScore()>21)
			System.out.println("Congratulation!!!The "+this.player1.name+" is the Winner!");
		//一个大于21点 其中两个不等
		else if(this.player1.getScore()>21)
		{
			if(this.player2.getScore()>this.player3.getScore())
				System.out.println("Congratulation!!!The "+this.player2.name+" is the Winner!");
			else
				System.out.println("Congratulation!!!The "+this.player3.name+" is the Winner!");
		}
		else if(this.player2.getScore()>21)
		{
			if(this.player1.getScore()>this.player3.getScore())
				System.out.println("Congratulation!!!The "+this.player1.name+" is the Winner!");
			else
				System.out.println("Congratulation!!!The "+this.player3.name+" is the Winner!");
		}
		else if(this.player3.getScore()>21)
		{
			if(this.player1.getScore()>this.player2.getScore())
				System.out.println("Congratulation!!!The "+this.player1.name+" is the Winner!");
			else
				System.out.println("Congratulation!!!The "+this.player2.name+" is the Winner!");
		}
		//都小于等于21点
		//两个相等 但是小于第三个
		if(this.player1.getScore()==this.player2.getScore() && this.player1.getScore()<this.player3.getScore())
			System.out.println("Congratulation!!!The "+this.player3.name+" is the Winner!");
		else if(this.player1.getScore()==this.player3.getScore() && this.player1.getScore()<this.player2.getScore())
			System.out.println("Congratulation!!!The "+this.player2.name+" is the Winner!");
		else if(this.player2.getScore()==this.player3.getScore() && this.player2.getScore()<this.player1.getScore())
			System.out.println("Congratulation!!!The "+this.player1.name+" is the Winner!");
		//三个都不相等
		else if(Math.max(Math.max(this.player1.getScore(), this.player2.getScore()), this.player3.getScore())==this.player1.getScore())
			System.out.println("Congratulation!!!The "+this.player1.name+" is the Winner!");
		else if(Math.max(Math.max(this.player1.getScore(), this.player2.getScore()), this.player3.getScore())==this.player2.getScore())
			System.out.println("Congratulation!!!The "+this.player2.name+" is the Winner!");
		else if(Math.max(Math.max(this.player1.getScore(), this.player2.getScore()), this.player3.getScore())==this.player3.getScore())
			System.out.println("Congratulation!!!The "+this.player3.name+" is the Winner!");
	 }
}
package Game;

public class Game {
	Player player1;
	Player player2;
	Player player3;
	Dealer dealer;
	public static void main(String[] args) {
		System.out.println("**************Wellcom to the game '21 point'***************");
		System.out.println("The follow in is the rule of the game.");
		System.out.println("enter 'R' or 'r' mean not pass,'P' or 'p' mean pass.");
		System.out.println("enter 'H' or 'h' mean hidden,'V' or 'v' mean visible");
		System.out.println("enter sample: 'rv' mean not pass and visible");
		System.out.println("------------------some tips----------------------");
		System.out.println("Before make your chioce,you can check you hidden and visible,");
		System.out.println("check others' visible and witch player passed or do nothig.");
		System.out.println("enter 'cm' means check yours,'co' means check others,'cp' means check where pass,'no' means do nothing");
		System.out.println("***********************Game Start***************************");
		//初始化操作
		Game game=new Game();
		game.player1=new Player("Mike",game);
		game.player2=new Player("Jack",game);
		game.player3=new Player("Bob",game);
		game.dealer=new Dealer("LWD",game.player1,game.player2,game.player3);
		System.out.println("player1:"+game.player1.name+"\nplayer2:"+game.player2.name+"\nplayer1:"+game.player3.name+"\ndealer:"+game.dealer.name);
		System.out.println("********************************");
		game.dealer.runGame();
		System.out.println("Game over! Thanks dealer LWD so much  q'(^_^)'p");
		game.dealer.scoreGame();
	}
}

package Game;
//作为函数的参数,是一个辅助工具
public class Key {
	int key;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值