Java applet 井字游戏——怎样重新开始((留问题))

炒沙作縻终不饱,缕冰文章费工巧。这篇文章主要讲述Java applet 井字游戏——怎样重新开始?(留问题)相关的知识,希望能为你提供帮助。
今天在课本上看到applet井字小游戏的源码,但是并没有重新开始这个功能,我这边准备编写一个isRepaint()方法来弹出对话框询问玩家是否重新开始,但是不会初始化游戏,留待以后解决。
如果有路过的高手大大,望不吝赐教,青竹感激不尽!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
【Java applet 井字游戏——怎样重新开始((留问题))】
@SuppressWarnings("serial")
public class TicTacToe extends JApplet{

private char whoseTurn = ‘X‘;

//Create and initialize cells(单元格)
private Cell[][] cells = new Cell[3][3];

//Create and initialize a status label
private JLabel jlblStatus = new JLabel("X‘s turn to play");

//initialize UI
public TicTacToe(){
JPanel p = new JPanel(new GridLayout(3,3,0,0));
for(int i=0; i< 3; i++)
for(int j=0; j< 3; j++)
p.add(cells[i][j] = new Cell());

p.setBorder(new LineBorder(Color.red,1));
jlblStatus.setBorder(new LineBorder(Color.yellow,1));

add(p,BorderLayout.CENTER);
add(jlblStatus,BorderLayout.SOUTH);
}

/**Determine whether the cells are all occupied*/
public boolean isFull(){
for(int i=0; i< 3; i++)
for(int j=0; j< 3; j++)
if(cells[i][j].getToken() == ‘ ‘)
return false;

return true;
}

/**Determine whether the player with the specified token wins*/
public boolean isWon(char token){
for(int i=0; i< 3; i++)
if((cells[i][0].getToken() == token)
& & (cells[i][1].getToken() == token)
& & (cells[i][2].getToken() == token)){
return true;
}

for(int j=0; j< 3; j++)
if((cells[0][j].getToken() == token)
& & (cells[1][j].getToken() == token)
& & (cells[2][j].getToken() == token)){
return true;
}

if((cells[0][0].getToken() == token)
& & (cells[1][1].getToken() == token)
& & (cells[2][2].getToken() == token)){
return true;
}

if((cells[0][2].getToken() == token)
& & (cells[1][1].getToken() == token)
& & (cells[2][0].getToken() == token)){
return true;
}

return false;
}

//An inner class Cell for a cell
public class Cell extends JPanel{
//Token used for this cell
private char token = ‘ ‘;

public Cell(){
setBorder(new LineBorder(Color.black,1));
addMouseListener(new MyMouseListener()); //register listener
}

/**Return token*/
public char getToken(){
return token;
}

/**Set a new token*/
public void setToken(char c){
token = c;
repaint();
}

/**Paint the cell*/
protected void paintComponent(Graphics g){
super.paintComponent(g);

if(token == ‘X‘){
g.drawLine(10, 10, getWidth()-10, getHeight()-10);
g.drawLine(getWidth()-10, 10, 10, getHeight()-10);
}
else if(token == ‘O‘){
g.drawOval(10, 10, getWidth()-20, getHeight()-20);
}
}

private class MyMouseListener extends MouseAdapter{
/**handle mouse click on a cell*/
public void mouseClicked(MouseEvent e){
//If cell is empty and game is not over
if(token == ‘ ‘ & & whoseTurn != ‘ ‘){
setToken(whoseTurn); //Set token in the cell

//Check game status
if(isWon(whoseTurn)){
jlblStatus.setText(whoseTurn+" won!The game is over");
whoseTurn = ‘ ‘; //Game is over
isRepaint();
}
else if(isFull()){
jlblStatus.setText("Draw!The Game is over");
whoseTurn = ‘ ‘;
}
else{
//change the turn
whoseTurn = (whoseTurn == ‘X‘)?‘O‘:‘X‘;
//Display whose turn
jlblStatus.setText(whoseTurn + "‘s turn");
}
}
}
}
public void isRepaint(){
int res = JOptionPane.showConfirmDialog(null, "是否重新开始游戏", "Game over", JOptionPane.YES_NO_OPTION);
if(res == JOptionPane.YES_OPTION){
//这样并不能初始化,,,
new TicTacToe();
new Cell();
repaint();
}else{
return;
}
}
}
}





















































































































































    推荐阅读