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;


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

转载于:https://www.cnblogs.com/qzsoul/p/7220989.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值