|
ЛАБОРАТОРНЫЕ РАБОТЫ
|
|
Игра "Пятнашки". Реализация в виде Java-апплета |
|
|
|
|
Задание:Заданием лабораторной работы являлось разроботка приложения на языке Java-applet. В моем случае приложение - это игра "Пятнашки". Весь отчет разбит на слеующие части:(Поиграть в игру вы можете в начале странице!!!)->Руководство пользователя ->Руководство программиста ->Листинг программы Руководство пользователя
При просмотре страницы автоматически стартует приложение реализованно в виде апплета. Пользователю предлагается следующий интерфейс:
import java.awt.*; import java.awt.event.*; public class fifts extends Applet { String[] data = new String[16]; Button fbutton; Button ubutton; Button dbutton; Button rbutton; Button lbutton; Button ng; TextField fTextField; int moves=0; String mymoves="0"; String abc="0"; int currentx=0; int currenty=0; int indexmas=0; int delta=5; int delta2=5; int row=1; int col=1; int posx=0; int cx,cy; String win="Winner"; boolean flag_win=false; public void start() { init_matr(); } public void init() { flag_win=false; setLayout(new BorderLayout()); Panel theControls=new Panel(); Panel theInd=new Panel(); fTextField=new TextField(mymoves); theInd.add(fTextField); add(theInd,BorderLayout.EAST); fbutton=new Button("Обмен"); ubutton=new Button(" ^ "); dbutton=new Button(" v "); rbutton=new Button(" > "); lbutton=new Button(" < "); ng=new Button("Новая игра"); theControls.add(fbutton); theControls.add(ubutton); theControls.add(dbutton); theControls.add(lbutton); theControls.add(rbutton); theControls.add(ng); add(theControls,BorderLayout.SOUTH); fbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (fbutton.getLabel()=="Обмен") { exchange(); repaint(); } } }); //------------------- ng.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ng.getLabel()=="Новая игра") { init_matr(); moves=0; mymoves="0" ; fTextField.setText("0"); repaint(); flag_win=false; } } }); //------------------ ubutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ubutton.getLabel()==" ^ ") { currenty--; if (col>1) { col--; delta2=delta2-(cx); } if (col==1) { delta2=delta2; } if (col<1) { col=1;} if (currenty<0) currenty=0; repaint(); } } }); //------------------ dbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { currenty++; if (dbutton.getLabel()==" v ") { if (col<4) { col++; delta2=delta2+(cx); } if (col==4) { delta2=delta2; } if (col>4) { col=4; } if (currenty>3) currenty=3; repaint(); } } }); //------------------ lbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (lbutton.getLabel()==" < ") { currentx--; if (row>1) { row--; delta=delta-(cy); } if (row==1) { delta=delta; } if (row<1) { row=1;} if (currentx<0) currentx=0; repaint(); } } }); //------------------ rbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (rbutton.getLabel()==" > ") { currentx++; if (row<4) { row++; delta=delta+(cy); } if (row==4) { delta=delta; } if (row>4) { row=4; } if (currentx>3) currentx=3; repaint(); } } }); //------------------ } public int loc_to_glob() { indexmas=0; indexmas=(col*4+row+1)-6; return indexmas; } public void exchange() { loc_to_glob(); for(int i=0;i<15;i++) if (data[i]==" ") posx=i; String buf=" "; if ((posx-indexmas==1)|(indexmas-posx==1)|(posx-indexmas==4)|(indexmas-posx==4)) { moves++; mymoves="0".valueOf(moves); fTextField.setText(mymoves); data[posx]=buf; data[posx]=data[indexmas]; data[indexmas]=buf; } if ( (data[0]==data[0])&(data[1]=="1")&(data[2]=="2")&(data[3]=="3")&(data[4]=="4")&(data[5]=="5")&(data[6]=="6")& (data[7]=="7")&(data[8]=="8")&(data[9]=="9")&(data[10]=="10")&(data[11]=="11")&(data[12]=="12")&(data[13]=="13")& (data[14]=="14")&(data[15]=="15")) { flag_win=true; repaint(); } } public void init_matr() { data[0]=""; data[1]="4 "; data[2]="5"; data[3]="7"; data[4]="11"; data[5]="3"; data[6]="1"; data[7]="15"; data[8]="14"; data[9]="8"; data[10]="2"; data[11]="6"; data[12]="9"; data[13]="10"; data[14]="12"; data[15]="13"; } public void paint(Graphics g) { Rectangle r=getBounds(); int gridw=r.width-60; int gridh=r.height-60; int disp=(gridh)/4; int disp2=(gridw)/4; int dx=(disp/2)-5; int dy=(disp2/2)+5; cx=disp; cy=disp2; loc_to_glob(); java.awt.Font theBigFont=new java.awt.Font("Times New Roman",java.awt.Font.BOLD,18); g.setFont(theBigFont); if (flag_win==true) { java.awt.Font theBigFont2=new java.awt.Font("comic sans ms",java.awt.Font.BOLD,24); g.setFont(theBigFont2); String x; x="Win in "+mymoves; g.setColor(Color.red); g.fillRect(1,1,gridw,gridh); g.setColor(Color.black); g.drawString(x,1,gridh/2); g.drawString("move(s)",gridw/2,gridh/2+20); } g.drawRoundRect(1,1,gridw,gridh,45,45); g.setColor(Color.cyan); g.fillRoundRect(1,1,gridw,gridh,45,45); g.setColor(Color.magenta); g.fillRoundRect((delta),(delta2),disp2-10,disp-10,60,60); g.setColor(Color.black); g.drawLine(1,disp,gridw,disp); g.drawLine(1,disp*2,gridw,disp*2); g.drawLine(1,disp*3,gridw,disp*3); g.drawLine(disp2,1,disp2,gridw); g.drawLine(disp2*2,1,disp2*2,gridw); g.drawLine(disp2*3,1,disp2*3,gridw); int index=-1; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { index++; if ((data[index]!=" ")){ g.setColor(Color.yellow); g.fillRoundRect(dx-(disp/4)+10, dy-disp/3, disp/2,disp/2,15,15) ; g.setColor(Color.black); g.drawRoundRect(dx-(disp/4)+10, dy-disp/3, disp/2,disp/2, 15,15) ; g.drawString(data[index], dx, dy); } dx=dx+disp; } dx=disp/2; dy=dy+disp; } } } |
|
|
Пишите письма на Mixali4@ua.fm
|
Обновлено 07.01.2005
|