1.java ?棋牌棋牌???Դ??
2.急需基于eclipse的JAVA小游戏源代码!!源码源码!棋牌棋牌
java ?源码源码???Դ??
import java.util.Scanner;public class Wuziqi {
/
*** 棋盘
*/
private final int[][] qipan;
/
*** 步数
*/
private int bushu;
/
*** 构造方法,设置棋盘规格
* @param x
* @param y
*/
public Wuziqi(int x,棋牌棋牌手机版筛选界面源码 int y) {
if (x < 1 || y < 1) {
System.out.println("棋盘规格应不小于1,使用默认规格");
qipan = new int[9][9];
} else {
qipan = new int[y][x];
}
}
/
*** 游戏开始
*/
public void play() {
int[] zuobiao = null;
//如果游戏没有结束
while (!end(zuobiao)) {
//落子,源码源码打车平台源码并取得坐标
zuobiao = luozi();
//输出棋盘
out();
}
}
/
*** 输出棋盘和棋子
*/
private void out() {
for (int i = 0; i < qipan.length; i++) {
for (int j = 0; j < qipan[i].length; j++) {
if (qipan[i][j] == 0) {
System.out.print(" +");
}else if (qipan[i][j] == -1) {
System.out.print(" 白");
}else if (qipan[i][j] == 1) {
System.out.print(" 黑");
}
}
System.out.println(" ");
}
}
/
*** 落子
*/
private int[] luozi() {
int[] zuobiao;
bushu++;
if (bushu % 2 == 1) {
System.out.println("请黑方落子");
zuobiao = input();
qipan[zuobiao[1]][zuobiao[0]] = 1;
}else {
System.out.println("请白方落子");
zuobiao = input();
qipan[zuobiao[1]][zuobiao[0]] = -1;
}
return zuobiao;
}
/
*** 输入坐标
* @return
*/
private int[] input() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入x轴坐标");
String x = sc.next();
System.out.println("请输入y轴坐标");
String y = sc.next();
//如果没有通过验证,棋牌棋牌则再次执行input(),源码源码递归算法
if (!validate(x,棋牌棋牌 y)) {
return input();
}
int int_x = Integer.valueOf(x);
int int_y = Integer.valueOf(y);
return new int[] { int_x, int_y};
}
/
*** 校验数据
* @param x
* @param y
* @return
*/
private boolean validate(String x, String y) {
Integer int_x = null;
Integer int_y = null;
//异常处理的方式判断字符串是否是一个整数
try {
int_x = Integer.valueOf(x);
int_y = Integer.valueOf(y);
} catch (NumberFormatException e) {
System.out.println("坐标格式错误,坐标应为整数");
return false;
}
if (int_x < 0 || int_y < 0 || int_x >= qipan[0].length || int_y >= qipan.length) {
System.out.println("坐标越界");
return false;
}
if (qipan[int_y][int_x] == 0) {
return true;
} else {
System.out.println("坐标上已有棋子");
}
return false;
};
/
*** 结束条件
* @return
*/
private boolean end(int[] zuobiao) {
if (zuobiao == null) {
return false;
}
//计数器
//表示棋盘上经过最近落子坐标的源码源码4条线上的连续(和最近落子颜色相同的)棋子的个数
//如果某条线上连续的棋子大于等于4(加上最近落子本身,大于等于5),棋牌棋牌则游戏结束,源码源码符合五子棋规则
int[] jieguo = new int[4];
int x = zuobiao[0];
int y = zuobiao[1];
//定义八个方向
final int[][] fangxiang = { { -1,棋牌棋牌头条采集源码 0}, { -1, 1}, { 0, 1}, { 1, 1}, { 1, 0}, { 1, -1}, { 0, -1}, { -1, -1}};
//最近落子的坐标上的棋子颜色
int number = qipan[y][x];
//搜索最近落子坐标为中心最远4的距离
for (int i = 1; i <= 4; i++) {
//每次搜索不同的距离都搜索八个方向
for (int j = 0; j < fangxiang.length; j++) {
//约定如果某个方向为null时,不再搜索这个方向。源码源码关键字continue是棋牌棋牌跳过本次(一次)循环的意思
if (fangxiang[j] == null) {
continue;
}
int mubiao_x = x + i * fangxiang[j][0];
int mubiao_y = y + i * fangxiang[j][1];
//如果搜索坐标相对于棋盘越界,则不再搜索这个方向
if (mubiao_y >= qipan.length || mubiao_y < 0 || mubiao_x >= qipan[0].length || mubiao_x < 0) {
fangxiang[j] = null;
continue;
}
//如果最近落子坐标上的值等于目标坐标上的值(颜色相同),则计数器上某条线加1
//否则认为这个方向没有棋子或有别的rocketmq源码打包颜色的棋子,不再搜索这个方向
if (number == qipan[mubiao_y][mubiao_x]) {
jieguo[j % 4]++;
}else {
fangxiang[j] = null;
}
}
}
//查看计数器上是否有比3更大的数(查看是否有一方胜出)
for (int i : jieguo) {
if (i > 3) {
System.out.println("游戏结束");
if (bushu % 2 == 1) {
System.out.println("黑方胜");
} else {
System.out.println("白方胜");
}
return true;
}
}
//没有胜出者的情况下,查看棋盘上是否还有空位置,如果有,则游戏可以继续
for (int[] arr : qipan) {
for (int i : arr) {
if (i == 0) {
return false;
}
}
}
//如果没有空位置,达达吧源码则平局
System.out.println("游戏结束,平局");
return true;
}
}
急需基于eclipse的JAVA小游戏源代码!!!
单人版五子棋,不用导入,直接新建一个mywindow类就行,然后把一下代码粘贴就Ok了。或者,直接用dos就可以了。。
---------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class mypanel extends Panel implements MouseListener
{
int chess[][] = new int[][];
boolean Is_Black_True;
mypanel()
{
Is_Black_True = true;
for(int i = 0;i < ;i++)
{
for(int j = 0;j < ;j++)
{
chess[i][j] = 0;
}
}
addMouseListener(this);
setBackground(Color.BLUE);
setBounds(0, 0, , );
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
if(x < || x > + ||y < || y > +)
{
return;
}
if(chess[x/-1][y/-1] != 0)
{
return;
}
if(Is_Black_True == true)
{
chess[x/-1][y/-1] = 1;
Is_Black_True = false;
repaint();
Justisewiner();
return;
}
if(Is_Black_True == false)
{
chess[x/-1][y/-1] = 2;
Is_Black_True = true;
repaint();
Justisewiner();
return;
}
}
void Drawline(Graphics g)
{
for(int i = ;i <= ;i += )
{
for(int j = ;j <= ; j+= )
{
g.setColor(Color.WHITE);
g.drawLine(i, j, i, );
}
}
for(int j = ;j <= ;j += )
{
g.setColor(Color.WHITE);
g.drawLine(, j, , j);
}
}
void Drawchess(Graphics g)
{
for(int i = 0;i < ;i++)
{
for(int j = 0;j < ;j++)
{
if(chess[i][j] == 1)
{
g.setColor(Color.BLACK);
g.fillOval((i + 1) * - 8, (j + 1) * - 8, , );
}
if(chess[i][j] == 2)
{
g.setColor(Color.WHITE);
g.fillOval((i + 1) * - 8, (j + 1) * - 8, , );
}
}
}
}
void Justisewiner()
{
int black_count = 0;
int white_count = 0;
int i = 0;
for(i = 0;i < ;i++)//横向判断
{
for(int j = 0;j < ;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 < ;i++)//竖向判断
{
for(int j = 0;j < ;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 < ;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<;i++)
{
for(int j=0;j<;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,, , );
setTitle("单人版五子棋");
setBounds(, , , );
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();
}
}