1. 程式人生 > >Java applet 井字遊戲——怎樣重新開始?(留問題)

Java applet 井字遊戲——怎樣重新開始?(留問題)

rmi ecif 方法 eth center token ttext jpanel lin

今天在課本上看到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;


@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;
}
}
}
}

Java applet 井字遊戲——怎樣重新開始?(留問題)