gpt4 book ai didi

java - Java Tic-tac-toe 中的 CheckTie 函数不起作用

转载 作者:行者123 更新时间:2023-12-01 09:57:23 24 4
gpt4 key购买 nike

所以我在使 CheckTie 方法正常工作时遇到问题,我尝试使用 boolean 值使其正常工作,但它只是宣布当我按下第一个按钮时有平局。我确实有一个方法可以检查游戏板是否已满,但当我尝试让我的 View 类实现它时似乎存在一些问题。

它至少正确地显示了获胜者。

我的 checkTie 方法做错了什么?

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class Model extends Observable implements ModelInterface {
private int[][] board = new int[3][3];

private int player;
private List<Observer> observers;
private char winner;
private boolean tie;

public Model(){

player =1;
this.observers=new ArrayList<>();
reset();
}
public int [][] getBoard(){
return board;
}


@Override
public void movePlayer(String input) {
int tmp = Integer.parseInt(input);
int x = tmp %3;
int y = tmp /3;
updateBoard(x,y);
checkWin();
checkTie();
}
private void updateBoard(int x, int y) {
if ( board[x][y] == 0){
board[x][y] = player;
if (player == 1)
player = 2;
else
player = 1;
}
this.setChanged();
this.notifyObservers(this);
}
private void checkWin(){

if(board[0][0] != 0 && board[0][0] == board[1][1] && board[1][1] == board[2][2]){
winnerFound(board[0][0]);
}
else if(board[0][2] != 0 && board[0][2] == board[1][1] && board[1][1] == board[2][0]){
winnerFound(board[0][2]);
}
else{
for(int i = 0; i <3; i++){
if(board[i][0] != 0 && board[i][0] == board[i][1] && board[i][1] == board[i][2]){
winnerFound(board[i][0]);
break;
}
if(board[0][i] != 0 && board[0][i] == board[1][i] && board[1][i] == board[2][i]){
winnerFound(board[0][i]);
break;

}
}
}


}


private void winnerFound(int board){
this.winner= (char) board;
reset();


}

private boolean checkTie(){
boolean tie = true;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(board[i][j] == 0)
{


tie = true;
}

}
}
return tie;
}


public int isThereAWinner() {
if (winner ==1)
return 1;
return winner;

}


public boolean isThereATie() {

if (tie = false)
return false;
else
return true;
}



@Override
public void register(Observer obj) {
if (obj == null) throw new NullPointerException();
if(!observers.contains(obj)) observers.add(obj);

}



private void reset() {
// for(JButton label : board){
// label.setText("");
// }
// panel.repaint();
setChanged();
this.notifyObservers();
}
@Override
public void unregister(Observer obj) {
}

@Override
public Object getUpdate(Observer obj) {
return null;
}

}

这是 View 类:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

@SuppressWarnings("serial")
public class View extends JFrame implements ViewInterface, Observer {
Controller controller;
JPanel panel;
ArrayList<JButton> board = new ArrayList();
JPanel messagePanel;
JLabel topText;
JPanel sidePanel;
JPanel westPanel;
JTextField Spelare;
JButton LäggTill, Exit;
JButton test;
JList lista;
DefaultListModel model; //Lista för spelare
//private String letter = " ";

// private int count = 0;
// private char winner;

public View(Controller controller){
super();
this.controller = controller;
setupUI();
}

private void setupUI(){
setupFrame();
setupMessagePanel();
setupPanel();

setupBoard();
setupPlayers();
setupLäggTill();

this.getContentPane().add(panel);
}

private void setupFrame(){
setSize(400,400);
setTitle("Tic-Tac-Toe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
}

private void setupMessagePanel(){
messagePanel = new JPanel();
topText = new JLabel("X startar");
messagePanel.add(topText);
this.getContentPane().add(messagePanel, BorderLayout.SOUTH);
}

private void setupPlayers(){
sidePanel = new JPanel();
westPanel = new JPanel();
Spelare = new JTextField("Skriv ditt namn", 9);
Spelare.addActionListener(controller);
model = new DefaultListModel(); //DefaultListModel för att lägga till personer
lista = new JList(model);
lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
lista.setLayoutOrientation(JList.VERTICAL);
lista.setVisibleRowCount(7);

lista.setPreferredSize(new Dimension(50,200));

sidePanel.add(Spelare);
westPanel.add(lista);

this.getContentPane().add(sidePanel, BorderLayout.NORTH);
this.getContentPane().add(westPanel, BorderLayout.WEST);
}



private void setupLäggTill()
{
LäggTill = new JButton("Lägg till");
Exit = new JButton("Exit");
LäggTill.setActionCommand("Lägg till");
LäggTill.addActionListener(controller);
Exit.setActionCommand("Exit");
Exit.addActionListener(controller);
sidePanel.add(LäggTill);
messagePanel.add(Exit);

this.getContentPane().add(sidePanel, BorderLayout.NORTH);

}
private void setupPanel(){
panel = new JPanel();


panel.setLayout(new GridLayout(3, 3, -1, -1));
}

private void setupBoard(){
for(int i = 0; i < 9; i++){
JButton symbol = new JButton();
symbol.setText("");
symbol.setSize(new Dimension(400/3,400/3));
symbol.setHorizontalAlignment(JLabel.CENTER);
symbol.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2,Color.RED));


symbol.addActionListener(controller);
symbol.setActionCommand(String.valueOf(i));
board.add(symbol);
panel.add(symbol);


}
}


@Override
public void gameTie() {

giveMessage("Oavgjort", "Otur, ingen vann");
reset();

}

@Override
public void gameWon(char winner) {

giveMessage("Spel klart", winner + " vann!");
reset();
}

private void giveMessage(String header, String message){

JOptionPane.showMessageDialog(this,
message,
header,
JOptionPane.PLAIN_MESSAGE);

}

private void reset() {
for(JButton label : board){
label.setText("");
}
panel.repaint();
}

public void update(Observable o, Object arg) {
Model model_copy = (Model)o;
int[][] test = model_copy.getBoard();
int winner = model_copy.isThereAWinner();
boolean tie = model_copy.isThereATie();

int index = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if(test[j][i] == 1)
{
this.board.get(index).setText("X");

}
else if (test[j][i] == 2)
{

this.board.get(index).setText("O");
}
// else if(test[j][i]==3)
// {
// this.board.get(index).setEnabled(false);
// }
index ++;
}
}



if (winner ==1)
{
giveMessage("Spel klart", "X" + " vann!");
reset();

}
else if (winner ==2)
{
giveMessage("Spel klart", "O" + " vann!");
reset();
}
else if (tie ==false)
{
giveMessage("Oavgjort", "Otur, ingen vann");
}

}

}

最佳答案

您的情况必须符合以下条件。否则,由于您始终返回 true,因此始终存在平局。如果我们仍然有一个带有 0 的单元格,则意味着游戏尚未结束,不可能是平局。

if(board[i][j] == 0) {
tie = false;
}
<小时/>
private boolean checkTie(){
boolean tie = true;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(board[i][j] == 0) {
tie = false;
}

}
}
return tie;
}

关于java - Java Tic-tac-toe 中的 CheckTie 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37088817/

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