0
我已經實現了對遊戲tictactoe的最簡單的解決方案,我用gui來表示它。所以我問你是否可以給我一些建議和想法,讓它更加優雅。我試圖改進的部分是TicTacToe類以及mousePressed和mouseEntered方法。這是代碼。tictactoe實現
/**The TicTacToe class with the GUI
*/
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class TicTacToe extends JFrame implements MouseListener
{
JLabel[] jl ={
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()} ;
ImageIcon[] ox = {
new ImageIcon("tic/o.gif"),
new ImageIcon("tic/x.gif")
};
static boolean isFirst;
static int counter = 1;
public TicTacToe()
{
JPanel panel = new JPanel(new GridLayout(3,3));
Border border = new LineBorder(Color.red, 2);
Border border2 = new LineBorder(Color.blue, 2);
for(int i=0; i<jl.length; i++)
{
jl[i].setBorder(border2);
jl[i].addMouseListener(this);
panel.add(jl[i]);
}
panel.setBorder(border);
this.add(panel);
}
public static String count()
{
counter++;
if(counter % 2 != 0)
{
isFirst = true;
return "First";
}
isFirst = false;
return "Second";
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent me)
{
int count = 0;
count();
for(int i=0; i<jl.length; i++)
{
if(jl[0].getIcon() == ox[0] && jl[1].getIcon() == ox[0] && jl[2].getIcon() == ox[0])
{
JOptionPane.showMessageDialog(null,
"Player with 0 won ");
System.exit(0);
}
else if(jl[3].getIcon() == ox[0] && jl[4].getIcon() == ox[0] && jl[5].getIcon() == ox[0])
{
JOptionPane.showMessageDialog(null,
"Player with 0 won ");
System.exit(0);
}
else if(jl[6].getIcon() == ox[0] && jl[7].getIcon() == ox[0] && jl[8].getIcon() == ox[0])
{
JOptionPane.showMessageDialog(null,
"Player with 0 won ");
System.exit(0);
}
else if(jl[0].getIcon() == ox[0] && jl[4].getIcon() == ox[0] && jl[8].getIcon() == ox[0])
{
JOptionPane.showMessageDialog(null,
"Player with 0 won with the major diagonal ");
System.exit(0);
}
else if(jl[2].getIcon() == ox[0] && jl[4].getIcon() == ox[0] && jl[6].getIcon() == ox[0])
{
JOptionPane.showMessageDialog(null,
"Player with 0 won with the subdiagonal ");
System.exit(0);
}
else if(jl[0].getIcon() == ox[0] && jl[3].getIcon() == ox[0] && jl[6].getIcon() == ox[0])
{
JOptionPane.showMessageDialog(null,
"Player with 0 won ");
System.exit(0);
}
else if(jl[1].getIcon() == ox[0] && jl[4].getIcon() == ox[0] && jl[7].getIcon() == ox[0])
{
JOptionPane.showMessageDialog(null,
"Player with 0 won ");
System.exit(0);
}
else if(jl[2].getIcon() == ox[0] && jl[5].getIcon() == ox[0] && jl[8].getIcon() == ox[0])
{
JOptionPane.showMessageDialog(null,
"Player with 0 won ");
System.exit(0);
}
else if(jl[0].getIcon() == ox[1] && jl[1].getIcon() == ox[1] && jl[2].getIcon() == ox[1])
{
JOptionPane.showMessageDialog(null,
"Player with X won ");
System.exit(0);
}
else if(jl[3].getIcon() == ox[1] && jl[4].getIcon() == ox[1] && jl[5].getIcon() == ox[1])
{
JOptionPane.showMessageDialog(null,
"Player with X won ");
System.exit(0);
}
else if(jl[6].getIcon() == ox[1] && jl[7].getIcon() == ox[1] && jl[8].getIcon() == ox[1])
{
JOptionPane.showMessageDialog(null,
"Player with X won ");
System.exit(0);
}
else if(jl[0].getIcon() == ox[1] && jl[4].getIcon() == ox[1] && jl[8].getIcon() == ox[1])
{
JOptionPane.showMessageDialog(null,
"Player with X won with the major diagonal ");
System.exit(0);
}
else if(jl[2].getIcon() == ox[1] && jl[4].getIcon() == ox[1] && jl[6].getIcon() == ox[1])
{
JOptionPane.showMessageDialog(null,
"Player with X won with the subdiagonal ");
System.exit(0);
}
else if(jl[0].getIcon() == ox[1] && jl[3].getIcon() == ox[1] && jl[6].getIcon() == ox[1])
{
JOptionPane.showMessageDialog(null,
"Player with X won ");
System.exit(0);
}
else if(jl[1].getIcon() == ox[1] && jl[4].getIcon() == ox[1] && jl[7].getIcon() == ox[1])
{
JOptionPane.showMessageDialog(null,
"Player with X won ");
System.exit(0);
}
else if(jl[2].getIcon() == ox[1] && jl[5].getIcon() == ox[1] && jl[8].getIcon() == ox[1])
{
JOptionPane.showMessageDialog(null,
"Player with X won ");
System.exit(0);
}
if(me.getSource() == jl[i])
{
if(jl[i].getIcon() == ox[0] || jl[i].getIcon() == ox[1])
{
JOptionPane.showMessageDialog(null,
"You can't insert at " + (i + 1) + " the place is already taken ");
break;
}
if(isFirst)
{
jl[i].setIcon(ox[0]);
}
else
{
jl[i].setIcon(ox[1]);
}
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e)
{
if((jl[0].getIcon() == ox[0] || jl[0].getIcon() == ox[1]) && (jl[1].getIcon() == ox[0] || jl[1].getIcon() == ox[1]) &&
(jl[2].getIcon() == ox[0] || jl[2].getIcon() == ox[1]) && (jl[3].getIcon() == ox[0] || jl[3].getIcon() == ox[1]) &&
(jl[4].getIcon() == ox[0] || jl[4].getIcon() == ox[1]) && (jl[5].getIcon() == ox[0] || jl[5].getIcon() == ox[1]) &&
(jl[6].getIcon() == ox[0] || jl[6].getIcon() == ox[1]) && (jl[7].getIcon() == ox[0] || jl[7].getIcon() == ox[1])
&& (jl[8].getIcon() == ox[0] || jl[8].getIcon() == ox[1]))
{
JOptionPane.showMessageDialog(null,
"Draw");
System.exit(0);
}
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
/** The tester class with the main method and the frame
*/
import javax.swing.JFrame;
public class TestTicTacToe
{
public static void main(String[] args)
{
// JFrame application = new JFrame();
TicTacToe frame = new TicTacToe();
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
}
更適合http://codereview.stackexchange.com – nullpotent 2012-08-08 10:06:43
我不知道這個網站的歡呼聲 – 2012-08-08 10:15:50