package com.tictactoe.client;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import com.tictactoe.constants.TicTacToeConstants;
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 = true;
//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
add(jlblTitle, BorderLayout.NORTH);
add(p, BorderLayout.CENTER);
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 (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Control the game on a separate thread
Thread thread = new Thread(this);
thread.start();
}
@Override
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("Player1 with token 'X'");
jlblStatus.setText("Waiting for player2 to join");
//Receive startup notification from the server
fromServer.readInt(); // Whatever read is ignored
//The other player has joined
jlblStatus.setText("Player2 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 player1 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 the player2's move to the server
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**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){
//Player1 won, stop playing
continueToPlay = false;
if(myToken == 'X'){
jlblStatus.setText("I won!(X)");
}else if(myToken == 'O'){
jlblStatus.setText("Player1 (X) has won!");
receiveMove();
}
}else if(status == PLAYER2_WON){
//Player2 won, stop playing
continueToPlay = false;
if(myToken == 'O'){
jlblStatus.setText("I won!(O)");
}else if(myToken == 'X'){
jlblStatus.setText("Player2 (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
public class Cell extends JPanel{
//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(new ClickListener()); //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 ClickListener extends MouseAdapter{
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
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计
资源推荐
资源详情
资源评论
收起资源包目录
分布式井字游戏(java后端和用于测试的java客户端).zip (8个子文件)
project_code_0719
TicTacToe
.classpath 301B
.settings
org.eclipse.jdt.core.prefs 598B
src
com
tictactoe
client
TicTacToeClient.java 7KB
constants
TicTacToeConstants.java 413B
server
HandleASession.java 4KB
TicTacToeServer.java 2KB
.project 385B
.gitignore 5B
共 8 条
- 1
资源评论
辣椒种子
- 粉丝: 3442
- 资源: 5724
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见的
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见的
- 基于Javascript的结婚请帖设计源码 - Invitation
- mysql语句大全及用法
- mysql语句大全及用法
- mysql语句大全及用法
- MySQL是一种广泛使用的开源关系型数据库管理系统
- MySQL是一种广泛使用的开源关系型数据库管理系统
- MySQL是一种广泛使用的开源关系型数据库管理系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功