⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monopolygame.java

📁 自己写的monopoly 游戏代码
💻 JAVA
字号:
package com.xmu.typot.domain;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import com.xmu.typot.constant.*;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import com.xmu.typot.constant.*;
import observer.ControlListener;

import com.xmu.typot.test.TestAdapter;
import com.xmu.typot.test.TestControlFrame;
import com.xmu.typot.test.TestListener;
import com.xmu.typot.ui.InitPlayerFrame;
import com.xmu.typot.ui.TestFrame;

/**
 * monopoly 后台控制程序
 * @author typot
 *
 */
public class MonopolyGame extends TestAdapter implements ControlListener{
	private  int PLAYER_TOTAL;
	private Board board;
	private Player[] player;
	private TestFrame frame;
	private  int playerRun = 0;
	private InitPlayerFrame initFrame;
	public MonopolyGame()
	{
		
		initFrame = new InitPlayerFrame(this);
		initFrame.setVisible(true);
		if(Constants.isTest)
		{
			TestControlFrame test = new TestControlFrame(this);
			this.addListener(test);
			test.setVisible(true);
		}
	}
	
	public void play(ArrayList<Integer> index)
	{
		board = new Board(this);
		initPlayer(index);
		initGUI();
		initFrame.setVisible(false);
		repaint();
	}
	
	private void initGUI() {
		// TODO Auto-generated method stub
		frame = new TestFrame(this);
		frame.setVisible(true);
	}
	
	
	
	public void initPlayer(ArrayList<Integer> index)
	{
		PLAYER_TOTAL = index.size();
		player = new Player[PLAYER_TOTAL];
		Player p;
		for(int i = 0; i < PLAYER_TOTAL; i ++)
		{
			p = new Player(i+1+"P",index.get(i),board.getSquares(0));
			player[i] = p;
		}
		
	
	}
	public void draw(Graphics2D g2d)
	{
		board.draw(g2d);
		for(int i = 0; i < PLAYER_TOTAL; i ++)
			player[i].getPiece().draw(g2d);
		drawPlayerMessage(g2d);
	}
	
	/**
	 * 画玩家信息
	 * @param g2d
	 */
	private void drawPlayerMessage(Graphics2D g2d) {
		// TODO Auto-generated method stub
		BufferedImage bf = null;
		try {
			bf = ImageIO.read(new File("image/money.gif"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		g2d.drawImage(bf,100,170,null);
		g2d.setPaint(Constants.COLOR1);
		g2d.fillRect(100, 210, 30, 15);
		g2d.setPaint(Constants.COLOR2);
		g2d.fillRect(100, 240, 30, 15);
		g2d.setPaint(Constants.COLOR3);
		g2d.fillRect(100, 270, 30, 15);
		g2d.setPaint(Constants.COLOR4);
		g2d.fillRect(100, 300, 30, 15);
		
		g2d.setPaint(Color.BLACK);
		for(int i = 0; i < PLAYER_TOTAL; i ++)
		{
			g2d.drawImage(player[i].getPiece().getBf(),150+70*i,100,null);
			Integer p = player[i].getMoney();
			g2d.drawString(p.toString(), 160+70*i, 180);
			int house[] = player[i].getHouse();
			for(int j = 0; j < 4; j ++)
			{
				p = house[j];
				g2d.drawString(p.toString(), 160+70*i, 210+30*j+10);
			}
		}
	}
	
	public void run()
	{
		player[playerRun].run();
		if(player[playerRun].getPiece().getLocation().getName().equals("bank"))
		{
			player[playerRun].setMoney((int) (player[playerRun].getMoney()*(1.05)));
			JOptionPane.showMessageDialog(null,"经过银行,获得现金"+(int)(player[playerRun].getMoney()*0.05));
			String s = player[playerRun].getName() + "  经过银行,获得现金"+(int)(player[playerRun].getMoney()*0.05);
			writeHistroy(s);
		}
		repaint();

	}
	/**
	 * 写记录
	 *
	 */
	public void writeHistroy(String s)
	{
		frame.writeHistroy(s);
	}
	
	public void setPlayerRun(int i)
	{
		playerRun = i;
	}
	
	public void run(int dice)
	{
		String s = player[playerRun].getName()+"  投掷到骰子"+dice+"\n"+ player[playerRun].getName()+" 开始移动";
		frame.writeHistroy(s);
		frame.playerRun(dice);
	}
	
	public static void main(String[] args)
	{
		MonopolyGame game = new MonopolyGame();
	}
	
	public int getPLAYER_TOTAL() {
		return PLAYER_TOTAL;
	}
	
	/**
	 * 处理信息
	 *
	 */
	public void doWith()
	{
		player[playerRun].getPiece().getLocation().doRun(this.player,playerRun);
		
		if(player[playerRun].getPiece().getLocation().getName().equals("prison")||
				player[playerRun].getPiece().getLocation().getName().equals("hospital"))
		{
			player[playerRun].setStopTime(2);
		}
		
		playerRun ++;
		playerRun %= PLAYER_TOTAL;
		String s;
		while(player[playerRun].getStopTime() != 0)
		{
			
			if(player[playerRun].getStopType() == Constants.HOSPITAL_TYPE)
			{
				s = player[playerRun].getName()+"还得老实得在医院待"+player[playerRun].getStopTime()+"天";
				JOptionPane.showMessageDialog(null, s);
			}
			else
			{
				s = player[playerRun].getName()+"还得老实得在监狱待"+player[playerRun].getStopTime()+"天";
				JOptionPane.showMessageDialog(null, s);
			}
			player[playerRun].setStopTime(player[playerRun].getStopTime()-1);
			frame.writeHistroy(s);
			playerRun ++;
			playerRun %= PLAYER_TOTAL;
		}
		
		checkEnd();
	}
	
	public void checkEnd()
	{
		for(int i = 0; i < this.PLAYER_TOTAL; i ++)
			if(player[i].getMoney() < 0)
			{
				JOptionPane.showMessageDialog(null, player[i].getName()+"破产了");
				deletePlayer(i);
			}
	}
	
	public void deletePlayer(int i)
	{
		this.PLAYER_TOTAL --;
		Player[] p = this.player;
		player = new Player[this.PLAYER_TOTAL];
		int j = 0; ;
		for(int ii = 0; ii < PLAYER_TOTAL; ii ++ )
		{
			if(j == i)
				j ++;
			player[ii] = p[j];
			j ++;
		}
		repaint();
	}
	public void onListener(String s) {
		// TODO Auto-generated method stub
		frame.writeHistroy(s);
		repaint();
	}
	
	public void repaint()
	{
		frame.repaint();
		if(Constants.isTest)
			this.onNotice();
	}

	public Player[] getPlayer() {
		return player;
	}

	public void setPlayer(Player[] player) {
		this.player = player;
	}
	
	public void dice(int dice)
	{
		frame.dice(dice);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -