骰子游戲《欲罷不能》JAVA實現(xiàn),2-4人游玩

游戲看著挺有意思,邏輯不難,隨便實現(xiàn)了一下,不到300行搞定了,代碼如下,歡迎改進:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainPanel extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new MainPanel();
}
public MainPanel(){
MyPanel mp=new MyPanel();
add(mp);
addMouseListener(mp);
setTitle("欲罷不能");
setSize(800,850);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements MouseListener{
private static final long serialVersionUID = 1L;
Point[] token;
int flag=0;
int[][] rate;
boolean ifTou=false;
int sel=-1;
int[] hasTaken;
int[] tou;
int maxFlag;
final int[] max={3,5,7,9,11,13,11,9,7,5,3};
Color[] cs={Color.red,Color.blue,Color.green,Color.yellow};
String[] flagString={"紅色","藍色","綠色","黃色"};
public MyPanel(){
init(2);
}
void init(int mf){
flag=0;
sel=-1;
ifTou=false;
token=new Point[3];
rate=new int[4][11];
tou=new int[]{-1,-1,-1,-1};
hasTaken=new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
maxFlag=mf;
}
void endTurn(){
for (int i = 0; i < token.length; i++) {
if(token[i]!=null){
rate[flag][token[i].x]=token[i].y;
if(max[token[i].x]==token[i].y){
hasTaken[token[i].x]=flag;
}
}
}
nextFlag();
token=new Point[3];
}
void failTurn(){
nextFlag();
token=new Point[3];
}
void nextFlag(){
flag++;
if(flag==maxFlag)
flag=0;
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < 11; i++) {
int s=Math.abs(i-5)*60+20;
if(hasTaken[i]>-1)
g.setColor(cs[hasTaken[i]]);
else
g.setColor(Color.black);
for (int j = 0; j < max[i]; j++) {
g.draw3DRect(i*60+50, j*60+s, 50, 50, true);
g.drawString(""+(i+2), i*60+70, j*60+s+30);
}
}
for (int i = 0; i < rate.length; i++) {
g.setColor(cs[i]);
for (int j = 0; j < rate[i].length; j++) {
if(rate[i][j]>0&&hasTaken[j]==-1){
int s=Math.abs(j-5)*60+20;
g.fill3DRect(j*60+50, (max[j]-rate[i][j])*60+s+(12*i), 50, 12, true);
}
}
}
g.setColor(Color.black);
for (int i = 0; i < token.length; i++) {
if(token[i]!=null){
int s=Math.abs(token[i].x-5)*60+20;
g.fill3DRect(token[i].x*60+63, (max[token[i].x]-token[i].y)*60+s+13, 25, 25, true);
}
}
g.setFont(new Font("黑體", 1, 20));
if(ifTou){
g.draw3DRect(600, 650, 50, 50, true);
drawTou(g, 600, 650, tou[0]);
g.draw3DRect(600, 720, 50, 50, true);
drawTou(g, 600, 720, tou[1]);
g.draw3DRect(670, 650, 50, 50, true);
drawTou(g, 670, 650, tou[2]);
g.draw3DRect(670, 720, 50, 50, true);
drawTou(g, 670, 720, tou[3]);
g.setColor(cs[flag]);
switch (sel) {
case 0:
g.draw3DRect(599, 649, 52, 52, true);
break;
case 1:
g.draw3DRect(599, 719, 52, 52, true);
break;
case 2:
g.draw3DRect(669, 649, 52, 52, true);
break;
case 3:
g.draw3DRect(669, 719, 52, 52, true);
break;
}
}
else{
g.setColor(cs[flag]);
g.draw3DRect(600, 650, 100, 50, true);
g.drawString("骰子", 630, 685);
g.draw3DRect(600, 720, 100, 50, true);
g.drawString("結束", 630, 755);
}
g.drawString(flagString[flag]+"的回合", 20, 100);
g.setColor(Color.black);
g.drawString("參與人數(shù)為"+maxFlag, 20, 130);
g.drawString("選擇人數(shù)重新開始:", 520, 100);
for (int i = 1; i < 4; i++) {
g.draw3DRect(600+i*40, 105, 40, 40, true);
g.drawString((i+1)+"", 615+i*40, 135);
}
}
void drawTou(Graphics g,int x,int y,int t){
g.setColor(Color.black);
switch (t) {
case 1:
g.fillOval(x+23, y+23, 4, 4);
break;
case 2:
g.fillOval(x+18, y+23, 4, 4);
g.fillOval(x+29, y+23, 4, 4);
break;
case 3:
g.fillOval(x+23, y+18, 4, 4);
g.fillOval(x+18, y+29, 4, 4);
g.fillOval(x+29, y+29, 4, 4);
break;
case 4:
g.fillOval(x+18, y+18, 4, 4);
g.fillOval(x+29, y+18, 4, 4);
g.fillOval(x+18, y+29, 4, 4);
g.fillOval(x+29, y+29, 4, 4);
break;
case 5:
g.fillOval(x+18, y+18, 4, 4);
g.fillOval(x+29, y+18, 4, 4);
g.fillOval(x+23, y+23, 4, 4);
g.fillOval(x+18, y+29, 4, 4);
g.fillOval(x+29, y+29, 4, 4);
break;
case 6:
g.fillOval(x+18, y+18, 4, 4);
g.fillOval(x+29, y+18, 4, 4);
g.fillOval(x+18, y+23, 4, 4);
g.fillOval(x+29, y+23, 4, 4);
g.fillOval(x+18, y+29, 4, 4);
g.fillOval(x+29, y+29, 4, 4);
break;
}
}
boolean moveToken(int[] h){
boolean hasMove=false;
for (int j = 0; j < h.length; j++) {
if(hasTaken[h[j]]==-1){
for (int i = 0; i < token.length; i++) {
if(token[i]==null){
token[i]=new Point(h[j],rate[flag][h[j]]+1);
hasMove=true;
break;
}
else if(token[i].x==h[j]&&token[i].y<max[h[j]]){
token[i].y++;
hasMove=true;
break;
}
else if(token[i].y==max[h[j]]){
break;
}
}
}
}
return hasMove;
}
@Override
public void mouseClicked(MouseEvent e) {
int x=e.getX()-10;
int y=e.getY()-38;
if(x>640&&x<680&&y>105&&y<145)init(2);
else if(x>680&&x<720&&y>105&&y<145)init(3);
else if(x>720&&x<760&&y>105&&y<145)init(4);
else if(ifTou){
int s=-1;
if(x>600&&x<650&&y>650&&y<700)s=0;
else if(x>600&&x<670&&y>720&&y<770)s=1;
else if(x>670&&x<720&&y>650&&y<700)s=2;
else if(x>670&&x<720&&y>720&&y<770)s=3;
if(s==-1){}
else if(s==sel){
sel=-1;
}
else if(sel==-1){
sel=s;
}
else{
int[] h={0,0};
for (int i = 0; i < 4; i++) {
if(i==sel||i==s)h[0]+=tou[i];
else h[1]+=tou[i];
}
h[0]-=2;
h[1]-=2;
if(!moveToken(h)){
failTurn();
}
ifTou=false;
sel=-1;
}
}
else{
if(x>600&&x<700&y>650&&y<700){
for (int i = 0; i < tou.length; i++) {
tou[i]=(int)(Math.random()*6+1);
}
ifTou=true;
}
else if(x>600&&x<700&y>720&&y<770){
endTurn();
}
}
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}