对象的实例超过对象java

时间:2018-09-16 18:41:52

标签: java

这是用于学校项目的在此版本的纸牌游戏战争中,当出现平局时,一张纸牌面朝下,但是我的问题出现在playerTwo的前30行代码中-删除自身并将其复制到playerOne上,以便在我测试代码时将playerOne和playerTwo连续“捆绑”。

public class War {
public static WarQueue playerTwo;
public static WarQueue playerOne;
public static void main(String[] args) throws FileNotFoundException {
    Scanner temp = new Scanner(new File("war.txt"));
     {
        /*String one = temp.nextLine();
        String two = temp.nextLine();*/

        String[] a = temp.nextLine().split(" ");
         playerOne = new WarQueue(a);

        String[] b = temp.nextLine().split(" ");
         playerTwo = new WarQueue(b);
        for (int i = 0; i < 26; i++) {
            playerOne.enqueue(a[i]);


        }
        System.out.println(playerOne.toString());
        for (int i = 0; i < 26; i++) {
            playerTwo.enqueue(b[i]);

        }
        System.out.println(playerOne.toString());
        System.out.println(playerTwo.toString());
        System.out.println(playerTwo.peek());
        System.out.println(playerOne.peek());
        System.out.println(playerOne.peek() + " " + playerTwo.peek());
        int hand = hand(playerOne.peek(), playerTwo.peek());
        if (hand == 1) {
            playerOne.enqueue(playerOne.dequeue());
            playerOne.enqueue(playerTwo.dequeue());
            System.out.println(playerOne.peek());
        } else if (hand == 2) {
            playerTwo.enqueue(playerTwo.dequeue());
            playerTwo.enqueue(playerOne.dequeue());
            System.out.println(playerTwo.peek());
        } else {
            ArrayList<String> warCards = new ArrayList<>();

            String first = null;
            String second = null;
            int win = 0;
            do {

                String down1 = playerOne.dequeue();
                String down2 = playerTwo.dequeue();

                first = playerOne.dequeue();
                second = playerTwo.dequeue();

                warCards.add(down1);
                warCards.add(down2);

                warCards.add(first);
                warCards.add(second);

                win = hand(first, second);
            } while (win == 10);

            if (win == 1) {
                for (String card : warCards) {
                    playerOne.enqueue(card);
                }
            } else if (win == 2) {
                for (String card : warCards) {
                    playerTwo.enqueue(card);
                }
            }

            warCards.clear();
        }
        if (playerOne.size() == 52) { 
            System.out.println("player 1 wins");
        } else {
            System.out.println("player 2 wins");
        }
    } while (temp.hasNextLine());

}
static int hand(String one, String two) {
    String[] card = one.split("");
    String[] card2 = two.split("");
    int cardOne = 0;
    int cardTwo = 0;
    int win = 0;
    switch (card[0]) {
        case "T":
            cardOne = 10;
            break;
        case "J":
            cardOne = 11;
            break;
        case "Q":
            cardOne = 12;
            break;
        case "K":
            cardOne = 13;
            break;
        case "A":
            cardOne = 14;
            break;
        case "2":
            cardOne = 2;
            break;
        case "3":
            cardOne = 3;
            break;
        case "4":
            cardOne = 4;
            break;
        case "5":
            cardOne = 5;
            break;
        case "6":
            cardOne = 6;
            break;
        case "7":
            cardOne = 7;
            break;
        case "8":
            cardOne = 8;
            break;
        case "9":
            cardOne = 9;
            break;

    }
    switch (card2[0]) {
        case "T":
            cardTwo = 10;
            break;
        case "J":
            cardTwo = 11;
            break;
        case "Q":
            cardTwo = 12;
            break;
        case "K":
            cardTwo = 13;
            break;
        case "A":
            cardTwo = 14;
            break;
        case "2":
            cardTwo = 2;
            break;
        case "3":
            cardTwo = 3;
            break;
        case "4":
            cardTwo = 4;
            break;
        case "5":
            cardTwo = 5;
            break;
        case "6":
            cardTwo = 6;
            break;
        case "7":
            cardTwo = 7;
            break;
        case "8":
            cardTwo = 8;
            break;
        case "9":
            cardTwo = 9;
            break;

    }
    if (cardOne > cardTwo)
        win = 1;
    if (cardTwo > cardOne)
        win = 2;
    if (cardOne == cardTwo)
        win = 10;
    return win;
}

}

这是我的WarQueue类

public class WarQueue {
/**
 * Created by 148439 on 9/4/2018.
 */

    public static String[] ringBuffer;
    int front = 0;
    int size = 0;

    WarQueue(String[] a )
    {
        ringBuffer = new String[a.length];
    }// create an empty ring buffer, with given max capacity
    int size()
    {
        return size;
    }// return number of items currently in the buffer
    boolean isEmpty()
    {
        return size() == 0;
    }// is the buffer empty (size equals zero)?
    boolean isFull()
    {
        return size() == ringBuffer.length;
    }
    // buffer full (size equals capacity)?
    void enqueue(String b)
    {
        if (isFull()) {
            return;
        }
        ringBuffer[(front + size()) % ringBuffer.length] = String.valueOf(b);
        size++;
    }// add item x to the end
    String dequeue()
    {
        if (isEmpty())
            return null;
        String pos = ringBuffer[front];
        front = (front + 1) % ringBuffer.length;
        size--;
        return pos;


    }// delete and return item from the front

    String peek()
    {
        if (isEmpty())
        {
            return  null;
        }
        return ringBuffer[front];
    }

    void empty() {
        ringBuffer = new String[ringBuffer.length];
        front = 0;
        size = 0;
    }
public String toString()
{

    String str = "";

    for(int i = 0; i < ringBuffer.length; i++)
    {

        str = str + " " + ringBuffer[i];

    }

    return str;

}
}

此代码显示出playerOne和playerTwo相同,但是在自己的for循环中进行测试时,它们是不同的,直到第二个for循环结束为止。

0 个答案:

没有答案