本篇文章给大家谈谈java五子棋小游戏,以及基于java的五子棋游戏的设计代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、JAVA小游戏五子棋中想加入一个倒计时器,每回合玩家有20秒下棋时间,求如何实现?2、通过Java控制台程序完成一个五子棋游戏,能够实现黑白子交互下棋,程序自动判断输赢(需要完成判断输赢3、在Java小游戏五子棋为什么true是黑子?4、想用Java 做五子棋游戏而且是多线程的应该怎么做
JAVA小游戏五子棋中想加入一个倒计时器,每回合玩家有20秒下棋时间,求如何实现?
用线程 间隔1s
private Thread playerThread;// 玩家时间所对应的线程
private Thread robotThread;// 机器人时间所对应的线程
private int playerTime = 1800;// 玩家的总时间 30*60, 30分钟
// 初始化-玩家时间所对应的线程
playerThread = new Thread(new Runnable() {
public void run() {// 相执行start方法时该线程启动
while (true) {
try {
playerThread.sleep(1000);// 线程睡一秒钟
} catch (Exception e) {
e.printStackTrace();
}
playerTime–;// 玩家时间减一秒钟
canvas.repaint();// 绘制窗囗
if (playerTime == 0) {// 如果玩家的时间用完了,表示玩家输
isGameBegin = false;// 游戏没有开始
isGameOver = true;// 游戏结束
canvas.repaint();// 绘制窗囗
JOptionPane.showMessageDialog(null, “机器人赢了!”);
}
}
}
}
});
通过Java控制台程序完成一个五子棋游戏,能够实现黑白子交互下棋,程序自动判断输赢(需要完成判断输赢
import java.util.Scanner;
public class FiveChessGame {
public static void display(char[][] arr) {// 图形显示函数
System.out.print(” “);
for (char i = ‘0’; i = ‘9’; i++)
// 打印行号0~9
System.out.print(i + ” “);
for (char j = ‘a’; j = ‘f’; j++)
// 打印行号a~f
System.out.print(j + ” “);
System.out.println();
char c = ‘0’;
for (int i = 0; i 10; i++) {
System.out.print(c++ + ” “);
for (int j = 0; j 16; j++) {
System.out.print(arr[i][j] + ” “);
}
System.out.println();
}
c = ‘a’;
for (int i = 10; i 16; i++) {
System.out.print(c++ + ” “);
for (int j = 0; j 16; j++) {
System.out.print(arr[i][j] + ” “);
}
System.out.println();
}
}
public static int getNum(char c) {
int num;
switch (c) {
case ‘0’:
num = 0;
break;
case ‘1’:
num = 1;
break;
case ‘2’:
num = 2;
break;
case ‘3’:
num = 3;
break;
case ‘4’:
num = 4;
break;
case ‘5’:
num = 5;
break;
case ‘6’:
num = 6;
break;
case ‘7’:
num = 7;
break;
case ‘8’:
num = 8;
break;
case ‘9’:
num = 9;
break;
case ‘a’:
case ‘A’:
num = 10;
break;
case ‘b’:
case ‘B’:
num = 11;
break;
case ‘c’:
case ‘C’:
num = 12;
break;
case ‘d’:
case ‘D’:
num = 13;
break;
case ‘e’:
case ‘E’:
num = 14;
break;
case ‘f’:
case ‘F’:
num = 15;
break;
default:
System.out.println(“输入有错误!”);
return -1;
}
return num;
}
public static int isWinner(char c, char[][] arr, int a, int b) {
if (c == ‘@’) {
int count = 0;
for (int i = 0; i 16; i++) {
if (arr[a][i] == ‘@’) {
count++;
if (count = 5) {
return 1;
}
} else {
count = 0;
}
}
count = 0;
for (int i = 0; i 16; i++) {
if (arr[i][b] == ‘@’) {
count++;
if (count = 5) {
return 1;
}
} else {
count = 0;
}
}
count = 0;
if (a = b) {
int j = 0;
for (int i = a – b; i = 15 – a + b; i++) {
if (arr[i][j] == ‘@’) {
count++;
j++;
if (count = 5) {
return 1;
}
} else {
count = 0;
j++;
}
}
} else {
int j = 0;
for (int i = b – a; i = 15 – b + a; i++) {
if (arr[j][i] == ‘@’) {
count++;
j++;
if (count = 5) {
return 1;
}
} else {
count = 0;
j++;
}
}
}
int j = a + b;
count = 0;
if (a + b = 15) {
for (int i = 0; i = a + b; i++) {
if (arr[i][j] == ‘@’) {
count++;
j–;
if (count = 5) {
return 1;
}
} else {
count = 0;
j–;
}
}
} else {
j = 15;
for (int i = a + b – 15; i = 15; i++) {
if (arr[i][j] == ‘@’) {
count++;
j–;
if (count = 5) {
return 1;
}
} else {
count = 0;
j–;
}
}
}
} else {// 判断白色胜
int count = 0;
for (int i = 0; i 16; i++) {
if (arr[a][i] == ‘O’) {
count++;
if (count = 5) {
return 2;
}
} else {
count = 0;
}
}
count = 0;
for (int i = 0; i 16; i++) {
if (arr[i][b] == ‘O’) {
count++;
if (count = 5) {
return 2;
}
} else {
count = 0;
}
}
count = 0;
if (a = b) {
int j = 0;
for (int i = a – b; i = 15 – a + b; i++) {
if (arr[i][j] == ‘O’) {
count++;
j++;
if (count = 5) {
return 2;
}
} else {
count = 0;
j++;
}
}
} else {
int j = 0;
for (int i = b – a; i = 15 – b + a; i++) {
if (arr[j][i] == ‘O’) {
count++;
j++;
if (count = 5) {
return 2;
}
} else {
count = 0;
j++;
}
}
}
int j = a + b;
count = 0;
if (a + b = 15) {
for (int i = 0; i = a + b; i++) {
if (arr[i][j] == ‘O’) {
count++;
j–;
if (count = 5) {
return 2;
}
} else {
count = 0;
j–;
}
}
} else {
j = 15;
for (int i = a + b – 15; i = 15; i++) {
if (arr[i][j] == ‘O’) {
count++;
j–;
if (count = 5) {
return 2;
}
} else {
count = 0;
j–;
}
}
}
}
return 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
char[][] arr = new char[16][16];
for (int i = 0; i 16; i++)
// 二维数组初始化
for (int j = 0; j 16; j++)
arr[i][j] = ‘.’;
display(arr);
int i = 0;
while (true) {
if (i % 2 == 0) {
System.out.println(“请黑方落子:”);
String in = sc.next();
char c1 = in.charAt(0);
char c2 = in.charAt(1);
int a = getNum(c1);
int b = getNum(c2);
if (arr[a][b] != ‘.’) {
System.out.println(“该位置已经有棋子,请重新输入!”);
} else {
arr[a][b] = ‘@’;
display(arr);
int result = isWinner(‘@’, arr, a, b);
if (result == 1) {
System.out.println(“黑方获胜!”);
return;
}
i++;
}
} else {
System.out.println(“请白方落子:”);
String in = sc.next();
char c1 = in.charAt(0);
char c2 = in.charAt(1);
int a = getNum(c1);
int b = getNum(c2);
if (arr[a][b] != ‘.’) {
System.out.println(“该位置已经有棋子,请重新输入!”);
} else {
arr[a][b] = ‘O’;
display(arr);
int result = isWinner(‘O’, arr, a, b);
if (result == 2) {
System.out.println(“白方获胜!”);
return;
}
i++;
}
}
}
}
}
在Java小游戏五子棋为什么true是黑子?
不太明白你这个Java实现的小游戏五子棋具体的编程逻辑,不过既然是五子棋,棋子只有黑白两色,那么是可以用布尔值的true和false来表示黑和白的,反之,应该也可以用true代表白,用false代表黑。
想用Java 做五子棋游戏而且是多线程的应该怎么做
直接上程序吧:
//wuziqi.java
import java.applet.Applet;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
@SuppressWarnings(“serial”)
public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener
{
int color_Qizi=0;//旗子的颜色标识 0:白子 1:黑子
int intGame_Start=0;//游戏开始标志 0未开始 1游戏中
int intGame_Body[][]=new int[16][16]; //设置棋盘棋子状态 0 无子 1 白子 2 黑子
Button b1=new Button(“游戏开始”);
Button b2=new Button(“重置游戏”);
Label lblWin=new Label(” “);
Checkbox ckbHB[]=new Checkbox[2];
CheckboxGroup ckgHB=new CheckboxGroup();
public void init()
{
setLayout(null);
addMouseListener(this);
add(b1);
b1.setBounds(330,50,80,30);
b1.addActionListener(this);
add(b2);
b2.setBounds(330,90,80,30);
b2.addActionListener(this);
ckbHB[0]=new Checkbox(“白子先”,ckgHB,false);
ckbHB[0].setBounds(320,20,60,30);
ckbHB[1]=new Checkbox(“黑子先”,ckgHB,false);
ckbHB[1].setBounds(380,20,60,30);
add(ckbHB[0]);
add(ckbHB[1]);
ckbHB[0].addItemListener(this);
ckbHB[1].addItemListener(this);
add(lblWin);
lblWin.setBounds(330,130,80,30);
Game_start_csh();
}
public void itemStateChanged(ItemEvent e)
{
if (ckbHB[0].getState()) //选择黑子先还是白子先
{
color_Qizi=0;
}
else
{
color_Qizi=1;
}
}
public void actionPerformed(ActionEvent e)
{
@SuppressWarnings(“unused”)
Graphics g=getGraphics();
if (e.getSource()==b1)
{
Game_start();
}
else
{
Game_re();
}
}
public void mousePressed(MouseEvent e){}
@SuppressWarnings(“unused”)
public void mouseClicked(MouseEvent e)
{
Graphics g=getGraphics();
int x1,y1;
x1=e.getX();
y1=e.getY();
if (e.getX()20 || e.getX()300 || e.getY()20 || e.getY()300)
{
return;
}
if (x1%2010)
{
x1+=20;
}
if(y1%2010)
{
y1+=20;
}
x1=x1/20*20;
y1=y1/20*20;
set_Qizi(x1,y1);
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void paint(Graphics g)
{
draw_qipan(g);
}
public void set_Qizi(int x,int y) //落子
{
if (intGame_Start==0) //判断游戏未开始
{
return;
}
if (intGame_Body[x/20][y/20]!=0)
{
return;
}
Graphics g=getGraphics();
if (color_Qizi==1)//判断黑子还是白子
{
g.setColor(Color.black);
color_Qizi=0;
}
else
{
g.setColor(Color.white);
color_Qizi=1;
}
g.fillOval(x-10,y-10,20,20);
intGame_Body[x/20][y/20]=color_Qizi+1;
if (Game_win_1(x/20,y/20)) //判断输赢
{
lblWin.setText(Get_qizi_color(color_Qizi)+”赢了!”);
intGame_Start=0;
}
if (Game_win_2(x/20,y/20)) //判断输赢
{
lblWin.setText(Get_qizi_color(color_Qizi)+”赢了!”);
intGame_Start=0;
}
if (Game_win_3(x/20,y/20)) //判断输赢
{
lblWin.setText(Get_qizi_color(color_Qizi)+”赢了!”);
intGame_Start=0;
}
if (Game_win_4(x/20,y/20)) //判断输赢
{
lblWin.setText(Get_qizi_color(color_Qizi)+”赢了!”);
intGame_Start=0;
}
}
public String Get_qizi_color(int x)
{
if (x==0)
{
return “黑子”;
}
else
{
return “白子”;
}
}
public void draw_qipan(Graphics G) //画棋盘 15*15
{
G.setColor(Color.lightGray);
G.fill3DRect(10,10,300,300,true);
G.setColor(Color.black);
for(int i=1;i16;i++)
{
G.drawLine(20,20*i,300,20*i);
G.drawLine(20*i,20,20*i,300);
}
}
public void Game_start() //游戏开始
{
intGame_Start=1;
Game_btn_enable(false);
b2.setEnabled(true);
}
public void Game_start_csh() //游戏开始初始化
{
intGame_Start=0;
Game_btn_enable(true);
b2.setEnabled(false);
ckbHB[0].setState(true);
for (int i=0;i16 ;i++ )
{
for (int j=0;j16 ;j++ )
{
intGame_Body[i][j]=0;
}
}
lblWin.setText(“”);
}
public void Game_re() //游戏重新开始
{
repaint();
Game_start_csh();
}
public void Game_btn_enable(boolean e) //设置组件状态
{
b1.setEnabled(e);
b2.setEnabled(e);
ckbHB[0].setEnabled(e);
ckbHB[1].setEnabled(e);
}
public boolean Game_win_1(int x,int y) //判断输赢 横
{
int x1,y1,t=1;
x1=x;
y1=y;
for (int i=1;i5 ;i++ )
{
if (x115)
{
break;
}
if (intGame_Body[x1+i][y1]==intGame_Body[x][y])
{
t+=1;
}
else
{
break;
}
}
for (int i=1;i5 ;i++ )
{
if (x11)
{
break;
}
if(intGame_Body[x1-i][y1]==intGame_Body[x][y])
{
t+=1;
}
else
{
break;
}
}
if (t4)
{
return true;
}
else
{
return false;
}
}
public boolean Game_win_2(int x,int y) //判断输赢 竖
{
int x1,y1,t=1;
x1=x;
y1=y;
for (int i=1;i5 ;i++ )
{
if (x115)
{
break;
}
if (intGame_Body[x1][y1+i]==intGame_Body[x][y])
{
t+=1;
}
else
{
break;
}
}
for (int i=1;i5 ;i++ )
{
if (x11)
{
break;
}
if(intGame_Body[x1][y1-i]==intGame_Body[x][y])
{
t+=1;
}
else
{
break;
}
}
if (t4)
{
return true;
}
else
{
return false;
}
}
public boolean Game_win_3(int x,int y) //判断输赢 左斜
{
int x1,y1,t=1;
x1=x;
y1=y;
for (int i=1;i5 ;i++ )
{
if (x115)
{
break;
}
if (intGame_Body[x1+i][y1-i]==intGame_Body[x][y])
{
t+=1;
}
else
{
break;
}
}
for (int i=1;i5 ;i++ )
{
if (x11)
{
break;
}
if(intGame_Body[x1-i][y1+i]==intGame_Body[x][y])
{
t+=1;
}
else
{
break;
}
}
if (t4)
{
return true;
}
else
{
return false;
}
}
public boolean Game_win_4(int x,int y) //判断输赢 左斜
{
int x1,y1,t=1;
x1=x;
y1=y;
for (int i=1;i5 ;i++ )
{
if (x115)
{
break;
}
if (intGame_Body[x1+i][y1+i]==intGame_Body[x][y])
{
t+=1;
}
else
{
break;
}
}
for (int i=1;i5 ;i++ )
{
if (x11)
{
break;
}
if(intGame_Body[x1-i][y1-i]==intGame_Body[x][y])
{
t+=1;
}
else
{
break;
}
}
if (t4)
{
return true;
}
else
{
return false;
}
}
}
关于java五子棋小游戏和基于java的五子棋游戏的设计代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。