「java棋牌算法」java棋类游戏

博主:adminadmin 2023-03-22 14:09:07 478

本篇文章给大家谈谈java棋牌算法,以及java棋类游戏对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

求JAVA 24点游戏算法,界面和发牌器已弄好,求算法

package zhidao;

import java.util.Scanner;

public class Game21

{

public static void main ( String[] args )

{

System.out.println ("游戏规则:");

System.out.println ("开始游戏后屏幕上将出现一个随机数字");

System.out.println ("按a可以随机增加一个1-10范围内的一个数字");

System.out.println ("按s则揭晓你和电脑对决的结果!");

System.out.println ("如果你的数字大于21点,则游戏直接结束!");

int rand = (int) ( Math.random () * 10 + 1 );

int sum1 = rand, sum2 = 0, which = 0; // 0电脑,1我

System.out.println ("系统先给出1个数字: " + rand);

System.out.print ("轮到你了: ");

Scanner scanner = new Scanner (System.in);

while (true)

{

String input = "";

if (which == 0)

{

input = scanner.next ();

}

if (which == 0  "a".equals (input))

{

which = 1;

int r = (int) ( Math.random () * 10 + 1 );

sum2 += r;

System.out.println ("您抽到的数字是: " + r);

if (sum2  21)

{

System.out.println ("你的点数为 " + sum2 + " , 超过了最大限制 21 点,游戏提前结束!");

break;

}

System.out.print ("该电脑了: a\n");

}

else if (which == 1)

{

which = 0;

int r = (int) ( Math.random () * 10 + 1 );

sum1 += r;

System.out.println ("电脑抽到的数字是: " + r);

if (sum1  21)

{

System.out.println ("电脑点数为 " + sum1 + " , 超过了最大限制 21 点,游戏提前结束!");

break;

}

System.out.print ("轮到你了: ");

}

else if (which == 0  "s".equals (input))

{

System.out.println ("电脑共有:  " + sum1 + " 点");

System.out.println ("你共有: " + sum2 + " 点");

if (sum1  sum2)

{

System.out.println ("YOU LOSE !!!");

}

else if (sum1  sum2)

{

System.out.println ("YOU WIN !!!");

}

else

{

System.out.println ("DRAW GAME !!!");

}

System.out.print ("轮到你了: ");

which = 0;

}

}

scanner.close ();

}

}

求java围棋提子算法

import java.awt.*;import java.awt.event.*;

//创建棋盘的类:

class ChessPad extends Panel implements MouseListener,ActionListener

{ int x=-1,y=-1, 棋子颜色=1; //控制棋子颜色的成员变量。

Button button=new Button("重新开局"); //控制重新开局的按扭。

TextField text_1=new TextField("请黑棋下子"),

text_2=new TextField(); //提示下棋的两个文本框。

ChessPad()

{setSize(440,440);

setLayout(null);setBackground(Color.pink);

addMouseListener(this);add(button);button.setBounds(10,5,60,26);

button.addActionListener(this);

add(text_1);text_1.setBounds(90,5,90,24);

add(text_2);text_2.setBounds(290,5,90,24);

text_1.setEditable(false);text_2.setEditable(false);

}

public void paint(Graphics g) //绘制围棋棋盘的外观。

{for(int i=40;i=380;i=i+20)

{g.drawLine(40,i,400,i);}

g.drawLine(40,400,400,400);

for(int j=40;j=380;j=j+20)

{g.drawLine(j,40,j,400);}

g.drawLine(400,40,400,400);

g.fillOval(97,97,6,6); g.fillOval(337,97,6,6);

g.fillOval(97,337,6,6);g.fillOval(337,337,6,6);

g.fillOval(217,217,6,6);

}

public void mousePressed(MouseEvent e) //当按下鼠标左键时下棋子。

{ if(e.getModifiers()==InputEvent.BUTTON1_MASK)

{ x=(int)e.getX();y=(int)e.getY(); //获取按下鼠标时的坐标位置。

ChessPoint_black chesspoint_black=new ChessPoint_black(this);

ChessPoint_white chesspoint_white=new ChessPoint_white(this);

int a=(x+10)/20,b=(y+10)/20;

if(x/202||y/202||x/2019||y/2019) //棋盘以外不下棋子。

{}

else{ if(棋子颜色==1) //当棋子颜色是1时下黑棋子。

{this.add(chesspoint_black);

chesspoint_black.setBounds(a*20-7,b*20-7,16,16);

棋子颜色=棋子颜色*(-1);

text_2.setText("请白棋下子");

text_1.setText("");

}

else if(棋子颜色==-1) //当棋子颜色是-1时下白棋子。

{this.add(chesspoint_white);

chesspoint_white.setBounds(a*20-7,b*20-7,16,16);

棋子颜色=棋子颜色*(-1);

text_1.setText("请黑棋下子");

text_2.setText("");

}

}

}

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e){}

public void actionPerformed(ActionEvent e)

{this.removeAll();棋子颜色=1;

add(button);button.setBounds(10,5,60,26);

add(text_1);text_1.setBounds(90,5,90,24);

text_2.setText("");text_1.setText("请黑棋下子");

add(text_2);text_2.setBounds(290,5,90,24);

}

}

//负责创建黑色棋子的类:

class ChessPoint_black extends Canvas implements MouseListener

{ ChessPad chesspad=null; //棋子所在的棋盘。

ChessPoint_black(ChessPad p)

{setSize(20,20);chesspad=p; addMouseListener(this);

}

public void paint(Graphics g) //绘制棋子的大小。

{ g.setColor(Color.black);g.fillOval(0,0,14,14);

}

public void mousePressed(MouseEvent e)

{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)

{chesspad.remove(this);//当用鼠标右键点击棋子时,从棋盘中去掉该棋子(悔棋)。

chesspad.棋子颜色=1;

chesspad.text_2.setText("");chesspad.text_1.setText("请黑棋下子");

}

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e)

{ if(e.getClickCount()=2)

chesspad.remove(this); //当用左键双击该棋子时,吃掉该棋子。

}

}

//负责创建白色棋子的类:

class ChessPoint_white extends Canvas implements MouseListener

{

ChessPad chesspad=null;

ChessPoint_white(ChessPad p)

{setSize(20,20);addMouseListener(this);

chesspad=p;

}

public void paint(Graphics g)

{ g.setColor(Color.white);g.fillOval(0,0,14,14);

}

public void mousePressed(MouseEvent e)

{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)

{chesspad.remove(this);chesspad.棋子颜色=-1;

chesspad.text_2.setText("请白棋下子"); chesspad.text_1.setText("");

}

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e)

{if(e.getClickCount()=2)

chesspad.remove(this);

}

}

public class Chess extends Frame //添加棋盘的窗口。

{ChessPad chesspad=new ChessPad();

Chess()

{setSize(600,600);

setVisible(true);

setLayout(null);

Label label=

new Label("单击下棋子,双击吃棋子,右击棋子悔棋",Label.CENTER);

add(label);label.setBounds(70,55,440,26);

label.setBackground(Color.orange);

add(chesspad);chesspad.setBounds(70,90,440,440);

addWindowListener(new WindowAdapter()

{public void windowClosing(WindowEvent e){System.exit(0);}

});

}

public static void main(String args[])

{ Chess chess=new Chess();

}

}

java五子棋有哪些算法

以下是现写的 实现了两人对战 自己复制后运行把 没什么难度 类名 Games

import java.util.Scanner;

public class Games {

private String board[][];

private static int SIZE = 17;

private static String roles = "A玩家";

//初始化数组

public void initBoard() {

board = new String[SIZE][SIZE];

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

// if(i==0){

// String str = "";

// str += j+" ";

// board[i][j]= str;

// }else if(i!=0j==0){

// String str = "";

// str += i+" ";

// board[i][j]= str;

// }else{

board[i][j] = "╋";

// }

}

}

}

//输出棋盘

public void printBoard() {

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

System.out.print(board[i][j]);

}

System.out.println();

}

}

//判断所下棋子位置是否合理

public boolean isOk(int x, int y) {

boolean isRight = true;

if (x = 16 || x 1 || y = 16 | y 1) {

//System.out.println("输入错误,请从新输入");

isRight = false;

}

if (board[x][y].equals("●") || board[x][y].equals("○")) {

isRight = false;

}

return isRight;

}

//判断谁赢了

public void whoWin(Games wz) {

// 从数组挨个查找找到某个类型的棋子就从该棋子位置向右,向下,斜向右下 各查找5连续的位置看是否为5个相同的

int xlabel;// 记录第一次找到某个棋子的x坐标

int ylabel;// 记录第一次找到某个棋子的y坐标

// ●○╋

// 判断人是否赢了

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

if (board[i][j].equals("○")) {

xlabel = i;

ylabel = j;

// 横向找 x坐标不变 y坐标以此加1连成字符串

String heng = "";

if (i + 5 SIZE j + 5 SIZE) {

for (int k = j; k j + 5; k++) {

heng += board[i][k];

}

if (heng.equals("○○○○○")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

// 向下判断y不变 x逐增5 连成字符串

String xia = "";

for (int l = j; l i + 5; l++) {

xia += board[l][j];

// System.out.println(xia);

}

if (xia.equals("○○○○○")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

// 斜向右下判断

String youxia = "";

for (int a = 1; a = 5; a++) {

youxia += board[xlabel++][ylabel++];

}

if (youxia.equals("○○○○○")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

}

}

}

}

// 判断电脑是否赢了

for (int i = 0; i SIZE; i++) {

for (int j = 0; j SIZE; j++) {

if (board[i][j].equals("●")) {

xlabel = i;

ylabel = j;

// 横向找 x坐标不变 y坐标以此加1连成字符串

String heng = "";

if (j + 5 SIZE i + 5 SIZE) {

for (int k = j; k j + 5; k++) {

heng += board[i][k];

}

if (heng.equals("●●●●●")) {

System.out.println(roles+"赢输了!您输了!");

System.exit(0);

}

// 向下判断y不变 x逐增5 连成字符串

String xia = "";

for (int l = i; l i + 5; l++) {

xia += board[l][ylabel];

// System.out.println(xia);

}

if (xia.equals("●●●●●")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

// 斜向右下判断

String youxia = "";

for (int a = 1; a = 5; a++) {

youxia += board[xlabel++][ylabel++];

}

if (youxia.equals("●●●●●")) {

System.out.println(roles+"赢了!您输了!");

System.exit(0);

}

}

}

}

}

}

public static void main(String[] args) {

Games wz = new Games();

Scanner sc = new Scanner(System.in);

wz.initBoard();

wz.printBoard();

while (true) {

System.out.print("请"+roles+"输入X,Y坐标,必须在0-15范围内,xy以空格隔开,输入16 16结束程序");

int x = sc.nextInt();

int y = sc.nextInt();

if (x == SIZE y == SIZE) {

System.out.println("程序结束");

System.exit(0);

}

if (x SIZE || x 0 || y SIZE | y 0) {

System.out.println("输入错误,请从新输入");

continue;

}

//如果roles是A玩家 就让A玩家下棋,否则就让B玩家下棋。

if (wz.board[x][y].equals("╋")roles.equals("A玩家")) {

wz.board[x][y] = "○";

wz.printBoard();

//判断输赢

wz.whoWin(wz);

}else if(wz.board[x][y].equals("╋")roles.equals("B玩家")){

wz.board[x][y] = "●";

wz.printBoard();

//判断输赢

wz.whoWin(wz);

} else {

System.out.println("此处已经有棋子,从新输入");

continue;

}

if(roles.equals("A玩家")){

roles = "B玩家";

}else if(roles.equals("B玩家")){

roles = "A玩家";

}

}

}

}

五子棋判断输赢算法向八个方向寻找,用java算法

可以写一个15乘15的矩阵用数组表示棋盘

例如你的棋子在a[2][2]的位置八个方向的位置分别为a[1][1] a[1][2] a[1][3] a[2][1] a[2][3] a[3][1] a[3][2] a[3][3],你可以以每个数组的方位的值表示棋是否已经加入,a[x][x]=1表示白棋,a[x][x]=2表示黑棋,a[x][x]=0表示没有走过。

Java跳棋算法问题

这个其实和五子棋 象棋的寻址区别不大,都是最佳算法

其实主要看搜索深度的,我以前自己做过五子棋和象棋的java版本

有很多算法实现 你用alpha beta或者置换表都可以

验证做好的是最忧搜索+置换表+历史启发,现在不写那些了 代码不好找 你去网上搜索五子棋人工智能就应该有

java棋牌算法的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java棋类游戏、java棋牌算法的信息别忘了在本站进行查找喔。