java 简单版五子棋 递归算法 但是有点小问题 求大神帮助一下
来源:互联网 发布:域名授权系统源码 编辑:程序博客网 时间:2024/05/22 04:52
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ChessGame extends JFrame implements MouseListener {
private int blackcount=0;
private int whitecount=0;
private boolean flag=true;
private boolean whitewin;
private boolean blackwin;
private boolean flash=false;
public Timer time;
private ArrayList<mypoint>blacklist;
private ArrayList<mypoint>whitelist;
public ChessGame(){
setSize(450,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
blacklist=new ArrayList<mypoint>();
whitelist=new ArrayList<mypoint>();
Container contain=this.getContentPane();
JPanel toolbar=new JPanel();//工具面板实例化
//toolbar.setOpaque(true);
//三个按钮初始化
JButton start=new JButton("重新开始");
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reset();
}
});
JButton undo=new JButton("悔棋");
undo.addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseClicked(MouseEvent e) {//仅当鼠标单击时响应
undo();
}
});
//将工具面板按钮用FlowLayout布局
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
//将三个按钮添加到工具面板
toolbar.add(start);
toolbar.add(undo);
start.doClick();
add(toolbar,BorderLayout.SOUTH);
this.addMouseListener(this);
setVisible(true);
}
public void undo(){//悔棋
if(flag==true){
flash=true;
whitelist.remove(whitelist.size()-1);
flag=false;
repaint();
}else{
blacklist.remove(blacklist.size()-1);
flag=true;
flash=true;
repaint();
}
}
public void reset(){
whitelist=new ArrayList<mypoint>();
blacklist=new ArrayList<mypoint>();
flash=true;
whitecount=0;
blackcount=0;
repaint();
}
@Override
public void paint(Graphics g) {
if(flash==true){
super.paint(g);
}
flash=false;
g.setColor(Color.WHITE);
g.fillRect(0, 0, 420, 420);
g.setColor(Color.DARK_GRAY);
g.drawLine(5, 5, 420, 5);
g.drawLine(5, 5, 5, 420);
g.drawLine(420, 5, 420, 420);
g.drawLine(5, 420, 420, 420);
for(int i=1;i<=21;i++){
g.drawLine(5, 20*i, 420, 20*i);
g.drawLine(20*i, 5, 20*i, 420);
}
g.setColor(Color.BLACK);
for(int i=0;i<blacklist.size();i++){
g.fillOval(blacklist.get(i).getX()*20+12,blacklist.get(i).getY()*20+32, 16, 16);
}
g.setColor(Color.RED);
for(int i=0;i<whitelist.size();i++){
g.fillOval(whitelist.get(i).getX()*20+12,whitelist.get(i).getY()*20+32, 16, 16);
}
this.checkwin(blacklist, 0, 0, 0,0);
this.checkwin(whitelist, 0, 0, 0,1);
g.setColor(Color.black);
String str1 = "黑方胜利局数为:" + blackcount;
g.drawString(str1, 10, 450);
String str2 = "红方胜利局数为:" + whitecount;
g.drawString(str2, 150, 450);
}
public static void main(String[] args){
ChessGame snake=new ChessGame();
}
public boolean checkExist(mypoint a){
for(int i=0;i<whitelist.size();i++){
if(whitelist.get(i).getX()==a.getX()&&whitelist.get(i).getY()==a.getY()){
return true;
}
}
for(int i=0;i<blacklist.size();i++){
if(blacklist.get(i).getX()==a.getX()&&blacklist.get(i).getY()==a.getY()){
return true;
}
}
return false;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
double x=e.getX();
double y=e.getY();
if(x>=5&&x<=410&&y>=5&&y<=410){
double k=20;
x=x/k-1;
y=y/k-2;
int ax=(int) Math.round(x);
int ay=(int) Math.round(y);
if(checkExist(new mypoint(ax,ay))){
JOptionPane.showMessageDialog(null, "该位置已有棋子!");
repaint();
}else{
if(flag==true){
blacklist.add(new mypoint(ax,ay));
}
if(flag==false){
whitelist.add(new mypoint(ax,ay));
}
flag=!flag;
repaint();
}
}
}
public void checkwin(ArrayList<mypoint> list,int pos,int direct,int count,int type){
if(count==0){
for(int i=0;i<list.size();i++){
for(int j=0;j<list.size();j++){
if(list.get(i).getY()==list.get(j).getY()&&(list.get(j).getX()-list.get(i).getX()==-1)){
direct=2;
count++;
checkwin(list,j,2,count,type);
count=0;
}
if(list.get(i).getX()==list.get(j).getX()&&(list.get(j).getY()-list.get(i).getY()==-1)){
direct=0;
count++;
checkwin(list,j,0,count,type);
count=0;
}
if(list.get(j).getY()==list.get(i).getY()-1&&(list.get(j).getX()-list.get(i).getX()==1)){
direct=4;
count++;
checkwin(list,j,4,count,type);
count=0;
}
if(list.get(j).getY()==list.get(i).getY()+1&&(list.get(j).getX()-list.get(i).getX()==1)){
direct=5;
count++;
checkwin(list,j,5,count,type);
count=0;
}
}
}
}
else{
if(direct==4){
for(int i=0;i<list.size();i++){
if(i!=pos){
if(list.get(i).getX()==list.get(pos).getX()+1&&(list.get(i).getY()-list.get(pos).getY()==-1)){
count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}
if(direct==5){
for(int i=0;i<list.size();i++){
if(i!=pos){
if(list.get(i).getX()==list.get(pos).getX()+1&&(list.get(i).getY()-list.get(pos).getY()==1)){
count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}
if(direct==0){
for(int i=0;i<list.size();i++){
if(i!=pos){
if(list.get(pos).getX()==list.get(i).getX()&&(list.get(i).getY()-list.get(pos).getY()==-1)){
count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}
if(direct==2){
for(int i=0;i<list.size();i++){
if(i!=pos){
if(list.get(pos).getY()==list.get(i).getY()&&(list.get(i).getX()-list.get(pos).getX()==-1)){
count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}
}
}
public void restart(){
whitelist=new ArrayList<mypoint>();
blacklist=new ArrayList<mypoint>();
flash=true;
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class mypoint{
int x;
int y;
public mypoint(int a,int b){
this.x=a;
this.y=b;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ChessGame extends JFrame implements MouseListener {
private int blackcount=0;
private int whitecount=0;
private boolean flag=true;
private boolean whitewin;
private boolean blackwin;
private boolean flash=false;
public Timer time;
private ArrayList<mypoint>blacklist;
private ArrayList<mypoint>whitelist;
public ChessGame(){
setSize(450,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
blacklist=new ArrayList<mypoint>();
whitelist=new ArrayList<mypoint>();
Container contain=this.getContentPane();
JPanel toolbar=new JPanel();//工具面板实例化
//toolbar.setOpaque(true);
//三个按钮初始化
JButton start=new JButton("重新开始");
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reset();
}
});
JButton undo=new JButton("悔棋");
undo.addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseClicked(MouseEvent e) {//仅当鼠标单击时响应
undo();
}
});
//将工具面板按钮用FlowLayout布局
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
//将三个按钮添加到工具面板
toolbar.add(start);
toolbar.add(undo);
start.doClick();
add(toolbar,BorderLayout.SOUTH);
this.addMouseListener(this);
setVisible(true);
}
public void undo(){//悔棋
if(flag==true){
flash=true;
whitelist.remove(whitelist.size()-1);
flag=false;
repaint();
}else{
blacklist.remove(blacklist.size()-1);
flag=true;
flash=true;
repaint();
}
}
public void reset(){
whitelist=new ArrayList<mypoint>();
blacklist=new ArrayList<mypoint>();
flash=true;
whitecount=0;
blackcount=0;
repaint();
}
@Override
public void paint(Graphics g) {
if(flash==true){
super.paint(g);
}
flash=false;
g.setColor(Color.WHITE);
g.fillRect(0, 0, 420, 420);
g.setColor(Color.DARK_GRAY);
g.drawLine(5, 5, 420, 5);
g.drawLine(5, 5, 5, 420);
g.drawLine(420, 5, 420, 420);
g.drawLine(5, 420, 420, 420);
for(int i=1;i<=21;i++){
g.drawLine(5, 20*i, 420, 20*i);
g.drawLine(20*i, 5, 20*i, 420);
}
g.setColor(Color.BLACK);
for(int i=0;i<blacklist.size();i++){
g.fillOval(blacklist.get(i).getX()*20+12,blacklist.get(i).getY()*20+32, 16, 16);
}
g.setColor(Color.RED);
for(int i=0;i<whitelist.size();i++){
g.fillOval(whitelist.get(i).getX()*20+12,whitelist.get(i).getY()*20+32, 16, 16);
}
this.checkwin(blacklist, 0, 0, 0,0);
this.checkwin(whitelist, 0, 0, 0,1);
g.setColor(Color.black);
String str1 = "黑方胜利局数为:" + blackcount;
g.drawString(str1, 10, 450);
String str2 = "红方胜利局数为:" + whitecount;
g.drawString(str2, 150, 450);
}
public static void main(String[] args){
ChessGame snake=new ChessGame();
}
public boolean checkExist(mypoint a){
for(int i=0;i<whitelist.size();i++){
if(whitelist.get(i).getX()==a.getX()&&whitelist.get(i).getY()==a.getY()){
return true;
}
}
for(int i=0;i<blacklist.size();i++){
if(blacklist.get(i).getX()==a.getX()&&blacklist.get(i).getY()==a.getY()){
return true;
}
}
return false;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
double x=e.getX();
double y=e.getY();
if(x>=5&&x<=410&&y>=5&&y<=410){
double k=20;
x=x/k-1;
y=y/k-2;
int ax=(int) Math.round(x);
int ay=(int) Math.round(y);
if(checkExist(new mypoint(ax,ay))){
JOptionPane.showMessageDialog(null, "该位置已有棋子!");
repaint();
}else{
if(flag==true){
blacklist.add(new mypoint(ax,ay));
}
if(flag==false){
whitelist.add(new mypoint(ax,ay));
}
flag=!flag;
repaint();
}
}
}
public void checkwin(ArrayList<mypoint> list,int pos,int direct,int count,int type){
if(count==0){
for(int i=0;i<list.size();i++){
for(int j=0;j<list.size();j++){
if(list.get(i).getY()==list.get(j).getY()&&(list.get(j).getX()-list.get(i).getX()==-1)){
direct=2;
count++;
checkwin(list,j,2,count,type);
count=0;
}
if(list.get(i).getX()==list.get(j).getX()&&(list.get(j).getY()-list.get(i).getY()==-1)){
direct=0;
count++;
checkwin(list,j,0,count,type);
count=0;
}
if(list.get(j).getY()==list.get(i).getY()-1&&(list.get(j).getX()-list.get(i).getX()==1)){
direct=4;
count++;
checkwin(list,j,4,count,type);
count=0;
}
if(list.get(j).getY()==list.get(i).getY()+1&&(list.get(j).getX()-list.get(i).getX()==1)){
direct=5;
count++;
checkwin(list,j,5,count,type);
count=0;
}
}
}
}
else{
if(direct==4){
for(int i=0;i<list.size();i++){
if(i!=pos){
if(list.get(i).getX()==list.get(pos).getX()+1&&(list.get(i).getY()-list.get(pos).getY()==-1)){
count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}
if(direct==5){
for(int i=0;i<list.size();i++){
if(i!=pos){
if(list.get(i).getX()==list.get(pos).getX()+1&&(list.get(i).getY()-list.get(pos).getY()==1)){
count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}
if(direct==0){
for(int i=0;i<list.size();i++){
if(i!=pos){
if(list.get(pos).getX()==list.get(i).getX()&&(list.get(i).getY()-list.get(pos).getY()==-1)){
count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}
if(direct==2){
for(int i=0;i<list.size();i++){
if(i!=pos){
if(list.get(pos).getY()==list.get(i).getY()&&(list.get(i).getX()-list.get(pos).getX()==-1)){
count=count+1;
if(count==4){
if(type==0){
System.out.println("blackwin!");
blackcount++;
JOptionPane.showMessageDialog(null, "黑方赢了!");
restart();
}if(type==1){
System.out.println("whitewin!");
whitecount++;
JOptionPane.showMessageDialog(null, "红方赢了!");
restart();
}
}else{
checkwin(list,i,direct,count,type);
}
}
}
}
}
}
}
public void restart(){
whitelist=new ArrayList<mypoint>();
blacklist=new ArrayList<mypoint>();
flash=true;
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class mypoint{
int x;
int y;
public mypoint(int a,int b){
this.x=a;
this.y=b;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}
其中 判断是否五子连在一起的算法checkwin是根据每一个棋子的位置,判断该棋子的纵向和横向以及两个斜向上是否有相同颜色的棋子 算法是可以模拟五子棋的运行的 但是如果假设 在一个方向上有三个黑子 隔了一个空格 之后有三个黑子 如果在该空格下一个黑子,程序的递归就会判定黑方赢了三次 并不是一次 导致记录黑红方胜利的局数错误
0 0
- java 简单版五子棋 递归算法 但是有点小问题 求大神帮助一下
- 这是一个有点小问题的,语音录制并显示波形的程序,求大神指点
- 求大神帮助
- 求大神帮助!!!!
- 求大神帮助
- ORACLE 跪求技术问题求大神 帮助
- 求帮助啊 求大神帮助
- 第一次使用SVN出错。网上搜了一下也有人遇到同样的问题但是都没有合理的解决方案,求大神。
- 五子棋问题;求解释;
- 工作之余,整理了一下平时处理数据的一些常用的,简单的方法。希望对学习JAVA的朋友有点帮助
- 大神帮忙 谢谢了 求哪里错了 新手 问题有点幼稚 谢谢了
- 简单五子棋算法
- Vickate_AES加密与解密(Objective-C版),暂未实现与java的交互(求大神帮助)
- 超简单小算法求圆面积(测多组数据及小用一下正则表达式)(入门)
- java小项目--五子棋
- 安卓开发 小问题,求大神解答~
- java 简单递归问题
- irprot问题请大神帮助
- 1056. Mice and Rice (25)
- 前端网站收集
- Android退出启动了多个Activity的应用程序
- Kali linux下拓实N95外置网卡驱动安装教程
- 权限管理——用户认证和用户授权
- java 简单版五子棋 递归算法 但是有点小问题 求大神帮助一下
- 4. Median of Two Sorted Arrays
- mysql简单入门三——创建表相关语句
- 【Get深一度】ADS2011.10在win10系统中闪退问题解决方法
- Android-逆向工程
- 关于@synthesize 和 @dynamic 关键字
- 网络信息系统安全检测方案设计(上)
- 第3章 垃圾收集器与内存分配策略
- VS助手安装失败---VS番茄助手安装成功重启VS却没有效果!!!