Java Applet Tic-Tac-String game-how to start again? (Leave problem)

Source: Internet
Author: User

Today in the textbook to see the source of the applet tic-tac-Small game, but did not restart this function, I am prepared to write a Isrepaint () method to pop-up dialog box to ask the player whether to start again, but will not initialize the game, left to solve later.

If there is a passing master greatly, look at the liberal enlighten, Green bamboo grateful!

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 (cell)
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 is 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 (Ten, GetWidth () -10, GetHeight ()-10);
G.drawline (GetWidth () -10, ten, Ten, GetHeight ()-10);
}
else if (token = = ' O ') {
G.drawoval (Ten, 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 are 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";
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, "Play Again", "Game Over", joptionpane.yes_no_option);
if (res = = joptionpane.yes_option) {
//This does not initialize ,,,
new TicTacToe ();
new Cell ();
repaint ();
}else{
return;
}
}
}
}

Java Applet Tic-Tac-String game-how to start again? (Leave problem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.