简体   繁体   中英

Implementing a game loop and ask for user input

I have two classes : game.java contains the logic and console take user input and print the "game" state.

My issue is that, right now the flow of the game is being controlled by the user, he is the one calling the method of the game class. I'd like to implement the loop inside the game class however when I call the placeCard method, how can I still get the user input ?

In the future I'd like to implement a graphical interface, and still be able to switch between graphical interface and console without having to modify the game class, not sure if what I'm saying is clear sorry

Player class

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;
    }

}

Game class

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);
    }
}

Card class

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;
    }

}

Console class

public class Console {

    Game game;

    public Console(Game game) {
        this.game = game;
    }

    // ...

}

Here is an example for testing with one player, the register player process needs to be more elaborated to work with multiple players

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();
    }
}

the game class

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"));
    }

}

the player class

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);
    }
}

The CardWithColAndRow class

class CardWithColAndRow {

    private int card;
    private int col;
    private int row;

    public CardWithColAndRow(int card, int col, int row) {
        this.card = card;
        this.col = col;
        this.row = row;
    }

    public int getCard() {
        return card;
    }

    public int getCol() {
        return col;
    }

    public int getRow() {
        return row;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM