// File Name: FiveInLine.java
// Description: In a 9*9 grid , You can move and make five same color pieces in one line(‘-|\/’) then you get score follow the rule.
// Future Improvements: 1.we can add a kind of polychrome piece that can match some existing color(like a blue-green pieces)
2.we can make our program interface beautiful than before(make the 3D color piece etc.)
3. we can create a ranking for the ‘top 10’who get
High scores
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.border.LineBorder;
public class FiveInLine extends JFrame {
static int scores=0;//record scores
static int multiple = 0;//record how many times free line continuously
static int preX=0,preY=0;//record the Panel active
static String[] nextColor = new String[4];//record the next turn three pieces color(String)
static int[] changeColor = new int[4];//record the next turn three pieces color(String)
static int[][] saveColor = new int[11][11];//record every cell drawColor
static Cell[][] cells = new Cell[11][11];//record all panel status
public boolean GameOver(){//check the game is over or not
int counter = 0;
boolean gameOver = false;
for(int i=1;i<=9;i++){//check how many cells be filled
for(int j=1;j<=9;j++){
if(cells[i][j].filled == true )counter++;
}
}
if(counter>=79)gameOver = true;//if next time can't add new three pieces
else gameOver = false;
counter = 0;
return gameOver;
}
public void NewPieces(){//add new three pieces
for(int i=1;i<=3;i++){
changeColor[i] = (int)(Math.random()*7+1);
switch(changeColor[i]){
case 1 : nextColor[i] = "red" ;continue;
case 2 : nextColor[i] = "blue";continue;
case 3 : nextColor[i] = "black";continue;
case 4 : nextColor[i] = "gray";continue;
case 5 : nextColor[i] = "orange";continue;
case 6 : nextColor[i] = "yellow";continue;
case 7 : nextColor[i] = "green";continue;
}
}
}
public void ReStart(){//restart the game
for(int i=0;i<11;i=i+10){//reset the wall of the 'maze'
for(int j=0;j<11;j++){
cells[i][j].canReach = false;
saveColor[i][j] = 0;
}
}
for(int j=0;j<11;j=j+10){//reset the wall of the 'maze'
for(int i=1;i<10;i++){
cells[i][j].canReach = false;
saveColor[i][j] = 0;
}
}
for(int i=1;i<=9;i++)//reset the 81 panels
for(int j=1;j<=9;j++){
cells[i][j].filled = false;
cells[i][j].selected = false;
cells[i][j].canReach = true;
saveColor[i][j] = 0;
}
for(int i=1;i<=5;i++){//add 5 pieces for initial
int randomX = (int)(Math.random()*9+1);
int randomY = (int)(Math.random()*9+1);
while(cells[randomX][randomY].filled){
randomX = (int)(Math.random()*9+1);
randomY = (int)(Math.random()*9+1);
}
cells[randomX][randomY].filled = true;
cells[randomX][randomY].canReach = false;
int randomColor = (int)(Math.random()*7+1);
saveColor[randomX][randomY] = randomColor;
cells[randomX][randomY].setDrawColor(randomColor);
repaint();
}
NewPieces();
scores = 0;
}
public class Cell extends JPanel{
//the cell store all data of status
int direct = 0;//the piece's move direct in the 'maze'
boolean filled = false;//the cell be filled by Oval or not
boolean selected = false;//the play select the cell or not
boolean canReach= true;//if the cell be filled, it can't reach
Color drawColor = Color.black;
public void paintComponent(Graphics g){//draw the Oval or Line in the panel
g.setColor(Color.black);
if(selected){
g.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);
g.drawLine(getWidth()/2,0,getWidth()/2,getHeight());
}
if(filled){
g.setColor(drawColor);
g.fillOval(getWidth()/6, getHeight()/6, getWidth()*2/3, getHeight()*2/3);}
}
public void setDrawColor(int x){//set the draw color
switch(x){
case 1:drawColor = Color.red;break;
case 2:drawColor = Color.blue;break;
case 3:drawColor = Color.black;break;
case 4:drawColor = Color.gray;break;
case 5:drawColor = Color.orange;break;
case 6:drawColor = Color.yellow;break;
case 7:drawColor = Color.green;break;
}
}
}
public boolean MazePath(int preX,int preY,int x,int y){//the problem of 'MazePath' that we have
//learned in the course of data structure
ArrayList passby = new ArrayList();//record one cell passed or not in the maze
for(int i=1;i<=9;i++)//reset the data about MazePath
for(int j=1;j<=9;j++){
cells[i][j].direct = 0;
}
cells[preX][preY].canReach = true;
int moveX = preX;
int moveY = preY;
boolean canMove = false;
Stack path = new Stack();//create a new stack to record the path passed
do{
if(cells[moveX][moveY].canReach && !passby.contains(cells[moveX][moveY])){//if the cell isn't passed
passby.add(cells[moveX][moveY]);
path.Push(moveX, moveY);
if(moveX == x && moveY == y){//if we have a path to the cell we want to arrive
canMove = true;
break;
}
else{//move north
moveY++;
}
}
else{// if the cell is passed
if(!path.isEmpty()){
moveX = path.TopX[path.top];
moveY = path.TopY[path.top];
path.Pop();
while(cells[moveX][moveY].direct == 4 && !path.isEmpty()){//if a cell's all direct
//are passed
moveX = path.TopX[path.top];
moveY = path.TopY[path.top];
passby.add(cells[moveX][moveY]);
path.Pop();
}
if(cells[moveX][moveY].direct<4){//check which direct are not passed;
cells[moveX][moveY].direct++;
path.Push(moveX, moveY);
if(cells[moveX][moveY].direct == 1)moveX++;//move east
else if(cells[moveX][moveY].direct == 2)moveY--;//move south
else if(cells[moveX][moveY].direct == 3)moveX--;//move west
}
}
}
}while(!path.isEmpty());
cells[preX][preY].canReach = false;
return canMove;
}
public class Stack {//this class can record the path we passed
int[] TopX = new int[100];
int[] TopY = new int[100];
int top = -1;
public void Push(int x,int y){
top++;
TopX[top] = x;
TopY[top] = y;
}
public void Pop(){
top--;
}
public boolean isEmpty(){
if(top == -1) return true;
else return false;
}
}
public boolean CalculateScores (int curX, int curY){//calculate score follow the rule
int preScores = scores;
int onceCompleted = 0;//how many lines completed cone;
int[]pieces = {1,1,1,1,1};//record how many pieces for each line
int[]partScores = {0,0,0,0,0};//record the scores for each line
int curColor = saveColor[curX][curY];
boolean switchOfmultiple = false;//
for(int i=curX,j=curY-1;j>=1;j--){//the line of '-'
if(cells[i][j].filled&&(saveColor[i][j] == curColor))pieces[1]++;
else break;}
for(int i=curX,j=curY+1;j<=9;j++){
if(cells[i][j].filled&&(saveColor[i][j] == curColor))pieces[1]++;
else break;}
for(int i=curX-1,j=curY;i>=1;i--){//the line of '|'
if(cells[i][j].filled && (saveColor[i][j] == curColor))pieces[2]++;
else break;}
for(int i=curX+1,j=curY;i<=9;i++){
if(cells[i][j].filled && (saveColor[i][j] == curColor))pieces[2]++;
else break;}
for(int i=curX-1,j=curY-1;i>=1&&j>=1;i--,j--){//the line of '\'
if(cells[i][j].filled && (saveColor[i][j] == curColor))pieces[3]++;
else break;}
for(int i=curX+1,j=curY+1;i<=9&&j<=9;i++,j++){
if(cells[i][j].filled && (saveColor[i][j] == curColor))pieces[3]++;
else break;}
for(int i=curX-1,j=curY+1;i>1&&j<=9;i--,j++){//the line of '/'
if(cells[i][j].filled && (saveColor[i][j] == curColor))pieces[4]++;
else break;}
for(int i=curX+1,j=curY-1;i<=9&&j>=1;i++,j--){
if(cells[i][j].filled && (saveColor[i][j] == curColor))pieces[4]++;
else break;}
while(true){
int maxPieces = 0;
int maxNumber = 0;
for(int i=1;i<=4;i++)//find out the max pieces of the four line
if(pieces[i]>maxPieces){
maxPieces = pieces[i];
maxNumber = i;
}
if(maxPieces < 5)//if the max pieces is less than five
break;
if(maxPieces >= 5 && !switchOfmultiple){//if the max pieces is more than five
multiple++;
switchOfmultiple = true;
cells[curX][curY].filled = false;
cells[curX][curY].canReach = true;
saveColor[curX][curY] = 0;
}
if(maxNumber == 1){//erase the pieces of line '-' and calculate scores
for(int i=curX,j=curY-1;j>0;j--){
if(cells[i][j].filled&&(saveColor[i][j] == curColor)){
cells[i][j].filled = false;
cells[i][j].canReach = true;
saveColor[i][j] = 0;
}
else break;
}
for(int i=curX,j=curY+1;j<10;j++){
if(cells[i][j].filled&&(saveColor[i][j] == curColor)){
cells[i][j].filled = false;
cells[i][j].canReach = true;
saveColor[i][j] = 0;
}
else break;
}
onceCompleted++;
partScores[1] = (pieces[1]-2)*onceCompleted*multiple;
}
if(maxNumber ==2){//erase the pieces of line '|' and calculate scores
for(int i=curX-1,j=curY;i>0;i--){
if(cells[i][j].filled&&(saveColor[i][j] == curColor)){
cells[i][j].filled = false;
cells[i][j].canReach = true;
saveColor[i][j] = 0;
}
else break;
}
for(int i=curX+1,j=curY;i<10;i++){
if(cells[i][j].filled&&(saveColor[i][j] == curColor)){
cells[i][j].filled = false;
cells[i][j].canReach = true;
saveColor[i][j] = 0;
}
else break;
}
onceCompleted++;
partScores[2] = (pieces[2]-2)*onceCompleted*multiple;
}
if(maxNumber ==3){//erase the pieces of line '\' and calculate scores
for(int i=curX-1,j=curY-1;i>0&&j>0;i--,j--){
if(cells[i][j].filled&&(saveColor[i][j] == curColor)){
cells[i][j].filled = false;
cells[i][j].canReach = true;
saveColor[i][j] = 0;
}
else break;
}
for(int i=curX+1,j=curY+1;i<10&&j<10;i++,j++){
if(cells[i][j].filled&&(saveColor[i][j] == curColor)){
cells[i][j].filled = false;
cells[i][j].canReach = true;
saveColor[i][j] = 0;
}
else break;
}
onceCompleted++;
partScores[3] = (pieces[3]-2)*onceCompleted*multiple;
}
if(maxNumber ==4){//erase the pieces of line '/' and calculate scores
for(int i=curX-1,j=curY+1;i>0&&j<10;i--,j++){
if(cells[i][j].filled&&(saveColor[i][j] == curColor)){
cells[i][j].filled = false;
cells[i][j].canReach = true;
saveColor[i][j] = 0;
}
else break;
}
for(int i=curX+1,j=curY-1;i<10&&j>0;i++,j--){
if(cells[i][j].filled&&(saveColor[i][j] == curColor)){
cells[i][j].filled = false;
cells[i][j].canReach = true;
saveColor[i][j] = 0;
}
else break;
}
onceCompleted++;
partScores[4] = (pieces[4]-2)*onceCompleted*multiple;
}
pieces[maxNumber] = 1;
}
onceCompleted = 0;
scores+= partScores[1] + partScores[2] + partScores[3] + partScores[4];
if(scores == preScores)return false;//check the scores changed or not
else return true;
}
public FiveInLine() {
JPanel p1 = new JPanel(new FlowLayout());
p1.setBorder(BorderFactory.createRaisedBevelBorder());
JPanel p2 = new JPanel(new GridLayout(9,9));
p2.setBorder(new LineBorder(Color.black,1));
JPanel p3 = new JPanel(new GridLayout(1,2));
p3.setBorder(BorderFactory.createRaisedBevelBorder());
final JLabel info1 = new JLabel("Score: "+""+scores);
p3.add(info1);
NewPieces();
final JLabel info2 = new JLabel("Next:"+nextColor[1]+","+nextColor[2]+","+nextColor[3]);
p3.add(info2);
for(int i=0;i<11;i=i+10){//initialize the wall of maze
for(int j=0;j<11;j++){
cells[i][j] = new Cell();
}
}
for(int j=0;j<11;j=j+10){
for(int i=1;i<10;i++){
cells[i][j] = new Cell();
}
}
for(int i=1;i<=9;i++){//initialize the cell
for(int j=1;j<=9;j++){
final int x = i;
final int y = j;
cells[i][j]=new Cell();
cells[i][j].setBorder(BorderFactory.createLoweredBevelBorder());
p2.add(cells[i][j]);
cells[i][j].addMouseListener(new MouseAdapter(){//when we click the cell
public void mouseClicked(MouseEvent e){
if(cells[x][y].filled&&(preX==0&&preY==0)){//if no cell be selected
cells[x][y].selected = true;
preX = x;
preY = y;
repaint();
}
else if(cells[x][y].filled&&preX!=0){//if one cell has been selected
cells[preX][preY].selected = false;
cells[x][y].selected = true;
preX = x;
preY = y;
repaint();
}
else if(!cells[x][y].filled&&preX!=0){//if we want to move a pieces
if(MazePath(preX,preY,x,y)){//check we can move it or not
saveColor[x][y] = saveColor[preX][preY];
saveColor[preX][preY] = 0;
cells[x][y].setDrawColor(saveColor[x][y]);
cells[preX][preY].selected = false;
cells[preX][preY].filled = false;
cells[preX][preY].canReach = true;
cells[x][y].filled = true;
cells[x][y].canReach = false;
preX = 0;
preY = 0;
if(!CalculateScores(x,y)){//check we get scores or not
if(!GameOver()){
for(int i=1;i<=3;i++){//add three pieces
int randomX = (int)(Math.random()*9+1);
int randomY = (int)(Math.random()*9+1);
while(cells[randomX][randomY].filled){
randomX = (int)(Math.random()*9+1);
randomY = (int)(Math.random()*9+1);
}
cells[randomX][randomY].filled = true;
cells[randomX][randomY].canReach = false;
saveColor[randomX][randomY] = changeColor[i];
cells[randomX][randomY].setDrawColor(changeColor[i]);
CalculateScores(randomX,randomY);
repaint();
}
multiple = 0;
if(!GameOver()){//show the next three pieces color
NewPieces();
info2.setText("Next:"+nextColor[1]+","+nextColor[2]+","+nextColor[3]);
}
else{
JOptionPane.showMessageDialog(null, "Game over!\n" + "Your Scores is " + scores);
info2.setText("Game over");
ReStart();
}
repaint();
}
else{
JOptionPane.showMessageDialog(null, "Game over!\n" + "Your Scores is " + scores);
info2.setText("Game over");
ReStart();
}
}
repaint();
info1.setText("Score: "+""+scores);
}
}
}
});
}
}
JButton reStart = new JButton("Restart Game");
reStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ReStart();
NewPieces();
info2.setText("Next:"+nextColor[1]+","+nextColor[2]+","+nextColor[3]);
}
});
p1.add(reStart);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
add(p3,BorderLayout.SOUTH);
ReStart();
}
public static void main(String[] args){
FiveInLine frame = new FiveInLine();
frame.setTitle("Five in A Line");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(442,502);
frame.setVisible(true);
}
}
| |
本文档为【Java小游戏——FiveInLine】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。