大家好,又见面了,我是你们的朋友全栈君。
展开全部
简单的五子棋程序
新建一个chess类,其中代码如下
package work;
import java.awt.Color;
public class Chess {
public static final int diamter=30;
private int x;//在网格中的索引,0~e68a84e8a2ad62616964757a686964616f3133323865663715
private int y;//在网格中的索引,0~15
private Color color;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Chess(int x, int y, Color color) {
super();
this.x = x;
this.y = y;
this.color = color;
}
}
然后在同一个包中新建FirstFrame类。代码如下
package work;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
//主函数
public class FirstFrame{
public static void main(String[] args) {
FirstFrame1 f=new FirstFrame1();
f.setVisible(true);
}
}
//窗体函数
class FirstFrame1 extends JFrame implements ActionListener{
JMenuBar menuBar;
JMenu sysMenu;
JMenuItem startMenuItem;
JMenuItem backMenuItem;
JMenuItem exitMenuItem;
DrawPanel myPanel=new DrawPanel();
public FirstFrame1(){
super(“娃哈哈”);
add(myPanel);
menuBar=new JMenuBar();
setJMenuBar(menuBar);
sysMenu=new JMenu(“系统(s)”);
sysMenu.setMnemonic(‘s’);
menuBar.add(sysMenu);
startMenuItem=new JMenuItem(“开始”);
backMenuItem=new JMenuItem(“悔棋”);
exitMenuItem=new JMenuItem(“退出”);
sysMenu.add(startMenuItem);
sysMenu.add(backMenuItem);
sysMenu.addSeparator();
sysMenu.add(exitMenuItem);
startMenuItem.addActionListener(this);
backMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
super.setSize(600,650);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
Object obj=e.getSource();
if(obj==startMenuItem){
myPanel.start(); //DrawPanel p=new DrawPanel();是错的;
}else if(obj==backMenuItem){
myPanel.back();
}else{
System.exit(0);
}
}
}
最后新建一个DrawPanel类代码如下
package work;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import sun.awt.geom.AreaOp.AddOp;
//画柄;
class DrawPanel extends JPanel implements MouseListener,MouseMotionListener{
final int MARGIN_TOP=60;
final int MARGIN_LEFT=30;
final int CELL_WIDTH=35;
final int COLS=15;
final int ROWS=15;
Chess[] chessList=new Chess[(COLS 1)*(ROWS 1)];
int chessCount=0;
boolean isBlack=true;
boolean gameOver=false;
public DrawPanel(){ //构造函数
Color color=new Color(200,250,200);
setBackground(color);
this.addMouseMotionListener(this);
this.addMouseListener(this);
}
public void start(){
for(int i=0;i
chessList[i]=null;
isBlack=true;
gameOver=false;
chessCount=0;
repaint();
}
public void back(){
if (chessCount==0||gameOver==true)
return;
chessList[chessCount-1]=null;
chessCount–;
isBlack=!isBlack;
repaint();
}
@Override
protected void paintComponent(Graphics g) { //方法重写
// TODO Auto-generated method stub
super.paintComponent(g);
for(int i=0;i<=ROWS;i ){
g.drawLine(MARGIN_LEFT, MARGIN_TOP i*CELL_WIDTH, MARGIN_LEFT COLS*CELL_WIDTH, MARGIN_TOP i*CELL_WIDTH);
}
for(int i=0;i<=COLS;i ){
g.drawLine(MARGIN_LEFT i*CELL_WIDTH, MARGIN_TOP, MARGIN_LEFT i*CELL_WIDTH, MARGIN_TOP ROWS*CELL_WIDTH);
}
for(int i=0;i
Chess chess=chessList[i];
int xPos=MARGIN_LEFT CELL_WIDTH*chess.getX()-Chess.diamter/2;
int yPos=MARGIN_TOP CELL_WIDTH*chess.getY()-Chess.diamter/2;
g.setColor(chess.getColor());
g.fillOval(xPos,yPos, Chess.diamter, Chess.diamter);
if(i==chessCount-1){
g.setColor(Color.red);
g.drawRect(xPos, yPos, Chess.diamter, Chess.diamter);
}
}
if(isBlack)
g.setColor(Color.black);
else
g.setColor(Color.white);
g.fillOval(8*CELL_WIDTH, CELL_WIDTH/2,Chess.diamter, Chess.diamter);
String colorStr=isBlack==false?”白方下”:”黑方下”;
g.setColor(Color.blue);
g.drawString(colorStr ” 第” chessCount/2 ”步”, 9*CELL_WIDTH, CELL_WIDTH);
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
super.setCursor(new Cursor(Cursor.HAND_CURSOR));
int x=(e.getX()-MARGIN_LEFT Chess.diamter/2)/CELL_WIDTH;
int y=(e.getY()-MARGIN_TOP Chess.diamter/2)/CELL_WIDTH;
if(x<0||y<0||x>COLS||y>ROWS) //超出棋盘边界
super.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
if(findChess(x,y)) //下过的地方不能再下
super.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
if(gameOver)
super.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public void mousePressed(MouseEvent e) {
int x=(e.getX()-MARGIN_LEFT Chess.diamter/2)/CELL_WIDTH;
int y=(e.getY()-MARGIN_TOP Chess.diamter/2)/CELL_WIDTH;
if(x<0||y<0||x>COLS||y>ROWS) //超出棋盘边界
return;
Color color=Color.black;
if(!isBlack) // 变颜色
color=Color.white;
if(findChess(x,y)) //下过的地方不能再下
return;
if(gameOver)
return;
chessList[chessCount]=new Chess(x,y,color);
repaint(); //重绘
isBlack=!isBlack;
chessCount ;
String colorStr=color==Color.white?”白方”:”黑方”;
int[] count={0,0,0,0};
for(int i=x-1;i>=0;i–){
if(findChess(i,y,color)){
count[0] ;
}
else
break;
}
for(int i=x 1;i<=COLS;i ){
if(findChess(i,y,color)){
count[0] ;
}
else
break;
}
for(int i=y-1;i>=0;i–){
if(findChess(x,i,color)){
count[1] ;
}
else
break;
}
for(int i=y 1;i<=ROWS;i ){
if(findChess(x,i,color)){
count[2] ;
}
else
break;
}
for(int i=x 1,j=y 1;i<=COLS&&j<=ROWS;i ,j ){
if(findChess(i,j,color)){
count[3] ;
}
else
break;
}
for(int i=x-1,j=y-1;i>=0&&j>=0;i–,j–){
if(findChess(i,j,color)){
count[3] ;
}
else
break;
}
for(int i=x 1,j=y-1;i<=COLS&&j>=0;i ,j–){
if(findChess(i,j,color)){
count[3] ;
}
else
break;
}
for(int i=x-1,j=y 1;i>=0&&j<=ROWS;i–,j ){
if(findChess(i,j,color)){
count[3] ;
}
else
break;
}
for(int i=0;i<4;i ){
if(count[i]>=4){
gameOver=true;
JOptionPane.showMessageDialog(this, “恭喜” colorStr ”胜利”);
return;
}
}
System.out.println(e.getX() ”,” e.getY());
}
public boolean findChess(int x,int y){
for(int i=0;i
if(chessList[i].getX()==x && chessList[i].getY()==y)
return true;
}
return false;
}
public boolean findChess(int x,int y,Color color){
for(int i=0;i
if(chessList[i].getX()==x && chessList[i].getY()==y && chessList[i].getColor()==color)
return true;
}
return false;
}
}
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158059.html原文链接:https://javaforall.cn