四子棋剩余的源代码

 

ChessBoardPanel.java:

/**
 * By 2006-10-5,I have finished how to draw chess board
 
*/

package  com.lilijun.game.bingo.ui;

import  javax.swing.JPanel;

import  java.awt.Color;
import  java.awt.Graphics;
import  java.awt.GridBagLayout;

import  com.lilijun.game.bingo.util. * ;
public   class  ChessBoardPanel  extends  JPanel  {

    
private static final long serialVersionUID = 1L;
    
private GameTable gameTable = GameTable.getInstance();
    
public ChessBoardPanel() {
        
// TODO Auto-generated constructor stub
        super();
        initialize();
    }


    
/**
     * This method initializes this
     * 
     * 
@return void
     
*/

    
private void initialize() {
        
this.setSize(300200);
        setBackground(Color.white);
        setLayout(
new GridBagLayout());
        
        
//this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
    }

    
/**
     * Created on 2006-10-5 
     * 
     * TO DO to paint the panel with different color
     *
     * 
@return void
     
*/

    
public void paintComponent(Graphics g){
        
super.paintComponent(g);
        
int width = this.getWidth();
        
int height = this.getHeight();
        
int x,y;
        
boolean flag = true;
        x 
= 0;
        
for(int i = 0;i < 7;i ++{
            
if(flag) 
                g.setColor(Color.gray);
            
else 
                g.setColor(Color.darkGray);
            flag 
= !flag;
            g.fillRect(x, 
0, width/7, height);
            y 
= 0;
            
for(int j = 0;j < 6;j ++{
                
switch(gameTable.getValue(j, i)){
                    
case 0
                        g.setColor(Color.white);
                        
break;
                    
case 1:
                        g.setColor(Color.red);
                        
break;
                    
case 2:
                        g.setColor(Color.yellow);
                        
break;
                    
default:;
                }

                g.fillOval(x, y, width
/7, height/6);
                y 
+= height/6;
            }
            
            x 
+= width/7;
        }

    }


}
   //   @jve:decl-index=0:visual-constraint="94,45"

 
package  com.lilijun.game.bingo.player;


/**
 * 
@author lilijun
 *
 
*/

public   class  ManPlayer  extends  Player  {

    
/**
     * 
@param gameTable
     
*/

    
public ManPlayer() {
        
super();
        
// TODO 自动生成构造函数存根
    }


}
package  com.lilijun.game.bingo.util;

import  java.util.Observable;

/**
 * 
@author lilijun
 *
 
*/

public   class  GameTable  extends  Observable  implements  GameController  {

    
/**
     * Constructor method 
     
*/

    
private GameTable() {
        
// TODO 自动生成构造函数存根
        beginGame();
    }

    
    
/* (非 Javadoc)
     * @see bingo.util.GameCtroller#beginGame()
     
*/

    
public void beginGame() {
        
// TODO 自动生成方法存根
        isFirst = true;
        isWin 
= false;
        
for(int i = 0;i < 6;i ++)
            
for(int j = 0;j < 7;j ++)
                table[i][j] 
= 0;
    }


    
/* (非 Javadoc)
     * @see bingo.util.GameCtroller#endGame()
     
*/

    
public void endGame() {
        
// TODO 自动生成方法存根

    }


    
/* (非 Javadoc)
     * @see bingo.util.GameCtroller#putChess(int)
     
*/

    
public void putChess(int position) {
        
// TODO 自动生成方法存根
        
        
int p = 5;
        
for(;p >= 0;p --
            
if(table[p][position]==0
                
break;
        
if(p >= 0) table[p][position] = getPlayerID();
        
else ;
        isFirst 
= !isFirst;
        testWin();
    }


    
/*
     * to test whether it's win or not now.
     
*/

    
public boolean testWin(){
        
//...to be finished
        isWin = TestWin.test(table,6,7);
        
if(isWin){
            setChanged();
            notifyObservers();
        }

        
return isWin;
    }

    
/**
     * singleton pattern
     
*/

    
public static GameTable getInstance(){
        
if(gameTable == null){
            gameTable 
= new GameTable();
        }

        
return gameTable;
    }

    
/*
     * 
     
*/

    
public int getValue(int i, int j){
        
return table[i][j];
    }

    
/**
     * 
@return int the player's id
     * 
     
*/

    
private int getPlayerID(){
        
if(isFirst) 
            
return 1;
        
else 
            
return 2;
    }

    
//variables declaration
    private int[][] table = new int[6][7];
    
private boolean isFirst = true;
    
private boolean isWin = false;
    
private static GameTable gameTable = null;
}


package  com.lilijun.game.bingo.util;


public   class  TestWin  {
    
protected static boolean  test(int table[][],int N,int M){
        
//scan by rows
        int i,j,c,pre,s;
        
for(i = 0;i < N;i ++){
            c 
= 1;pre = 0;
            
for(j = 0;j < M;j ++){
                
if(j == 0) pre = 0;
                
else pre = table[i][j -1];
                
if(table[i][j] == 0continue;
                
else{
                    
if(table[i][j] != pre) c = 1;
                    
else c ++;
                    
if(c == 4return true;
                }

            }

        }

        
//scan by colum
        for(j = 0;j < M;j ++){
            c 
= 1;pre = 0;
            
for(i = 0;i < N;i ++){
                
if(i == 0) pre = 0;
                
else pre = table[i-1][j];
                
if(table[i][j] == 0continue;
                
else {
                    
if(table[i][j] != pre) c = 1;
                    
else c ++;
                    
if(c == 4return true;
                }

            }

        }

        
//scan by "/"
        for(i = 3;i < N;i ++){
            c 
= 1;pre = 0;
            
for(s = i,j = 0;s >= 0;s --,j ++){             
                
if(s == i) pre = 0;
                
else pre = table[s+1][j-1];
                
if(table[s][j] == 0continue;
                
else{
                    
if(table[s][j] != pre) c = 1;
                    
else c ++;
                    
if(c == 4return true;
                }

            }

            
        }

        
for(j = 1;j < M - 3;j ++){
            c 
= 1;pre = 0;
            
for(i = N-1,s = j;s < M;s ++,i--){
                
if(i == N-1) pre = 0;
                
else pre = table[i+1][s-1];
                
if(table[i][s] == 0continue;
                
else{
                    
if(table[i][s] != pre) c = 1;
                    
else c ++;
                    
if(c == 4return true;
                }

            }

        }

        
//scan by ""
        for(i = 0;i < N-3;i ++){
            c 
= 1;pre = 0;
            
for(s = i,j = 0;s < N;s ++,j ++){             
                
if(s == i) pre = 0;
                
else pre = table[s-1][j-1];
                
if(table[s][j] == 0continue;
                
else{
                    
if(table[s][j] != pre) c = 1;
                    
else c ++;
                    
if(c == 4return true;
                }

            }

            
        }

        
for(j = 1;j < M - 3;j ++){
            c 
= 1;pre = 0;
            
for(i = 0,s = j;s < M&&< N;s ++,i++){
                
if(i == 0) pre = 0;
                
else pre = table[i-1][s-1];
                
if(table[i][s] == 0continue;
                
else{
                    
if(table[i][s] != pre) c = 1;
                    
else c ++;
                    
if(c == 4return true;
                }

            }

        }

        
return false;
    }

    
public static void main(String args[]){
        
int a[][] = new int[][]{
                
{0,0,0,1,0,0,0},
                
{0,1,1,2,1,2,1},
                
{0,1,1,1,2,2,1},
                
{1,2,2,1,1,1,2},
                
{2,1,1,2,2,2,1},
                
{1,1,2,1,2,1,1}
        }
;
        
if(TestWin.test(a, 67)) 
            System.out.println(
"You win!");
        
else System.out.println("Game Over!");
    }

}

/**
 * 
 
*/

package  com.lilijun.game.bingo.util;

/**
 * 
@author lilijun
 * 
 
*/

public   interface  GameController  {
    
/*
     * initialize data used in game and begin the game
     
*/

     
public void beginGame();
     
     
/*
      * end the game and quit from the game
      
*/

     
public void endGame();
     
     
/**
      * 
@param position the x position of game table
      * put a chess on the game table 
      
*/

     
public void putChess(int position);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值