用java编写五子棋游戏_用java写一个五子棋游戏软件是eclipse

本文介绍了使用Java编写五子棋游戏客户端和服务器的详细步骤。客户端包括用户界面交互,服务器负责处理游戏逻辑和连接管理。通过Socket进行通信,实现了玩家与服务器之间的实时交互。
摘要由CSDN通过智能技术生成

棋盘已经画好了,可以下有服务器和玩家,怎么写判断输赢?

脚本附上:

第一个脚本客户端(玩家)代码:

package WuZiQi;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.GridLayout;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.net.Socket;

import java.net.InetAddress;

import java.io.IOException;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.SwingUtilities;

import java.util.Formatter;

import java.util.Scanner;

import java.util.concurrent.Executors;

import java.util.concurrent.ExecutorService;

public class Client extends JFrame implements Runnable

{

private JTextField idField; // textfield to display player's mark

private JTextArea displayArea; // JTextArea to display output

private JPanel boardPanel; // panel for tic-tac-toe board

private JPanel panel2; // panel to hold board

private Square[][] board; // tic-tac-toe board

private Square currentSquare; // current square

private Socket connection; // connection to server

private Scanner input; // input from server

private Formatter output; // output to server

private String ticTacToeHost; // host name for server

private String myMark; // this client's mark

private boolean myTurn; // determines which client's turn it is

private final String X_MARK = "X"; // mark for first client

private final String O_MARK = "O"; // mark for second client

// set up user-interface and board

public Client( String host )

{

ticTacToeHost = host; // set name of server

displayArea = new JTextArea( 21,21 ); // set up JTextArea

displayArea.setEditable( false );

add( new JScrollPane( displayArea ), BorderLayout.SOUTH );

boardPanel = new JPanel(); // set up panel for squares in board

boardPanel.setLayout( new GridLayout( 19, 19, 0, 0 ) );

board = new Square[ 19 ][ 19 ]; // create board

// loop over the rows in the board

for ( int row = 0; row < board.length; row++ )

{

// loop over the columns in the board

for ( int column = 0; column < board[ row ].length; column++ )

{

// create square

board[ row ][ column ] = new Square( " ", row * 19 + column );

boardPanel.add( board[ row ][ column ] ); // add square

} // end inner for

} // end outer for

idField = new JTextField(); // set up textfield

idField.setEditable( false );

add( idField, BorderLayout.NORTH );

panel2 = new JPanel(); // set up panel to contain boardPanel

panel2.add( boardPanel, BorderLayout.CENTER ); // add board panel

add( panel2, BorderLayout.CENTER ); // add container panel

setSize( 300, 225 ); // set size of window

setVisible( true ); // show window

startClient();

} // end TicTacToeClient constructor

// start the client thread

public void startClient()

{

try // connect to server and get streams

{

// make connection to server

connection = new Socket(

InetAddress.getByName( ticTacToeHost ), 12345 );

// get streams for input and output

input = new Scanner( connection.getInputStream() );

output = new Formatter( connection.getOutputStream() );

} // end try

catch ( IOException ioException )

{

ioException.printStackTrace();

} // end catch

// create and start worker thread for this client

ExecutorService worker = Executors.newFixedThreadPool( 1 );

worker.execute( this ); // execute client

} // end method startClient

// control thread that allows continuous update of displayArea

public void run()

{

myMark = input.nextLine(); // get player's mark (X or O)

SwingUtilities.invokeLater(

new Run

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值