Недавно я построил на Java программу для подключения четырех человек.Тем не менее, я построил его процедурно, поэтому теперь я пытаюсь реорганизовать свой код, чтобы убедиться, что он является объектно-ориентированным.
До сих пор мне удалось разделить исходную программу на два разных класса, а именно::
ConnectFour.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ConnectFour {
private BufferedReader input;
public ConnectFour() {
input = new BufferedReader(new InputStreamReader(System.in));
playGame();
}
private String getUserInput(){
String toReturn = null;
try{
toReturn = input.readLine();
}
catch(Exception e){
}
return toReturn;
}
public void playGame() {
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 column 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("");
Board board = new Board();
board.printBoard();
boolean win = false;
while(!win){
// player 1
String userInput = getUserInput();
int move = Integer.parseInt(userInput);
Board counter = new Board();
counter.placeCounter('r', move);
Board bop = new Board();
char[][] boardx = bop.getBoard();
boolean hasWon = false;
int count = 0;
// check horizontal
for(int i=0; i<boardx.length; i++){
for(int j=0; j<boardx[i].length; j++){
if(boardx[i][j] == 'r'){
count = count + 1;
if(count >= 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
// check vertical
count = 0;
for(int i=0; i<boardx[0].length; i++){
for(int j=0; j<boardx.length; j++){
if(boardx[j][i] == 'r'){
count = count + 1;
if(count >= 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
board.printBoard();
if(hasWon){
win = true;
System.out.println("");
System.out.println("R, You Have Won!!!");
}
else{
//player 2
userInput = getUserInput();
move = Integer.parseInt(userInput);
counter.placeCounter('y', move);;
hasWon = false;
count = 0;
// check horizontal
for(int i=0; i<boardx.length; i++){
for(int j=0; j<boardx[i].length; j++){
if(boardx[i][j] == 'y'){
count = count + 1;
if(count >= 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
// check vertical
count = 0;
for(int i=0; i<boardx[0].length; i++){
for(int j=0; j<boardx.length; j++){
if(boardx[j][i] == 'y'){
count = count + 1;
if(count >= 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
board.printBoard();
if(hasWon){
win = true;
System.out.println("");
System.out.println("Y, You Have Won!!!");
}
}
}
}
public static void main(String[] args) {
new ConnectFour();
}
}
Board.java
public class Board {
private char [][] board;
public Board() {
board = new char[6][7];
}
public char[][] getBoard() {
return this.board;
}
public void placeCounter(char player, int position){
boolean placed = false;
if(player == 'r'){
for( int i=board.length-1; i>=0; i--){
if(!placed && board[i - 1][position] != 'r' && board[i - 1][position] != 'y') {
if(board[i][position] == 'y'){
board[i-1][position] = 'r';
placed = true;
}
else if(board[i][position] != 'r'){
board[i][position] = 'r';
placed = true;
}
}
}
}
else if (player == 'y') {
for( int i=board.length-1; i>=0; i--){
if (!placed && board[i - 1][position] != 'r' && board[i - 1][position] != 'y'){
if(board[i][position] == 'r'){
board[i-1][position] = 'y';
placed = true;
}
else if(board[i][position] != 'y'){
board[i][position] = 'y';
placed = true;
}
}
}
}
}
public void printBoard(){
for(int i=0;i<board.length;i++){
for(int j=0;j<board[0].length;j++){
if(board[i][j] == 0)
System.out.print(". ");
else
System.out.print(board[i][j]+" ");
}
System.out.println();
}
System.out.println("* * * * * * *");
System.out.println("0 1 2 3 4 5 6");
}
}
Когда я запускаю программу,плата отображается нормально.Однако, когда я пытаюсь разместить счетчик, выбрав номер строки, счетчик не добавляется на доску.
Может кто-нибудь объяснить мне, почему это происходит?Я проверял свой код много раз, и из того, что я вижу, все правильно инкапсулировано и вызывается правильно.
Спасибо за любую помощь, спасибо