java國際象棋
A. 用java程序列印出國際象棋的棋盤樣子
import java.awt.Canvas;
import java.awt.Graphics;
import javax.swing.JFrame;
public class IC extends Canvas{
private static final long serialVersionUID = -2611185708860234130L;
public void paint(Graphics g){
int s = 50;//單元格大小
int c = 0;//可取0或1以改變第一單元格是否填充
int rc=10;//行列數
for(int i=0; i<rc; i++){
c++;
if(rc%2>0)++c;
for(int j=0; j<rc; j++){
g.drawRect(i*s,j*s,s,s);
if(++c%2!=0)
g.fillRect(i*s,j*s,s,s);
}
}
g.dispose();
}
public static void main(String[] args){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600,600);
f.setLocationRelativeTo(null);
f.getContentPane().add(new IC());
f.setVisible(true);
}
}
上面是直接按你的要求在畫布上列印的,下面是有組件排版的:
import java.awt.Color;
import javax.swing.*;
public class Chess extends JPanel{
private static final long serialVersionUID = -5380742576273506004L;
//grids:行數和列數, gridsize:單元格的高和寬
public Chess(int grids,int gridsize){
super(null);
for(int i=0; i<grids; i++){
for(int j=0; j<grids; j++){
JLabel l = new JLabel();
l.setSize(gridsize,gridsize);
l.setLocation(i*gridsize+i,j*gridsize+j);
if((i+j)%2==0){
l.setBackground(Color.black);
l.setOpaque(true);
}
l.setBorder(BorderFactory.createLineBorder(Color.black));
add(l);
}
}
}
public static void main(String[] args){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(620,645);
f.setLocationRelativeTo(null);
f.add(new Chess(10,60));
f.setVisible(true);
}
}
注:這些已經很簡單了,如果還看不懂,你回去再多補補基礎吧!
B. java中國際象棋模擬
有一個我們老師編寫的貪吃蛇程序 供你參考一下:
GreedSnake.java (也是程序入口):
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GreedSnake implements KeyListener {
JFrame mainFrame;
Canvas paintCanvas;
JLabel labelScore;// 計分牌
SnakeModel snakeModel = null;// 蛇
public static final int canvasWidth = 200;
public static final int canvasHeight = 300;
public static final int nodeWidth = 10;
public static final int nodeHeight = 10;
// ----------------------------------------------------------------------
// GreedSnake():初始化游戲界面
// ----------------------------------------------------------------------
public GreedSnake() {
// 設置界面元素
mainFrame = new JFrame("GreedSnake");
Container cp = mainFrame.getContentPane();
labelScore = new JLabel("Score:");
cp.add(labelScore, BorderLayout.NORTH);
paintCanvas = new Canvas();
paintCanvas.setSize(canvasWidth + 1, canvasHeight + 1);
paintCanvas.addKeyListener(this);
cp.add(paintCanvas, BorderLayout.CENTER);
JPanel panelButtom = new JPanel();
panelButtom.setLayout(new BorderLayout());
JLabel labelHelp;// 幫助信息
labelHelp = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.NORTH);
labelHelp = new JLabel("ENTER or R or S for start;", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.CENTER);
labelHelp = new JLabel("SPACE or P for pause", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.SOUTH);
cp.add(panelButtom, BorderLayout.SOUTH);
mainFrame.addKeyListener(this);
mainFrame.pack();
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
begin();
}
// ----------------------------------------------------------------------
// keyPressed():按鍵檢測
// ----------------------------------------------------------------------
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (snakeModel.running)
switch (keyCode) {
case KeyEvent.VK_UP:
snakeModel.changeDirection(SnakeModel.UP);
break;
case KeyEvent.VK_DOWN:
snakeModel.changeDirection(SnakeModel.DOWN);
break;
case KeyEvent.VK_LEFT:
snakeModel.changeDirection(SnakeModel.LEFT);
break;
case KeyEvent.VK_RIGHT:
snakeModel.changeDirection(SnakeModel.RIGHT);
break;
case KeyEvent.VK_ADD:
case KeyEvent.VK_PAGE_UP:
snakeModel.speedUp();// 加速
break;
case KeyEvent.VK_SUBTRACT:
case KeyEvent.VK_PAGE_DOWN:
snakeModel.speedDown();// 減速
break;
case KeyEvent.VK_SPACE:
case KeyEvent.VK_P:
snakeModel.changePauseState();// 暫停或繼續
break;
default:
}
// 重新開始
if (keyCode == KeyEvent.VK_R || keyCode == KeyEvent.VK_S
|| keyCode == KeyEvent.VK_ENTER) {
snakeModel.running = false;
begin();
}
}
// ----------------------------------------------------------------------
// keyReleased():空函數
// ----------------------------------------------------------------------
public void keyReleased(KeyEvent e) {
}
// ----------------------------------------------------------------------
// keyTyped():空函數
// ----------------------------------------------------------------------
public void keyTyped(KeyEvent e) {
}
// ----------------------------------------------------------------------
// repaint():繪制游戲界面(包括蛇和食物)
// ----------------------------------------------------------------------
void repaint() {
Graphics g = paintCanvas.getGraphics();
// draw background
g.setColor(Color.WHITE);
g.fillRect(0, 0, canvasWidth, canvasHeight);
// draw the snake
g.setColor(Color.BLACK);
LinkedList na = snakeModel.nodeArray;
Iterator it = na.iterator();
while (it.hasNext()) {
Node n = (Node) it.next();
drawNode(g, n);
}
// draw the food
g.setColor(Color.RED);
Node n = snakeModel.food;
drawNode(g, n);
updateScore();
}
// ----------------------------------------------------------------------
// drawNode():繪畫某一結點(蛇身或食物)
// ----------------------------------------------------------------------
private void drawNode(Graphics g, Node n) {
g.fillRect(n.x * nodeWidth, n.y * nodeHeight, nodeWidth - 1,
nodeHeight - 1);
}
// ----------------------------------------------------------------------
// updateScore():改變計分牌
// ----------------------------------------------------------------------
public void updateScore() {
String s = "Score: " + snakeModel.score;
labelScore.setText(s);
}
// ----------------------------------------------------------------------
// begin():游戲開始,放置貪吃蛇
你可以參考一下 每次遇到不會的都來求助是很不方便的
如果你想了解和學習更多的JAVA編程,成為一名真正的JAVA高手,你可以來這個裙,前面三個數是四二六 中間是三九六 後面是二八四
把這串數字連起來就可以了,這是一個高手的學習裙,在這里你可以學習到書上學不到的知識,還有大牛相互討論並指導你解答哦!
// ----------------------------------------------------------------------
void begin() {
if (snakeModel == null || !snakeModel.running) {
snakeModel = new SnakeModel(this, canvasWidth / nodeWidth,
this.canvasHeight / nodeHeight);
(new Thread(snakeModel)).start();
}
}
// ----------------------------------------------------------------------
// main():主函數
// ----------------------------------------------------------------------
public static void main(String[] args) {
GreedSnake gs = new GreedSnake();
}
}
Node.java:
public class Node {
int x;
int y;
Node(int x, int y) {
this.x = x;
this.y = y;
}
}
SnakeModel.java:
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JOptionPane;
public class SnakeModel implements Runnable {
GreedSnake gs;
boolean[][] matrix;// 界面數據保存在數組里
LinkedList nodeArray = new LinkedList();
Node food;
int maxX;// 最大寬度
int maxY;// 最大長度
int direction = 2;// 方向
boolean running = false;
int timeInterval = 200;// 間隔時間(速度)
double speedChangeRate = 0.75;// 速度改變程度
boolean paused = false;// 游戲狀態
int score = 0;
int countMove = 0;
// UP和DOWN是偶數,RIGHT和LEFT是奇數
public static final int UP = 2;
public static final int DOWN = 4;
public static final int LEFT = 1;
public static final int RIGHT = 3;
// ----------------------------------------------------------------------
// GreedModel():初始化界面
// ----------------------------------------------------------------------
public SnakeModel(GreedSnake gs, int maxX, int maxY) {
this.gs = gs;
this.maxX = maxX;
this.maxY = maxY;
matrix = new boolean[maxX][];
for (int i = 0; i < maxX; ++i) {
matrix[i] = new boolean[maxY];
Arrays.fill(matrix[i], false);// 沒有蛇和食物的地區置false
}
// 初始化貪吃蛇
int initArrayLength = maxX > 20 ? 10 : maxX / 2;
for (int i = 0; i < initArrayLength; ++i) {
int x = maxX / 2 + i;
int y = maxY / 2;
nodeArray.addLast(new Node(x, y));
matrix[x][y] = true;// 蛇身處置true
}
food = createFood();
matrix[food.x][food.y] = true;// 食物處置true
}
// ----------------------------------------------------------------------
// changeDirection():改變運動方向
// ----------------------------------------------------------------------
public void changeDirection(int newDirection) {
if (direction % 2 != newDirection % 2)// 避免沖突
{
direction = newDirection;
}
}
// ----------------------------------------------------------------------
// moveOn():貪吃蛇運動函數
// ----------------------------------------------------------------------
public boolean moveOn() {
Node n = (Node) nodeArray.getFirst();
int x = n.x;
int y = n.y;
switch (direction) {
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
if ((0 <= x && x < maxX) && (0 <= y && y < maxY)) {
if (matrix[x][y])// 吃到食物或者撞到身體
{
if (x == food.x && y == food.y)// 吃到食物
{
nodeArray.addFirst(food);// 在頭部加上一結點
// 計分規則與移動長度和速度有關
int scoreGet = (10000 - 200 * countMove) / timeInterval;
score += scoreGet > 0 ? scoreGet : 10;
countMove = 0;
food = createFood();
matrix[food.x][food.y] = true;
return true;
} else
return false;// 撞到身體
} else// 什麼都沒有碰到
{
nodeArray.addFirst(new Node(x, y));// 加上頭部
matrix[x][y] = true;
n = (Node) nodeArray.removeLast();// 去掉尾部
matrix[n.x][n.y] = false;
countMove++;
return true;
}
}
return false;// 越界(撞到牆壁)
}
// ----------------------------------------------------------------------
// run():貪吃蛇運動線程
// ----------------------------------------------------------------------
public void run() {
running = true;
while (running) {
try {
Thread.sleep(timeInterval);
} catch (Exception e) {
break;
}
if (!paused) {
if (moveOn())// 未結束
{
gs.repaint();
} else// 游戲結束
{
JOptionPane.showMessageDialog(null, "GAME OVER",
"Game Over", JOptionPane.INFORMATION_MESSAGE);
break;
}
}
}
running = false;
}
// ----------------------------------------------------------------------
// createFood():生成食物及放置地點
// ----------------------------------------------------------------------
private Node createFood() {
int x = 0;
int y = 0;
do {
Random r = new Random();
x = r.nextInt(maxX);
y = r.nextInt(maxY);
} while (matrix[x][y]);
return new Node(x, y);
}
// ----------------------------------------------------------------------
// speedUp():加快蛇運動速度
// ----------------------------------------------------------------------
public void speedUp() {
timeInterval *= speedChangeRate;
}
// ----------------------------------------------------------------------
// speedDown():放慢蛇運動速度
// ----------------------------------------------------------------------
public void speedDown() {
timeInterval /= speedChangeRate;
}
// ----------------------------------------------------------------------
// changePauseState(): 改變游戲狀態(暫停或繼續)
// ----------------------------------------------------------------------
public void changePauseState() {
paused = !paused;
}
}
滿意請採納
C. java 中用GridLayout開發國際象棋棋盤
樓主,完整的代碼
D. java 國際象棋,根據已有棋盤判斷出所有黑色棋子的下一步走向,並逐一輸出
是要ai演算法還是實現要實現規則的代碼?
E. 用java寫一個國際象棋的棋盤,輸出結果要是一張 國際象棋的棋盤
import java.awt.*;
import javax.swing.*;
public class Chessboard extends JApplet {
int baseXPosition, baseYPosition;
int currentXPosition, currentYPosition;
public void init() {
baseXPosition = 40; // 棋盤的開始x位置
baseYPosition = 40; // 棋盤的開始y位置
setBackground(Color.black); // 設置背景顏色黑色
}
public void paint(Graphics g) { // 繪制棋盤
currentXPosition = baseXPosition; // currentXPosition當前的x位置
currentYPosition = baseYPosition; // currentYPosition當前的y位置
for (int row = 0; row < 8; row++) {
currentXPosition = baseXPosition + row * 40;
for (int column = 0; column < 8; column++) {
if ((column + row) % 2 == 0)
g.setColor(Color.white); // 設置棋盤格子的顏色
else
g.setColor(Color.red); // 設置棋盤格子的顏色
currentYPosition = baseXPosition + column * 40;
g.drawRect(currentXPosition,currentYPosition,40,40);//;代碼4 //在當前位置繪制棋盤的格子;每個格子的大小是40*40像
g.fillRect(currentXPosition,currentYPosition,40,40);
}
}
}
}
F. java國際象棋
你可以這樣,先設置contentPane 的布局為空:
*.setLayout(null);
然後再這樣(以button按鈕為例):
button.setBounds(a,b,c,d);z這樣就可以把他添加到指定位置.你試試吧
G. 使用java編程:國際象棋棋盤64個格,第一格放一粒米,每一格比前一格多一顆,放滿需要多少米 急!急!
package com.plague.math;
import java.lang.Math;
public class Chess {
private final static int CHESS_NUMBER = 64;
private int total;
public Chess () {
System.out.println("國王的賞賜,其實,問題是這樣的:" +
"在國際象棋的棋盤上,第一格放一粒米,第二格放2粒米,第三個放四粒米。。。最後國王要賞賜多少米");
for (int i = 0; i < CHESS_NUMBER; i++) {
total += Math.pow(2.0,(double)i);
}
System.out.println("國王的賞賜是:"+total);
}
public static void main(String[] args) {
Chess c1 = new Chess();
}
}
H. java程序編的國際象棋棋盤,沒有錯誤卻出不來圖像,,程序如下。。求解。。。
choose=(char)System.in.read();這一行
需要你在控制台輸入點什麼才能往下執行
I. 顯示一個國際象棋棋盤的java源代碼
import java.awt.Color;
import javax.swing.*;
public class Chess extends JPanel
{// 繼承面板類
public Chess(int grids,int gridsize)
{//grids:行數和列數, gridsize:單元格的高和寬
super(null);
for(int i=0; i<grids; i++)
{
for(int j=0; j<grids; j++)
{
JLabel l = new JLabel();//生成標簽實例
l.setSize(gridsize,gridsize);
l.setLocation(i*gridsize,j*gridsize);
if((i+j)%2==0)
{ //當小方格的坐標和剛好是偶數時,
l.setBackground(Color.black); //設置為方格為黑色
l.setOpaque(true); //設置為不透明
}
l.setBorder(BorderFactory.createLineBorder(Color.black)); //設置邊界為黑色
add(l);//將l標簽添加到面板
}
}
}
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setSize(658,677); //邊框的長和寬
f.setLocationRelativeTo(null); //設置窗口相對於指定組件的位置
f.add(new Chess(8,80));
f.setVisible(true);
}
}