如何让这个纸牌游戏的“动作”继续执行,直到找到胜利者为止?

时间:2017-12-04 10:56:03

标签: java loops playing-cards

我制作了这款纸牌游戏,要求用户采取行动。 “攻击”和“交换”。它们在第一次执行时都能很好地工作,但我该如何制作它以便系统一直要求用户采取行动直到其中一个玩家获胜?

这包含GameConsole将引用的GameMaster类的代码。它链接到我称为播放器和卡的其他两个类。

import java.util.*;
public class GameMaster
{
private int turnCounter = 0;
private ArrayList<Card> deck;
private boolean isThereWinner; 
private Player player1;
private Player player2;
public GameMaster (Player a, Player b) //initializes values
{
    turnCounter = turnCounter + 1;
    player1 = a;
    player2 = b;
    deck = new ArrayList<Card>();
    assembleDeck();
    dealCard();
}
private void assembleDeck() //given order of cards
{
    deck.add( new Card( "Dragon", "Aquira", 174, 26 ) );
    deck.add( new Card( "Ghost", "Brawn", 130, 48 ) );
    deck.add( new Card( "Fairy", "Cerulea", 162, 29 ) );
    deck.add( new Card( "Dragon", "Demi", 147, 28 ) );
    deck.add( new Card( "Ghost", "Elba", 155, 37 ) );
    deck.add( new Card( "Fairy", "Fye", 159, 42 ) );
    deck.add( new Card( "Dragon", "Glyede", 129, 26 ) );
    deck.add( new Card( "Ghost", "Hydran", 163, 35 ) );
    deck.add( new Card( "Fairy", "Ivy", 146, 45 ) );
    deck.add( new Card( "Dragon", "Jet", 170, 24 ) );
    deck.add( new Card( "Ghost", "Kineti", 139, 21 ) );
    deck.add( new Card( "Fairy", "Levi", 160, 43 ) );
    deck.add( new Card( "Dragon", "Meadow", 134, 29 ) );
    deck.add( new Card( "Ghost", "Naidem", 165, 26 ) );
    deck.add( new Card( "Fairy", "Omi", 145, 21 ) );
    deck.add( new Card( "Dragon", "Puddles", 170, 34 ) );
    deck.add( new Card( "Ghost", "Quarrel", 151, 29 ) );
    deck.add( new Card( "Fairy", "Raven", 168, 32 ) );
    deck.add( new Card( "Dragon", "Surge", 128, 27 ) );
    deck.add( new Card( "Ghost", "Takiru", 140, 26 ) );
    deck.add( new Card( "Fairy", "Ustelia", 163, 47 ) );
    deck.add( new Card( "Dragon", "Verwyn", 145, 25 ) );
    deck.add( new Card( "Ghost", "Wyverin", 158, 32 ) );
    deck.add( new Card( "Fairy", "Xios", 155, 27 ) );
    deck.add( new Card( "Dragon", "Yora", 159, 44 ) );
    deck.add( new Card( "Ghost", "Zulu", 125, 46 ) );
}
public java.lang.String play (java.lang.String action) // lets player enter action swap or attack
{
   if (turnCounter%2==1) //player 1's turn
   {
        if (action.equals("swap")) // if player one enters "swap"
        {
            turnCounter = turnCounter + 1;
            System.out.println("   "+player1.getName() + " swaps out " + player1.getActiveCard().getName()+".");
            player1.swap();

        }
        else if (action.equals("attack")) // if player one enter "attack"
        {
            turnCounter = turnCounter + 1;
            System.out.println("   "+player1.getName() + " attacks with " + player1.getActiveCard().getName()+".");
            dealDamage(player1.getActiveCard(), player2.getActiveCard());

            if (player2.getActiveCard().getHealth()<1) //if player 2's health goes below 1
            {
                player2.discard();
                System.out.println("   "+player2.getName() + " discards " + player2.getActiveCard().getName());
                player2.drawCard(deck.get(0));
                System.out.println(player2.getName() + " draws " + deck.get(0)+".");
                if (deck.size()==0) // if there are no more cards in the deck
                {
                    System.out.println("The deck is empty.");
                }
                player1.claimToken();
                System.out.println("   "+player1.getName() + " gets a token!");
                if (player1.getTokens()==3) //if player1 kills three of player2's cards = 3 tokens
                {
                    hasWinner();
                    System.out.println(player1.getName() + " wins!");
                }
            }
        }
    }
    else if (turnCounter%2==0) //player 2's turn
    {
        if (action.equals("swap")) //if player 2 chooses "swap"
        {
            turnCounter = turnCounter + 1;
            System.out.println("   "+player2.getActiveCard().getName() + " swaps out " + player2.getActiveCard().getName());
            player2.swap();

        }
        else if (action.equals("attack")) //if player two chooses "attack"
        {
            turnCounter = turnCounter + 1;
            System.out.println("   "+player2.getName() + " attacks with " + player2.getActiveCard());
            dealDamage(player2.getActiveCard(), player1.getActiveCard());
            if (player1.getActiveCard().getHealth()<1) //if player 1's health drops below 1
            {
                player1.discard();
                System.out.println("   "+player1.getName() + " discards " + player1.getActiveCard().getName());
                player1.drawCard(deck.get(0));
                System.out.println("   "+player1.getName() + " draws " + deck.get(0)+".");
                if (deck.size()==0) //if there are no more cards in the deck
                {
                    System.out.println("The deck is empty.");
                }
                player2.claimToken();
                System.out.println("   "+player2.getName() + " gets a token!");
                if (player2.getTokens()==3) //if player 2 kills 3 of player1's cards = get three tokens
                {
                    hasWinner();
                    System.out.println(player2.getName() + " wins!");
                }
            }



        }

   }
   return "";
}
public boolean checkResistance(java.lang.String type1, java.lang.String type2) //checks for resistance
{
    if (type1.equals("Dragon")&&type2.equals("Ghost"))
        return true;
    else if (type1.equals("Ghost")&&type2.equals("Fairy"))
        return true; 
    else if (type1.equals("Fairy")&&type2.equals("Dragon"))
        return true;
    else
        return false;
}
public boolean checkWeakness(java.lang.String type1, java.lang.String type2) //checks for weakness
{
    if (type1.equals("Dragon")&&type2.equals("Fairy"))
        return true;
    else if (type1.equals("Fairy")&&type2.equals("Ghost"))
        return true;
    else if (type1.equals("Ghost")&&type2.equals("Dragon"))
        return true;
    else
        return false;
}
public java.lang.String dealCard() //altertately deals cards to each player at the start of the game
{
    if (turnCounter==1)
    {
        for (int i=0; i<5; i++)
        {
            player1.playerHand.add(deck.get(0));
            deck.remove(0);
            System.out.println(player1.getName() + " draws " + player1.playerHand.get(i).getName()+".");
            player2.playerHand.add(deck.get(0));
            deck.remove(0);
            System.out.println(player2.getName() + " draws " + player2.playerHand.get(i).getName()+".");
        }
    }
    else if (turnCounter%2==1) //odd turns = player 1
    {
        if (player1.playerHand.size()==5)
        {
            System.out.println(player1.getName() + "'s hand is full.");
        }
        else if (player1.playerHand.size()<5)
        {
            turnCounter = turnCounter + 1;
            player1.playerHand.add(deck.get(0));
            deck.remove(0);
            System.out.println(player1.getName() + " draws " + player1.playerHand.get(0).getName()+".");
        }
        turnCounter = turnCounter + 1;
    }
    else if (turnCounter%2==0) //even turns = player2
    {
        if (player2.playerHand.size()==5)
        {
            System.out.println(player2.getName() + "'s hand is full.");
        }
        else if (player2.playerHand.size()<5)
        {
            turnCounter = turnCounter + 1;
            player2.playerHand.add(deck.get(0));
            deck.remove(0);
            System.out.println(player2.getName() + " draws " + player2.playerHand.get(0).getName()+".");
        }
        turnCounter = turnCounter;
    }


    return "";
}
public java.lang.String dealDamage(Card inPlay, Card target) //calculates how much damage a card receives, taking into account weakness and resistance
{
    if (checkResistance(target.getType(),inPlay.getType())==true) //if there is resistance
    {
        System.out.println("      "+target.getType() +" is resistant to "+inPlay.getType()+".");
        target.takeHalfDamage(inPlay.getPower());
        checkResistance(inPlay.getType(),target.getType());
        System.out.println("   "+inPlay.getName()+" deals "+inPlay.getPower()/2+" damage on "+target.getName()+".");
        System.out.println("   "+target.getName()+" has "+target.getHealth()+" left.");
    }
    else if (checkWeakness(target.getType(),inPlay.getType())==true) //if there is weakness
    {
        System.out.println("      "+target.getType() +" is weak to "+inPlay.getType()+".");
        target.takeDoubleDamage(inPlay.getPower());
        System.out.println("   "+inPlay.getName()+" deals "+inPlay.getPower()*2+" damage on "+target.getName()+".");
        System.out.println("   "+target.getName()+" has "+target.getHealth()+" left.");
    }
    else //if it does normal damage
    {
        target.takeDamage(inPlay.getPower());
        System.out.println("   "+inPlay.getName()+" deals "+inPlay.getPower()+" damage on "+target.getName()+".");
        System.out.println("   "+target.getName()+" has "+target.getHealth()+" left.");
    }
    return "";
}
public boolean hasWinner() //as long as there is no winner, it is false
{
    return true;
}
public java.lang.String gameReport() //prints out a summary of a game as well as each player's stats
{
    System.out.println("___=== GAME SUMMARY ===___");
    System.out.println("This game lasted "+turnCounter+" turns.");
    System.out.println();
    System.out.println(player1.getName());
    System.out.println("        "+player1.statusReport());
    System.out.println();
    System.out.println(player2.getName());
    System.out.println("        "+player2.statusReport());
    return "";

}    

这是我目前称为GameConsole的跑步者。

import java.util.Scanner;
public class GameConsole
{
    public static void main (String[] args)
    {
    Scanner player = new Scanner(System.in);
    System.out.print("Player 1: ");
    String player1String = player.next(); // input player1 name
    System.out.print("Player 2: "); 
    String player2String = player.next(); // input player2 name
    System.out.println("Welcome, "+player1String+" and "+player2String+"!");
    System.out.println("The game begins.");
    Player player1 = new Player(player1String); // turns player1's String name into an object
    Player player2 = new Player(player2String); // turns player2's Strign name into an object
    System.out.println();
    GameMaster game = new GameMaster(player1,player2); // uses the player1 and player2 objects as objects for the GameMAster
    System.out.println();
    Scanner move = new Scanner(System.in);
    System.out.print("Attack or Swap? ");
    String whatMove = move.next(); //input for action

    //i tried putting this in a while-loop but the action won't even execute once
    game.play(whatMove); // executes play for action
    }
}

当我运行它时,这就是//用户输入在ALL CAPS

中发生的情况
Player 1: ADAM
Player 2: BOB
Welcome, ADAM and BOB!
The game begins.
//space
ADAM draws Aquira.
BOB draws Brawn.
ADAM draws Cerulea.
BOB draws Demi.
ADAM draws Elba.
BOB draws Fye.
ADAM draws Glyede.
BOB draws Hydran.
ADAM draws Ivy.
BOB draws Jet.
//space
Attack or Swap? ATTACK    //SWAP works fine too
   ADAM attacks with Aquira.
      Ghost is weak to Dragon.
   Aquira deals 52 damage on Brawn.
   Brawn has 78 left.
//and then it just stops

我不完全确定我的代码是否有效,但因为我不知道如何重复这个动作,我不知道......我希望我所说的有道理......我是新的到CS所以我会感激任何帮助。

3 个答案:

答案 0 :(得分:1)

你需要某种循环。我建议在代码中使用while循环代表1轮。 (我假设在主方法中播放了1轮)

这取决于您的游戏何时结束,例如,如果您的游戏必须在10轮之后结束,您可以执行以下操作:

while(game.turnCounter <= 10) { // Code for one round goes here };

否则你可以制作一个布尔值来检查游戏是否完成。

while(!game.gameIsFinshed) { // Code for one round goes here };

答案 1 :(得分:0)

当然它只运行一次 - play()命令只在代码中调用一次,在任何循环之外。就像现在一样,代码按预期运行完美。

您需要将其置于循环中,很可能是基于您的要求的while循环。评论说你已经尝试过它并没有用,所以你为什么不显示这些代码呢?这样我们至少可以弄清楚是什么让代码表现得不像预期的那样。

顺便说一下,你所拥有的hasWinner()函数似乎未完成,因为它所做的只是一直返回true。如果你使用while(!hasWinner())作为while条件,当然它根本不会运行。

答案 2 :(得分:0)

用这个替换最后几行:

String whatMove = "QUIT";
do {
    whatMove = move.next(); //input for action
    (whatMove.equalsIgnoreCase("QUIT")) ? break : game.play(whatMove);
} while (true);

这为你提供了一个循环和一个命令选项来结束游戏中的游戏。