好玩的cmd命令贪吃蛇(cmd贪吃蛇代码)

http://www.itjxue.com  2023-01-26 00:04  来源:未知  点击次数: 

谁有贪吃蛇_vs2008 C++代码啊?

使用链表完成贪吃蛇游戏的代码非常适合,贪吃蛇的每次移动可以看做链表的头部插入和尾部删除

具体代码如下

#include?"snake.h"

#define?UP_CMD?'w'

#define?DOWN_CMD?'s'

#define?LEFT_CMD?'a'

#define?RIGHT_CMD?'d'

#define?EAT_FOOD?0x02 //吃到食物

#define?EAT_SELF?0x04 //迟到自己

#define?EAT_NONE?0x05 //无吃到

typedef?char?DIRECTION;

class?SnakeNode

{

private:

COORD?pos; //节点坐标

SnakeNode?*next;

public:

static?COORD?foodxy; //食物坐标

static?SnakeNode?*head; //头部

static?DIRECTION?direction;//方向

static?int?speed; //移动速度

static?void?food(); //产生食物

static?void?create(); //产生蛇

static?void?free(); //销毁

static?int?move(); //移动

static?void?getcmd(DIRECTION?d); //改变方向

};

COORD?SnakeNode::foodxy;

SnakeNode*?SnakeNode::head?=?NULL;

DIRECTION?SnakeNode::direction?=?RIGHT_CMD;//方向

int?SnakeNode::speed?=?400;//方向

//蛇类

void?SnakeNode::free()

{

SnakeNode?*p,*q;

p?=?head;

while(p)

{

?????????q?=?p-next;

?delete?p;

?????????p=q;????

}

}

void?SnakeNode::getcmd(DIRECTION?d)

{

switch(d)

{

case?'w':case?'W':

if(direction!='s')?direction='w';break;

case?'s':case?'S':

if(direction!='w')?direction='s';break;

case?'a':case?'A':

if(direction!='d')?direction='a';break;

case?'d':case?'D':

if(direction!='a')?direction='d';break;

}

}

void?SnakeNode::food()

{

rand:

srand(GetTickCount());

foodxy.X=(rand()%38)*2;

foodxy.Y=rand()%25;

SnakeNode?*p?=?head;

while(p!=NULL)

{

if(p-pos.X==foodxy.Xp-pos.Y==foodxy.Y)

{

goto?rand;

break;?

}

p?=?p-next;

}

gotoxy(foodxy.X,foodxy.Y);

printf("☆");

}

void?SnakeNode::create()

{

SnakeNode?*p?=?new?SnakeNode;

head?=?p;

int?i;

for(i=26;i=20;i-=2)

{

p-pos.X=i;

p-pos.Y=10;

gotoxy(i,10);

cout"■";

p-next?=?new?SnakeNode;

if(i==20)

{

p-next?=?NULL;

}

p?=?p-next;

}

food();

}

int?SnakeNode::move()

{

SnakeNode?*newhead?=?new?SnakeNode;

int?cX,cY;

switch(direction)

{

case?'w':

{?

cX?=?0;?cY?=?-1;

}break;//创建一个新头部,删除尾巴

case?'s':

{???

cX?=?0;?cY?=?1;

}break;

case?'a':

{???

cX?=?-2;?cY?=?0;

}break;

case?'d':

{???

cX?=?+2;?cY?=?0;

}break;

}

newhead-pos.X?=?head-pos.X?+?cX;

newhead-pos.Y?=?head-pos.Y?+?cY;

newhead-next?=?head;

head?=?newhead;?

if(head-pos.Y0)???head-pos.Y=24;?

if(head-pos.Y24)??head-pos.Y=0;

if(head-pos.X0)???head-pos.X=78;

if(head-pos.X78)??head-pos.X=0;

//判断是否咬到自己

SnakeNode?*q?=?head-next;

while(q!=NULL)

{

if((q-pos.X==head-pos.X)(q-pos.Y==head-pos.Y))

{

free();

return?EAT_SELF;

}

q=q-next;

}

gotoxy(head-pos.X,head-pos.Y);

printf("■");

//判断是否吃到食物

SnakeNode?*p?=?head;

while(p!=NULL)

{

if(head-pos.X==foodxy.Xhead-pos.Y==foodxy.Y)//是否吃到食物

{

//吃到食物加速

if(speed?=?100)

speed?-=?20;

food();

return?EAT_FOOD;

break;//吃到了则不删除尾部,相当于增加一节

}

//删除尾部

if(p-next-next==NULL)

{

gotoxy(p-next-pos.X,p-next-pos.Y);

printf("??");

delete?p-next;

p-next=NULL;

return?EAT_NONE;

}

p?=?p-next;

}

}

int?main(){

SnakeNode::create();

while(1)

{

if(_kbhit())

{

int?ch=getch();

switch(ch)

???{

case?'w':case?'W':?case?'s':case?'S':case?'a':case?'A':

case?'d':case?'D':

{

SnakeNode::getcmd(ch);

}break;

case?'b':

{

SnakeNode::free();

return?0;

}

}

}

if(SnakeNode::move()==EAT_SELF)

{

gotoxy(30,11);

cout"游戏结束";

getch();

return?0;

}

Sleep(SnakeNode::speed);

}

}

在dos环境下c语言编程编一个贪吃蛇游戏

程序设计及说明

1

、边墙(

Wall

该类规定游戏的范围大小。

2

、蛇类(

Snake

用该类生成一个实例蛇

snake

3

、移动(

Move

该类用于实现对蛇的操作控制,即蛇头方向的上下左右的移动操作。

4

、食物类(

Food

该类是游戏过程中食物随机产生的控制和显示。

5

、判断死亡(

Dead

该类是对游戏过程中判断玩家操作是否导致蛇的死亡,其中包括蛇头咬食自己身体和蛇头是否触

及游戏“边墙”。

6

、蛇结点(

SnakeNode

该类是蛇吃下随机产生的食物从而增加长度的控制类,其中包括蛇长度增加和尾部的变化。

7

、计分统计(

Score

该类由于玩家的游戏成绩记录,及游戏结束时的得分输出。

...

部分函数及说明

1.Char menu();

/*

用于玩家选择的游戏速度,返回一个

char

*/

2.DELAY(char ch1);

/*

用于控制游戏速度

*/

3.void drawmap();

/*

绘制游戏地图函数

*

4

void menu()

/*

游戏帮助信息的输出

*

...

部分类细节解说

1

、蛇的构建

Snake

class Snake{

public:

int x[n]

int y[n];

int node;

//

蛇身长度

int direction;//

蛇运动方向

int

life;//

蛇生命,判断死亡

2

、随机食物

Food

利用

rand(

)函数进行随机数产生,然后就行坐标定位

void Food(void){

...

int pos_x = 0;

int pos_y = 0;

pos_x = rand() % length;//x

坐标的确定

pos_y = rand() % (width-1);//y

坐标的确定

...

3

、蛇头方向确定

利用

switch

语句进行方向确定

...

switch(){

case VK_UP:{

OutChar2.Y--;

y--;

break;

}

case VK_LEFT:{

OutChar2.Y++;

y++;

break;

}

case VK_DOWN:{

OutChar2.X---;

x--;

break;

}

case 'VK_RIGHT:{

OutChar2.X++;

x++;

break;

}

}

代码

#include iostream

#include ctime

#include conio.h

#include windows.h

#include time.h

using namespace std;

int score=0,t=300,f=1;//

得分与时间间隔

/ms

(控制贪吃蛇的速度)

double ss=0,tt=0;//

统计时间所用参数

class Node

{

Node(): x(0), y(0), prior(0), next(0) { }

int x;

int y;

Node *prior;

Node *next;

friend class Snake;

};

class Snake

{

public:

Snake();

~Snake();

void output();

void move();

void change_point(char);

private:

Node *head;

Node *tail;

enum p{ UP

, RIGHT, DOWN, LEFT }point; //

方向

int food_x, food_y; //

食物的坐标

static const int N = 23;

int game[N][N];

void add_head(int, int); //

添加坐标为

a,b

的结点

void delete_tail(); //

删除最后一个结点

void greate_food(); //

产生食物

void gotoxy(int, int);

};

void menu();

//

游戏操作菜单

int main()

{

system("color a");

//

初始

cmd

窗口颜色为黑(背景)淡绿(文字)

cout"\n\n\n\n\n\n

";

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

{char star[]={"Welcome To Snake Game!"};

coutstar[i];

Sleep(170);}

cout"\n\n

祝你好运!

"endl;

Sleep(3000);

if(kbhit()){char kk=getch();if(kk==9)f=5;} //

如果执行,吃一颗星加

5

system("cls");

Snake s;

menu();

system("color 1a");

s.output();

while (true)

{

char keydown = getch();

if(keydown==32)getch();

if(keydown==27)return 0;

s.change_point(keydown);

while (!kbhit())

{clock_t start,end;start=clock();

s.move();

s.output();

Sleep(t);

end=clock();tt=(double)(end-start)/CLOCKS_PER_SEC;ss+=tt;

cout"

时间:

"(int)ss;

有没有可以帮我写c语言贪吃蛇的代码 cmd运行的

//code?by?wlfryq??71693456@qq.com

#include?stdio.h

#include?stdlib.h

#include?conio.h

#include?windows.h

#include?time.h

#include?direct.h

#include?stdbool.h

#define?W?80????//屏幕宽度?

#define?H?37????//屏幕高度?

#define?SNAKE_ALL_LENGTH?200???//蛇身最长为?

void?CALLBACK?TimerProc(

HWND?hwnd,???????

????????UINT?message,?????

????????UINT?idTimer,?????

????????DWORD?dwTime);

void?start();

struct?MYPOINT

{

int?x;

int?y;

}?s[SNAKE_ALL_LENGTH]?,?head,?end,?food;

int?max_count=0;?//历史最高分,如果countmax_count,?则max_count=count

int?old_max_count=0;??//历史最高分,不会变动,?用于死亡时判断max_count是否大于old_max_count,如果大于,则写入文件?

int?count=0;??//得分?

int?len=20;???//当前蛇身长度?

int?direct=0;?//方向:?0-向右,?1-向下,?2-向左,?3-向上

int?speed=200;??//速度:毫秒?

bool?isfood=false;?//食物是否存在

int?timerID;

bool?stop=false;???//暂停?

char*?ini_path;????//数据文件绝对路径?

void?setxy(int?x,?int?y)??//设置CMD窗口光标位置

{

???COORD?coord?=?{x,?y};

???SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),?coord);

}

void?hide_cursor()?//隐藏CMD窗口光标

{

CONSOLE_CURSOR_INFO?cci;

cci.bVisible?=?FALSE;

cci.dwSize?=?sizeof(cci);

HANDLE?handle?=?GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleCursorInfo(handle,?cci);

}

void?set_food()??????//设置食物坐标?

{

if(isfood==true)

{

return;

}

int?x,y,i;

bool?flag=false;

while(1)

{

flag=false;

x=rand()%(W-14)+6;

y=rand()%(H-12)+6;

for(i=0;ilen;i++)??????//判断食物是否落在蛇身上?

{

if(s[i].x==x??s[i].y==y)

{

flag=true;

break;

}

}

if(flag==true)

{

continue;

}

else

{

food.x=x;

food.y=y;

break;

}

}

setxy(food.x,food.y);

printf("*");

isfood=true;

}

void?check_board()????//检测蛇身是否越界和相交?

{

int?i;

if(s[0].x=W-3?||?s[0].x=2?||?s[0].y=H-1?||?s[0].y=2)

{

setxy(W/2-5,0);

printf("game?over\n");

stop=true;

if(old_max_countmax_count)

{

char?t[5]={'\0'};

sprintf(t,"%d",max_count);

WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);

}

}

for(i=1;ilen;i++)

{

if(s[i].x==s[0].x??s[i].y==s[0].y)

{

setxy(W/2-5,0);

printf("game?over\n");

stop=true;

if(old_max_countmax_count)

{

char?t[5]={'\0'};

sprintf(t,"%d",max_count);

WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);

}

break;

}

}

if(stop==true)

{

KillTimer(NULL,timerID);

int?c;

while(1)

{

fflush(stdin);

c=_getch();

if(c=='n'?||?c=='N')

{

start();

}

else?if(c=='q'?||?c=='Q')

{

exit(0);

}

else?continue;

}

}

}

void?printf_body(bool?is_first)??//打印蛇身?

{

if(is_first==true)?????//如果是第一次打印蛇身?

{

int?i;

for(i=0;ilen;i++)

{

setxy(s[i].x,s[i].y);

printf("O");

}

}

else??????????????????//如果不是第一次打印蛇身?

{

setxy(end.x,end.y);

printf("?");

setxy(s[0].x,s[0].y);

printf("O");

}

if(food.x==s[0].x??food.y==s[0].y)??//如果吃到食物?

{

count++;

isfood=false;?????????????????????//重置食物?

set_food();

len++;

KillTimer(NULL,timerID);

if(speed100)?speed-=10;

else?if(speed50)?speed-=5;

else?if(speed30)?speed-=2;

else?if(speed16)?speed-=1;

else?;

setxy(0,0);

if(max_countcount) max_count=count;

printf("??speed?:?%d?ms?????score?:?%d???????????????????????????????????best?score:%d??",speed,count,max_count);

timerID=SetTimer(NULL,001,speed,TimerProc);

}

}

void?change_body_pos(int?x,?int?y)???//改变蛇身的坐标数据?

{

end.x=s[len-1].x;

end.y=s[len-1].y;

int?i;

for(i=len-1;i0;i--)

{

s[i].x=s[i-1].x;

s[i].y=s[i-1].y;

}

s[0].x=x;

s[0].y=y;

}

void?CALLBACK?TimerProc(

HWND?hwnd,???????

????????UINT?message,?????

????????UINT?idTimer,?????

????????DWORD?dwTime)

{

switch(direct)

{

case?0:

head.x++;

change_body_pos(head.x,head.y);

printf_body(false);

check_board();

break;

case?1:

head.y++;

change_body_pos(head.x,head.y);

printf_body(false);

check_board();

break;

case?2:

head.x--;

change_body_pos(head.x,head.y);

printf_body(false);

check_board();

break;

case?3:

head.y--;

change_body_pos(head.x,head.y);

printf_body(false);

check_board();

break;

}

}

void?start()

{

int?i;

KillTimer(NULL,timerID);

count=0;??//得分?

len=20;???//当前蛇身长度?

direct=0;?//方向:?0-向右,?1-向下,?2-向左,?3-向上

speed=200;??//速度:毫秒?

isfood=false;?//食物是否存在

stop=false;???//停止?

system("cls");

setxy(1,4);

printf("┌─────────────────────────────────────┐\n");

for(i=0;i33;i++)

{

printf("?│??????????????????????????????????????????????????????????????????????????│\n");

}

printf("?└─────────────────────────────────────┘");

head.x=len-1+5;

head.y=H/2;

for(i=0;ilen;i++)

{

s[i].x=head.x-i;

s[i].y=head.y;

}

setxy(0,0);

printf("??speed?:?%d:ms?????score?:?%d???????????????????????????????????best?score:%d??",speed,count,max_count);

printf_body(true);

set_food();

timerID=SetTimer(NULL,001,speed,TimerProc);

int?c;

MSG?msg;

while(GetMessage(msg,NULL,0,0))

{

if(stop==true) break;

if(_kbhit())???//如果按下的是方向键或功能键,?_getch()要调用两次,第一次返回0XE0或0?

{

fflush(stdin);

c=_getch();???//上:?72?下:80??左:75??右:77?

if(c==0XE0?||?c==0)

{

c=_getch();

if(c==72??direct!=1??direct!=3)

{

direct=3;

}

else?if(c==80??direct!=1??direct!=3)

{

direct=1;

}

else?if(c==75??direct!=0??direct!=2)

{

direct=2;

}

else?if(c==77??direct!=0??direct!=2)

{

direct=0;

}

}

else?if(c=='?')

{

setxy(W/2-10,0);

system("pause");

setxy(W/2-10,0);

printf("????????????????????");

}

}

if(msg.message==WM_TIMER)

{

DispatchMessage(msg);

}

}

}

int?main()

{

ini_path=(char*)malloc(sizeof(char)*50);

srand((unsigned)time(0));

getcwd(ini_path,50);//取得当前程序绝对路径

ini_path=strcat(ini_path,"snake.dat");

max_count=GetPrivateProfileInt("MAX_COUNT","max_count",0,ini_path);

old_max_count=max_count;

char?cmd[50];

sprintf(cmd,"mode?con?cols=%d?lines=%d\0",W,H);

system(cmd);//改变CMD窗口大小

hide_cursor();

start();

return?0;

}

10个很酷的cmd命令是什么?

10个很酷的cmd命令:

1、ipconfig。

功能:查询本机IP地址。

操作方法:只要在在打开的cmd命令界面中输入“ipconfig”就可以了。

2、msg。

功能:向对方电脑发送一条文本提示。

操作方法:首先你要知道对方的IP地址,接下来输入命令“msg /server:对方电脑IP *?”。在“*”后输入你要发送的内容即可。

3、Net user。

功能:查看本机账户情况。

操作方法:和ipconfig一样,net user也有很多衍生的命令后缀,比方说“net user xxx 123456 /add”,输入后就会在系统中创建一个名为“xxx”的新用户,而新用户密码则是“123456”。

4、Net share。

作用:查看共享资源。

操作方法:在cmd界面中输入“net share”查看所有已共享资源,然后输入“net share 要删除的共享文件夹 /delete”就可以啦!

5、Nslookup。

作用:检查网站IP地址。

操作方法:在提示符状态输入“nslookup 对方网站域名”,哒哒哒哒……结果出来了!

6、Netsh wlan show。

作用:探秘Wi-Fi配置文件。

操作方法:在提示符状态输入命令“netsh wlan show profile SSID key=clear”,输入完成后Windows会自动返回当前已连接WIFI的详细信息,包括SSID和连接密码。当前这里有一个前提,那就是你现在已经成功连接了。

7、telnet。

作用:看电影《星球大战》。

操作方法:在提示符状态输入命令“telnet towel.blinkenlights.nl”,输入完成后稍等一会即可,电影会自动开演!

8、|。

作用:将命令结果输出到剪贴板。

操作方法:在需要导出结果的命令后方添加“|”,再加入导出位置就可以了。比方说“| clip”是导出到剪贴板,“| xxx.txt”是导出到xxx.txt。总之,你需要什么地方用,就放到哪儿,“|”支持绝大多数CMD指令。

9、。

作用:将多个命令“连接”起来,一步运行多组命令。

操作方法:是CMD里一项“命令连接”语句,直接放在要连接的命令行中间即可。效果就是下图所示,一次输入后CMD会顺序执行所有命令。

10、dfrg.msc 。

作用:硬盘碎片整理工具。

磁盘碎片整理程序,windows组件之一。

操作方法:主要用于系统对硬盘的文件管理。该程序可将磁盘的碎片压缩,转移,优化磁盘的文件系统。定期进行磁盘碎片整理,可以改善硬盘的读写速率。还有专门的磁盘整理软件,如windows优化大师集成的磁盘整理程序,oo Defrag等。

编写一个在cmd环境下运行的Java小游戏

24点。。。。

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class calculate24 extends JFrame{

private javax.swing.JPanel jContentPane = null;

private JLabel jLabel = null;

private JLabel jLabel1 = null;

private JTextField jTextField = null;

private JTextField jTextField1 = null;

private JTextArea jTextArea = null;

private JLabel jLabel2 = null;

private JButton jButton = null;

private JScrollPane jScrollPane = null;

private JButton jButton1 = null;

private JButton jButton2 = null;

private JButton jButton3 = null;

private JButton jButton4 = null;

private JButton jButton5 = null;

private JButton jButton6 = null;

private JButton jButton7 = null;

private JButton jButton8 = null;

private JButton jButton9 = null;

private JButton jButton10 = null;

/**

* This is the default constructor

*/

public calculate24() {

super();

initialize();

}

/**

* This method initializes this

*

* @return void

*/

private void initialize() {

this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

this.setBounds(200, 200, 565, 452);

this.setContentPane(getJContentPane());

this.setTitle("24点");

}

/**

* This method initializes jContentPane

*

* @return javax.swing.JPanel

*/

private javax.swing.JPanel getJContentPane() {

if (jContentPane == null) {

jLabel2 = new JLabel();

jLabel1 = new JLabel();

jLabel = new JLabel();

jContentPane = new javax.swing.JPanel();

jContentPane.setLayout(null);

jLabel.setBounds(66, 52, 150, 45);

jLabel.setText("please unter four number");

jLabel1.setBounds(253, 52, 282, 45);

jLabel1.setText("please unter how many result do you want to get");

jLabel2.setBounds(354, 201, 70, 36);

jLabel2.setText("result");

jContentPane.add(getJButton(), null);

jContentPane.add(jLabel, null);

jContentPane.add(jLabel1, null);

jContentPane.add(getJTextField(), null);

jContentPane.add(getJTextField1(), null);

jContentPane.add(jLabel2, null);

jContentPane.add(getJScrollPane(), null);

jContentPane.add(getJButton1(), null);

jContentPane.add(getJButton2(), null);

jContentPane.add(getJButton3(), null);

jContentPane.add(getJButton4(), null);

jContentPane.add(getJButton5(), null);

jContentPane.add(getJButton6(), null);

jContentPane.add(getJButton7(), null);

jContentPane.add(getJButton8(), null);

jContentPane.add(getJButton9(), null);

jContentPane.add(getJButton10(), null);

}

return jContentPane;

}

/**

* This method initializes jTextField

*

* @return javax.swing.JTextField

*/

private JTextField getJTextField() {

if (jTextField == null) {

jTextField = new JTextField();

jTextField.setBounds(67, 84, 149, 41);

jTextField.addFocusListener(new java.awt.event.FocusAdapter() {

public void focusGained(java.awt.event.FocusEvent e) {

jTextField.select(0,jTextField.getText().length());

}

});

}

return jTextField;

}

/**

* This method initializes jTextField1

*

* @return javax.swing.JTextField

*/

private JTextField getJTextField1() {

if (jTextField1 == null) {

jTextField1 = new JTextField();

jTextField1.setBounds(293, 81, 161, 41);

jTextField1.setNextFocusableComponent(jButton);

}

return jTextField1;

}

/**

* This method initializes jTextArea

*

* @return javax.swing.JTextArea

*/

private JTextArea getJTextArea() {

if (jTextArea == null) {

jTextArea = new JTextArea();

jTextArea.setTabSize(8);

}

return jTextArea;

}

public static String bbb(List list1, List list2) {

float result = 0;

for (int i = list1.size(); i 0; i-- ) {

if (list1.contains("*")) {

int j = list1.indexOf("*");

result = Float.parseFloat((String)list2.get(j))

* Float.parseFloat((String)list2.get(j + 1));

list1.remove(j);

list2.remove(j);

list2.remove(j);

list2.add(j, String.valueOf(result));

} else if (list1.contains("/")) {

int j = list1.indexOf("/");

result = Float.parseFloat((String)list2.get(j))

/ Float.parseFloat((String)list2.get(j + 1));

list1.remove(j);

list2.remove(j);

list2.remove(j);

list2.add(j, String.valueOf(result));

} else if (list1.contains("+")) {

int j = list1.indexOf("+");

result = Float.parseFloat((String)list2.get(j))

+ Float.parseFloat((String)list2.get(j + 1));

list1.remove(j);

list2.remove(j);

list2.remove(j);

list2.add(j, String.valueOf(result));

} else if (list1.contains("-")) {

int j = list1.indexOf("-");

result = Float.parseFloat((String)list2.get(j))

- Float.parseFloat((String)list2.get(j + 1));

list1.remove(j);

list2.remove(j);

list2.remove(j);

list2.add(j, String.valueOf(result));

}

}

return (String)list2.get(0);

}

private static void bbb(String str, String sPrint, List list) {

if (!"".equals(str.trim()) ? false : list.add(sPrint))

;

for (int i = 0; i str.length() ( !"".equals(str.trim()) ); i++ )

if (str.charAt(i) != ' ')

bbb(str.replace(str.charAt(i), ' '), sPrint + str.charAt(i),

list);

}

private static List bbb(String str, List list) {

List result = new ArrayList();

String a1 = str.substring(0, 1);

String b1 = str.substring(1, 2);

String c1 = str.substring(2, 3);

String d1 = str.substring(3, 4);

String[] a11 = new String[] { a1, b1, c1, d1 };

for (int i = 0; i list.size(); i++ ) {

String temp = (String)list.get(i);

int a = Integer.parseInt(temp.substring(0, 1));

int b = Integer.parseInt(temp.substring(1, 2));

int c = Integer.parseInt(temp.substring(2, 3));

int d = Integer.parseInt(temp.substring(3, 4));

String tempStr = a11[a] + a11[b] + a11[c] + a11[d];

if(!result.contains(tempStr)){

result.add(tempStr);

}

}

return result;

}

public List test(String param, int x) {

int y = 0;

List result = new ArrayList();

List a11 = new ArrayList();

calculate24.bbb("0123", "", a11);

List a1 = calculate24.bbb(param, a11);

for (int m = 0; m a1.size(); m++ ) {

String param1 = (String)a1.get(m);

int[] a = new int[] { Integer.parseInt(param1.substring(0, 1)),

Integer.parseInt(param1.substring(1, 2)),

Integer.parseInt(param1.substring(2, 3)),

Integer.parseInt(param1.substring(3, 4)) };

String[] e = new String[] { "*", "/", "+", "-" };

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

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

for (int k = 0; k 4; k++ ) {

List aa = new ArrayList();

aa.add(String.valueOf(a[0]));

aa.add(String.valueOf(a[1]));

aa.add(String.valueOf(a[2]));

aa.add(String.valueOf(a[3]));

List bb = new ArrayList();

bb.add(e[i]);

bb.add(e[j]);

bb.add(e[k]);

String s = a[0] + e[i] + a[1] + e[j] + a[2] + e[k]

+ a[3];

String tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

List temp1 = new ArrayList();

List temp2 = new ArrayList();

temp1.add(String.valueOf(a[0]));

temp1.add(String.valueOf(a[1]));

temp2.add(e[i]);

String temp = calculate24.bbb(temp2, temp1);

aa.clear();

aa.add(temp);

aa.add(String.valueOf(a[2]));

aa.add(String.valueOf(a[3]));

bb.clear();

bb.add(e[j]);

bb.add(e[k]);

s = "(" + a[0] + e[i] + a[1] + ")" + e[j] + a[2] + e[k]

+ a[3];

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

temp1.clear();

temp2.clear();

temp1.add(String.valueOf(a[1]));

temp1.add(String.valueOf(a[2]));

temp2.add(e[j]);

temp = calculate24.bbb(temp2, temp1);

aa.clear();

aa.add(String.valueOf(a[0]));

aa.add(temp);

aa.add(String.valueOf(a[3]));

bb.clear();

bb.add(e[i]);

bb.add(e[k]);

s = a[0] + e[i] + "(" + a[1] + e[j] + a[2] + ")" + e[k]

+ a[3];

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

temp1.clear();

temp2.clear();

temp1.add(String.valueOf(a[2]));

temp1.add(String.valueOf(a[3]));

temp2.add(e[k]);

temp = calculate24.bbb(temp2, temp1);

aa.clear();

aa.add(String.valueOf(a[0]));

aa.add(String.valueOf(a[1]));

aa.add(temp);

bb.clear();

bb.add(e[i]);

bb.add(e[j]);

s = a[0] + e[i] + a[1] + e[j] + "(" + a[2] + e[k]

+ a[3] + ")";

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

temp1.clear();

temp2.clear();

temp1.add(String.valueOf(a[0]));

temp1.add(String.valueOf(a[1]));

temp1.add(String.valueOf(a[2]));

temp2.add(e[i]);

temp2.add(e[j]);

temp = calculate24.bbb(temp2, temp1);

aa.clear();

aa.add(temp);

aa.add(String.valueOf(a[3]));

bb.clear();

bb.add(e[k]);

s = "(" + a[0] + e[i] + a[1] + e[j] + a[2] + ")" + e[k]

+ a[3];

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

temp1.clear();

temp2.clear();

temp1.add(String.valueOf(a[1]));

temp1.add(String.valueOf(a[2]));

temp1.add(String.valueOf(a[3]));

temp2.add(e[j]);

temp2.add(e[k]);

temp = calculate24.bbb(temp2, temp1);

aa.clear();

aa.add(String.valueOf(a[0]));

aa.add(temp);

bb.clear();

bb.add(e[i]);

s = a[0] + e[i] + "(" + a[1] + e[j] + a[2] + e[k]

+ a[3] + ")";

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

temp1.clear();

temp2.clear();

temp1.add(String.valueOf(a[0]));

temp1.add(String.valueOf(a[1]));

temp2.add(e[i]);

temp = calculate24.bbb(temp2, temp1);

List temp3 = new ArrayList();

List temp4 = new ArrayList();

temp3.add(String.valueOf(a[2]));

temp3.add(String.valueOf(a[3]));

temp4.add(e[k]);

String temp11 = calculate24.bbb(temp4, temp3);

aa.clear();

aa.add(temp);

aa.add(temp11);

bb.clear();

bb.add(e[j]);

s = "(" + a[0] + e[i] + a[1] + ")" + e[j] + "(" + a[2]

+ e[k] + a[3] + ")";

tempS = s;

s = calculate24.bbb(bb, aa);

if (Float.parseFloat(s) == 24) {

y++ ;

result.add(tempS + "=24");

if (y == x) {

return result;

}

}

}

}

}

}

return result;

}

public static boolean check(String param1) {

Pattern pattern = Pattern.compile("[0-9]{4}");

Matcher matcher = pattern.matcher((CharSequence)param1);

boolean result = matcher.matches();

if (result == false) {

JOptionPane.showMessageDialog(null, "please enter correct number");

return false;

} else {

return true;

}

}

public static boolean check1(String param2) {

if(param2 == null){

JOptionPane.showMessageDialog(null, "please enter correct number");

return false;

}

Pattern pattern = Pattern.compile("[0-9]{0,99}");

Matcher matcher = pattern.matcher((CharSequence)param2);

boolean result = matcher.matches();

if (result == false) {

JOptionPane.showMessageDialog(null, "please enter correct number");

return false;

} else {

return true;

}

}

/**

* This method initializes jButton

*

* @return javax.swing.JButton

*/

private JButton getJButton() {

if (jButton == null) {

jButton = new JButton();

jButton.setBounds(81, 275, 110, 54);

jButton.setText("calculate");

jButton.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(java.awt.event.KeyEvent e) {

if(e.getKeyCode()==10){

if (check(jTextField.getText())

check1(jTextField1.getText())) {

if(!jTextField1.getText().equals("0")){

List b = test(jTextField.getText(), Integer

.parseInt(jTextField1.getText()));

String temp = "";

for (int i = 0; i b.size(); i++ ) {

temp = temp + b.get(i) + "\n";

}

if (b.size() == 0) {

jTextArea.setText("NO RESULT");

} else {

jTextArea.setText(temp);

}

}else{

JOptionPane.showMessageDialog(null, "please enter correct number");

}

}

}

}

});

jButton.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

if (check(jTextField.getText())

check1(jTextField1.getText())) {

if(!jTextField1.getText().equals("0")){

List b = test(jTextField.getText(), Integer

.parseInt(jTextField1.getText()));

String temp = "";

for (int i = 0; i b.size(); i++ ) {

temp = temp + b.get(i) + "\n";

}

if (b.size() == 0) {

jTextArea.setText("NO RESULT");

} else {

jTextArea.setText(temp);

}

}else{

JOptionPane.showMessageDialog(null, "please enter correct number");

}

}

}

});

}

return jButton;

}

/**

* This method initializes jScrollPane

*

* @return javax.swing.JScrollPane

*/

private JScrollPane getJScrollPane() {

if (jScrollPane == null) {

jScrollPane = new JScrollPane();

jScrollPane.setBounds(267, 238, 216, 124);

jScrollPane.setViewportView(getJTextArea());

}

return jScrollPane;

}

/**

* This method initializes jButton1

*

* @return javax.swing.JButton

*/

private JButton getJButton1() {

if (jButton1 == null) {

jButton1 = new JButton();

jButton1.setBounds(40, 148, 42, 28);

jButton1.setText("1");

jButton1.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"1");

}

});

}

return jButton1;

}

/**

* This method initializes jButton2

*

* @return javax.swing.JButton

*/

private JButton getJButton2() {

if (jButton2 == null) {

jButton2 = new JButton();

jButton2.setBounds(90, 148, 42, 28);

jButton2.setText("2");

jButton2.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"2");

}

});

}

return jButton2;

}

/**

* This method initializes jButton3

*

* @return javax.swing.JButton

*/

private JButton getJButton3() {

if (jButton3 == null) {

jButton3 = new JButton();

jButton3.setBounds(140, 148, 42, 28);

jButton3.setText("3");

jButton3.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"3");

}

});

}

return jButton3;

}

/**

* This method initializes jButton4

*

* @return javax.swing.JButton

*/

private JButton getJButton4() {

if (jButton4 == null) {

jButton4 = new JButton();

jButton4.setBounds(190, 148, 42, 28);

jButton4.setText("4");

jButton4.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"4");

}

});

}

return jButton4;

}

/**

* This method initializes jButton5

*

* @return javax.swing.JButton

*/

private JButton getJButton5() {

if (jButton5 == null) {

jButton5 = new JButton();

jButton5.setBounds(240, 148, 42, 28);

jButton5.setText("5");

jButton5.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"5");

}

});

}

return jButton5;

}

/**

* This method initializes jButton6

*

* @return javax.swing.JButton

*/

private JButton getJButton6() {

if (jButton6 == null) {

jButton6 = new JButton();

jButton6.setBounds(40, 188, 42, 28);

jButton6.setText("6");

jButton6.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"6");

}

});

}

return jButton6;

}

/**

* This method initializes jButton7

*

* @return javax.swing.JButton

*/

private JButton getJButton7() {

if (jButton7 == null) {

jButton7 = new JButton();

jButton7.setBounds(90, 188, 42, 28);

jButton7.setText("7");

jButton7.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"7");

}

});

}

return jButton7;

}

/**

* This method initializes jButton8

*

* @return javax.swing.JButton

*/

private JButton getJButton8() {

if (jButton8 == null) {

jButton8 = new JButton();

jButton8.setBounds(140, 188, 42, 28);

jButton8.setText("8");

jButton8.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"8");

}

});

}

return jButton8;

}

/**

* This method initializes jButton9

*

* @return javax.swing.JButton

*/

private JButton getJButton9() {

if (jButton9 == null) {

jButton9 = new JButton();

jButton9.setBounds(190, 188, 42, 28);

jButton9.setText("9");

jButton9.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"9");

}

});

}

return jButton9;

}

/**

* This method initializes jButton10

*

* @return javax.swing.JButton

*/

private JButton getJButton10() {

if (jButton10 == null) {

jButton10 = new JButton();

jButton10.setBounds(240, 188, 42, 28);

jButton10.setText("0");

jButton10.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

jTextField.setText(jTextField.getText()+"0");

}

});

}

return jButton10;

}

/**

* Launches this application

*/

public static void main(String[] args) {

calculate24 application = new calculate24();

application.show();

}

} // @jve:decl-index=0:visual-constraint="10,10"

贪吃蛇cmd代码

在命令提示符下cd到C++代码link生成的exe文件所在的debug目录,然后直接输入exe文件名比如说,你的cpp文件路径为D:\...\example.cpp,那么你在命令提示符下输入d:回车cd d:\...\debug回车example回车这样就可以了

(责任编辑:IT教学网)

更多

推荐linux服务器文章