簡単な四つ手ゲーム

36574 ワード

ターゲット
简単で、あまり拘束されていない、端末ベースの、インタラクティブな四子棋ゲームです.java视线を采用します.
コア
胜ち方を判断するには、一度も落子がなく、周囲の「上」を除いた7つの方向を検索し、自分と同じ色のコマ数を近くから远くまで连続して数える.そして、位置合わせの2つの方向の和が3であるかどうかを见る(自分を加えると4になる).
コード#コード#
//Board.java
import java.util.Scanner;
public class Board{
    int width =6; int height =6;
    int[] state = new int[7]; //     7        
    char[] board = new char[width*height];
    public Board(){
        for(int i=0;i<board.length;i++){
            board[i] = ' ';
        }
    }
    
    public static void main(String []args){
        System.out.println("hi");
        Board b = new Board();
        b.interact();
    }
    public void show(){
        for(int row=height-1;row>=0;row--){
            for(int col=0;col<width;col++){
                System.out.print(board[row*width+col]+" ");
            }
            System.out.print("
"
); } for(int col = 0;col<width;col++){ System.out.print(col); System.out.print(' '); } System.out.print('
'
); } public void setRandom(){ push(0,'X'); push(0,'X'); push(0,'X'); push(0,'X'); push(0,'X'); } public void interact(){ int res; int col; Scanner input = new Scanner(System.in); while(true){ System.out.print("waiting for X column:"); col = input.nextInt(); res = push(col,'X'); if(res == 1){ show(); System.out.println("X winning!!"); break; } show(); System.out.print("waiting for O column:"); col = input.nextInt(); res = push(col,'O'); if(res == 1){ show(); System.out.print("O winning!!"); break; } show(); } } private int getNextIndex(int col){ int row = 0; while(true){ char value = board[row*width+col]; if(value == ' '){ return row; } row++; } } public int check(int row, int col, char value){ int k; int Nrow,Ncol; k = 1; while(true){ Nrow = row+k; Ncol = col+k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[0] = k++; }else{break;} } k = 1; while(true){ Nrow = row+0; Ncol = col+k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[1] = k++; }else{break;} } k = 1; while(true){ Nrow = row-k; Ncol = col+k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[2] = k++; }else{break;} } k = 1; while(true){ Nrow = row-k; Ncol = col+0; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[3] = k++; }else{break;} } k = 1; while(true){ Nrow = row-k; Ncol = col-k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[4] = k++; }else{break;} } k = 1; while(true){ Nrow = row+0; Ncol = col-k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[5] = k++; }else{break;} } k = 1; while(true){ Nrow = row+k; Ncol = col-k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[6] = k++; }else{break;} } if(state[0]+state[4]==3||state[1]+state[5]==3||state[2]+state[6]==3||state[3]==3){return 1;} else{return 0;} } public int push(int col, char value){ int row=getNextIndex(col); if(row==height){ System.out.println("full"); return -1; } board[row*width+col] = value; int res = check(row,col,value); return res; } public void test(){ setRandom(); System.out.print("
"
); show(); } }

結果
hi
waiting for X column:0 
            
            
            
            
            
X           
0 1 2 3 4 5 
waiting for O column:1
            
            
            
            
            
X O         
0 1 2 3 4 5 
waiting for X column:2
            
            
            
            
            
X O X       
0 1 2 3 4 5 
waiting for O column:2
            
            
            
            
    O       
X O X       
0 1 2 3 4 5 
waiting for X column:3
            
            
            
            
    O       
X O X X     
0 1 2 3 4 5 
waiting for O column:3
            
            
            
            
    O O     
X O X X     
0 1 2 3 4 5 
waiting for X column:2
            
            
            
    X       
    O O     
X O X X     
0 1 2 3 4 5 
waiting for O column:3
            
            
            
    X O     
    O O     
X O X X     
0 1 2 3 4 5 
waiting for X column:4
            
            
            
    X O     
    O O     
X O X X X   
0 1 2 3 4 5 
waiting for O column:4
            
            
            
    X O     
    O O O   
X O X X X   
0 1 2 3 4 5 
waiting for X column:4
            
            
            
    X O X   
    O O O   
X O X X X   
0 1 2 3 4 5 
waiting for O column:4
O winning!!            
            
        O   
    X O X   
    O O O   
X O X X X   
0 1 2 3 4 5