JAVA五子棋判断输赢
答案:2 悬赏:50
解决时间 2021-02-10 15:26
- 提问者网友:长安小才冯
- 2021-02-10 07:04
JAVA五子棋判断输赢
最佳答案
- 二级知识专家网友:山鬼偶尔也合群
- 2021-02-10 08:12
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);
}
}
}
三个类,可以直接使用了
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
}
}
}
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
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);
}
}
}
三个类,可以直接使用了
全部回答
- 1楼网友:woshuo
- 2021-02-10 08:31
没用过java,说个思路希望对你有帮助
准备两个变量 一个 黑赢变量 一个白赢变量
为棋盘上每个点准备一个变量 叫点变量
该变量空为0 下白棋加1 下黑棋加2
看看拥有超过4个点的线(包括斜线)有多少个 作为外循环次数
外循环的工作
要6次重置起始点的位置,和内循环次数,和内循环里面的起始点内存地址
以及距离一下个点的内存地址的距离,每循环一次是加还是减也要知道
规律会变化6次
内循环的工作
1.测试点
如果为0则把黑赢和白赢变量清0
如果为1把白赢变量加1黑赢变量清0
如果为2把黑赢变量加1白赢变量清0
2.测试黑赢和白赢变量是否为5 哪个为5就是哪个赢了 结束循环
3.根据外循环提供的规律调整指向点变量的指针,和距离一下个点变量在内存里的距离
判断当前点周围也可以,不过涉及到边界问题,
我觉得那样更复杂
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯