中易网

JAVA五子棋判断输赢

答案:2  悬赏:50  
解决时间 2021-02-10 15:26
JAVA五子棋判断输赢
最佳答案
import java.awt.*;
import javax.swing.*;
public class ChessFrame extends JFrame{
private ChessPanel cp;
private ChessModel cm;

public ChessFrame(){
this.setTitle("五子棋");
cm=new ChessModel();
cp=new ChessPanel(cm);
this.add(cp);
}
public void showMe(){
this.setVisible(true);
this.setSize(480,400);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ChessFrame().showMe();
}
}

----------------------------------------
import javax.swing.*;
public class ChessModel {
private int width,height;
private int x=0,y=0;
private int [][] arrMapShow;
private boolean isOdd,isExist;

public ChessModel(){
this.isOdd=true;
PaneInit(20,15);
}

private void PaneInit(int width,int height){
this.width=width;
this.height=height;
arrMapShow=new int[width+1][height+1];
for(int i=0;i<=width;i++){
for(int j=0;j arrMapShow[i][j]=5;
}
}
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int[][] getArrMapShow() {
return arrMapShow;
}
public void setArrMapShow(int[][] arrMapShow) {
this.arrMapShow = arrMapShow;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
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 boolean getisOdd() {
return isOdd;
}
public void setisOdd(boolean isOdd) {
if(isOdd)
this.isOdd = true;
else
this.isOdd=false;
}
public boolean isExist() {
return isExist;
}
public void setExist(boolean isExist) {
this.isExist = isExist;
}

private boolean badxy(int x,int y){
if(x>=width+20||x<0)
return true;
return y>=height+20||y<0;
}
public boolean chessExist(int i,int j){
if(this.arrMapShow[i][j]==1||this.arrMapShow[i][j]==2)
return true;
return false;
}

public void redyplay(int x,int y){
if(badxy(x,y))
return;
if(chessExist(x,y))
return;
this.arrMapShow[x][y]=3;
}

public void play(int x,int y){
if(badxy(x,y))
return;
if(chessExist(x,y)){
this.isExist=true;
return;
}else
this.isExist=false;
if(getisOdd()){
setisOdd(false);
this.arrMapShow[x][y]=1;
}else{
setisOdd(true);
this.arrMapShow[x][y]=2;
}
}

//判断胜利的条件
public boolean judgeSuccess(int x,int y,boolean isodd){
int num=1;
int arrvalue;
int x_temp=x,y_temp=y;
if(isodd)
arrvalue=2;
else
arrvalue=1;
int x_temp1=x_temp,y_temp1=y_temp;
//判断右边
for(int i=1;i<=6;i++){
x_temp1+=1;
if(x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
//判断左边
x_temp1=x_temp;
for(int i=1;i<=6;i++){
x_temp1-=1;
if(x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;

//判断上方
x_temp1=x_temp;
y_temp1=y_temp;
num=1;
for(int i=1;i<=6;i++){
y_temp1-=1;
if(y_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
//判断下方
y_temp1=y_temp;
for(int i=1;i<=6;i++){
y_temp1+=1;
if(y_temp1>this.height)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;

//判断左上
x_temp1=x_temp;
y_temp1=y_temp;
num=1;
for(int i=1;i<=6;i++){
x_temp1-=1;
y_temp1-=1;
if(y_temp1<0 || x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
//判断右下
x_temp1=x_temp;
y_temp1=y_temp;
for(int i=1;i<=6;i++){
x_temp1+=1;
y_temp1+=1;
if(y_temp1>this.height || x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;

//判断右上
x_temp1=x_temp;
y_temp1=y_temp;
num=1;
for(int i=1;i<=6;i++){
x_temp1+=1;
y_temp1-=1;
if(y_temp1<0 || x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
//判断左下
x_temp1=x_temp;
y_temp1=y_temp;
for(int i=1;i<=6;i++){
x_temp1-=1;
y_temp1+=1;
if(y_temp1>this.height || x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;
return false;
}

//五子成线后的提示
public void showSuccess(JPanel jp){
JOptionPane.showMessageDialog(jp,"你赢了,好厉害!","win",
JOptionPane.INFORMATION_MESSAGE);
new ChessFrame().showMe();
}

}

----------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
public class ChessPanel extends JPanel {
private int width, height;
private ChessModel cm;
public ChessPanel(ChessModel mm) {
cm = mm;
width = cm.getWidth();
height = cm.getHeight();
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
int x=(e.getX()-10)/20;
int y=(e.getY()-10)/20;
cm.play(x,y);
repaint();
if(cm.judgeSuccess(x,y,cm.getisOdd())){
cm.showSuccess(null);
}
}
});
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for(int j=0;j<=height;j++){
for(int i=0;i int v=cm.getArrMapShow()[i][j];
draw(g,i,j,v);
}
}
}
public void setModel(ChessModel mm){
cm=mm;
width=cm.getWidth();
height=cm.getHeight();
}

public void draw(Graphics g,int i,int j,int v){
int x=20*i+20;
int y=20*j+20;

if(i!=width&&j!=height){
g.setColor(Color.black);
g.drawRect(x, y, 20, 20);
}
if(i!=width&&j!=height){
g.setColor(Color.black);
g.drawRect(x, y, 20, 20);
}
if(v==1){
g.setColor(Color.gray);
g.drawOval(x-8, y-8, 16, 16);
g.setColor(Color.black);
g.fillOval(x-8, y-8, 16, 16);

}
if(v==2){
g.setColor(Color.gray);
g.drawOval(x-8, y-8, 16, 16);
g.setColor(Color.white);
g.fillOval(x-8, y-8, 16, 16);
}
if(v==3){
g.setColor(Color.cyan);
g.drawOval(x-8, y-8, 316, 16);
}

}
}

三个类,可以直接使用了
全部回答
没用过java,说个思路希望对你有帮助 准备两个变量 一个 黑赢变量 一个白赢变量 为棋盘上每个点准备一个变量 叫点变量 该变量空为0 下白棋加1 下黑棋加2 看看拥有超过4个点的线(包括斜线)有多少个 作为外循环次数 外循环的工作 要6次重置起始点的位置,和内循环次数,和内循环里面的起始点内存地址 以及距离一下个点的内存地址的距离,每循环一次是加还是减也要知道 规律会变化6次 内循环的工作 1.测试点 如果为0则把黑赢和白赢变量清0 如果为1把白赢变量加1黑赢变量清0 如果为2把黑赢变量加1白赢变量清0 2.测试黑赢和白赢变量是否为5 哪个为5就是哪个赢了 结束循环 3.根据外循环提供的规律调整指向点变量的指针,和距离一下个点变量在内存里的距离 判断当前点周围也可以,不过涉及到边界问题, 我觉得那样更复杂
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
经济学动态是什么水平的期刊
http://www.tudou.com/programs/view/J3AJB7u
小数乘小数,小数点要不要对齐?如果对齐会错
健之佳健康药房地址在什么地方,想过去办事
为什么HF酸这么弱,可以腐蚀二氧化硅
工人叔叔把一根高是1米的圆柱形木料沿底面直
尚官线/G319(路口)地址在哪,我要去那里办事
交房屋维修基金单子在什么地方开
车前挡风玻璃清洁剂没了,可以自己➕吗,怎么
有人说报喜我该怎么回答
浦发银行和万科地产的股票代码是多少呢?
本田飞度左边有暖气,右边是冷风
几年来晚上做梦老梦见蛇是怎么回事
道宽海尔专卖店地址在什么地方,想过去办事
菲尼克斯接线端子有什么优点?
推荐资讯
吸血鬼日记中Damon和Elena跳舞时的插曲是什么
oppoR7plus 在保修期内 屏幕摔坏了 需要修多
福城四件宝特产华天店地址在哪,我要去那里办
数学两个字设计字体
求大神教过手机版植物大战僵尸2海盗港湾(最新
在比例尺为一比三百万的地图上量得甲乙两地间
冀凡外墙保温地址在什么地方,想过去办事
银海商务酒店怎么去啊,有知道地址的么
如何查询云南省曲靖市的住房公积金?
a=12 b=5 求c
气候变化危与机观后感
有谁有touch炫舞的号?用过也行,借我用几个
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?