我目前正在为一项作业制定一个tic tac toe程序。我遇到的问题是,当我计算我的玩家时(无论是' X'玩家还是' Y'玩家),它似乎都在计算两个玩家的数量。例如,在第三场比赛之后,它看到了三场比赛,并将其视为胜利者,尽管只完成了一场比赛的两场比赛和另一场比赛的一场比赛。
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;
}
}
我的问题是,是否有人可以查看我的逻辑,看看为什么游戏只允许三个动作。换句话说,O去,X去,然后O去,游戏宣布胜利者。此外,我不是在寻找代码答案,而是我应该检查哪一部分来修复我的代码。
答案 0 :(得分:4)
我想当你给didWin打电话时我只用了一个&#39; &#39;不要以为它真的改变了什么