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

📄 reversi.java

📁 REVERSI ai parts for java
💻 JAVA
字号:
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

/**
 * @author Bengi Mizrahi
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class Reversi extends Frame {

	public class BoardMouseListener extends MouseAdapter {
		public void mouseReleased (MouseEvent me) {
			if (board.playingSide == Board.WHITE) {
				int i = me.getX () / 51;
				int j = (me.getY () - 23) / 51;
				board.performMove (j, i);
				repaint ();
				pl1.play ();
			}
		}
	}
	
	static BufferedImage image;
	Board board;
	Player pl1;
	Player pl2;
	
	public Reversi () {
		pl1 = new AI (Board.BLACK);
		pl2 = new Human (Board.WHITE);
		board = new Board (Board.BLACK, pl1, pl2);
		pl1.setBoard (board);
		pl2.setBoard (board);
		
		try {image = ImageIO.read (new File (".\\images\\board.png"));}
		catch (Exception e) {e.printStackTrace ();}
		setBounds (100, 100, image.getWidth () + getInsets ().left, image.getHeight () + getInsets ().top + 26);
		addWindowListener (
			new WindowAdapter () {
				public void windowClosing (WindowEvent e) {
					System.exit (0);
				}
			}
		);
		addMouseListener (new BoardMouseListener ());
		setResizable (false);
		setVisible (true);
		
		pl1.play ();
	}
	
	public void paint (Graphics g) {
		g.drawImage (image, 0, getInsets ().top, this);
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				Rectangle r = getDiskCoordinates (i, j);
				switch (board.getCell (i, j)) {
					case Board.EMPTY:
						break;
					case Board.WHITE:
						g.setColor (Color.WHITE);
						g.fillOval (r.x, r.y, r.width, r.height);
						break;
					case Board.BLACK:
						g.setColor (Color.BLACK);
						g.fillOval (r.x, r.y, r.width, r.height);
						break;
				}
			}
		}
	}

	public Rectangle getDiskCoordinates (int i, int j) {
		return new Rectangle ((j * 51) + getInsets ().left + 2, (i * 51) + getInsets ().top + 5, 41, 41);
	}
	
	public static void main(String[] args) {
		Reversi b = new Reversi ();
		b.repaint ();
	}
}

⌨️ 快捷键说明

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