我有两个类:game.java包含逻辑和控制台,接受用户输入并打印“游戏”状态。
我的问题是,现在游戏的流程正在由用户控制,他是调用游戏类方法的人。 我想在游戏类中实现循环 当我调用placeCard方法时,如何仍能获得用户输入?
将来我想实现图形界面,并且仍然能够在图形界面和控制台之间切换而不必修改游戏类,不确定我要说的是不是很抱歉
播放器类
import java.util.ArrayList;
public class Player {
private ArrayList<Card> hand;
public Player() {
hand = new ArrayList<Card>();
}
public void dealCards() {
hand.add(new Card(1));
hand.add(new Card(2));
hand.add(new Card(3));
}
public ArrayList<Card> getHand() {
return hand;
}
public void setHand(ArrayList<Card> hand) {
this.hand = hand;
}
public int readCard(int card) {
return card;
}
public int readCol(int col) {
return col;
}
public int readRow(int row) {
return row;
}
}
游戏课
import java.util.ArrayList;
public class Game {
private ArrayList<Player> players;
private int[][] board;
public Game() {
players = new ArrayList<Player>();
board = new int[5][5];
}
public void run() {
while (!isGameOver()) {
// Need to execute : place card
switchPlayer();
}
}
public void placeCardOnBoard(int cardAtindex, int row, int col) {
board[row][col] = players.get(0).getHand().get(cardAtindex).getValue();
players.get(0).getHand().remove(cardAtindex);
}
public void switchPlayer() {
Player temp = players.get(0);
players.remove(0);
players.add(temp);
}
public boolean isGameOver() {
if (players.get(0).getHand().isEmpty()) {
return true;
}
return false;
}
public void registerPlayer(Player p) {
players.add(p);
}
}
卡类
import java.util.ArrayList;
public class Player {
private ArrayList<Card> hand;
public Player() {
hand = new ArrayList<Card>();
}
public void dealCards() {
hand.add(new Card(1));
hand.add(new Card(2));
hand.add(new Card(3));
}
public ArrayList<Card> getHand() {
return hand;
}
public void setHand(ArrayList<Card> hand) {
this.hand = hand;
}
public int readCard(int card) {
return card;
}
public int readCol(int col) {
return col;
}
public int readRow(int row) {
return row;
}
}
控制台类
public class Console {
Game game;
public Console(Game game) {
this.game = game;
}
// ...
}
答案 0 :(得分:0)
这里是一个与一名玩家进行测试的示例,需要更详细地说明注册玩家的过程才能与多个玩家一起工作
readelf
游戏类
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Console {
public static void main(String[] args) {
// display a user friendly console
// build a player for this user
// find a game
// register the player to the game
// for testing purpose the console builds a player, builds the game, registers the player to the game and starts the game
Player player1 = new Player(new Scanner(System.in), System.out);
Game game = new Game();
game.registerPlayer(player1);
game.run();
}
}
玩家职业
class Game {
private static final int MAX_NUMBER_OF_PLAYERS = 5;
private static final int NUMBER_OF_ROUNDS_IN_A_GAME = 3;
private final List<Player> players;
public Game() {
this.players = new ArrayList<>();
}
public void run() {
int roundsCounter = 0;
while (roundsCounter < NUMBER_OF_ROUNDS_IN_A_GAME) {
for (Player currentPlayer : this.players) {
CardWithColAndRow cardWithColAndRow = currentPlayer.playRound(roundsCounter);
placeCardOnBoard(cardWithColAndRow.getCard(), cardWithColAndRow.getRow(), cardWithColAndRow.getCol());
}
roundsCounter++;
}
end();
}
public void placeCardOnBoard(int cardAtindex, int row, int col) {
// logic for placing card
}
public boolean registerPlayer(Player player) {
if (this.players.size() == MAX_NUMBER_OF_PLAYERS) {
return false;
}
this.players.add(player);
return true;
}
public void end() {
this.players.stream().forEach(player -> player.displayMessage("Game ended"));
}
}
CardWithColAndRow类
class Player {
private Scanner inputScanner;
private PrintStream ps;
public Player(Scanner inputScanner, PrintStream ps) {
this.inputScanner = inputScanner;
this.ps = ps;
}
public CardWithColAndRow playRound(int roundNumber) {
this.displayMessage(String.format("Play round %s", roundNumber + 1));
this.displayMessage("Type in the card: ");
int card = inputScanner.nextInt();
this.displayMessage("Type in the column: ");
int col = inputScanner.nextInt();
this.displayMessage("Type in the row: ");
int row = inputScanner.nextInt();
return new CardWithColAndRow(card, col, row);
}
public void displayMessage(String message) {
ps.println(message);
}
}