gpt4 book ai didi

java - 从不同的类访问时不执行方法

转载 作者:行者123 更新时间:2023-12-02 05:59:18 25 4
gpt4 key购买 nike

最近,我在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");
}

}

当我运行该程序时,该板显示正常。但是,当我尝试通过选择行号来放置计数器时,没有计数器添加到板上。

有人可以向我解释一下为什么会发生这种情况吗?我已经检查了我的代码很多次,从我所看到的,一切都被正确封装并正确调用。

感谢任何帮助,谢谢

最佳答案

问题是你不断在同一个循环中创建新的 Boards() 并清除旧的。为什么有counter作为棋盘,bop作为棋盘?您打印哪一张?

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

编辑:

不要使用多个 Board 类的实例。请执行下列操作。将这两个语句放在 while 语句上方,如下所示。

      counter = new Board();
counter.printBoard();
while (!win) {

然后删除您的板的所有其他实例,并将对这些板的所有引用替换为计数器。您只需要一个。

关于java - 从不同的类访问时不执行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55987591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com