亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? chessgame.java

?? Java五子棋 支持本地游戲
?? JAVA
字號:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.lang.*;
import java.io.*;
import java.net.*;


enum ChessMan
{
	Black, White, Empty
}

enum GameType
{
	LocalGame, ComputerGame, NetGame
}

enum GameStatus
{
	NotReady, IsReady, Running, Over
}



/**
 * @author JiangLei
 * 
 * this class maintains the main components of the game
 *
 */

class ChessGame
{
	private static final long serialVersionUID = 1L;
	
	//define the map size
	static public final int mapSize = 15;
	//two dimension array stores the board information
	public ChessMan[][] chessMap = new ChessMan[mapSize][mapSize];
	
	//player information
	Player playerA;
	Player playerB;
	Player notLocalPlayer;
	Player activePlayer = playerA;
	
	//the game panel
	ChessPad cpad = new ChessPad(this);
	
	//the player status panel
	PlayerStatusPanel playerPanelA;
	PlayerStatusPanel playerPanelB;
	
	//specify whether it's a local game, or a Human Vs. Computer or net game
	GameType gameType;
	
	//number of chess left in the board
	int chessLeft = mapSize * mapSize;
	
	//total time for the game
	int totalTime;
	
	//specify whether the game is ready or running or over
	GameStatus status = GameStatus.NotReady;
	
	//some additional elements for a net game
	Thread netChessThread; 
	Socket sock;
	Socket chatSock;
	
	//thread for a computer game
	Thread AIThread;
	
	
	/**
	 * 
	 * @param p1 specify the player with black chessman
	 * @param p2 specify the player with white chessman
	 * @param localPlayer specify the player who is in front of this computer
	 * @param totalTime the total time set up for the game
	 * @param gType specify which kind of game it is expected to be
	 */
	ChessGame(Player p1, Player p2, Player localPlayer, int totalTime, GameType gType)
	{
		playerA = p1;
		playerB = p2;
		playerPanelA = new PlayerStatusPanel(playerA, totalTime, this);
		playerPanelB = new PlayerStatusPanel(playerB, totalTime, this);
		
		playerA.playerPanel = playerPanelA;
		playerB.playerPanel = playerPanelB;
		
		gameType = gType;
		this.totalTime = totalTime;

		//empty the chess board
		for(int i = 0; i < mapSize; i++)
			for(int j = 0; j < mapSize; j++)
				chessMap[i][j] = ChessMan.Empty;
		
		cpad.listeningPlayer = localPlayer;
		notLocalPlayer = (localPlayer == playerA)?playerB:playerA;
	}
	
	/**
	 * startGame() start a new game
	 * 
	 */
	void startGame()
	{
		//empty the board
		for(int i = 0; i < mapSize; i++)
			for(int j = 0; j < mapSize; j++)
				chessMap[i][j] = ChessMan.Empty;
		//set up status
		status = GameStatus.Running;
		//set up player
		activePlayer = playerA;
		if(gameType == GameType.LocalGame)
			cpad.listeningPlayer = playerA;
		//reset the game time
		playerPanelA.timeLeftInSeconds = this.totalTime;
		playerPanelB.timeLeftInSeconds = this.totalTime;
		//refresh the frame
		MainFrame.mainFrame.repaint();
		
		//start count time 
		playerPanelA.chessTimer.start();
		
		//if not a local game, start the additional thread
		if(gameType == GameType.NetGame)
		{
			netChessThread = new NetChessThread(this);
			netChessThread.start();
		}
		else if(gameType == GameType.ComputerGame)
		{
			AIThread = new AIThread(this);
			AIThread.start();
		}
		
	}
	
	/**
	 * this method simulates the play game process, it put a chess of the active player on board, 
	 * and then judge if the player wins or there're no space left, else it'll automatically 
	 * switch to the other player 
	 * 
	 * @param x 	the x position of the chess 
	 * @param y		the y position of the chess
	 */
	void playChess(int x, int y)
	{
		this.chessMap[x][y] = this.activePlayer.chessMan;
		this.cpad.repaint();
    	
    	if(ChessGame.judge(x, y, this.chessMap))
    	{
    		gameOver();
    		//System.out.println(this.listeningPlayer.playerName + " wins!");
			String s = this.activePlayer.playerName + "獲勝!";
			GameResultDialog d = new GameResultDialog(MainFrame.mainFrame, 100, 100, s); 
			d.setVisible(true);
    	}
    	else if(--chessLeft == 0)
    	{
    		gameOver();
    		//System.out.println("Nobody wins!");
			String s = "雙方平局";
			GameResultDialog d = new GameResultDialog(MainFrame.mainFrame, 100, 100, s); 
			d.setVisible(true);
    	}
    	else
    	{
    		activePlayer.playerPanel.chessTimer.stop();
	    	activePlayer = (activePlayer == playerA)?playerB:playerA;
	    	activePlayer.playerPanel.chessTimer.start();
    	}
    	
    	return;
	}
	
	/**
	 * this method test if the latest chess will contribute to a FIVE
	 * 
	 * @param x		the x position of the chess
	 * @param y		the y position of the chess
	 * @return		whether there is a FIVE
	 */
	static boolean judge(int x, int y, ChessMan[][] chessMap)
	{
		int ax = x;
		int ay = y;
		int count = 0;
		
		//analysis the horizontal line
	    while(ax < mapSize && chessMap[ax][ay] == chessMap[x][y]){
	    	count++;
    		ax++;
	    }
	    ax=x;
	    count--;
	    
	    while(ax >= 0 && chessMap[ax][ay] == chessMap[x][y]){
	    	 count++;
    		 ax--;
	    }

	    if(count > 4){
	    	 return true;
    	}
	     
	    //analysis the vertical line
	    ax=x;
	    count=0;
	    while(ay < mapSize && chessMap[ax][ay] == chessMap[x][y]){
	    	count++;
    		ay++;
	    }
	    ay=y;
	    count--;
	    while(ay >= 0 && chessMap[ax][ay] == chessMap[x][y]){
	    	count++;
    		ay--;
	    }
	    if(count>4) {
	    	return true;
	    }
	    
	    //analysis the south east line
	    ax=x;
	    ay=y;
	    count=0;
	    while(ax >= 0 && ay >= 0 && chessMap[ax][ay] == chessMap[x][y]){
	    	count++;
    		ay--;
    		ax--;
	    }
	    ay=y;
	    ax=x;
	    count--;
	    while(ax < mapSize && ay < mapSize && chessMap[ax][ay] == chessMap[x][y]){
	    	count++;
    		ay++;
    		ax++;
	    }
	    if(count>4) {
	    	return true;
	    }
	    
	    //analysis the south west line
	    ax=x;
	    ay=y;
	    count=0;
	    while(ay < mapSize && ax >= 0 && chessMap[ax][ay] == chessMap[x][y]){
	    	count++;
	    	ay++;
	    	ax--;
	    }
	    ay=y;
	    ax=x;
	    count--;
	    while(ay > 0 && ax < mapSize && chessMap[ax][ay] == chessMap[x][y]){
	    	count++;
	    	ay--;
	    	ax++;
	    }
	    if(count>4){
	    	return true;
	    }
	    
		return false;
	}
	
	/**
	 * set game over
	 */
	void gameOver()
	{
		status = GameStatus.Over;
	}
}


/**
 * 
 * @author JiangLei
 * this class records the information about the player
 *
 */
class Player{
	public String playerName;
	public ChessMan chessMan;
	public PlayerStatusPanel playerPanel;

	Player(String name, ChessMan chessMan){
		playerName = name;
		this.chessMan = chessMan;
	}
}

/**
 * 
 * this class draws the game board and process player's interaction with it
 *
 */
class ChessPad extends JPanel implements MouseListener
{
	private static final long serialVersionUID = 1L;

	//game on this board
	ChessGame game;
	//the layout parameter of the board
	public final int locationX = 20;
	public final int locationY = 20;
	public final int width = 30;
	public final int sizeX;
	public final int sizeY;
	public int chessManRadius = 12;
	//the player who will interact with the board
	Player listeningPlayer;

	
	ChessPad(ChessGame g)
	{
		this.game = g;
		//set the appearance of the board
		this.setBackground(Color.orange);
		sizeX = (game.mapSize - 1) * width + 2 * locationX;
		sizeY = (game.mapSize - 1) * width + 2 * locationY;
		this.setSize(sizeX, sizeY);

		addMouseListener(this);	
	}
	
	/**
	 * 
	 * @param g		the graphics handler
	 * @param i		the x position of the chess 
	 * @param j		the y position of the chess
	 */
	private void drawChessMan(Graphics g, int i,int j)
	{
		//x,y: the coordinates on panel
		int x = i * width + locationX;
		int y = j * width + locationY;
		
        if(game.chessMap[i][j] == ChessMan.Black)
        {
        	//draw the outline
            g.setColor(Color.gray);
            g.drawOval(x - chessManRadius,y - chessManRadius,chessManRadius * 2, chessManRadius * 2);
           	//fill the center
            g.setColor(Color.BLACK);
            g.fillOval(x - chessManRadius,y - chessManRadius,chessManRadius * 2, chessManRadius * 2);
        }
		else if(game.chessMap[i][j] == ChessMan.White)
		{
			//draw the outline
			g.setColor(Color.gray);
			g.drawOval(x - chessManRadius,y - chessManRadius,chessManRadius * 2, chessManRadius * 2);
			//fill the center
			g.setColor(Color.white);
			g.fillOval(x - chessManRadius,y - chessManRadius,chessManRadius * 2, chessManRadius * 2);
		}
   	}
   
    public void paint(Graphics g)
    {
    	super.paint(g);
		
    	//draw the grid
		for (int i = 0; i < game.mapSize; i++)
		{
			g.drawLine(locationX, locationY + width * i, locationX + width * (game.mapSize - 1), locationY + width * i);
			g.drawLine(locationX + width * i, locationY, locationX + width * i, locationY + width * (game.mapSize - 1));
			
		}
		
		//draw the chess
		for(int i = 0; i < game.mapSize; i++)
		{
			for(int j = 0;j < game.mapSize; j++)
			{
				drawChessMan(g,i,j);
			}
		}
		
		//System.out.println("Paint() finished!");
 
	}
    
    public void mousePressed(MouseEvent e)
    {
		//System.out.println("Mouse Pressed!");
		if(game.status != GameStatus.Running)
			return;
		
    	if(listeningPlayer == game.activePlayer)
    	{	
    		//x, y: the position on board when mouse is pressed
		    int x = Math.round((float)(e.getX() - locationX) / (float)width);
		    int y = Math.round((float)(e.getY() - locationY) / (float)width);
		    
		    //if all conditions permits
		    if(x >= 0 && x < game.mapSize && y >= 0 && y < game.mapSize && game.activePlayer == this.listeningPlayer && game.chessMap[x][y] == ChessMan.Empty)
		    {
				//System.out.println("x:" + x + " " + "y:" + y);
		    	
		    	game.playChess(x, y);
		    	
		    	if(game.gameType == GameType.LocalGame)
		    	{
		    		//switch player
		    		listeningPlayer = game.activePlayer;
		    	}
		    	else if(game.gameType == GameType.NetGame)
		    	{
		    		//send the chess information to the other player
			    	try{
			    		game.sock.getOutputStream().write(x);
			    		game.sock.getOutputStream().write(y);
						System.out.println("Write stream finished!");
					}
	    			catch(IOException ex)
	    			{
	    				System.out.println("Write Error!");
	    			}
		    	}
		    	else if(game.gameType == GameType.ComputerGame)
		    	{
	    			game.AIThread.resume();
		    	}
		    	
		    }
    	}
    }
    
    public void mouseReleased(MouseEvent e){}    
    public void mouseEntered(MouseEvent e){}    
    public void mouseExited(MouseEvent e){}    
    public void mouseClicked(MouseEvent e){}

}

/**
 * 
 * this class draws the player status panel
 *
 */
class PlayerStatusPanel extends JPanel
{
	private static final long serialVersionUID = 1L;
	
	//the appearance parameter
	static final int width = 200;
	static final int height = 80;
	static final int chessManPosX = 20;
	static final int chessManPosY = 30;
	
	//player on this panel
	Player player;
	//label which shows the time left
	JLabel timeLabel;
	public int timeLeftInSeconds;
	Timer chessTimer;
	ChessGame game;
	
	PlayerStatusPanel(Player pl, int seconds, ChessGame g)
	{
		this.player = pl;
		this.game = g;
		this.timeLeftInSeconds = seconds;
		//set the layout of this panel
		this.setBackground(Color.yellow);
		this.setSize(PlayerStatusPanel.width, PlayerStatusPanel.height);
		setLayout(new BorderLayout());
		JLabel nameLb = new JLabel("玩家名稱:" + player.playerName);
		JPanel p = new JPanel();
		p.setBackground(Color.yellow);
		p.setLayout(new GridLayout(2, 1));
		p.add(nameLb);
		p.setSize(width - 2 * chessManPosX, height);
		timeLabel = new JLabel("所剩時間:" + seconds/60 + ":" + seconds%60);
		p.add(timeLabel);
		add("East", p);

		//add Timer
		chessTimer = new Timer(1000, new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				if(game.status != GameStatus.Running)
					chessTimer.stop();
				timeLeftInSeconds--;
				timeLabel.setText("所剩時間:" + timeLeftInSeconds/60 + ":" + timeLeftInSeconds%60);
				if(timeLeftInSeconds == 0)
				{	//超時判負
					game.gameOver();
					String s = player.playerName + "超時判負";
					GameResultDialog d = new GameResultDialog(MainFrame.mainFrame, 100, 100, s); 
					d.setVisible(true);
				}
			}
		});
	}
	
	/**
	 * this method paint this panel
	 * 
	 */
	public void paint(Graphics g)
	{
		super.paint(g);
		//refresh the label
		timeLabel.setText("所剩時間:" + timeLeftInSeconds/60 + ":" + timeLeftInSeconds%60);
		//paint the chess indicates the side
		g.setColor(Color.gray);
		g.drawOval(chessManPosX, chessManPosY, game.cpad.chessManRadius * 2, game.cpad.chessManRadius * 2);
        if(player.chessMan == ChessMan.Black)
        {
           	g.setColor(Color.BLACK);
        }
		else 
		{
			g.setColor(Color.white);
		}		
		g.fillOval(chessManPosX, chessManPosY, game.cpad.chessManRadius * 2, game.cpad.chessManRadius * 2);
	}

}

/**
 * 
 * this class is a dialog, it will show the result of the game
 *
 */
class GameResultDialog extends JDialog
{
	GameResultDialog(JFrame parent, int w, int h, String resultStr)
	{
		super(parent, "本局結果", true);
		setSize(w, h);
		setLocation(500, 300);
		
		setLayout(new FlowLayout());
		JLabel lb = new JLabel(resultStr);
		JButton bn = new JButton("確定");
		add("North", lb);
		add("South", bn);
		
		bn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				MainFrame.mainFrame.startGameBn.setEnabled(true);
				setVisible(false);
			}
		});
		
		addWindowListener(new WindowAdapter()
        {public void windowClosing(WindowEvent e)
             {
				MainFrame.mainFrame.startGameBn.setEnabled(true);
        		setVisible(false);
        		}
             });
	}
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
老司机免费视频一区二区三区| 777欧美精品| 亚洲欧美激情视频在线观看一区二区三区 | 一区二区三区中文字幕电影| 色综合色综合色综合| 亚洲免费大片在线观看| 3atv一区二区三区| 国产在线视视频有精品| 国产精品每日更新在线播放网址| 99r国产精品| 亚洲成人先锋电影| 亚洲精品一区二区三区影院| 国产一区二区成人久久免费影院| 欧美国产综合一区二区| 欧美亚洲日本国产| 日本女优在线视频一区二区| 日本系列欧美系列| 成人黄色av网站在线| 色综合久久综合网欧美综合网| 欧美在线播放高清精品| 91精品国产手机| 亚洲天堂网中文字| 韩国午夜理伦三级不卡影院| 成人综合在线观看| 日韩精品一区二区三区swag | 粉嫩av亚洲一区二区图片| 99riav一区二区三区| 国产人久久人人人人爽| 视频在线在亚洲| 欧美一卡二卡三卡| 亚洲乱码国产乱码精品精小说| 成人免费毛片aaaaa**| 欧美性大战久久久| 91精品国产91久久久久久最新毛片| 欧美日韩视频在线一区二区 | 国产蜜臀97一区二区三区 | 国产传媒日韩欧美成人| 亚洲蜜臀av乱码久久精品| 日韩免费视频一区| 91福利国产成人精品照片| 激情综合网最新| 亚洲一区二区三区激情| 国产精品久久久久久久第一福利| 欧美放荡的少妇| 91蜜桃传媒精品久久久一区二区| 美女任你摸久久| 亚洲欧美日韩久久| 国产欧美日韩精品在线| 精品乱码亚洲一区二区不卡| 欧美午夜精品久久久久久超碰| 国产风韵犹存在线视精品| 日韩一区精品字幕| 一区二区三区免费网站| 国产精品免费观看视频| 日韩欧美国产一区二区在线播放 | 久久精品国产澳门| 亚洲成人av电影在线| 亚洲美女电影在线| 国产精品久久777777| 国产欧美一区二区精品忘忧草| 欧美成人高清电影在线| 91精品麻豆日日躁夜夜躁| 欧美日韩一卡二卡| 欧美亚洲动漫另类| 欧美羞羞免费网站| 欧美亚洲国产bt| 欧美中文字幕不卡| 欧美日韩在线播放三区| 欧美亚洲愉拍一区二区| 欧美优质美女网站| 欧美性色欧美a在线播放| 91国产成人在线| 欧美色偷偷大香| 欧美日韩精品免费| 欧美区在线观看| 欧美二区在线观看| 日韩你懂的在线观看| 精品久久久久久久人人人人传媒| 7777精品久久久大香线蕉| 7777精品伊人久久久大香线蕉的| 3751色影院一区二区三区| 欧美一级免费大片| 欧美成人高清电影在线| 久久久精品免费网站| 欧美极品少妇xxxxⅹ高跟鞋| 国产精品色眯眯| 一区二区三区精品| 丝袜美腿亚洲色图| 狠狠色丁香婷婷综合久久片| 国产成人8x视频一区二区| 成人短视频下载| 色视频欧美一区二区三区| 欧美系列亚洲系列| 日韩精品在线一区二区| 国产欧美精品国产国产专区| 日韩伦理电影网| 天堂精品中文字幕在线| 国产一区三区三区| 91尤物视频在线观看| 欧美三级电影在线观看| 欧美sm美女调教| 国产精品白丝在线| 亚洲h动漫在线| 激情五月播播久久久精品| 97se亚洲国产综合自在线不卡| 欧美色综合影院| www久久久久| 一区二区三区四区不卡视频| 久久精品国产久精国产爱| 成人午夜看片网址| 欧美日韩国产不卡| 国产午夜精品一区二区三区嫩草| 一区二区三区四区在线免费观看| 日韩av午夜在线观看| 99精品欧美一区二区蜜桃免费 | 欧美性三三影院| 26uuu国产电影一区二区| 《视频一区视频二区| 日韩av不卡一区二区| 成人的网站免费观看| 这里只有精品电影| 亚洲人成网站在线| 精品一区二区免费视频| 欧美中文字幕不卡| 国产网红主播福利一区二区| 午夜视频在线观看一区二区| 国产91色综合久久免费分享| 777久久久精品| 成人免费一区二区三区视频| 激情文学综合插| 欧美美女黄视频| 18成人在线观看| 国产一区二区0| 制服丝袜激情欧洲亚洲| 亚洲免费av高清| 成人黄动漫网站免费app| 日韩欧美一二区| 午夜免费久久看| 日本精品一级二级| 中文字幕av在线一区二区三区| 另类中文字幕网| 欧美揉bbbbb揉bbbbb| 亚洲品质自拍视频| 岛国一区二区在线观看| xvideos.蜜桃一区二区| 蜜桃一区二区三区在线观看| 欧美日韩国产综合草草| 亚洲欧美另类久久久精品| 粗大黑人巨茎大战欧美成人| 欧美sm美女调教| 美女国产一区二区| 欧美一区二区三区在线电影 | 麻豆精品在线观看| 欧美日韩高清一区二区不卡 | 日韩毛片视频在线看| 成人午夜在线播放| 国产欧美一区二区三区在线看蜜臀 | 亚洲精品在线电影| 免费看精品久久片| 日韩免费一区二区三区在线播放| 日韩不卡一二三区| 日韩一区二区三区免费观看| 青青草一区二区三区| 制服丝袜激情欧洲亚洲| 免费观看在线综合| 精品国产伦一区二区三区观看体验| 免费美女久久99| 亚洲精品一区二区精华| 国产一区二区三区最好精华液| 久久伊人中文字幕| 国产成人av在线影院| 国产精品久久久爽爽爽麻豆色哟哟| 成人免费视频app| 亚洲欧洲精品一区二区三区| 99久久99久久精品免费观看| 亚洲摸摸操操av| 欧美丝袜丝交足nylons图片| 亚洲自拍欧美精品| 欧美一区二区三区思思人| 国产美女精品一区二区三区| 欧美韩日一区二区三区| 99精品国产视频| 亚洲成人精品一区二区| 精品乱人伦一区二区三区| 国产999精品久久久久久| 亚洲乱码国产乱码精品精可以看 | 亚洲精品一区二区三区在线观看| 国产精品一二三在| 中文字幕一区二区在线播放| 91久久精品午夜一区二区| 亚洲五月六月丁香激情| 精品久久免费看| 高清beeg欧美| 亚洲香肠在线观看| 精品免费一区二区三区| 91麻豆.com| 久久99精品国产| 中文字幕日本不卡| 日韩欧美一二三区| 色综合久久六月婷婷中文字幕|