Tôi hiện đang làm việc trên một chương trình toe tic tac cho một nhiệm vụ. Vấn đề tôi gặp phải là khi tôi đếm số cầu thủ của mình (hoặc là cầu thủ 'X' hoặc cầu thủ 'Y'), có vẻ như cả hai người chơi đều giống nhau. Ví dụ, sau vở kịch thứ ba, nó thấy ba vở kịch và đếm nó như một người chiến thắng, mặc dù chỉ có hai vở kịch của một người chơi và một vở kịch của người chơi khác đã được hoàn thành.Biên dịch vấn đề?
public class TicTacToeApp
{
public static void main(String[] args)
{
TicTacToeView view = new TicTacToeView();
TicTacToeModel model = new TicTacToeModel();
TicTacToeViewController controller = new
TicTacToeViewController(view,model);
view.setVisible(true);
}
}
public class TicTacToeModel
{
double xpos,ypos,xr,yr;
char[][] position = {{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '}};
public void computePos(int row, int col, int h, int w)
{
xpos=(col+0.5)*w/3.0;
ypos=(row+0.5)*h/3.0;
xr=w/8.0;
yr=h/8.0;
}
public boolean isEmpty(int xpos, int ypos)
{
if(position[xpos][ypos]==' ')
return true;
return false;
}
public void placeO(int xpos, int ypos)
{
position[xpos][ypos]='O';
}
public int putX(){
for(int i=0; i<3;i++){
for(int j = 0;j<3;j++) {
if(position[i][j]==' ') {
position[i][j]='X';
return 0;
}
}
}
return -1; //some error occurred. This is odd. No cells were free.
}
public void printBoard(){
for(int i=0;i<3;i++)
System.out.println(position[i][0]+"|"+position[i][1]+"|"+position[i][2]);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.ArrayList;
public class TicTacToeView extends JFrame{
private JButton oButton, xButton;
public JPanel board;
public ArrayList<Shape> shapes;
public TicTacToeView(){
shapes = new ArrayList<Shape>();
JPanel topPanel=new JPanel();
topPanel.setLayout(new FlowLayout());
add(topPanel, BorderLayout.NORTH);
add(board=new Board(), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
}
private class Board extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w=getWidth();
int h=getHeight();
Graphics2D g2d = (Graphics2D) g;
// Draw the grid
g2d.setPaint(Color.WHITE);
g2d.fill(new Rectangle2D.Double(0, 0, w, h));
g2d.setPaint(Color.BLACK);
g2d.setStroke(new BasicStroke(4));
g2d.draw(new Line2D.Double(0, h/3, w, h/3));
g2d.draw(new Line2D.Double(0, h*2/3, w, h*2/3));
g2d.draw(new Line2D.Double(w/3, 0, w/3, h));
g2d.draw(new Line2D.Double(w*2/3, 0, w*2/3, h));
//draw circles and xs by visiting elements in the array List.
for(Shape shape : shapes){
g2d.setPaint(Color.BLUE);
g2d.draw(shape);
}
}
}
public void addMouseListener(MouseListener ml){
board.addMouseListener(ml);
}
public static void main(String[] args) {
TicTacToeView ttv = new TicTacToeView();
ttv.setVisible(true);
}
}
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.Graphics2D;
import java.awt.Color;
import javax.swing.JOptionPane;
public class TicTacToeViewController implements MouseListener{
TicTacToeView view;
TicTacToeModel model;
Color oColor=Color.BLUE, xColor=Color.RED;
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public TicTacToeViewController(TicTacToeView view, TicTacToeModel model) {
this.view = view;
this.model = model;
view.addMouseListener(this);
}
public void play(int xpos, int ypos) {
if (model.isEmpty(xpos,ypos)) {
model.placeO(xpos, ypos);
drawBoard();
view.repaint();
model.putX();
if(didWin("X")){
JOptionPane.showMessageDialog(null,"X Wins","Winner", JOptionPane.INFORMATION_MESSAGE);
}
if(didWin("O"))
JOptionPane.showMessageDialog(null,"O Wins","Winner",JOptionPane.INFORMATION_MESSAGE);
}
}
public void drawBoard() {
Graphics2D g2d = (Graphics2D)view.board.getGraphics();
for (int i=0; i<3; i++)
for(int j=0; j<3;j++) {
model.computePos(i,j,view.board.getHeight(),view.board.getWidth());
double xpos = model.xpos;
double xr = model.xr;
double ypos = model.ypos;
double yr = model.yr;
if (model.position[i][j]=='O') {
view.shapes.add(new Ellipse2D.Double(xpos-xr, ypos-yr, xr*2, yr*2));
}
else if (model.position[i][j]=='X') {
view.shapes.add(new Line2D.Double(xpos-xr, ypos-yr, xpos+xr, ypos+yr));
view.shapes.add(new Line2D.Double(xpos-xr, ypos+yr, xpos+xr, ypos-yr));
}
System.out.println("Coords: xpos:"+xpos+", ypos:"+ypos+", xr"+xr+", yr"+yr);
}
}
public void mouseClicked(MouseEvent e) {
int ypos=e.getX()*3/view.getWidth();
int xpos=e.getY()*3/view.getHeight();
//System.out.println("Play "+xpos+","+ypos);
play(xpos,ypos);
}
public boolean didWin(char player) {
int count = 0;
int count2 = 0;
for(int i = 0; i<3; i++)
{
for(int j= 0; j<3; j++)
{
if(model.position[i][j]==player)
{
count++;
if(count ==2)
return true;
}
}
count=0;
}
for(int k = 0; k<3; k++)
{
for(int l= 0; l<3; l++)
{
if(model.position[l][k]==player)
{
count2++;
if(count2 ==2)
return true;
}
}
count2=0;
}
if(model.position[0][0]==player && model.position[1][1]==player && model.position[2][2]==player)
return true;
if(model.position[0][2]==player && model.position[1][1]==player && model.position[2][0]==player)
return true;
return false;
}
}
Câu hỏi của tôi là nếu ai đó có thể xem qua logic của tôi và xem tại sao trò chơi chỉ cho phép ba di chuyển. Nói cách khác, O đi, X đi, sau đó O đi, và trò chơi thông báo một người chiến thắng. Ngoài ra, tôi không tìm kiếm một câu trả lời mã, mà là phần mà tôi nên kiểm tra để sửa mã của tôi.
Bạn không yêu cầu một "câu trả lời mã" nhưng bạn đang yêu cầu một câu trả lời gỡ lỗi. Hãy chắc chắn rằng mỗi lớp và chức năng làm những gì bạn nghĩ rằng họ làm độc lập - không phải bằng cách đọc chúng một lần nữa vì tôi chắc chắn bạn đã thực hiện nhiều lần, nhưng bằng cách kiểm tra chúng. Bạn không phải chính thức kiểm tra chúng; chỉ cần viết một đoạn mã nhỏ sử dụng từng phần, từng phần một và đảm bảo nó hoạt động tốt. – Fabian