⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 riskgame.java

📁 java 开源游戏源码 RISK 联机对战 战棋类
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
				if (recycleCards) {

					Cards.add(card1);
                                	Cards.add(card2);
                                	Cards.add(card3);
                                	//Return the cards to the deck

				}

				cardState=getNewCardState();

				int armies = 0;

				if (cardMode==CARD_FIXED_SET) {

					String n1 = card1.getName();
					String n2 = card2.getName();
					String n3 = card3.getName();
					if( n1.equals(n2) && n1.equals(n3) ) // Implies n2.equals(n3)
					{
						if(n1.equals( Card.CAVALRY ))
							armies = 6;
						else if(n1.equals( Card.INFANTRY ))
							armies = 4;
						else if(n1.equals( Card.CANNON ))
							armies = 8;
						else if(n1.equals( Card.WILDCARD ))
							armies = 12; // Incase someone puts 3 wildcards into his set
					}
					// If there is 1 cavalry,1 infantry and one cannon
					else if( !n1.equals(n2) && !n2.equals(n3) && !n1.equals(n3) && !n1.equals(Card.WILDCARD) && !n2.equals(Card.WILDCARD) && !n3.equals(Card.WILDCARD) )
					{
						armies = 10;
					}//All the same w/1 or two wildcards
					else if(
					 (  n1.equals(Card.WILDCARD) && n2.equals(n3) && !n2.equals(Card.WILDCARD) )
					 ||(n2.equals(Card.WILDCARD) && n1.equals(n3) && !n1.equals(Card.WILDCARD) )
					 ||(n3.equals(Card.WILDCARD) && n1.equals(n2) && !n1.equals(Card.WILDCARD) ) )
					 {
					 	if(n1.equals(Card.CANNON)||n2.equals(Card.CANNON)||n3.equals(Card.CANNON))
					 	{
					 	 armies = 8;
					 	}
					 	else if (n1.equals(Card.INFANTRY)||n2.equals(Card.INFANTRY)||n3.equals(Card.INFANTRY))
					 	{
					 	 armies = 4;
					 	}
					 	else if (n1.equals( Card.CAVALRY )||n2.equals( Card.CAVALRY )||n3.equals( Card.CAVALRY ))
					 	{
					 	 armies = 6;
					 	}
					 }//All different w/1 or two wildcards
					else if(
					 (  n1.equals(Card.WILDCARD) && !n2.equals(n3) && !n2.equals(Card.WILDCARD) )
					 ||(n2.equals(Card.WILDCARD) && !n1.equals(n3) && !n1.equals(Card.WILDCARD) )
					 ||(n3.equals(Card.WILDCARD) && !n1.equals(n2) && !n1.equals(Card.WILDCARD) ) )
					{
						armies = 10;
					}

				}
				else {

					armies = cardState;

				}

				currentPlayer.addArmies(armies);

				if ( canTrade()==false || (tradeCap==true && ((Vector)currentPlayer.getCards()).size() < 5 ) ) {
					gameState=STATE_PLACE_ARMIES;
					tradeCap=false;
				}

				return cardState;

			}
		}
		return 0;

	}

	public boolean canTrade() {

		Vector cards = currentPlayer.getCards();
		Card card1=null, card2=null, card3=null;

		if (setup == Players.size() && cards.size() >= 3 ) { // ie the initial setup has been compleated and there are 3 cards or more

			for (int a=0; a< cards.size() ; a++) {
				if (card1 != null && card2 != null && card3 != null) { break; }
				card1 = (Card)cards.elementAt(a);

				for (int b=(a+1); b< cards.size() ; b++) {
					if (card1 != null && card2 != null && card3 != null) { break; }
					card2 = (Card)cards.elementAt(b);

					for (int c=(b+1); c< cards.size() ; c++) {
						if (card1 != null && card2 != null && card3 != null) { break; }
						card3 = (Card)cards.elementAt(c);

						if ( checkTrade(card1, card2, card3) ) { break; }
						else { card3=null; }

					}
				}
			}
		}

		if (card3 == null) {
			return false;
		}
		else {
			return true;
		}

	}

	public int getNewCardState() {

		if (cardState < 4) {
			return cardState+4;
		}
		else if (cardState < 12) {
			return cardState+2;
		}
		else if (cardState < 15) {
			return cardState+3;
		}
		else {
			return cardState+5;
		}

	}

	/**
	 * checks if a set of cards can be traded
	 * @param card1 First card to trade
	 * @param card2 Second card to trade
	 * @param card3 Third card to trade
	 * @return boolean true if they can be traded false if they can not
	 */
	public boolean checkTrade(Card card1, Card card2, Card card3) {

		// at this point we know that the player DOES have all the cards
		if (tradeCap==true && ((Vector)currentPlayer.getCards()).size() < 5 ) {
			return false;
		}

		if (
				( card1.getName().equals(Card.WILDCARD) || card2.getName().equals(Card.WILDCARD) || card3.getName().equals(Card.WILDCARD) ) ||			// if one of the cards IS a wildcard then they CAN be traded
				( card1.getName().equals(card2.getName()) && card1.getName().equals(card3.getName()) ) || 						// if all the cards have the same name
				( !card1.getName().equals(card2.getName()) && !card1.getName().equals(card3.getName()) && !card2.getName().equals(card3.getName()) )	// if all the cards have diferent names
		) { return true; }
		else { return false; }
	}

	/**
	 * Ends the trading phase by checking if the player has less than 5 cards
	 * @return boolean Returns true if the player has ended the trade phase, returns false if the player cannot end the trade phase
	 */
	public boolean endTrade() {

		if (gameState==STATE_TRADE_CARDS) {

			if (tradeCap==true && ((Vector)currentPlayer.getCards()).size() > 5 ) {
				return false;
			}

			if (((Vector)currentPlayer.getCards()).size() < 5) {

				gameState=STATE_PLACE_ARMIES;
				tradeCap=false;
				return true;
			}

		}
		return false;

	}

	/**
	 * Places an army on the Country
	 * @param t Country that the player wants to add armies to
	 * @param n Number of armies the player wants to add to the country
	 * @return boolean Returns true if the number of armies are added the country, returns false if the armies cannot be added to the territory
	 */
	public int placeArmy(Country t, int n) {

		int done=0;

		if ( gameState==STATE_PLACE_ARMIES ) {

			if ( setup != Players.size() ) { // ie the initial setup has not been compleated
				if (n != 1) return 0;
				// if it has the player as a owner
				if ( t.getOwner()==currentPlayer ) {

					if ( NoEmptyCountries() ) { // no empty country are found
						t.addArmy();
						currentPlayer.loseExtraArmy(1);
						done=1;
						//System.out.print("army placed in: " + t.getName() + "\n"); // testing
					}

				}
				// if there is no owner
				else if ( t.getOwner()==null ) {

					t.setOwner(currentPlayer);
					currentPlayer.newCountry(t);
					t.addArmy();
					currentPlayer.loseExtraArmy(1);
					done=1;
					//System.out.print("country taken and army placed in: " + t.getName() + "\n"); // testing
				}

			}
			else { // initial setup is completed

				// if it has the player as a owner
				if ( t.getOwner()==currentPlayer && currentPlayer.getExtraArmies() >=n ) {

					currentPlayer.currentStatistic.addReinforcements(n);

					t.addArmies(n);
					currentPlayer.loseExtraArmy(n);
					//System.out.print("army placed in: " + t.getName() + "\n"); // testing
					done=1;

				}
			}

			if (done==1) {

				if (setup == Players.size() ) { // ie the initial setup has been compleated
					if ( currentPlayer.getExtraArmies()==0 ) { gameState=STATE_ATTACKING; }
					else { gameState=STATE_PLACE_ARMIES; }
				}
				else { // initial setup is not compleated
					if (currentPlayer.getExtraArmies()==0) {
						setup++; // another player has finished initial setup

					}

					gameState=STATE_END_TURN;

				}

				if ( checkPlayerWon() ) {
					done=2;
				}

			}

		}
		return done;

	}

	/**
	 * Automatically places an army on an unoccupied country
	 * @return int Returns the country id which an army was added to
	 */
	public int getEmptyCountry() {

		// find a empty country
		int nEmpty = -1;

		if (gameState==STATE_PLACE_ARMIES) {

			int a = r.nextInt(Countries.length);
			boolean done = false;

			for (int c=a; c < Countries.length ; c++) {

				if ( Countries[c].getOwner() == null ) {
					nEmpty = Countries[c].getColor();
					break;
				}
				else if ( c == Countries.length-1 && done == false ) {
					c = -1;
					done = true;
				}
				else if ( c == Countries.length-1 && done == true ) {
					break;
				}

			}

		}

		return nEmpty;

	}//public int getEmptyCountry()

	/**
	 * Attacks one country and another
	 * @param t1 Attacking country
	 * @param t2 Defending country
	 * @return int[] Returns an array which determines if the player is allowed to roll dice
	 */
	public int[] attack(Country t1, Country t2) {

		int[] result = new int[3];
		result[0]=0;
		result[1]=0;
		result[2]=0;

		if (gameState==STATE_ATTACKING) {

			if (
					t1!=null &&
					t2!=null &&
					t1.getOwner()==currentPlayer &&
					t2.getOwner()!=currentPlayer &&
					t1.isNeighbours(t2) &&
					// t2.isNeighbours(t1) && // not needed as there is code to check this
					t1.getArmies() > 1
			) {

				currentPlayer.currentStatistic.addAttack();
				((Player)t2.getOwner()).currentStatistic.addAttacked();

				result[0]=1;

				if ( t1.getArmies() > 4 ) { result[1]=3; }
				else { result[1]=t1.getArmies()-1; }

				int a;
				if (simone) { a=3; }
				else { a=2; }

				if ( t2.getArmies() > a ) { result[2]=a; }
				else { result[2]=t2.getArmies(); }

				attacker=t1;
				defender=t2;

				gameState=STATE_ROLLING;
				//System.out.print("Attacking "+t2.getName()+" ("+t2.getArmies()+") with "+t1.getName()+" ("+t1.getArmies()+").\n"); // testing

			}

		}
		return result;
	}

	/**
	 * Ends the attacking phase
	 * @return boolean Returns true if the player ended the attacking phase, returns false if the player cannot end the attacking phase
	 */
	public boolean endAttack() {

		if (gameState==STATE_ATTACKING) { // if we were in the attack phase

			// YURA:TODO check if there are any countries with more then 1 amy, maybe even check that a move can be made

			gameState=STATE_FORTIFYING; // go to move phase
			//System.out.print("Attack phase ended\n");
			return true;
		}
		return false;
	}

	/**
	 * Rolls the attackersdice
	 * @param dice1 Number of dice to be used by the attacker
	 * @return boolean Return if the roll was successful
	 */
	public boolean rollA(int dice1) {

		if (gameState==STATE_ROLLING) { // if we were in the attacking phase

			if ( attacker.getArmies() > 4 ) {
				if (dice1<=0 || dice1>3) return false;
			}
			else {
				if (dice1<=0 || dice1> (attacker.getArmies()-1) ) return false;
			}

			attackerDice = dice1; // 5 2 0

			// System.out.print("NUMBER OF DICE: " + dice1 + " or " + attackerDice.length + "\n");

			currentPlayer=defender.getOwner();
			gameState=STATE_DEFEND_YOURSELF;
			return true;

		}
		else return false;

	}

	public boolean rollD(int dice2) {

		if (gameState==STATE_DEFEND_YOURSELF) { // if we were in the defending phase

			if ( defender.getArmies() > 2 ) {
				if (dice2<=0 || dice2>2) return false;
			}
			else {
				if (dice2<=0 || dice2> (defender.getArmies()) ) return false;
			}

			currentPlayer=attacker.getOwner();

			defenderDice = dice2; // 4 3

			return true;

		}
		else return false;

	}

	public int getAttackerDice() {
		return attackerDice;
	}

	public int getDefenderDice() {
		return defenderDice;
	}

	/**
	 * Rolls the defenders dice
	 * @param attackerResults The results for the attacker
	 * @param defenderResults The results for the defender
	 * @return int[] Returns an array which will determine the results of the attack
	 */
	public int[] battle(int[] attackerResults, int[] defenderResults) {

		int[] result = new int[6];
		result[0]=0; // worked or not
		result[1]=0; // no of armies attacker lost
		result[2]=0; // no of armies defender lost
		result[3]=0; // did you win
		result[4]=0; // min move
		result[5]=0; // max move

		if (gameState==STATE_DEFEND_YOURSELF) { // if we were in the defending phase

			// battle away!
			for (int c=0; c< Math.min(attackerResults.length, defenderResults.length) ; c++) {

				if (attackerResults[c] > defenderResults[c]) {
					defender.looseArmy();
					((Player)defender.getOwner()).currentStatistic.addCasualty();
					((Player)attacker.getOwner()).currentStatistic.addKill();
					result[2]++;
				}
				else {
					attacker.looseArmy();
					((Player)attacker.getOwner()).currentStatistic.addCasualty();
					((Player)defender.getOwner()).currentStatistic.addKill();
					result[1]++;
				}

			}

			// if all the armies have been defeated
			if (defender.getArmies() == 0) {

				((Player)attacker.getOwner()).currentStatistic.addCountriesWon();
				((Player)defender.getOwner()).currentStatistic.addCountriesLost();

				result[5]=attacker.getArmies()-1;

				capturedCountry=true;

				Player lostPlayer=(Player)defender.getOwner();

				lostPlayer.lostCountry(defender);
				currentPlayer.newCountry(defender);

				defender.setOwner( (Player)attacker.getOwner() );
				result[3]=1;
				gameState=STATE_BATTLE_WON;
				mustmove=attackerResults.length;

				result[4]=mustmove;


				// if the player has been eliminated
				if ( lostPlayer.getNoTerritoriesOwned() == 0) {

					result[3]=2;

					currentPlayer.addPlayersEliminated(lostPlayer);

					while (((Vector)lostPlayer.getCards()).size() > 0) {

						//System.out.print("Hes got a card .. i must take it!\n");
						currentPlayer.giveCard( lostPlayer.takeCard() );

					}

					if ( ((Vector)currentPlayer.getCards()).size() > 5) {
						// gameState=STATE_BATTLE_WON;
						tradeCap=true;
					}

				}

			}
			else if (attacker.getArmies() == 1) {
				gameState=STATE_ATTACKING;
				//System.out.print("Retreating (FORCED)\n");
				currentPlayer.currentStatistic.addRetreat();
			}
			else { gameState=STATE_ROLLING; }

			defenderDice = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -