面向对象的简单游戏方法

时间:2014-06-12 01:28:47

标签: java

enter image description here

我正在努力构建这个游戏的简化版本。问题表明你可以拥有一台智能或愚蠢的计算机,但我已经决定暂时排除该功能,并决定只选择一台随机选择对象的计算机。我之前发布了一个问题并进行了处理。 (https://softwareengineering.stackexchange.com/questions/244680/object-oriented-design-of-a-small-java-game/244683#244683

所以最初我只设计了一个班级。但是现在我按照问题的规定设计了3个班级。我有一个桩类,一个玩家类和一个游戏类(也是我的主要类)。

我到目前为止编写了Pile和Player类。我开始编写Game类,但是我现在卡住了,因为我不知道如何让游戏类与Player和Pile类交互。我基本上决定轮到谁先转,然后转动直到完成一堆物品并宣布胜利者......我不知道如何让不同的班级互相交流。所以如果有人能够进一步指导我并帮助我完成这个简单的游戏,我将非常感激。

如果我问了一个糟糕的问题,我真诚地道歉。这是我在java中处理多个类的第一个这样的程序,所以它有点令人困惑。我不希望任何人编写代码但是甚至提到或告诉我我应该这样做等等方法会很棒!

这是我已经远远的代码。

PILE CLASS

package Nim;

import java.util.Random;

public class Pile {

    private int initialSize;

    public Pile() {

    }

    Random rand = new Random();

    public void setPile() {

        initialSize = (rand.nextInt(100 - 10) + 10);
    }

    public void reducePile(int x) {

        initialSize = initialSize - x;

    }

    public int getPile() {

        return initialSize;
    }

    public boolean hasStick() {

        if (initialSize > 0) {

            return true;
        }

        else {
            return false;
        }
    }

}

球员类

package Nim;

import java.util.Scanner;
import java.util.Random;

public class Player {

    public static final int HUMAN = 0;
    public static final int COMPUTER = 1;
    private int type;

    public Player(int theType) {

        type = theType;

    }

    Scanner in = new Scanner(System.in);

    // n is number of marbles left in the pile

    public int makeMove(int n) {

        int max = n / 2;
        int grab;

        if (type == HUMAN) {

            System.out.println("There are " + n
                    + " marbles in total. However you can only"
                    + "grab no more than " + max + " marbles");
            System.out.println("Please Enter the number of marbles to grab: ");
            grab = in.nextInt();

            while (grab > max || grab < 0) {

                System.out.println("You have entered a illelgal value. Please enter a legal value: ");
                grab = in.nextInt();

            }

            return grab;

        }

        else {
            Random rand = new Random();

            grab = (rand.nextInt(n / 2 - 1) + 1);

            System.out.println("Computer has grabbed: " + grab + " marbles");

            return grab;

        }
    }

    public void updateTurn(int n) {

        type = n;

    }

}

游戏类别(正在进行中)

package Nim;

import java.util.Scanner;
import java.util.Random;

public class Game {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        Random rand = new Random();

        System.out.println(welcome());

        Pile marbles = new Pile();
        Player human = new Player(Player.HUMAN);
        Player computer = new Player(Player.COMPUTER);
        marbles.setPile();

        System.out.println("There are total: " + marbles.getPile()
                + " marbles in the Pile.");
        System.out.println("Time for a toin coss to see who goes first!");

        System.out.println("Please Select heads(0) or Tails(1): ");
        int choice = in.nextInt();

        int tossResult = rand.nextInt(2);

        boolean playerTurn = false;
        boolean computerTurn = false;

        if (choice == tossResult) {
            System.out.println("You have won the Coin Toss! You will go first!");

            human.updateTurn(0);
            playerTurn = true;

        }

        else {
            System.out.println("Computer has won the coin toss! Computer will go first");

            computer.updateTurn(1);
            computerTurn = true;
        }

        while (marbles.getPile() > 0 && marbles.hasStick()) {

            while (playerTurn) {

                int removeMarbles = human.makeMove(marbles.getPile());

                marbles.reducePile(removeMarbles);
                computerTurn = true;
                playerTurn = false;
            }

            while (computerTurn) {
                int removeMarbles = computer.makeMove(marbles.getPile());
                marbles.reducePile(removeMarbles);

                playerTurn = true;
                computerTurn = false;

            }

        }

    }

    private static String welcome() {

        return "Welcome to the Game of Nim";

    }

}

1 个答案:

答案 0 :(得分:1)

所以我要退一步看看你的班级图。

你有三个类:桩,玩家和游戏。桩可以代表中心的桩。游戏可以是具有管理一切的主要方法的类。和球员?好吧,因为你有三个(或两个)可能状态(人类,愚蠢和智能)的一个Player类,它应该很容易实现。

让我们从游戏开始吧。它需要包含两个Player(人类和计算机)实例和一个Pile实例。它还需要一个循环,直到游戏结束。让我们开始提出一个基本的实现:

从桩开始:

private int size;

public Pile(int newSize) {
    size = newSize;
}

public boolean takeMarbles(int amount) {
    size -= amount;
    if (size < 1)
        return false;
    return true;
}

构造函数非常自我解释。如果玩家输了,takeMarbles方法返回false,如果他们仍然在游戏中,则返回true。

现在开始玩家:

public static final int HUMAN = 0;
public static final int COMPUTER = 1;

private int type;

public Player(int type) {
    this.type = type;
}

这两个静态字段可能看起来有点不合适,但它会在一瞬间汇集在一起​​。在游戏课上:

Pile pile = new Pile(size);
Player human = new Player(Player.HUMAN);
Player computer = new Player(Player.COMPUTER);

现在我们有一堆具有一定规模的球员,以及两名球员因类型而异。这就是我们使用这些类的方式。

现在我们需要让主游戏循环工作。基本上,只需向Player添加一个update()方法,该方法取决于它的类型(即如果播放器提示输入,则随机决定是否计算机)。更新方法应返回要使用的弹珠数量。然后在Game中的main方法中创建一个循环。循环将重复调用播放器和计算机的更新,直到有人输了(takeMarbles返回false)。然后它将跳出循环并打印输出。

这是基本的想法,祝你好运并随意提出更多问题!