頁:
[1]
Java的賓果遊戲~~
請問有誰看得懂這個賓果遊戲的判斷式~~他是利用什麼方法來判斷電腦如何下的呢~~
可不可以請JAVA高手來為我解答~~
import java.util.*;
import java.awt.*;// 匯入視窗處理套件及類別
import java.awt.event.*;// 匯入視窗套件及類別之事件處理相關API
import java.applet.*;
import javax.swing.*;
class Bingo extends JFrame implements Runnable
{
final static int PLAYER =1;
final static int COMPUTER =0;
ClassLoader cl = this.getClass().getClassLoader();
Toolkit tk = Toolkit.getDefaultToolkit();
int game_state, num /* 佈子數, 當玩家把25個數字都佈完後 開始遊戲 */, turn, winner=-1;
int connects[] = new int; /* 雙方連線數 */
int weight[] = new int; /* 計算權重, w=x w=y w=weight */
int locX, locY /* 下棋位置 */, count /* 連棋數 */, x, y /* 暫存位置 */, temp_value, displace_x=0, displace_y=0 /* 位移量 */;
ArrayList steps = new ArrayList(); /* 記錄棋步 */
int[][] dir = { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} };
boolean[] dir2 = new boolean;
String message;
Font font=new Font("new_font", Font.BOLD, 20);
Grid grids[][][] = new Grid;
Image temp;
JPanel[] boardPanel = new JPanel;
JPanel containerPanel = new JPanel();
final static int Start =0;
final static int Select =1;
final static int Playing =2;
final static int End =3;
final static int nil=-1; /* 無方向 */
final static int oblique_1 =0; /* 右上向左下 */
final static int oblique_2 =1; /* 左上向右下 */
final static int horizontal =2; /* 橫向 */
final static int vertical=3; /* 直向 */
Bingo()
{
super("賓果");
for(int i=0; i<2; i++)
{
boardPanel = new JPanel();
boardPanel.setLayout(new GridLayout(5, 5, 0, 0));
boardPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
for(int j=0; j<5; j++)
for(int k=0; k<5; k++)
{
grids = new Grid(i, j, k);
boardPanel.add(grids);
}
containerPanel.add(boardPanel, BorderLayout.CENTER);
}
getContentPane().add(containerPanel, BorderLayout.CENTER);
game_state=Start;
ThreadStart();
setResizable(false);
setSize(200, 280);//宣告外框大小
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String[] arg)
{
Bingo application = new Bingo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void ReStart()
{
for(int i=0; i<2; i++)
{
connects=0;
for(int j=0; j<5; j++)
for(int k=0; k<5; k++)
grids.Initial();
}
num=weight=0;
winner=-1;
steps.clear();
game_state=Select;
}
/* 電腦隨新重新洗值 */
public void washValue()
{
for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
{
temp_value=grids.getValue();
x=(int)(Math.random()*5);
y=(int)(Math.random()*5);
grids.setValue(grids);
grids.setValue(temp_value);
}
}
/* 當一方下完後 另一方也下同一個值 */
public void redo(int value)
{
for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
{
if(grids.getValue()==value)
{
grids.setSelected(true);
break;
}
}
}
public void WinCheck()
{
if(game_state==Playing)
{
WinCheck(oblique_1);
repaint();
}
}
public void WinCheck(int direction)
{
count=1;
switch(direction)
{
case oblique_1: displace_x=1;
displace_y=-1;
break;
case oblique_2: displace_x=displace_y=1;
break;
case horizontal: displace_x=1;
displace_y=0;
break;
case vertical: displace_x=0;
displace_y=1;
break;
}
x=locX+displace_x;
y=locY+displace_y;
while(x>=0 && x<5 && y>=0 && y<5 && grids.getSelected())
{
count=count+1;
x=x+displace_x;
y=y+displace_y;
}
x=locX-displace_x;
y=locY-displace_y;
while(x>=0 && x<5 && y>=0 && y<5 && grids.getSelected())
{
count=count+1;
x=x-displace_x;
y=y-displace_y;
}
if(count>=5)
{
x=locX;
y=locY;
while(x>=0 && x<5 && y>=0 && y<5 && grids.getSelected())
{
grids.setConnected(direction);
x=x+displace_x;
y=y+displace_y;
}
x=locX-displace_x;
y=locY-displace_y;
while(x>=0 && x<5 && y>=0 && y<5 && grids.getSelected())
{
grids.setConnected(direction);
x=x-displace_x;
y=y-displace_y;
}
connects=connects+1;
if(connects>=5)
{
game_state=End;
winner=turn;
ThreadStart();
}
}
if(direction!=vertical)
WinCheck(direction+1);
}
public void BingoRandom()
{
int i, j;
if(!grids.getSelected()) /* 正中央的位置 */
i=j=2;
else
{
do
{
i=(int)(Math.random()*5);
j=(int)(Math.random()*5);
}while(grids.getSelected());
}
grids.setSelected(true);
turn=PLAYER;
redo(grids.getValue());
}
public void BingoAI()
{
for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
{
if(!grids.getSelected())
BingoAI2(i, j);
}
if(weight>1)
{
//System.out.println(weight);
weight=0;
grids]].setSelected(true);
turn=PLAYER;
redo(grids]].getValue());
}
else
BingoRandom();
}
public void BingoAI2(int i, int j)
{
locX=i;
locY=j;
int w=0, direction=oblique_1;
while(direction!=nil)
{
switch(direction)
{
case oblique_1: displace_x=1;
displace_y=-1;
direction=oblique_2;
break;
case oblique_2: displace_x=displace_y=1;
direction=horizontal;
break;
case horizontal: displace_x=1;
displace_y=0;
direction=vertical;
break;
case vertical: displace_x=0;
displace_y=1;
direction=nil;
break;
}
w=w+calculateWeight(displace_x, displace_y);
}
if(w>weight)
{
weight=locX;
weight=locY;
weight=w;
}
}
public int calculateWeight(int displace_x, int displace_y)
{
int w=0;
count=0;
x=locX;
y=locY;
while(x>=0 && x<5 && y>=0 && y<5)
{
if(grids.getSelected())
w=w+1;
count=count+1;
x=x+displace_x;
y=y+displace_y;
}
x=locX-displace_x;
y=locY-displace_y;
while(x>=0 && x<5 && y>=0 && y<5)
{
if(grids.getSelected())
w=w+1;
count=count+1;
x=x-displace_x;
y=y-displace_y;
}
if(w==4) /* 調重已有四個被選擇的行列比重 */
w=w+1;
return (count==5)? w*w: 0;
}
public void ThreadStart()
{
new Thread(this).start();
}
public void run()
{
try
{
switch(game_state)
{
case Start: Thread.sleep(2000);
game_state=Select;
repaint();
break;
case End: repaint();
Thread.sleep(1500);
ReStart();
repaint();
break;
}
}
catch(InterruptedException ex)
{
}
}
public void paint(Graphics g)
{
super.paint(g);
switch(game_state)
{
case Start: g.drawString("資一B 第五組", 50,250);
g.setFont(font);
g.setColor(Color.BLUE);
g.drawString("Bingo", getWidth()/3, 120);
break;
case Select:
case Playing:
case End:
g.drawString("電腦", 2, 75);
g.drawString("C: "+connects, 2, 89);
g.drawString("玩家", 2, 180);
g.drawString("C: "+connects, 2, 194);
if(winner!=-1)
{
g.setFont(font);
g.setColor(Color.BLUE);
message = winner==PLAYER? "你贏了":"你輸了";
g.drawString(message, 40, 130);
}
break;
}
}
public void undo()
{
if(steps.size()>0)
{
for(int i=0; i<2; i++)
{
((Grid)steps.get(steps.size()-1)).Initial();
steps.remove(steps.size()-1);
}
}
}
public void undoNum()
{
if(steps.size()>0)
{
num=num-1;
((Grid)steps.get(steps.size()-1)).Initial();
steps.remove(steps.size()-1);
}
}
private class Grid extends JPanel implements MouseListener
{
int x, y, value, owner;
boolean choice, selected, connected;
boolean[] direction = new boolean;
public Grid(int own, int x, int y)
{
owner=own;
this.x=x;
this.y=y;
Initial();
addMouseListener(this);
}
public void mousePressed(MouseEvent event)
{
int button=event.getButton();
if(owner==PLAYER)
{
switch(game_state)
{
case Select: if(button==MouseEvent.BUTTON1)
{
if(value==0 && num<25)
{
value=num=num+1;
steps.add(this);
if(num==25)
{
steps.clear(); /* 洗掉已下數字 */
washValue();
game_state=Playing;
}
repaint();
}
}
else
undoNum();
break;
case Playing: if(button==MouseEvent.BUTTON1)
{
if(!selected)
{
steps.add(this);
turn=PLAYER;
setSelected(true);
if(game_state==Playing)
{
turn=COMPUTER;
redo(value);
if(game_state==Playing)
BingoAI();
}
}
}
else
{
if(steps.size()!=0)
undo();
}
break;
default: break;
}
}
}
public void mouseEntered(MouseEvent event)
{
if(owner==PLAYER && game_state!=Start)
setChoice(true);
}
public void mouseExited(MouseEvent event)
{
if(owner==PLAYER && game_state!=Start)
setChoice(false);
}
public void mouseClicked(MouseEvent event)
{
}
public void mouseReleased(MouseEvent event)
{
}
public Dimension getPreferredSize()
{
return new Dimension(22, 22);//開新視窗大小
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public int getOwner()
{
return owner;
}
public int getLocX()
{
return x;
}
public int getLocY()
{
return y;
}
public int getValue()
{
return value;
}
public void Initial()
{
value= owner==COMPUTER? x*5+(y+1): 0;
choice=selected=connected=false;
for(int i=0; i<direction.length; i++)
direction=false;
repaint();
}
public void setValue(int new_value)
{
value=new_value;
repaint();
}
public void setValue(Grid gird)
{
value=gird.getValue();
repaint();
}
public void setChoice(boolean choice)
{
this.choice=choice;
repaint();
}
public boolean getSelected()
{
return selected;
}
public void setSelected(boolean select)
{
selected=select;
if(select)
{
locX=x;
locY=y;
WinCheck();
}
repaint();
}
public boolean getConnected()
{
return connected;
}
public void setConnected(boolean connect)
{
connected=connect;
repaint();
}
public void setConnected(int direct)
{
connected=direction=true;
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//g.drawString(x+" "+y, 1, 10);
if(game_state!=Start)
{
g.drawRect(0, 0, 19, 19);
if(choice)
setBackground(Color.WHITE);
else if(connected)
setBackground(Color.RED);
else if(selected)
setBackground(Color.YELLOW);
else if(value!=0 && owner==PLAYER)
setBackground(Color.LIGHT_GRAY);
else
setBackground(Color.GRAY);
for(int i=0; i<direction.length; i++)
{
if(direction)
{
g.setColor(Color.WHITE);
switch(i)
{
case oblique_1: g.drawLine(0, 0, 20, 20);
break;
case oblique_2: g.drawLine(20, 0, 0, 20);
break;
case horizontal: g.drawLine(10, 0, 10, 20);
break;
case vertical: g.drawLine(0, 10, 20, 10);
break;
}
}
}
g.setColor(Color.BLACK);
if((owner==PLAYER && value!=0) || (selected || connected))
g.drawString(""+value, (value>=10? 4: 7), 15);
}
}
}
}
/**
*
*/
/**
* @author user
*
*/
public class bing {
/**
*
*/
public bing() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>
頁:
[1]