JAVA围棋棋盘等基础题

JAVA围棋棋盘等基础题,第1张

第一个:用Integer类的parseInt方法:

如:int a=parseInt("1111111","2")那么a就是127了。(好像是parseInt("2","1111111")自己试试)

第二个:

static final int huLu=7.5//中元美元汇率

int a=100*huLu

System.out.println("¥"+Integer.toString(a))

第三个:做一个围棋棋盘的图片,加入程序就是了。

我自己写了一个简单的程序,可选择落子的先后顺序,重新开始,最后判断某一方是否为五子连珠。选择落子的先后顺序,只需定义一个boolean变量,每次切换取其反值;重制棋盘或重新开始就把棋盘重新绘制一遍;判断某一方是否为五子连珠,就判断某方的每个棋子,以它为中心与之紧邻的水平,垂直,左斜,右斜四个方向是否有五子连珠。用一个二维数组position存储棋盘上的棋子情况,position[x][y]=1,0,-1分别表示棋盘的第x行第y列下有黑子,无子,白子。源代码如下:

package com.test

import java.awt.*

import java.util.*

import java.awt.geom.*

import java.awt.event.*

import javax.swing.*

public class MyFiveChess {

public static void main(String[] args) {

JFrame f = new JFrame()

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize()

int screenWidth = screenSize.width

int screenHeight = screenSize.height

f.setSize(screenWidth / 2, screenHeight / 2)

f.setLocation(screenWidth / 4, screenHeight / 4)

f.setTitle("FiveChess")

MyPanel panel = new MyPanel()

f.add(panel)

f.setVisible(true)

}

}

class MyPanel extends JPanel {

private static final int SIDELENGTH = 10

private ArrayList<Ellipse2D>squares = new ArrayList<Ellipse2D>()

private Ellipse2D current = null

JButton jb = new JButton("重新开始")

JButton jb2 = new JButton("切换先手")

boolean isBlack

boolean first = true

boolean isOver

int l = 16

int n = 20

int bx = 20

int by = 20

int[][] position = new int[n + 1][n + 1]

public MyPanel(){

jb.addActionListener(new MyActionHandler())

jb2.addActionListener(new MyActionHandler())

addMouseListener(new MouseHandler())

addMouseMotionListener(new MouseMotionHandler())

add(jb)

add(jb2)

}

public void initMyPenal(){

squares = new ArrayList<Ellipse2D>()

current = null

isBlack = first

isOver = false

position = new int[n + 1][n + 1]

for(int i = 0i <= ni++)

for(int j = 0j <= nj++)

position[i][j] = 0

repaint()

}

public void paint(Graphics g) {

super.paint(g)

jb.setLocation(400,150)

jb2.setLocation(400,200)

g.setColor(Color.RED)

g.setFont(new Font(null, Font.BOLD, 20))

g.drawString((first ? "黑" : "白")+"方下子", 400, 100)

g.setColor(new Color(240, 210, 120))

g.fillRect(bx - l, by - l, l * (n + 2), l * (n + 2))

g.setColor(Color.BLACK)

for (int i = 0i <= ni++){

g.drawLine(bx, by + i * l, bx + l * n, by + i * l)

g.drawLine(bx + i * l, by, bx + i * l, by + l * n)

}

Graphics2D g2 = (Graphics2D)g

isBlack = first

for(Ellipse2D r : squares){

g2.setColor(isBlack ? Color.BLACK : Color.WHITE)

g2.fill(r)

isBlack = !isBlack

}

if(isOver) {

g.setColor(Color.RED)

g.setFont(new Font("TimesRoman", Font.BOLD, 60))

g.drawString((isBlack ? "白" : "黑") + "方获胜", 120, 200)

}

}

public Ellipse2D find(Point2D p){

for(Ellipse2D r : squares)

if(r.contains(p))

return r

return null

}

public void add(Point2D p) {

if(p.getX() >bx - l / 2 &&p.getX() <bx + l * n + l / 2 &&

p.getY() >by - l / 2 &&p.getY() <by + l * n + l / 2){

for (int i = 0i <= ni++) {

for (int j = 0j <= nj++) {

if(position[i][j] == 1 || position[i][j] == -1) continue

current = new Ellipse2D.Double(bx + j * l - l / 2,

by + i * l - l / 2, l, l)

if (current.contains(p)) {

position[i][j] = isBlack ? 1 : -1

isOver = isWin(position, isBlack, i , j) ? true : false

current.setFrame(bx + j * l - l / 2 + 1,

by + i * l - l / 2 + 1, l - 2, l - 2)

squares.add(current)

repaint()

return

}

}

}

}

}

private class MouseHandler extends MouseAdapter{

public void mousePressed(MouseEvent event){

if(isOver) return

current = find(event.getPoint())

if(current == null)

add(event.getPoint())

}

}

private class MyActionHandler implements ActionListener{

public void actionPerformed(ActionEvent e) {

String cmd=e.getActionCommand()

if("重新开始".equals(cmd)){

initMyPenal()

}else if("切换先手".equals(cmd)){

initMyPenal()

first=!first

}

}

}

private class MouseMotionHandler implements MouseMotionListener{

public void mouseMoved(MouseEvent event){

Rectangle r = new Rectangle(bx - l, by - l, l * (n + 2), l * (n + 2))

if(r.contains(event.getPoint())){

setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR))

}else setCursor(Cursor.getDefaultCursor())

}

public void mouseDragged(MouseEvent event){}

}

public boolean isWin(int[][] state, boolean isBlack, int x, int y) {//四个方向中是否有五子连珠

return isCzWin(state, isBlack, x, y)

|| isSpWin(state, isBlack, x, y)

|| isYxWin(state, isBlack, x, y)

|| isZxWin(state, isBlack, x, y)

}

public boolean isCzWin(int[][] state, boolean isBlack, int x, int y) {//判断垂直方向是否有五子连珠

int n = 0

int a = (x >= 4 ? x - 4 : 0)

int b = (x <= state.length - 5 ? x + 4 : state.length - 1)

for (int i = ai <= bi++)

if (state[i][y] == (isBlack ? 1: -1)) {

if (++n == 5) return true

} else n = 0

return false

}

public boolean isSpWin(int[][] state, boolean isBlack, int x, int y) {//判断水平方向是否有五子连珠

int n = 0

int a = (y >= 4 ? y - 4 : 0)

int b = (y <= state[0].length - 5 ? y + 4 : state[0].length - 1)

for (int i = ai <= bi++)

if (state[x][i] == (isBlack ? 1: -1)) {

if (++n == 5) return true

} else n = 0

return false

}

public boolean isZxWin(int[][] state, boolean isBlack, int x, int y) {//判断左斜方向是否有五子连珠

int n = 1, a = x, b = y

for (int i = 1i <= 4 &&a >0 &&b >0i++)

if (state[a - 1][b - 1] == (isBlack ? 1: -1)) {

n++a--b--

} else break

for (int i = 1i <= 4 &&x <state.length - 1 &&y <state[0].length - 1i++)

if (state[x + 1][y + 1] == (isBlack ? 1: -1)) {

n++x++y++

} else break

if (n >= 5) return true

return false

}

public boolean isYxWin(int[][] state, boolean isBlack, int x, int y) {//判断右斜方向是否有五子连珠

int n = 1, a = x, b = y

for (int i = 1i <= 4 &&a >0 &&b <state[0].length - 1i++)

if (state[a - 1][b + 1] == (isBlack ? 1: -1)) {

n++a--b++

} else break

for (int i = 1i <= 4 &&x <state.length - 1 &&y >0i++)

if (state[x + 1][y - 1] == (isBlack ? 1: -1)) {

n++x++y--

} else break

if (n >= 5) return true

return false

}

}

不是很完善,希望对你有帮助。

/*首先,

应用程序必须为 Canvas 类创建子类,以获得有用的功能(如创建自定义组件)。必须重写 paint 方法,以便在 canvas 上执行自定义图形。 

以下是代码

*/

import java.awt.Canvas

import java.awt.Font

import javax.swing.*

public class P {

public static void main(String[] args){

JFrame jf=new JFrame()

jf.setVisible(true)

jf.setLocation(200,200)

jf.setSize(300, 300)

jf.add(new a())

}

}

class a extends Canvas{

public void paint(java.awt.Graphics g){

int j=0,i=0

while(i<6){//for循环也可以

g.drawLine(0, 20*i, 100, 20*i)//横线,y坐标不变

g.drawLine(20*i, 0, 20*i, 100)

i++

}

g.drawString("楚河汉界", 20, 130)

i=0

while(i<6){

g.drawLine(0,160+20*i, 100, 160+20*i)

g.drawLine(20*i, 160, 20*i, 260)

i++}

}

}

虽然画出来了,但我想简化两个循环,如下:

class a extends Canvas{

public void paint(java.awt.Graphics g){

int j=0,i=0

do{//用for循环也可以

if(j==0){

g.drawLine(0, 20*i, 100, 20*i)//横线

g.drawLine(20*i, 0, 20*i, 100)

}

else{

g.drawLine(0,160+20*i, 100, 160+20*i)

g.drawLine(20*i, 160, 20*i, 260)

}

if(i==5&&j==0){j=1i=-1g.drawString("楚河汉界", 20, 130)}//因为判断第一次之后,i会被加一次,而这次是没有画线的,会导致少了一条线,所以i=-1

i++

}while(i<6)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8028815.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存