?? gamemanager.java
字號:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Panel;
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.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* this class creates a GUI for playing a game and testing it.
* Not part of Project Specification
* This Applet is hosted on net at
* http://www.freewebs.com/justsans/play.html
* @author sanshack
*
*/
//the start of an applet - HelloWorld will be the executable class
//Extends applet means that you will build the code on the standard Applet class
public class GameManager extends Applet implements MouseListener,ActionListener, ItemListener, WindowListener
{
public static boolean gameStatus=true;
public static int posArray;
public static String initialPosition="xxxxxxxxxxxxxxxxxxxxxxx";
public static int moveNo =0;
public static int offset=50;
public static int level=6;
public static int todo=0;
public static String message;
public static int playerMode=0;
JPanel jp;
JButton playButton;
Button in,playerMode2,playerMode1;
JTextField jf;
boolean mouseEntered;
int xpos;
int ypos;
//The method that will be automatically called when the applet is started
public void init()
{
// It is required but does not need anything.
setLayout(new FlowLayout());
// initialze the button and give it a text.
// jp= new JPanel();
//playButton = new JButton("Play");
// text and length of the field
// now that all is set we can add these components to the applet
//jp.add(playButton);
Panel tb = new Panel();
jf = new JTextField("Enter level here");
jf.addActionListener(this);
tb.add(jf);
in=new Button("Play Again");
in.addActionListener(this);
tb.add(in);
playerMode2=new Button("Play Auto Mode");
playerMode2.addActionListener(this);
tb.add(playerMode2);
playerMode1=new Button("Play On Click");
playerMode1.addActionListener(this);
tb.add(playerMode1);
add(tb,BorderLayout.NORTH);
addMouseListener(this);
//jp.setVisible(true);
}
public void mouseClicked (MouseEvent me) {
// Save the coordinates of the click lke this.
xpos = me.getX();
ypos = me.getY();
// Check if the click was inside the rectangle area.
/*if (xpos > rect1xco && xpos < rect1xco+rect1width && ypos >rect1yco && ypos < rect1yco+rect1height)
rect1Clicked = true;
// if it was not then rect1Clicked is false;
else
rect1Clicked = false;*/
//show the results of the click
if(playerMode==0)
repaint();
}
// This is called when the mous has been pressed
public void mousePressed (MouseEvent me) {}
// When it has been released
// not that a click also calls these Mouse-Pressed and Released.
// since they are empty nothing hapens here.
public void mouseReleased (MouseEvent me) {
if(gameStatus==true) {
todo=0;repaint();
}
}
// This is executed when the mouse enters the applet. it will only
// be executed again when the mouse has left and then re-entered.
public void mouseEntered (MouseEvent me) {
// Will draw the "inside applet message"
mouseEntered = true;
//repaint();
}
// When the Mouse leaves the applet.
public void mouseExited (MouseEvent me) {
// will draw the "outside applet message"
mouseEntered = false;
//repaint();
}
//This method gets called when the applet is terminated
//That's when the user goes to another page or exits the browser.
public void stop()
{
// required but no actions neede here now.
}
//The standard method that you have to use to paint things on screen
//This overrides the empty Applet method, you can't called it "display" for example.
public void paint(Graphics g)
{
//method to draw text on screen
// String first, then x and y coordinate.
//g.drawString("Nine Men Morris",20,20);
System.out.println("i am here="+moveNo);
//jp.setVisible(true);
g.drawString("Playing at level="+level, 10, 10);
g.drawRect(0+offset, 0+offset, 500, 500);
g.drawRect(50+offset, 50+offset, 400, 400);
g.drawRect(100+offset, 100+offset, 300, 300);
g.drawLine(0+offset,0+offset, 100+offset, 100+offset);
g.drawLine(0+offset,500+offset, 100+offset, 400+offset);
g.drawLine(500+offset,0+offset, 400+offset, 100+offset);
g.drawLine(500+offset,500+offset, 400+offset, 400+offset);
g.drawLine(250+offset, 0+offset, 250+offset, 100+offset);
g.drawLine(0+offset, 250+offset, 100+offset, 250+offset);
g.drawLine(250+offset, 500+offset, 250+offset, 450+offset);
g.drawLine(500+offset, 250+offset, 400+offset, 250+offset);
String tempString = initialPosition.toLowerCase();
drawCoins(g,tempString);
switch(todo) {
case 0:
System.out.println("i am here1");
if (gameStatus==false) {
//System.exit(0);
break;
}
System.out.println("i am here2");
moveNo++;
if(moveNo%2==1){
message="Black to Move";
todo=1;
repaint();
} else {
message="White to Move";
todo=1;
repaint();
}
String finalPosition;
if(moveNo <=18) {
if(moveNo%2==1) {
finalPosition= new MMab().getMaxMove(initialPosition,level,0);
System.out.println("this guy is using non improved:move="+moveNo);
} else {
finalPosition= new MMab().getMaxMove(Utility.reverseColors(initialPosition),level,0);
finalPosition = Utility.reverseColors(finalPosition);
}
} else {
if(moveNo%2==1) {
finalPosition = new MMab().getMaxMove(initialPosition,level,1);
} else {
finalPosition= new MMab().getMaxMove(Utility.reverseColors(initialPosition),level,1);
finalPosition = Utility.reverseColors(finalPosition);
}
int x= Utility.countChar(finalPosition,'W');
int y= Utility.countChar(finalPosition,'B');
if(x<=2) {
message="Black Won";
todo=1;
gameStatus=false;
repaint();
//break;
} else if(y <= 2) {
message="White Won";
todo=1;
gameStatus=false;
repaint();
//break;
}
}
initialPosition = finalPosition;
break;
case 1:
g.drawString(message,550+offset,250+offset);
}
if(playerMode==1&& gameStatus==true ) {
try {
Thread.sleep(10);
todo=0;
repaint();
} catch(Exception e){System.out.println("interupted");}
}
}
public void actionPerformed(ActionEvent ae)
{
String cmd=ae.getActionCommand();
if (cmd.equals("Play Again"))
{
moveNo=0;
gameStatus=true;
playerMode=0;
initialPosition="xxxxxxxxxxxxxxxxxxxxxxx";
int temp=level;
try {
temp = Integer.parseInt(jf.getText());
}catch(Exception e) {}
level=temp;
repaint();
}
if (cmd.equals("Play Auto Mode"))
{
playerMode=1;
repaint();
}
if (cmd.equals("Play On Click"))
{
playerMode=0;
//repaint();
}
if (cmd.equals("Enter level here"))
{
moveNo=0;
gameStatus=true;
playerMode=0;
level = Integer.parseInt(jf.getText());
initialPosition="xxxxxxxxxxxxxxxxxxxxxxx";
repaint();
}
}
public static void drawCoins(Graphics g,String initialPosition) {
int width=30;
int height=30;
for(int i=0;i<23;i++) {
int x,y;
x=getCood(i,1)+offset;
y=getCood(i,2)+offset;
switch(initialPosition.charAt(i)) {
case 'x':break;
case 'w':
g.drawOval(x-width/2, y-width/2, width, height);
//g.drawRect(x-width/2, y-width/2, width, height);
break;
case 'b':
g.fillOval(x-width/2, y-width/2, width, height);
break;
}
}
}
public static int getCood(int pos,int mode) {
int x,y;
x=y=0;
switch(pos) {
case 0:
x=0;y=500;
break;
case 1:
x=250;y=500;
break;
case 2:
x=500;y=500;
break;
case 3:
x=50;y=450;
break;
case 4:
x=250;y=450;
break;
case 5:
x=450;y=450;
break;
case 6:
x=100;y=400;
break;
case 7:
x=400;y=400;
break;
case 8:
x=0;y=250;
break;
case 9:
x=50;y=250;
break;
case 10:
x=100;y=250;
break;
case 11:
x=400;y=250;
break;
case 12:
x=450;y=250;
break;
case 13:
x=500;y=250;
break;
case 14:
x=100;y=100;
break;
case 15:
x=250;y=100;
break;
case 16:
x=400;y=100;
break;
case 17:
x=50;y=50;
break;
case 18:
x=250;y=50;
break;
case 19:
x=450;y=50;
break;
case 20:
x=0;y=0;
break;
case 21:
x=250;y=0;
break;
case 22:
x=500;y=0;
break;
}
if(mode==1) return x;
return y;
}
public void windowDeiconified(WindowEvent event) {
}
public void windowIconified(WindowEvent event) {
}
public void windowActivated(WindowEvent event) {
}
public void windowDeactivated(WindowEvent event) {
}
public void windowOpened(WindowEvent event) {
}
public void windowClosed(WindowEvent event) {
}
public void windowClosing(WindowEvent event) {
setVisible(false);
System.exit(0);
}
public void itemStateChanged(ItemEvent ae)
{
//repaint();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -