简体   繁体   English

如何为我的游戏实现玩家界面?

[英]How do I implement a Player Interface for my game?

I have just implemented a player interface in order to make my program more object oriented. 我刚刚实现了一个播放器界面,以使我的程序更加面向对象。 The game has a computer player and a human player, both of which implement from the player interface. 该游戏有一个计算机玩家和一个人类玩家,两者都可以从玩家界面实现。

However, I cannot work out why the code isn't working. 但是,我无法弄清楚为什么代码不起作用。 When running the program now, I am unable to input a counter onto the board and it only seems to be asking the human player to play (ignoring the computer logic). 现在运行程序时,我无法在板上输入一个计数器,而这似乎只是在要求人类玩家玩(忽略计算机逻辑)。

Here is an image of the issue im experiencing: 这是正在遇到的问题的图像:

在此处输入图片说明

Player.java 播放器

import java.io.IOException;

public interface Player {

    public String getUserInput() throws IOException;
    }

HumanPlayer.Java 人类播放器

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class HumanPlayer implements Player {

    @Override
    public String getUserInput() throws IOException {

        Board Connect4 = new Board();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        outer:
        while(true){

            int boardColumn = 0;

            //Player One Logic ----------------------------------------------------------------

            while(true){
                System.out.println("");
                System.out.println("Player 1, please select your column:");
                boardColumn = Integer.parseInt(br.readLine());

                if(Connect4.canMakeMove(boardColumn)){
                    if(Connect4.placeCounter(boardColumn, 1)){
                        Connect4.printBoard();
                        System.out.println("");
                        System.out.println("Congratulations! Player 1 has won the game");
                        break outer;
                    }
                    break;
                }
                else
                    System.out.println("Column "+boardColumn+" is already full!!");
            }


    }
        return null;

    }   

}

ComputerPlayer.java ComputerPlayer.java

import java.io.IOException;
import java.util.Random;

public class ComputerPlayer implements Player{

    @Override
    public String getUserInput() throws IOException {

        Board Connect4 = new Board();

        outer:
        while(true){
            System.out.println("");
            System.out.println("The Computer has selected a column and played a counter");
            System.out.println("");

            Random r = new Random();
            int num = r.nextInt(7);

            int boardColumn = num;

            if(Connect4.canMakeMove(boardColumn)){
                if(Connect4.placeCounter(boardColumn, 2)){
                    Connect4.printBoard();
                    System.out.println("");
                    System.out.println("Unlucky! The Computer has won this game");
                    break outer;
                }
                break;
            }
            else
                System.out.println("Column "+boardColumn+" is already full!!");
        }

        return null;
    }



}

Main.java Main.java

import java.io.InputStreamReader;
import java.util.Random;
import java.io.BufferedReader;
import java.io.IOException;

public class Main {

    public static void main(String args[])throws IOException{

        Board Connect4 = new Board();


        welcomeMessage();
        Connect4.printBoard();


            HumanPlayer human = new HumanPlayer();
            human.getUserInput();

                Connect4.printBoard();

                //Player Two Logic ---------------------------------------------------------------   

                ComputerPlayer computer = new ComputerPlayer();
                computer.getUserInput();

                Connect4.printBoard();

            }


    public static void welcomeMessage() {
        System.out.println("Welcome to Connect 4");
        System.out.println("There are 2 players red and yellow");
        System.out.println("Player 1 is Red, Player 2 is Yellow");
        System.out.println("To play the game type in the number of the boardColumn you want to drop you counter in");
        System.out.println("A player wins by connecting 4 counters in a row - vertically, horizontally or diagonally");
        System.out.println(""); 
    }

Counter.java Counter.java

public abstract class Counter {

    int widthOfBoard = 7;
    int heightOfBoard = 7;
    int totalPlayed;
    int [][] gameBoard;

    public boolean placeCounter(int boardColumn, int playerNum){
        int i = 0;
        for(i = 0; i<widthOfBoard; i++){
            if(gameBoard[i][boardColumn] == 1 || gameBoard[i][boardColumn] == 2){
                gameBoard[i-1][boardColumn]=playerNum;
                break;
            }
        }
        if(i == widthOfBoard)
            gameBoard[i-1][boardColumn]=playerNum;

        totalPlayed++;
        return isFourConnected(i-1,boardColumn);
    }

    public boolean canMakeMove(int boardColumn){
        return gameBoard[0][boardColumn] == 0; 
    }

    public boolean isFourConnected(int n1, int n2){
        int num=gameBoard[n1][n2];
        int count=0;
        int x = n2;

        // Vertical Logic ----------------------------------------------------
        count=0;
        int j=n1;
        while(j < widthOfBoard && gameBoard[j][n2] == num){
            count++;
            j++;
        }
        if(count == 4)
            return true;

        // Right Diagonal Logic ----------------------------------------------
        count=0;
        x = n1;
        j = n2;
        while(x< widthOfBoard && j < widthOfBoard && gameBoard[x][j] == num){
            count++;
            x++;
            j++;
        }

        if(count == 4)
            return true;

        // Left Diagonal Logic ------------------------------------------------
        count=0;
        x = n1;
        j = n2;
        while(x < widthOfBoard && j >= 0 && gameBoard[x][j] == num){
            count++;
            x++;
            j--;
        }

        if(count == 4)
            return true;

        // Horizontal Logic -------------------------------------------------
        while(x < widthOfBoard && gameBoard[n1][x] == num){
            count++;
            x++;
        }
        x = n2-1;
        while(x >= 0 && gameBoard[n1][x] == num){
            count++;
            x--;
        }
        if(count == 4)
            return true;

        return false;
    }
}

Board.java Board.java

public class Board extends Counter {

    public Board() {
        gameBoard = new int[super.heightOfBoard][super.widthOfBoard];
        super.totalPlayed = 0;
    }

    public void printBoard(){
        for(int i = 0; i<super.gameBoard.length; i++){
            for(int j = 0; j<super.gameBoard[0].length; j++){
                if(super.gameBoard[i][j] == 0)
                    System.out.print(".  ");
                else
                    System.out.print(super.gameBoard[i][j]+"  ");
            }
            System.out.println("");
        }
        System.out.println("--------------------");
        System.out.println("0  1  2  3  4  5  6");
    }


}

break outer; drops you just before while(true) loop which keeps the main thread busy. while(true) loop之前使您while(true) loop ,这使main thread保持繁忙。 Due to this reason, Your code is unable to execute past the line human.getUserInput(); 由于这个原因,您的代码无法执行到human.getUserInput();human.getUserInput(); as it keeps on iterating until the breaking condition hits for human.getUserInput(); 因为它一直在迭代,直到遇到破坏条件为human.getUserInput();为止human.getUserInput();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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