java 程序设计
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(new File("d:/info.txt")));
String line = "第一行文本
第二行文本";
out.write(line.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream("d:/info.txt"));
StringBuffer buffer = new StringBuffer();
byte[] buff = new byte[in.available()];
while (in.read(buff) != -1) {
buffer.append(new String(buff));
}
System.out.println(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
in = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(new File("d:/info.txt")));
String line = "第一行文本
第二行文本";
out.write(line.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream("d:/info.txt"));
StringBuffer buffer = new StringBuffer();
byte[] buff = new byte[in.available()];
while (in.read(buff) != -1) {
buffer.append(new String(buff));
}
System.out.println(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
in = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
我自己写了一个简单的程序,可选择落子的先后顺序,重新开始,最后判断某一方是否为五子连珠。选择落子的先后顺序,只需定义一个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 = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
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 = 0; i <= n; i++){
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 = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
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 = a; i <= b; i++)
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 = a; i <= b; i++)
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 = 1; i <= 4 && a > 0 && b > 0; i++)
if (state[a - 1][b - 1] == (isBlack ? 1: -1)) {
n++; a--; b--;
} else break;
for (int i = 1; i <= 4 && x < state.length - 1 && y < state[0].length - 1; i++)
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 = 1; i <= 4 && a > 0 && b < state[0].length - 1; i++)
if (state[a - 1][b + 1] == (isBlack ? 1: -1)) {
n++; a--; b++;
} else break;
for (int i = 1; i <= 4 && x < state.length - 1 && y > 0; i++)
if (state[x + 1][y - 1] == (isBlack ? 1: -1)) {
n++; x++; y--;
} else break;
if (n >= 5) return true;
return false;
}
}
比较简略,自己可以根据情况修改,改进改进!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class mypanel extends Panel implements MouseListener
{
int chess[][] = new int[11][11];
boolean Is_Black_True;
mypanel()
{
Is_Black_True = true;
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
chess[i][j] = 0;
}
}
addMouseListener(this);
setBackground(Color.BLUE);
setBounds(0, 0, 360, 360);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
if(x < 25 || x > 330 + 25 ||y < 25 || y > 330+25)
{
return;
}
if(chess[x/30-1][y/30-1] != 0)
{
return;
}
if(Is_Black_True == true)
{
chess[x/30-1][y/30-1] = 1;
Is_Black_True = false;
repaint();
Justisewiner();
return;
}
if(Is_Black_True == false)
{
chess[x/30-1][y/30-1] = 2;
Is_Black_True = true;
repaint();
Justisewiner();
return;
}
}
void Drawline(Graphics g)
{
for(int i = 30;i <= 330;i += 30)
{
for(int j = 30;j <= 330; j+= 30)
{
g.setColor(Color.WHITE);
g.drawLine(i, j, i, 330);
}
}
for(int j = 30;j <= 330;j += 30)
{
g.setColor(Color.WHITE);
g.drawLine(30, j, 330, j);
}
}
void Drawchess(Graphics g)
{
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
g.setColor(Color.BLACK);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
if(chess[i][j] == 2)
{
g.setColor(Color.WHITE);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
}
}
}
void Justisewiner()
{
int black_count = 0;
int white_count = 0;
int i = 0;
for(i = 0;i < 11;i++)//横向判断
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i][j] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i < 11;i++)//竖向判断
{
for(int j = 0;j < 11;j++)
{
if(chess[j][i] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[j][i] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i < 7;i++)//左向右斜判断
{
for(int j = 0;j < 7;j++)
{
for(int k = 0;k < 5;k++)
{
if(chess[i + k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i + k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
for(i = 4;i < 11;i++)//右向左斜判断
{
for(int j = 6;j >= 0;j--)
{
for(int k = 0;k < 5;k++)
{
if(chess[i - k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i - k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
}
void Clear_Chess()
{
for(int i=0;i<11;i++)
{
for(int j=0;j<11;j++)
{
chess[i][j]=0;
}
}
repaint();
}
public void paint(Graphics g)
{
Drawline(g);
Drawchess(g);
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}
class myframe extends Frame implements WindowListener
{
mypanel panel;
myframe()
{
setLayout(null);
panel = new mypanel();
add(panel);
panel.setBounds(0,23, 360, 360);
setTitle("单人版五子棋");
setBounds(200, 200, 360, 383);
setVisible(true);
addWindowListener(this);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
public class mywindow
{
public static void main(String argc [])
{
myframe f = new myframe();
}
}
况疯胸腺: Java,是一种可以撰写跨平台应用软件的面向对象的程序设计语言,由Sun公司的詹姆斯·高斯林(James Gosling)等人于1990年代初开发.它最初被命名为Oak,目标设置在家用电器等小型系统的编程语言,来解决诸如电视机、电话、闹钟...
连云港市14768819540: Java程序设计! - ?
况疯胸腺: /*设计一个程序,基于范型Map实现10个英文单词的汉语翻译, * 即通过单词得到它的中文含义 */ import java.util.HashMap; public class Map1 { public static void main(String[] args){ HashMap hm=new HashMap(); hm.put("class","类,课程")...
连云港市14768819540: java程序设计?
况疯胸腺: public class chuguo { public static void main(String args []){ String str="abc"; str=str.toUpperCase(); //toUpperCase();是将 String 中的所有字符都转换为大写 ,它是String类里面的一个函数, strtoUpperCase();就是将str里面的字符串转换为大写! 你应该多查下API 里面很多方法直接调用会很简单! System.out.println(str); } }
连云港市14768819540: JAVA程序设计 - ?
况疯胸腺: public class TestAs {public static void main(String[] args) {for (int i = 3; i <= 199; i += 2) {execute(i);}}public static void execute(int m) {// 1.设x=floor(根号m),如果x^2=...
连云港市14768819540: JAVA语言程序设计?
况疯胸腺: package building; public class Test { public static void main(String args[]) { Building b=new Building(); House h=new House(5,2); Office o=new Office(4,1); b.areas=h.getAreas(200)+o.getAreas(300); h.print(); o.print(); System.out.println("The total ...
连云港市14768819540: Java程序设计的基本结构有哪些? ?
况疯胸腺: 单的Java程序的构建 .注释.数据类型 .变量 .赋值和初始化.操作符 .字符串 .控制流程.大数字 .数组史前Java Java来自于Sun公司的一个叫Green的项目,其原...
连云港市14768819540: Java程序设计需要学习的内容 - ?
况疯胸腺: Java 程序设计基础,包括 J2sdk基础、Java面向对象基础、Java API使用、数据结构及算法基础、Java AWT图形界面程序开发; J2SE平台Java程序设计,包括Swing图形程序设计, Socket网络应用程序设计,对象序列化,Java 常用数据结构...
连云港市14768819540: JAVA程序的设计流程? - ?
况疯胸腺: 有关这方面的教程我没有,只是想说一下,因为你的经历跟我当初比较相似,你这种情况说明你语言学的比较不错了,实际上做程序设计,对语言没有特别大的要求,语言只是一种开发工具,差不多每种语言都能做出所以的项目来,语言只是用...