宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

本文目录一览:

  • 1、用java编写一个猜数字游戏
  • 2、JAVA小游戏程序代码
  • 3、Java数独游戏代码
  • 4、求一个简单的Java小游戏的代码
  • 5、java代码猜拳游戏相关代码请教

用java编写一个猜数字游戏,

package day06;

import java.util.Scanner;

//猜字符游戏

public class GuessingGame {

//主方法

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int count = 0; //猜错的次数

char[] chs = generate(); //随机生成的字符数组

System.out.println(chs); //作弊

while(true){ //自造死循环

System.out.println(“猜吧!”);

String str = scan.next().toUpperCase(); //获取用户输入的字符串

if(str.equals(“EXIT”)){ //判断str是否是EXIT

System.out.println(“下次再来吧!”);

break;

}

char[] input = str.toCharArray(); //将字符串转换为字符数组

int[] result = check(chs,input);  //对比

if(result[0]==chs.length){ //位置对为5

int score = chs.length*100 – count*10; //一个字符100分,错一次减10分

System.out.println(“恭喜你猜对了,得分:” + score);

break; //猜对时跳出循环

}else{ //没猜对

count++; //猜错次数增1

System.out.println(“字符对:”+result[1]+”个,位置对:”+result[0]+”个”);

}

}

}

//随机生成5个字符数组

public static char[] generate(){

char[] chs = new char[5];

char[] letters = { ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’,

‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’,

‘W’, ‘X’, ‘Y’, ‘Z’};

boolean[] flags = new boolean[letters.length]; //1.

for(int i=0;ichs.length;i++){

int index;

do{

index = (int)(Math.random()*letters.length); //0到25

}while(flags[index]==true); //2.

chs[i] = letters[index];

flags[index] = true; //3.

}

return chs;

}

//对比随机数组与用户输入的数组

public static int[] check(char[] chs,char[] input){

int[] result = new int[2];

for(int i=0;ichs.length;i++){

for(int j=0;jinput.length;j++){

if(chs[i]==input[j]){ //字符对

result[1]++; //字符对个数增1

if(i==j){ //位置对

result[0]++; //位置对个数增1

}

break;

}

}

}

return result;

}

}

JAVA小游戏程序代码

这个是比较有名的那个烟花,不知道你有没有用:

建个工程,以Fireworks为类即可

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import javax.swing.*;

public class Fireworks extends Applet implements MouseListener,Runnable

{

int x,y;

int top,point;

/**

*对小程序进行变量和颜色的初始化。

*/

public void init()

{

x = 0;

y = 0;

//设置背景色为黑色

setBackground(Color.black);

addMouseListener(this);

}

public void paint(Graphics g)

{

}

/**

*使该程序可以作为应用程序运行。

*/

public static void main(String args[]) {

Fireworks applet = new Fireworks();

JFrame frame = new JFrame(“TextAreaNew”);

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

frame.getContentPane().add(

applet, BorderLayout.CENTER);

frame.setSize(800,400);

applet.init();

applet.start();

frame.setVisible(true);

}

/**

*程序主线程,对一个烟花进行绘制。

*/

public void run()

{

//变量初始化

Graphics g1;

g1 = getGraphics();

int y_move,y_click,x_click;

int v;

x_click = x;

y_click = y;

y_move = 400;

v = 3;

int r,g,b;

while(y_move y_click)

{

g1.setColor(Color.black);

g1.fillOval(x_click,y_move,5,5);

y_move -= 5;

r = (((int)Math.round(Math.random()*4321))%200)+55;

g = (((int)Math.round(Math.random()*4321))%200)+55;

b = (((int)Math.round(Math.random()*4321))%200)+55;

g1.setColor(new Color(r,g,b));

g1.fillOval(x_click,y_move,5,5);

for(int j = 0 ;j=10;j++)

{

if(r55) r -= 20;

if(g55) g -= 20;

if(b55) b -=20;

g1.setColor(new Color(r,g,b));

g1.fillOval(x_click,y_move+j*5,5,5);

}

g1.setColor(Color.black);

g1.fillOval(x_click,y_move+5*10,5,5);

try

{

Thread.currentThread().sleep(v++);

} catch (InterruptedException e) {}

}

for(int j=12;j=0;j–)

{

g1.setColor(Color.black);

g1.fillOval(x_click,y_move+(j*5),5,5);

try

{

Thread.currentThread().sleep((v++)/3);

} catch (InterruptedException e) {}

}

y_move = 400;

g1.setColor(Color.black);

while(y_move y_click)

{

g1.fillOval(x_click-2,y_move,9,5);

y_move -= 5;

}

v = 15;

for(int i=0;i=25;i++)

{

r = (((int)Math.round(Math.random()*4321))%200)+55;

g = (((int)Math.round(Math.random()*4321))%200)+55;

b = (((int)Math.round(Math.random()*4321))%200)+55;

g1.setColor(new Color(r,g,b));

g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i);

if(i23)

{

g1.drawOval(x_click-3*(i+1),y_click-3*(i+1),6*(i+1),6*(i+1));

g1.drawOval(x_click-3*(i+2),y_click-3*(i+2),6*(i+2),6*(i+2));

}

try

{

Thread.currentThread().sleep(v++);

} catch (InterruptedException e) {}

g1.setColor(Color.black);

g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i);

}

}

/**

*对鼠标事件进行监听。

*临听其鼠标按下事件。

*当按下鼠标时,产生一个新线程。

*/

public void mousePressed(MouseEvent e)

{

x = e.getX();

y = e.getY();

Thread one;

one = new Thread(this);

one.start();

one = null;

}

/**

*实现MouseListener接中的方法。为一个空方法。

*/

public void mouseReleased(MouseEvent e)

{

}

/**

*实现MouseListener接中的方法。为一个空方法。

*/

public void mouseEntered(MouseEvent e)

{

}

/**

*实现MouseListener接中的方法。为一个空方法。

*/

public void mouseExited(MouseEvent e)

{

}

/**

*实现MouseListener接中的方法。为一个空方法。

*/

public void mouseClicked(MouseEvent e)

{

}

}

Java数独游戏代码

public class ShuDu {

/**存储数字的数组*/

static int[][] n = new int[9][9];

/**生成随机数字的源数组,随机数字从该数组中产生*/

static int[] num = {1,2,3,4,5,6,7,8,9};

public static void main(String[] args) {

//生成数字

for(int i = 0;i 9;i++){

//尝试填充的数字次数

int time = 0;

//填充数字

for(int j = 0;j 9;j++){

//产生数字

n[i][j] = generateNum(time);

//如果返回值为0,则代表卡住,退回处理

//退回处理的原则是:如果不是第一列,则先倒退到前一列,否则倒退到前一行的最后一列

if(n[i][j] == 0){

//不是第一列,则倒退一列

if(j 0){

j-=2;

continue;

}else{//是第一列,则倒退到上一行的最后一列

i–;

j = 8;

continue;

}

}

//填充成功

if(isCorret(i,j)){

//初始化time,为下一次填充做准备

time = 0;

}else{ //继续填充

//次数增加1

time++;

//继续填充当前格

j–;

}

}

}

//输出结果

for(int i = 0;i 9;i++){

for(int j = 0;j 9;j++){

System.out.print(n[i][j] + ” “);

}

System.out.println();

}

}

/**

* 是否满足行、列和3X3区域不重复的要求

* @param row 行号

* @param col 列号

* @return true代表符合要求

*/

public static boolean isCorret(int row,int col){

return (checkRow(row) checkLine(col) checkNine(row,col));

}

/**

* 检查行是否符合要求

* @param row 检查的行号

* @return true代表符合要求

*/

public static boolean checkRow(int row){

for(int j = 0;j 8;j++){

if(n[row][j] == 0){

continue;

}

for(int k =j + 1;k 9;k++){

if(n[row][j] == n[row][k]){

return false;

}

}

}

return true;

}

/**

* 检查列是否符合要求

* @param col 检查的列号

* @return true代表符合要求

*/

public static boolean checkLine(int col){

for(int j = 0;j 8;j++){

if(n[j][col] == 0){

continue;

}

for(int k =j + 1;k 9;k++){

if(n[j][col] == n[k][col]){

return false;

}

}

}

return true;

}

/**

* 检查3X3区域是否符合要求

* @param row 检查的行号

* @param col 检查的列号

* @return true代表符合要求

*/

public static boolean checkNine(int row,int col){

//获得左上角的坐标

int j = row / 3 * 3;

int k = col /3 * 3;

//循环比较

for(int i = 0;i 8;i++){

if(n[j + i/3][k + i % 3] == 0){

continue;

}

for(int m = i+ 1;m 9;m++){

if(n[j + i/3][k + i % 3] == n[j + m/3][k + m % 3]){

return false;

}

}

}

return true;

}

/**

* 产生1-9之间的随机数字

* 规则:生成的随机数字放置在数组8-time下标的位置,随着time的增加,已经尝试过的数字将不会在取到

* 说明:即第一次次是从所有数字中随机,第二次时从前八个数字中随机,依次类推,

* 这样既保证随机,也不会再重复取已经不符合要求的数字,提高程序的效率

* 这个规则是本算法的核心

* @param time 填充的次数,0代表第一次填充

* @return

*/

public static int generateNum(int time){

//第一次尝试时,初始化随机数字源数组

if(time == 0){

for(int i = 0;i 9;i++){

num[i] = i + 1;

}

}

//第10次填充,表明该位置已经卡住,则返回0,由主程序处理退回

if(time == 9){

return 0;

}

//不是第一次填充

//生成随机数字,该数字是数组的下标,取数组num中该下标对应的数字为随机数字

int ranNum = (int)(Math.random() * (9 – time));

//把数字放置在数组倒数第time个位置,

int temp = num[8 – time];

num[8 – time] = num[ranNum];

num[ranNum] = temp;

//返回数字

return num[8 – time];

}

}

java游戏代码(java游戏代码分享)(Java游戏项目合集整整12套毕设系统)-风君雪科技博客

求一个简单的Java小游戏的代码

连连看的小源码

package Lianliankan;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class lianliankan implements ActionListener

{

JFrame mainFrame; //主面板

Container thisContainer;

JPanel centerPanel,southPanel,northPanel; //子面板

JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组

JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮

JLabel fractionLable=new JLabel(“0”); //分数标签

JButton firstButton,secondButton; //分别记录两次被选中的按钮

int grid[][] = new int[8][7];//储存游戏按钮位置

static boolean pressInformation=false; //判断是否有按钮被选中

int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标

int i,j,k,n;//消除方法控制

public void init(){

mainFrame=new JFrame(“JKJ连连看”);

thisContainer = mainFrame.getContentPane();

thisContainer.setLayout(new BorderLayout());

centerPanel=new JPanel();

southPanel=new JPanel();

northPanel=new JPanel();

thisContainer.add(centerPanel,”Center”);

thisContainer.add(southPanel,”South”);

thisContainer.add(northPanel,”North”);

centerPanel.setLayout(new GridLayout(6,5));

for(int cols = 0;cols 6;cols++){

for(int rows = 0;rows 5;rows++ ){

diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));

diamondsButton[cols][rows].addActionListener(this);

centerPanel.add(diamondsButton[cols][rows]);

}

}

exitButton=new JButton(“退出”);

exitButton.addActionListener(this);

resetButton=new JButton(“重列”);

resetButton.addActionListener(this);

newlyButton=new JButton(“再来一局”);

newlyButton.addActionListener(this);

southPanel.add(exitButton);

southPanel.add(resetButton);

southPanel.add(newlyButton);

fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));

northPanel.add(fractionLable);

mainFrame.setBounds(280,100,500,450);

mainFrame.setVisible(true);

}

public void randomBuild() {

int randoms,cols,rows;

for(int twins=1;twins=15;twins++) {

randoms=(int)(Math.random()*25+1);

for(int alike=1;alike=2;alike++) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

while(grid[cols][rows]!=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

}

this.grid[cols][rows]=randoms;

}

}

}

public void fraction(){

fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));

}

public void reload() {

int save[] = new int[30];

int n=0,cols,rows;

int grid[][]= new int[8][7];

for(int i=0;i=6;i++) {

for(int j=0;j=5;j++) {

if(this.grid[i][j]!=0) {

save[n]=this.grid[i][j];

n++;

}

}

}

n=n-1;

this.grid=grid;

while(n=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

while(grid[cols][rows]!=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

}

this.grid[cols][rows]=save[n];

n–;

}

mainFrame.setVisible(false);

pressInformation=false; //这里一定要将按钮点击信息归为初始

init();

for(int i = 0;i 6;i++){

for(int j = 0;j 5;j++ ){

if(grid[i+1][j+1]==0)

diamondsButton[i][j].setVisible(false);

}

}

}

public void estimateEven(int placeX,int placeY,JButton bz) {

if(pressInformation==false) {

x=placeX;

y=placeY;

secondMsg=grid[x][y];

secondButton=bz;

pressInformation=true;

}

else {

x0=x;

y0=y;

fristMsg=secondMsg;

firstButton=secondButton;

x=placeX;

y=placeY;

secondMsg=grid[x][y];

secondButton=bz;

if(fristMsg==secondMsg secondButton!=firstButton){

xiao();

}

}

}

public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释

if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))){ //判断是否相邻

remove();

}

else{

for (j=0;j7;j++ ) {

if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空

if (yj) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边

for (i=y-1;i=j;i– ){ //判断第二按钮左侧直到第一按钮中间有没有按钮

if (grid[x][i]!=0) {

k=0;

break;

}

else{ k=1; } //K=1说明通过了第一次验证

}

if (k==1) {

linePassOne();

}

}

if (yj){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边

for (i=y+1;i=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮

if (grid[x][i]!=0){

k=0;

break;

}

else { k=1; }

}

if (k==1){

linePassOne();

}

}

if (y==j ) {

linePassOne();

}

}

if (k==2) {

if (x0==x) {

remove();

}

if (x0x) {

for (n=x0;n=x-1;n++ ) {

if (grid[n][j]!=0) {

k=0;

break;

}

if(grid[n][j]==0 n==x-1) {

remove();

}

}

}

if (x0x) {

for (n=x0;n=x+1 ;n– ) {

if (grid[n][j]!=0) {

k=0;

break;

}

if(grid[n][j]==0 n==x+1) {

remove();

}

}

}

}

}

for (i=0;i8;i++ ) { //列

if (grid[i][y0]==0) {

if (xi) {

for (j=x-1;j=i ;j– ) {

if (grid[j][y]!=0) {

k=0;

break;

}

else { k=1; }

}

if (k==1) {

rowPassOne();

}

}

if (xi) {

for (j=x+1;j=i;j++ ) {

if (grid[j][y]!=0) {

k=0;

break;

}

else { k=1; }

}

if (k==1) {

rowPassOne();

}

}

if (x==i) {

rowPassOne();

}

}

if (k==2){

if (y0==y) {

remove();

}

if (y0y) {

for (n=y0;n=y-1 ;n++ ) {

if (grid[i][n]!=0) {

k=0;

break;

}

if(grid[i][n]==0 n==y-1) {

remove();

}

}

}

if (y0y) {

for (n=y0;n=y+1 ;n–) {

if (grid[i][n]!=0) {

k=0;

break;

}

if(grid[i][n]==0 n==y+1) {

remove();

}

}

}

}

}

}

}

public void linePassOne(){

if (y0j){ //第一按钮同行空按钮在左边

for (i=y0-1;i=j ;i– ){ //判断第一按钮同左侧空按钮之间有没按钮

if (grid[x0][i]!=0) {

k=0;

break;

}

else { k=2; } //K=2说明通过了第二次验证

}

}

if (y0j){ //第一按钮同行空按钮在与第二按钮之间

for (i=y0+1;i=j ;i++){

if (grid[x0][i]!=0) {

k=0;

break;

}

else{ k=2; }

}

}

}

public void rowPassOne(){

if (x0i) {

for (j=x0-1;j=i ;j– ) {

if (grid[j][y0]!=0) {

k=0;

break;

}

else { k=2; }

}

}

if (x0i) {

for (j=x0+1;j=i ;j++ ) {

if (grid[j][y0]!=0) {

k=0;

break;

}

else { k=2; }

}

}

}

public void remove(){

firstButton.setVisible(false);

secondButton.setVisible(false);

fraction();

pressInformation=false;

k=0;

grid[x0][y0]=0;

grid[x][y]=0;

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==newlyButton){

int grid[][] = new int[8][7];

this.grid = grid;

randomBuild();

mainFrame.setVisible(false);

pressInformation=false;

init();

}

if(e.getSource()==exitButton)

System.exit(0);

if(e.getSource()==resetButton)

reload();

for(int cols = 0;cols 6;cols++){

for(int rows = 0;rows 5;rows++ ){

if(e.getSource()==diamondsButton[cols][rows])

estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);

}

}

}

public static void main(String[] args) {

lianliankan llk = new lianliankan();

llk.randomBuild();

llk.init();

}

}

//old 998 lines

//new 318 lines

java代码猜拳游戏相关代码请教

comp是电脑产生的随机数字(电脑出的拳),people 是人出的拳。 因为剪刀石头布只有 1 2 3

。如果电脑的数字比的你刚好大1,就是它比你的大。 如21,32对应就是(石头大于剪刀,布大于石头)。 但也有可能是剪刀大于布 。 那么剪刀的位子是1 ,布的位子是3. 所以当电脑数字减你的数字等于2时 就是(电脑出的布 ,你出的石头这样的情况了)。

所以是if((comp-people)==-1||(comp-people)==2) 这两者结合就是你赢的时候