之前有人问过同样的问题,我回答的,你们是不是同一个系的,要做课程设计?我直接粘贴过来:
我自己写了一个简单的程序,可选择落子的先后顺序,重新开始,最后判断某一方是否为五子连珠。选择落子的先后顺序,只需定义一个boolean变量,每次切换取其反值;重制棋盘或重新开始就把棋盘重新绘制一遍;判断某一方是否为五子连珠,就判断某方的每个棋子,以它为中心与之紧邻的水平,垂直,左斜,右斜四个方向是否有五子连珠。用一个二维数组position存储棋盘上的棋子情况,position[x][y]=1,0,-1分别表示棋盘的第x行第y列下有黑子,无子,白子。源代码如下:
package comtest;
import javaawt;
import javautil;
import javaawtgeom;
import javaawtevent;
import javaxswing;
public class MyFiveChess {
public static void main(String[] args) {
JFrame f = new JFrame();
fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
Dimension screenSize = ToolkitgetDefaultToolkit()getScreenSize();
int screenWidth = screenSizewidth;
int screenHeight = screenSizeheight;
fsetSize(screenWidth / 2, screenHeight / 2);
fsetLocation(screenWidth / 4, screenHeight / 4);
fsetTitle("FiveChess");
MyPanel panel = new MyPanel();
fadd(panel);
fsetVisible(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(){
jbaddActionListener(new MyActionHandler());
jb2addActionListener(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) {
superpaint(g);
jbsetLocation(400,150);
jb2setLocation(400,200);
gsetColor(ColorRED);
gsetFont(new Font(null, FontBOLD, 20));
gdrawString((first "黑" : "白")+"方下子", 400, 100);
gsetColor(new Color(240, 210, 120));
gfillRect(bx - l, by - l, l (n + 2), l (n + 2));
gsetColor(ColorBLACK);
for (int i = 0; i <= n; i++){
gdrawLine(bx, by + i l, bx + l n, by + i l);
gdrawLine(bx + i l, by, bx + i l, by + l n);
}
Graphics2D g2 = (Graphics2D)g;
isBlack = first;
for(Ellipse2D r : squares){
g2setColor(isBlack ColorBLACK : ColorWHITE);
g2fill(r);
isBlack = !isBlack;
}
if(isOver) {
gsetColor(ColorRED);
gsetFont(new Font("TimesRoman", FontBOLD, 60));
gdrawString((isBlack "白" : "黑") + "方获胜", 120, 200);
}
}
public Ellipse2D find(Point2D p){
for(Ellipse2D r : squares)
if(rcontains(p))
return r;
return null;
}
public void add(Point2D p) {
if(pgetX() > bx - l / 2 && pgetX() < bx + l n + l / 2 &&
pgetY() > by - l / 2 && pgetY() < 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 Ellipse2DDouble(bx + j l - l / 2,
by + i l - l / 2, l, l);
if (currentcontains(p)) {
position[i][j] = isBlack 1 : -1;
isOver = isWin(position, isBlack, i , j) true : false;
currentsetFrame(bx + j l - l / 2 + 1,
by + i l - l / 2 + 1, l - 2, l - 2);
squaresadd(current);
repaint();
return;
}
}
}
}
}
private class MouseHandler extends MouseAdapter{
public void mousePressed(MouseEvent event){
if(isOver) return;
current = find(eventgetPoint());
if(current == null)
add(eventgetPoint());
}
}
private class MyActionHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
String cmd=egetActionCommand();
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(rcontains(eventgetPoint())){
setCursor(CursorgetPredefinedCursor(CursorCROSSHAIR_CURSOR));
}else setCursor(CursorgetDefaultCursor());
}
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 <= statelength - 5 x + 4 : statelength - 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 < statelength - 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 < statelength - 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;
}
}
比较简略,自己可以根据情况修改,改进改进!
//人接口
public interface Human {
void speak();
}
//教师类
public class Teacher implements Human {
public void speak() {
Systemoutprintln("speak");
}
public void teach() {
Systemoutprintln("teach");
}
}
//学生类
public class Student implements Human {
public void speak() {
Systemoutprintln("speak");
}
public void learn() {
Systemoutprintln("learn");
}
}
//测试类
class HumanTest {
@Test
void test() {
Teacher teacher=new Teacher();
Student stu=new Student();
teacherteach();
teacherspeak();
stulearn();
stuspeak();
}
}
运行结果:
teach
speak
learn
speak
package test;
import javaawt;
import javaawtevent;
import javautil;
import javaxswing;
class TestCalculator {
JFrame jf=new JFrame("calculator");
JPanel jp=new JPanel();
JPanel jpn=new JPanel();
JTextArea jta=new JTextArea(2,14);
JButton bt;
JButton reset=new JButton("Reset");
String s=new String("1234567890=+-/");
String numberOne="";
String numberTwo="";
double number=0d;
double result=0d;
char sign;
public TestCalculator() {
jfsetSize(250,300);
jpsetLayout(new GridLayout(4,4));
jtasetEditable(false);
ArrayList list=new ArrayList();
for(int i=0;i<16;i++){
bt=new JButton(scharAt(i)+"");
jpadd(bt);
listadd(bt);
}
jpnsetLayout(new FlowLayout());
jpnadd(jta);
jpnadd(reset);
jfadd(jpn,"North");
jfadd(jp);
jtasetEditable(false);
jfsetVisible(true);
jfsetDefaultCloseOperation(jfEXIT_ON_CLOSE);
resetaddActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
reSet();
}
});
Iterator it=listiterator();
while(ithasNext()){
final JButton o=(JButton)itnext();
oaddActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
/判断被点击的按钮所代表的String是不是数字0-9/
if(ogetText()matches("\\d")||ogetText()equals("")){
jtaappend(ogetText());
getNumberTwo(ogetText());
}else{
getNumberOne();
double number1;
double number2;
if(numberOneequals("")){
number1=0;
}else{
number1=DoubleparseDouble(numberOne);
}
if(numberTwoequals("")){
number2=number;
}else{
number2=DoubleparseDouble(numberTwo);
}
switch(sign){
case '+':result=number1+number2;break;
case '-':result=number1-number2;break;
case '':result=number1number2;break;
case '/':result=number1/number2;break;
case '=':break;
default :result=number1;
}
number=number2;
numberTwo="";
jtasetText(result+"");
getSign(ogetText());
if(!ogetText()equals("="))
jtaappend(ogetText());
}
}
});
}
}
public void getNumberTwo(String s){
numberTwo+=s;
// Systemoutprintln("numberTwo:"+numberTwo);
}
public void getNumberOne(){
if(result==0){
numberOne=numberTwo;
}else{
numberOne=result+"";
}
// Systemoutprintln("numberOne: "+numberOne);
}
public void getSign(String s){
sign=scharAt(0);
// Systemoutprintln("sign:"+sign);
}
public void reSet(){
numberOne="";
numberTwo="";
sign=0;
result=0;
jtasetText("");
}
}
public class Calculator2{
public static void main(String[] agrs){
TestCalculator calculator=new TestCalculator();
}
}
计算器代码,用swing做的
import javaappletApplet;
import javaawtFont;
import javaawtFontMetrics;
import javaawtGraphics;
import javaawtImage;public class Welcome extends Applet {
int pos = 0;
Image graph;
int w=0;
String str = "热烈欢迎2009届新生入学";
Font f = new Font("黑体", FontBOLD, 20);
public Welcome(){
FontMetrics fm = getFontMetrics(f);
w = fmstringWidth(str);
pos=w;
}
public void paint(Graphics g) {
gsetFont(f);
gdrawString(str, pos-w, 40);
try {
Threadsleep(100);
} catch (InterruptedException e) {
eprintStackTrace();
}
pos = pos + 5;
if (pos-w > getWidth())
pos = w;
repaint();
}
}
以上就是关于java 程序设计全部的内容,包括:java 程序设计、Java程序语言设计 根据要求编写程序、java程序设计等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)