将程序代码转换为面向对象的技巧

时间:2019-05-05 12:23:43

标签: java eclipse oop

我一直在以程序方式构建“四连环”游戏。有人可以就如何将这段代码转换为面向对象的状态提出一些建议吗?这将是我第一次尝试面向对象,因此,希望能获得任何建议/提示。

我知道我需要将代码分成不同的类以使其成为OO,因此我正在考虑创建Board和Main类。这足够了还是我应该考虑更多的课程?

我一直在阅读有关界面的内容,也正在考虑为游戏的玩家(人类玩家和计算机玩家)使用一种界面,但是我不确定做到这一点的最佳方法。

ConnectFourGame.java

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

public class ConnectFourGame {

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

public ConnectFourGame(){
    gameBoard = new int[widthOfBoard][widthOfBoard];
    totalPlayed=0;
}

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

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

    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("");
    Connect4.printBoard();
    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("\n\nPlayer 1 wins!!!");
                    break outer;
                }
                break;
            }
            else
                System.out.println("Column "+boardColumn+" is already full!!");
        }
        Connect4.printBoard();

        //PLAYER 2.    
        while(true){
        System.out.println("");
        System.out.println("Player 2, please select your column");

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

        boardColumn=num;


            if(Connect4.canMakeMove(boardColumn)){
                if(Connect4.placeCounter(boardColumn, 2)){
                    Connect4.printBoard();
                    System.out.println("\n\nPlayer 2 wins!!!");
                    break outer;
                }
                break;
            }
            else
                System.out.println("Column "+boardColumn+" is already full!!");
        }
        Connect4.printBoard();

        if(Connect4.gameTied()){
            System.out.print("The game has ended in a draw. Please start the game again.");
            break;
        }
    }
}

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

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 isConnected(i-1,boardColumn);
}

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

public boolean gameTied(){
    return totalPlayed == widthOfBoard*widthOfBoard;
}

public void isHorizontal() {

}

public boolean isConnected(int x, int y){
    int num=gameBoard[x][y];
    int count=0;
    int i=y;

    //HORIZONTAL.
    while(i<widthOfBoard && gameBoard[x][i] == num){
        count++;
        i++;
    }
    i=y-1;
    while(i>=0 && gameBoard[x][i] == num){
        count++;
        i--;
    }
    if(count == 4)
        return true;

    //VERTICAL.
    count=0;
    int j=x;
    while(j<widthOfBoard && gameBoard[j][y] == num){
        count++;
        j++;
    }
    if(count == 4)
        return true;

    //SECONDARY DIAGONAL.
    count=0;
    i=x;
    j=y;
    while(i<widthOfBoard && j<widthOfBoard && gameBoard[i][j] == num){
        count++;
        i++;
        j++;
    }

    if(count == 4)
        return true;

    //LEADING DIAGONAL.
    count=0;
    i=x;
    j=y;
    while(i<widthOfBoard && j>=0 && gameBoard[i][j] == num){
        count++;
        i++;
        j--;
    }

    if(count == 4)
        return true;

    return false;
}
}

2 个答案:

答案 0 :(得分:0)

Board类将是一个不错的开始。它可能具有将光盘放入具有特定颜色的列中的方法。

Player类可以跟踪播放器的名称和颜色。

两种颜色的Color枚举。

Game类可以跟踪哪些Player以及谁来了,并且可以检查董事会是赢还是平。

游戏

private Board board;
private Player[] players;
private Player currentPlayer;

void reset();  // Resets game
Player getCurrentPlayer();
Player isGameWon();  // Returns player that won, or null if nobody won
boolean isGameOver();  // Returns true if no more moves can be made

// Places a disc, throws exception if it is not players turn.
// This delegates to the Board class, and advances the turn.
void placeDisc(Player player, int column);        

董事会

private Color[][] squares;

void clear();
void placeDisc(Color color, int column);  // Place a disc, throws exception if column full
boolean isColumnFull(int column);  // Could come in handy...
Color getDiscAt(int x, int y);  // Returns coloe of disc at location, or null if none

颜色

An enum (RED, YELLOW) ?

public enum Color {RED, YELLOW}

播放器

private String name;
private Color color;

String getName();
Color getColor();

答案 1 :(得分:0)

您应该在流程的初始阶段考虑OOD。编写代码后,其余过程为refactoring。我没有看到代码写为OO,因为您试图从方法中删除static修饰符。如果不创建其他方法,则考虑从也将属于的方法中将公共属性聚合到该类。首先创建对象,然后调用方法。