import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.io.*;
import java.net.*;
public class TicTacToeClient extends JApplet
implements Runnable, TicTacToeConstants {
// Indicate whether the player has the turn
private boolean myTurn = false;
// Indicate the token for the player
private char myToken = ' ';
// Indicate the token for the other player
private char otherToken = ' ';
// Create and initialize cells
private Cell[][] cell = new Cell[3][3];
// Create and initialize a title label
private JLabel jlblTitle = new JLabel();
// Create and initialize a status label
private JLabel jlblStatus = new JLabel();
// Indicate selected row and column by the current move
private int rowSelected;
private int columnSelected;
// Input and output streams from/to server
private DataInputStream fromServer;
private DataOutputStream toServer;
// Continue to play?
private boolean continueToPlay = true;
// Wait for the player to mark a cell
private boolean waiting = true;
// Indicate if it runs as application
private boolean isStandAlone = false;
// Host name or ip
private String host = "localhost";
/** Initialize UI */
public void init() {
// Panel p to hold cells
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(i, j));
// Set properties for labels and borders for labels and panel
p.setBorder(new LineBorder(Color.black, 1));
jlblTitle.setHorizontalAlignment(JLabel.CENTER);
jlblTitle.setFont(new Font("SansSerif", Font.BOLD, 16));
jlblTitle.setBorder(new LineBorder(Color.black, 1));
jlblStatus.setBorder(new LineBorder(Color.black, 1));
// Place the panel and the labels to the applet
this.getContentPane().add(jlblTitle, BorderLayout.NORTH);
this.getContentPane().add(p, BorderLayout.CENTER);
this.getContentPane().add(jlblStatus, BorderLayout.SOUTH);
// Connect to the server
connectToServer();
}
private void connectToServer() {
try {
// Create a socket to connect to the server
Socket socket;
if (isStandAlone)
socket = new Socket(host, 8000);
else
socket = new Socket(getCodeBase().getHost(), 8000);
// Create an input stream to receive data from the server
fromServer = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
toServer = new DataOutputStream(socket.getOutputStream());
}
catch (Exception ex) {
System.err.println(ex);
}
// Control the game on a separate thread
Thread thread = new Thread(this);
thread.start();
}
public void run() {
try {
// Get notification from the server
int player = fromServer.readInt();
// Am I player 1 or 2?
if (player == PLAYER1) {
myToken = 'X';
otherToken = 'O';
jlblTitle.setText("Player 1 with token 'X'");
jlblStatus.setText("Waiting for player 2 to join");
// Receive startup notification from the server
fromServer.readInt(); // Whatever read is ignored
// The other player has joined
jlblStatus.setText("Player 2 has joined. I start first");
// It is my turn
myTurn = true;
}
else if (player == PLAYER2) {
myToken = 'O';
otherToken = 'X';
jlblTitle.setText("Player 2 with token 'O'");
jlblStatus.setText("Waiting for player 1 to move");
}
// Continue to play
while (continueToPlay) {
if (player == PLAYER1) {
waitForPlayerAction(); // Wait for player 1 to move
sendMove(); // Send the move to the server
receiveInfoFromServer(); // Receive info from the server
}
else if (player == PLAYER2) {
receiveInfoFromServer(); // Receive info from the server
waitForPlayerAction(); // Wait for player 2 to move
sendMove(); // Send player 2's move to the server
}
}
}
catch (Exception ex) {
}
}
/** Wait for the player to mark a cell */
private void waitForPlayerAction() throws InterruptedException {
while (waiting) {
Thread.sleep(100);
}
waiting = true;
}
/** Send this player's move to the server */
private void sendMove() throws IOException {
toServer.writeInt(rowSelected); // Send the selected row
toServer.writeInt(columnSelected); // Send the selected column
}
/** Receive info from the server */
private void receiveInfoFromServer() throws IOException {
// Receive game status
int status = fromServer.readInt();
if (status == PLAYER1_WON) {
// Player 1 won, stop playing
continueToPlay = false;
if (myToken == 'X') {
jlblStatus.setText("I won! (X)");
}
else if (myToken == 'O') {
jlblStatus.setText("Player 1 (X) has won!");
receiveMove();
}
}
else if (status == PLAYER2_WON) {
// Player 2 won, stop playing
continueToPlay = false;
if (myToken == 'O') {
jlblStatus.setText("I won! (O)");
}
else if (myToken == 'X') {
jlblStatus.setText("Player 2 (O) has won!");
receiveMove();
}
}
else if (status == DRAW) {
// No winner, game is over
continueToPlay = false;
jlblStatus.setText("Game is over, no winner!");
if (myToken == 'O') {
receiveMove();
}
}
else {
receiveMove();
jlblStatus.setText("My turn");
myTurn = true; // It is my turn
}
}
private void receiveMove() throws IOException {
// Get the other player's move
int row = fromServer.readInt();
int column = fromServer.readInt();
cell[row][column].setToken(otherToken);
}
// An inner class for a cell
private class Cell extends JPanel implements MouseListener {
// Indicate the row and column of this cell in the board
private int row;
private int column;
// Token used for this cell
private char token = ' ';
public Cell(int row, int column) {
this.row = row;
this.column = column;
setBorder(new LineBorder(Color.black, 1)); // Set cell's border
addMouseListener(this); // 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);
}
}
/** Handle mouse click on a cell */
public void mouseClicked(MouseEvent e) {
// If cell is not occupied and the player has the turn
if ((token == ' ') && myTurn) {
setToken(myToken); // Set the player's token in the cell
myTurn = false;
rowSelected = row;
columnSelected = column;
jlblStatus.setText("Waiting for the other player to move");
waiting = false; // Just completed a successful move
}
}
public void mousePressed(MouseEvent e) {
// TODO: implement this java.awt.event.MouseListener method;
}
public void mouseReleased(MouseEvent e) {
// TODO: implement this java.awt.event.MouseListener method;
}
public void mouseEntered(MouseEvent e)
没有合适的资源?快使用搜索试试~ 我知道了~
Java语言程序设计基础片书中例子代码
共1214个文件
class:505个
java:351个
html:97个
5星 · 超过95%的资源 需积分: 9 74 下载量 68 浏览量
2008-09-08
21:40:29
上传
评论
收藏 7.87MB ZIP 举报
温馨提示
这是Y.Daniel Liang所著Java语言程序设计基础篇中的例子的代码
资源推荐
资源详情
资源评论
收起资源包目录
Java语言程序设计基础片书中例子代码 (1214个子文件)
michael.au 78KB
minute22.au 76KB
minute8.au 74KB
minute14.au 12KB
minute43.au 12KB
minute42.au 12KB
minute26.au 12KB
minute33.au 12KB
hour10.au 12KB
minute0.au 12KB
minute52.au 12KB
minute32.au 12KB
hour7.au 12KB
minute44.au 12KB
minute12.au 12KB
minute31.au 12KB
minute20.au 12KB
minute50.au 12KB
hour1.au 12KB
minute30.au 12KB
hour9.au 12KB
am.au 12KB
hour5.au 12KB
minute35.au 12KB
minute38.au 12KB
minute23.au 12KB
minute58.au 12KB
minute51.au 12KB
minute6.au 12KB
minute39.au 12KB
hour8.au 12KB
minute57.au 12KB
hour4.au 12KB
pm.au 12KB
minute27.au 12KB
hour0.au 12KB
minute48.au 12KB
minute45.au 12KB
hour2.au 12KB
minute49.au 12KB
minute10.au 12KB
minute18.au 12KB
minute59.au 12KB
minute36.au 12KB
minute29.au 12KB
minute55.au 12KB
minute17.au 12KB
minute19.au 12KB
minute21.au 12KB
minute47.au 12KB
minute16.au 12KB
minute13.au 12KB
minute3.au 12KB
minute37.au 12KB
hour11.au 12KB
minute9.au 12KB
minute5.au 12KB
minute28.au 12KB
hour3.au 12KB
minute54.au 12KB
minute53.au 12KB
minute11.au 12KB
minute24.au 12KB
minute34.au 12KB
minute2.au 12KB
minute56.au 12KB
minute25.au 12KB
minute41.au 12KB
minute1.au 12KB
minute15.au 12KB
minute46.au 12KB
minute40.au 12KB
hour6.au 12KB
minute7.au 12KB
t1.bin 3B
t.bin 0B
malaysia.bmp 92KB
canada.bmp 17KB
uk.bmp 17KB
usa.bmp 16KB
germany.bmp 14KB
france.bmp 13KB
india.bmp 13KB
norway.bmp 3KB
SQLClient.class 8KB
CopyFileToTable.class 8KB
TableEditor.class 7KB
ResourceBundleDemo.class 6KB
TableModelDemo.class 6KB
TicTacToeClient.class 6KB
RegistrationWithCookie.class 6KB
BorderDemo.class 6KB
TextEditor.class 6KB
RegistrationWithHttpSession.class 5KB
StoreAndRetrieveImage.class 5KB
TableEventsDemo.class 5KB
AddressBook.class 5KB
Registration.class 5KB
NumberFormatDemo.class 5KB
ObjectStreamDemo.class 5KB
共 1214 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
- JohnSnow0002013-08-30找了好久,终于找到了,我们教材jva语言程序设计基础版的配套的源代码
forrest_lv
- 粉丝: 1
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功