谁能给我个java源代码
- 提问者网友:做自己de王妃
- 2021-04-05 06:08
- 五星知识达人网友:往事隔山水
- 2021-04-05 07:30
import java.awt.*;
import javax.swing.border.LineBorder;
import java.awt.event.*;public class TicTacToe extends JApplet
{
private char whoseTurn='X';
private Cell[][] cell=new Cell[3][3];
private JLabel jlblStatus=new JLabel("X's turn to play");
public void init()
{
JPanel p=new JPanel();
p.setLayout(new GridLayout(3,3,0,0));
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
p.add(cell[i][j]=new Cell()); p.setBorder(new LineBorder(Color.red,1));
jlblStatus.setBorder(new LineBorder(Color.yellow,1));
this.getContentPane().add(p,BorderLayout.CENTER);
this.getContentPane().add(jlblStatus,BorderLayout.SOUTH);
}
public void start()
{}
public static void main(String[] args)
{
JFrame frame=new JFrame("TicTacToe");
TicTacToe applet=new TicTacToe();
frame.getContentPane().add(applet,BorderLayout.CENTER);
applet.init();
applet.start();
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation(
(screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2); frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public boolean isFull()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(cell[i][j].getToken()==' ')
return false;
return true;
}
public boolean isWin(char token)
{
for(int i=0;i<3;i++)
if((cell[i][0].getToken()==token)
&&(cell[i][1].getToken()==token)
&&(cell[i][2].getToken()==token))
{
return true;
}
for(int j=0;j<3;j++)
if((cell[0][j].getToken()==token)
&&(cell[1][j].getToken()==token)
&&(cell[2][j].getToken()==token))
{
return true;
}
if((cell[0][0].getToken()==token)
&&(cell[1][1].getToken()==token)
&&(cell[2][2].getToken()==token))
{
return true;
}
if((cell[0][2].getToken()==token)
&&(cell[1][1].getToken()==token)
&&(cell[2][0].getToken()==token))
{
return true;
}
return false;
}
public class Cell extends JPanel implements MouseListener
{
private char token=' ';
public Cell()
{
setBorder(new LineBorder(Color.black,1));
addMouseListener(this);
}
public char getToken()
{
return token;
}
public void setToken(char c)
{
token=c;
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(token=='X')
{
g.drawLine(10,10,getSize().width-10,getSize().height-10);
g.drawLine(getSize().width-10,10,10,getSize().height-10);
}
else if(token=='O')
{
g.drawOval(10,10,getSize().width-20,getSize().height-20);
}
}
public void mouseClicked(MouseEvent e)
{
if(token==' ')
{
if(whoseTurn=='X')
{
setToken('X');
whoseTurn='O';
jlblStatus.setText("O's turn");
if(isWin('X'))
{
JOptionPane.showMessageDialog(this,"X won! ","提示",
JOptionPane.YES_NO_CANCEL_OPTION);
jlblStatus.setText("X won! The game is over");
}
else if(isFull())
{
JOptionPane.showMessageDialog(this,"The game is over","提示",
JOptionPane.INFORMATION_MESSAGE);
jlblStatus.setText("Draw! The game is over");
}
}
else if(whoseTurn=='O')
{
setToken('O');
whoseTurn='X';
jlblStatus.setText("X's turn");
if(isWin('O'))
{
JOptionPane.showMessageDialog(this, "Y won! ","提示",
JOptionPane.INFORMATION_MESSAGE);
jlblStatus.setText("O won! The game is over");
}
else if(isFull())
{
JOptionPane.showMessageDialog(this,"The game is over","提示",
JOptionPane.INFORMATION_MESSAGE);
jlblStatus.setText("Draw! The game is over");
}
}
}
}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
}
}//一个简单JAVA #字三关 的游戏代码
- 1楼网友:酒醒三更
- 2021-04-05 08:37
- 2楼网友:平生事
- 2021-04-05 07:42
import javax.swing.*; import java.awt.*; import javax.swing.border.lineborder; import java.awt.event.*;
public class tictactoe extends japplet { private char whoseturn='x'; private cell[][] cell=new cell[3][3]; private jlabel jlblstatus=new jlabel("x's turn to play"); public void init() { jpanel p=new jpanel(); p.setlayout(new gridlayout(3,3,0,0)); for(int i=0;i<3;i++) for(int j=0;j<3;j++) p.add(cell[i][j]=new cell());
p.setborder(new lineborder(color.red,1)); jlblstatus.setborder(new lineborder(color.yellow,1)); this.getcontentpane().add(p,borderlayout.center); this.getcontentpane().add(jlblstatus,borderlayout.south); } public void start() {} public static void main(string[] args) { jframe frame=new jframe("tictactoe"); tictactoe applet=new tictactoe(); frame.getcontentpane().add(applet,borderlayout.center); applet.init(); applet.start(); //center the window dimension screensize = toolkit.getdefaulttoolkit().getscreensize(); dimension framesize = frame.getsize(); if (framesize.height > screensize.height) { framesize.height = screensize.height; } if (framesize.width > screensize.width) { framesize.width = screensize.width; } frame.setlocation( (screensize.width - framesize.width) / 2, (screensize.height - framesize.height) / 2);
frame.setsize(300,300); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } public boolean isfull() { for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(cell[i][j].gettoken()==' ') return false; return true; } public boolean iswin(char token) { for(int i=0;i<3;i++) if((cell[i][0].gettoken()==token) &&(cell[i][1].gettoken()==token) &&(cell[i][2].gettoken()==token)) { return true; } for(int j=0;j<3;j++) if((cell[0][j].gettoken()==token) &&(cell[1][j].gettoken()==token) &&(cell[2][j].gettoken()==token)) { return true; } if((cell[0][0].gettoken()==token) &&(cell[1][1].gettoken()==token) &&(cell[2][2].gettoken()==token)) { return true; } if((cell[0][2].gettoken()==token) &&(cell[1][1].gettoken()==token) &&(cell[2][0].gettoken()==token)) { return true; } return false; } public class cell extends jpanel implements mouselistener { private char token=' '; public cell() { setborder(new lineborder(color.black,1)); addmouselistener(this); } public char gettoken() { return token; } public void settoken(char c) { token=c; repaint(); } public void paintcomponent(graphics g) { super.paintcomponent(g); if(token=='x') { g.drawline(10,10,getsize().width-10,getsize().height-10); g.drawline(getsize().width-10,10,10,getsize().height-10); } else if(token=='o') { g.drawoval(10,10,getsize().width-20,getsize().height-20); } } public void mouseclicked(mouseevent e) { if(token==' ') { if(whoseturn=='x') { settoken('x'); whoseturn='o'; jlblstatus.settext("o's turn"); if(iswin('x')) { joptionpane.showmessagedialog(this,"x won! ","提示", joptionpane.yes_no_cancel_option); jlblstatus.settext("x won! the game is over"); } else if(isfull()) { joptionpane.showmessagedialog(this,"the game is over","提示", joptionpane.information_message); jlblstatus.settext("draw! the game is over"); } } else if(whoseturn=='o') { settoken('o'); whoseturn='x'; jlblstatus.settext("x's turn"); if(iswin('o')) { joptionpane.showmessagedialog(this, "y won! ","提示", joptionpane.information_message); jlblstatus.settext("o won! the game is over"); } else if(isfull()) { joptionpane.showmessagedialog(this,"the game is over","提示", joptionpane.information_message); jlblstatus.settext("draw! the game is over"); } } } } public void mousepressed(mouseevent e) {} public void mousereleased(mouseevent e) {} public void mouseentered(mouseevent e) {} public void mouseexited(mouseevent e) {} }
}//一个简单java #字三关 的游戏代码